Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Support an asterisk in bothify and optimize #701

Merged
merged 3 commits into from
Sep 14, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/Faker/Provider/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,19 @@ public static function shuffleString($string = '', $encoding = 'UTF-8')
return join('', static::shuffleArray($array));
}

private static function replaceWildcard($string, $wildcard = '#', $callback = 'static::randomDigit')
{
if (($pos = strpos($string, $wildcard)) === false) {
return $string;
}
for ($i = $pos, $last = strrpos($string, $wildcard, $pos) + 1; $i < $last; $i++) {
if ($string[$i] === $wildcard) {
$string[$i] = call_user_func($callback);
}
}
return $string;
}

/**
* Replaces all hash sign ('#') occurrences with a random number
* Replaces all percentage sign ('%') occurrences with a not null number
Expand All @@ -329,9 +342,11 @@ public static function numerify($string = '###')
// instead of using randomDigit() several times, which is slow,
// count the number of hashes and generate once a large number
$toReplace = array();
for ($i = 0, $count = strlen($string); $i < $count; $i++) {
if ($string[$i] === '#') {
$toReplace []= $i;
if (($pos = strpos($string, '#')) !== false) {
for ($i = $pos, $last = strrpos($string, '#', $pos) + 1; $i < $last; $i++) {
if ($string[$i] === '#') {
$toReplace[] = $i;
}
}
}
if ($nbReplacements = count($toReplace)) {
Expand All @@ -347,7 +362,7 @@ public static function numerify($string = '###')
$string[$toReplace[$i]] = $numbers[$i];
}
}
$string = preg_replace_callback('/\%/u', 'static::randomDigitNotNull', $string);
$string = self::replaceWildcard($string, '%', 'static::randomDigitNotNull');

return $string;
}
Expand All @@ -360,17 +375,21 @@ public static function numerify($string = '###')
*/
public static function lexify($string = '????')
{
return preg_replace_callback('/\?/u', 'static::randomLetter', $string);
return self::replaceWildcard($string, '?', 'static::randomLetter');
}

/**
* Replaces hash signs and question marks with random numbers and letters
* Replaces hash signs ('#') and question marks ('?') with random numbers and letters
* An asterisk ('*') is replaced with either a random number or a random letter
*
* @param string $string String that needs to bet parsed
* @return string
*/
public static function bothify($string = '## ??')
{
$string = self::replaceWildcard($string, '*', function () {
return mt_rand(0, 1) ? '#' : '?';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you directly pass randomDigit() or randomAscii() here? That's save one function call for each placeholder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does it save a function call? For randomLetter(), it will be called once anyway and randomDigit() is not called at all, there's an optimization already that gets many digits from a big number.

});
return static::lexify(static::numerify($string));
}

Expand Down
5 changes: 5 additions & 0 deletions test/Faker/Provider/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ public function testBothifyCombinesNumerifyAndLexify()
$this->assertRegExp('/foo[a-z]Ba\dr/', BaseProvider::bothify('foo?Ba#r'));
}

public function testBothifyAsterisk()
{
$this->assertRegExp('/foo([a-z]|\d)Ba([a-z]|\d)r/', BaseProvider::bothify('foo*Ba*r'));
}

public function testAsciifyReturnsSameStringWhenItContainsNoStarSign()
{
$this->assertEquals('fooBar?', BaseProvider::asciify('fooBar?'));
Expand Down