-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
175 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Resource\Generator; | ||
|
||
use Webmozart\Assert\Assert; | ||
|
||
final class RandomnessGenerator implements RandomnessGeneratorInterface | ||
{ | ||
private string $uriSafeAlphabet; | ||
|
||
private string $digits; | ||
|
||
public function __construct() | ||
{ | ||
$this->digits = implode(range(0, 9)); | ||
|
||
$this->uriSafeAlphabet = | ||
implode(range(0, 9)) | ||
. implode(range('a', 'z')) | ||
. implode(range('A', 'Z')) | ||
. implode(['-', '_', '~']) | ||
; | ||
} | ||
|
||
public function generateUriSafeString(int $length): string | ||
{ | ||
return $this->generateStringOfLength($length, $this->uriSafeAlphabet); | ||
} | ||
|
||
public function generateNumeric(int $length): string | ||
{ | ||
return $this->generateStringOfLength($length, $this->digits); | ||
} | ||
|
||
public function generateInt(int $min, int $max): int | ||
{ | ||
return random_int($min, $max); | ||
} | ||
|
||
private function generateStringOfLength(int $length, string $alphabet): string | ||
{ | ||
$alphabetMaxIndex = strlen($alphabet) - 1; | ||
|
||
Assert::greaterThanEq($alphabetMaxIndex, 1); | ||
|
||
$randomString = ''; | ||
|
||
for ($i = 0; $i < $length; ++$i) { | ||
$index = random_int(0, $alphabetMaxIndex); | ||
$randomString .= $alphabet[$index]; | ||
} | ||
|
||
return $randomString; | ||
} | ||
} | ||
|
||
class_alias(RandomnessGenerator::class, \Sylius\Component\Resource\Generator\RandomnessGenerator::class); |
25 changes: 25 additions & 0 deletions
25
src/Component/src/Generator/RandomnessGeneratorInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Resource\Generator; | ||
|
||
interface RandomnessGeneratorInterface | ||
{ | ||
public function generateUriSafeString(int $length): string; | ||
|
||
public function generateNumeric(int $length): string; | ||
|
||
public function generateInt(int $min, int $max): int; | ||
} | ||
|
||
class_alias(RandomnessGeneratorInterface::class, \Sylius\Component\Resource\Generator\RandomnessGeneratorInterface::class); |
63 changes: 63 additions & 0 deletions
63
src/Component/tests/spec/Generator/RandomnessGeneratorSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Resource\Generator; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Resource\Generator\RandomnessGeneratorInterface; | ||
|
||
final class RandomnessGeneratorSpec extends ObjectBehavior | ||
{ | ||
function it_implements_randomness_generator_interface(): void | ||
{ | ||
$this->shouldImplement(RandomnessGeneratorInterface::class); | ||
} | ||
|
||
function it_generates_random_uri_safe_string_of_length(): void | ||
{ | ||
$length = 9; | ||
|
||
$this->generateUriSafeString($length)->shouldBeString(); | ||
$this->generateUriSafeString($length)->shouldHaveLength($length); | ||
} | ||
|
||
function it_generates_random_numeric_string_of_length(): void | ||
{ | ||
$length = 12; | ||
|
||
$this->generateNumeric($length)->shouldBeString(); | ||
$this->generateNumeric($length)->shouldBeNumeric(); | ||
$this->generateNumeric($length)->shouldHaveLength($length); | ||
} | ||
|
||
function it_generates_random_int_in_range(): void | ||
{ | ||
$min = 12; | ||
$max = 2000000; | ||
|
||
$this->generateInt($min, $max)->shouldBeInt(); | ||
$this->generateInt($min, $max)->shouldBeInRange($min, $max); | ||
} | ||
|
||
public function getMatchers(): array | ||
{ | ||
return [ | ||
'haveLength' => function ($subject, $length) { | ||
return $length === strlen($subject); | ||
}, | ||
'beInRange' => function ($subject, $min, $max) { | ||
return $subject >= $min && $subject <= $max; | ||
}, | ||
]; | ||
} | ||
} |