Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Reuse NumberExtension #715

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion src/Faker/Core/Barcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
final class Barcode implements Extension\BarcodeExtension
{
private Extension\NumberExtension $numberExtension;

public function __construct(Extension\NumberExtension $numberExtension = null)
{
$this->numberExtension = $numberExtension ?: new Number();
}

private function ean(int $length = 13): string
{
$code = Extension\Helper::numerify(str_repeat('#', $length - 1));
Expand All @@ -38,7 +45,7 @@ public function isbn10(): string

public function isbn13(): string
{
$code = '97' . mt_rand(8, 9) . Extension\Helper::numerify(str_repeat('#', 9));
$code = '97' . $this->numberExtension->numberBetween(8, 9) . Extension\Helper::numerify(str_repeat('#', 9));

return sprintf('%s%s', $code, Calculator\Ean::checksum($code));
}
Expand Down
35 changes: 16 additions & 19 deletions src/Faker/Core/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
final class Color implements Extension\ColorExtension
{
private Extension\NumberExtension $numberExtension;

/**
* @var string[]
*/
Expand All @@ -20,7 +22,6 @@ final class Color implements Extension\ColorExtension
'purple', 'teal', 'lime', 'blue', 'silver',
'gray', 'yellow', 'fuchsia', 'aqua', 'white',
];

/**
* @var string[]
*/
Expand Down Expand Up @@ -53,23 +54,25 @@ final class Color implements Extension\ColorExtension
'Turquoise', 'Violet', 'Wheat', 'White', 'WhiteSmoke', 'Yellow', 'YellowGreen',
];

public function __construct(Extension\NumberExtension $numberExtension = null)
{
$this->numberExtension = $numberExtension ?: new Number();
}

/**
* @example '#fa3cc2'
*/
public function hexColor(): string
{
$number = new Number();

return '#' . str_pad(dechex($number->numberBetween(1, 16777215)), 6, '0', STR_PAD_LEFT);
return '#' . str_pad(dechex($this->numberExtension->numberBetween(1, 16777215)), 6, '0', STR_PAD_LEFT);
}

/**
* @example '#ff0044'
*/
public function safeHexColor(): string
{
$number = new Number();
$color = str_pad(dechex($number->numberBetween(0, 255)), 3, '0', STR_PAD_LEFT);
$color = str_pad(dechex($this->numberExtension->numberBetween(0, 255)), 3, '0', STR_PAD_LEFT);

return sprintf(
'#%s%s%s%s%s%s',
Expand Down Expand Up @@ -122,12 +125,10 @@ public function rgbCssColor(): string
*/
public function rgbaCssColor(): string
{
$number = new Number();

return sprintf(
'rgba(%s,%s)',
$this->rgbColor(),
$number->randomFloat(1, 0, 1),
$this->numberExtension->randomFloat(1, 0, 1),
);
}

Expand All @@ -152,13 +153,11 @@ public function colorName(): string
*/
public function hslColor(): string
{
$number = new Number();

return sprintf(
'%s,%s,%s',
$number->numberBetween(0, 360),
$number->numberBetween(0, 100),
$number->numberBetween(0, 100),
$this->numberExtension->numberBetween(0, 360),
$this->numberExtension->numberBetween(0, 100),
$this->numberExtension->numberBetween(0, 100),
);
}

Expand All @@ -169,12 +168,10 @@ public function hslColor(): string
*/
public function hslColorAsArray(): array
{
$number = new Number();

return [
$number->numberBetween(0, 360),
$number->numberBetween(0, 100),
$number->numberBetween(0, 100),
$this->numberExtension->numberBetween(0, 360),
$this->numberExtension->numberBetween(0, 100),
$this->numberExtension->numberBetween(0, 100),
];
}
}
13 changes: 10 additions & 3 deletions src/Faker/Core/Coordinates.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

namespace Faker\Core;

use Faker\Extension\Extension;
use Faker\Extension;

class Coordinates implements Extension
class Coordinates implements Extension\Extension
Copy link
Member Author

Choose a reason for hiding this comment

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

This class should probably be experimental and final, too.

{
private Extension\NumberExtension $numberExtension;

public function __construct(Extension\NumberExtension $numberExtension = null)
{
$this->numberExtension = $numberExtension ?: new Number();
}

/**
* @example '77.147489'
*
Expand Down Expand Up @@ -63,6 +70,6 @@ private function randomFloat(int $nbMaxDecimals, float $min, float $max): float
throw new \LogicException('Invalid coordinates boundaries');
}

return round($min + mt_rand() / mt_getrandmax() * ($max - $min), $nbMaxDecimals);
return $this->numberExtension->randomFloat($nbMaxDecimals, $min, $max);
}
}
10 changes: 5 additions & 5 deletions src/Faker/Core/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function numberBetween(int $min = 0, int $max = 2147483647): int

public function randomDigit(): int
{
return mt_rand(0, 9);
return $this->numberBetween(0, 9);
}

public function randomDigitNot(int $except): int
Expand All @@ -37,7 +37,7 @@ public function randomDigitNot(int $except): int

public function randomDigitNotZero(): int
{
return mt_rand(1, 9);
return $this->numberBetween(1, 9);
}

public function randomFloat(?int $nbMaxDecimals = null, float $min = 0, ?float $max = null): float
Expand All @@ -60,7 +60,7 @@ public function randomFloat(?int $nbMaxDecimals = null, float $min = 0, ?float $
$max = $tmp;
}

return round($min + mt_rand() / mt_getrandmax() * ($max - $min), $nbMaxDecimals);
return round($min + $this->numberBetween() / mt_getrandmax() * ($max - $min), $nbMaxDecimals);
}

public function randomNumber(int $nbDigits = null, bool $strict = false): int
Expand All @@ -75,9 +75,9 @@ public function randomNumber(int $nbDigits = null, bool $strict = false): int
}

if ($strict) {
return mt_rand(10 ** ($nbDigits - 1), $max);
return $this->numberBetween(10 ** ($nbDigits - 1), $max);
}

return mt_rand(0, $max);
return $this->numberBetween(0, $max);
}
}
16 changes: 11 additions & 5 deletions src/Faker/Core/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

namespace Faker\Core;

use Faker\Extension\UuidExtension;
use Faker\Extension;

final class Uuid implements UuidExtension
final class Uuid implements Extension\UuidExtension
{
public function uuid3(): string
private Extension\NumberExtension $numberExtension;

public function __construct(Extension\NumberExtension $numberExtension = null)
{
$number = new Number();

$this->numberExtension = $numberExtension ?: new Number();
}

public function uuid3(): string
{
// fix for compatibility with 32bit architecture; each mt_rand call is restricted to 32bit
// two such calls will cause 64bits of randomness regardless of architecture
$seed = $number->numberBetween(0, 2147483647) . '#' . $number->numberBetween(0, 2147483647);
$seed = $this->numberExtension->numberBetween(0, 2147483647) . '#' . $this->numberExtension->numberBetween(0, 2147483647);

// Hash the seed and convert to a byte array
$val = md5($seed, true);
Expand Down
32 changes: 19 additions & 13 deletions src/Faker/Core/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,35 @@

namespace Faker\Core;

use Faker\Extension\Helper;
use Faker\Extension\VersionExtension;
use Faker\Extension;
use Faker\Provider\DateTime;

final class Version implements VersionExtension
final class Version implements Extension\VersionExtension
{
private Extension\NumberExtension $numberExtension;
/**
* @var string[]
*/
private $semverCommonPreReleaseIdentifiers = ['alpha', 'beta', 'rc'];

public function __construct(Extension\NumberExtension $numberExtension = null)
{

$this->numberExtension = $numberExtension ?: new Number();
}

/**
* Represents v2.0.0 of the semantic versioning: https://semver.org/spec/v2.0.0.html
*/
public function semver(bool $preRelease = false, bool $build = false): string
{
return sprintf(
'%d.%d.%d%s%s',
mt_rand(0, 9),
mt_rand(0, 99),
mt_rand(0, 99),
$preRelease && mt_rand(0, 1) === 1 ? '-' . $this->semverPreReleaseIdentifier() : '',
$build && mt_rand(0, 1) === 1 ? '+' . $this->semverBuildIdentifier() : '',
$this->numberExtension->numberBetween(0, 9),
$this->numberExtension->numberBetween(0, 99),
$this->numberExtension->numberBetween(0, 99),
$preRelease && $this->numberExtension->numberBetween(0, 1) === 1 ? '-' . $this->semverPreReleaseIdentifier() : '',
$build && $this->numberExtension->numberBetween(0, 1) === 1 ? '+' . $this->semverBuildIdentifier() : '',
);
}

Expand All @@ -35,23 +41,23 @@ public function semver(bool $preRelease = false, bool $build = false): string
*/
private function semverPreReleaseIdentifier(): string
{
$ident = Helper::randomElement($this->semverCommonPreReleaseIdentifiers);
$ident = Extension\Helper::randomElement($this->semverCommonPreReleaseIdentifiers);

if (mt_rand(0, 1) !== 1) {
if ($this->numberExtension->numberBetween(0, 1) !== 1) {
return $ident;
}

return $ident . '.' . mt_rand(1, 99);
return $ident . '.' . $this->numberExtension->numberBetween(1, 99);
}

/**
* Common random build identifier
*/
private function semverBuildIdentifier(): string
{
if (mt_rand(0, 1) === 1) {
if ($this->numberExtension->numberBetween(0, 1) === 1) {
// short git revision syntax: https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection
return substr(sha1(Helper::lexify('??????')), 0, 7);
return substr(sha1(Extension\Helper::lexify('??????')), 0, 7);
}

// date syntax
Expand Down
Loading