-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redesign Randomizer, extract into small self-contained "drivers"
Now each type of randomizer has its own responsibilities and less changes of naming conflicts (e.g. shuffleBytes() vs. shuffleArray() have both been renamed to shuffle()). #150, #151
- Loading branch information
Showing
20 changed files
with
468 additions
and
251 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
* @author Alin Eugen Deac <[email protected]> | ||
* @package Aedart\Contracts\Utils\Random | ||
*/ | ||
interface ArrayRandomizer | ||
interface ArrayRandomizer extends Randomizer | ||
{ | ||
/** | ||
* Returns random array keys | ||
|
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,24 @@ | ||
<?php | ||
|
||
namespace Aedart\Contracts\Utils\Random; | ||
|
||
use Random\Engine; | ||
|
||
/** | ||
* Randomizer Factory | ||
* | ||
* @author Alin Eugen Deac <[email protected]> | ||
* @package Aedart\Contracts\Utils\Random | ||
*/ | ||
interface Factory | ||
{ | ||
/** | ||
* Returns a new randomizer instance of given type | ||
* | ||
* @param Type $type | ||
* @param Engine|null $engine [optional] | ||
* | ||
* @return Randomizer | ||
*/ | ||
public static function make(Type $type, Engine|null $engine = null): Randomizer; | ||
} |
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 |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
* @author Alin Eugen Deac <[email protected]> | ||
* @package Aedart\Contracts\Utils\Random | ||
*/ | ||
interface NumericRandomizer | ||
interface NumericRandomizer extends Randomizer | ||
{ | ||
/** | ||
* Returns a uniformly selected integer | ||
|
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 |
---|---|---|
|
@@ -2,21 +2,14 @@ | |
|
||
namespace Aedart\Contracts\Utils\Random; | ||
|
||
use Aedart\Contracts\Utils\HasDriver; | ||
|
||
/** | ||
* Randomizer | ||
* | ||
* @author Alin Eugen Deac <[email protected]> | ||
* @package Aedart\Contracts\Utils\Random | ||
*/ | ||
interface Randomizer extends | ||
StringRandomizer, | ||
NumericRandomizer, | ||
ArrayRandomizer | ||
interface Randomizer extends HasDriver | ||
{ | ||
/** | ||
* Returns the underlying driver of this randomizer | ||
* | ||
* @return mixed | ||
*/ | ||
public function driver(): mixed; | ||
} |
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 |
---|---|---|
|
@@ -5,12 +5,12 @@ | |
use Throwable; | ||
|
||
/** | ||
* String Randomizer | ||
* String (Bytes) Randomizer | ||
* | ||
* @author Alin Eugen Deac <[email protected]> | ||
* @package Aedart\Contracts\Utils\Random | ||
*/ | ||
interface StringRandomizer | ||
interface StringRandomizer extends Randomizer | ||
{ | ||
/** | ||
* Returns random bytes | ||
|
@@ -32,5 +32,5 @@ public function bytes(int $length): string; | |
* | ||
* @throws Throwable | ||
*/ | ||
public function shuffleBytes(string $bytes): string; | ||
public function shuffle(string $bytes): string; | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace Aedart\Contracts\Utils\Random; | ||
|
||
/** | ||
* Randomizer Type | ||
* | ||
* @author Alin Eugen Deac <[email protected]> | ||
* @package Aedart\Contracts\Utils\Random | ||
*/ | ||
enum Type | ||
{ | ||
/** | ||
* Array Randomizer | ||
*/ | ||
case Array; | ||
|
||
/** | ||
* Numeric Randomizer | ||
*/ | ||
case Numeric; | ||
|
||
/** | ||
* String (bytes) Randomizer | ||
*/ | ||
case String; | ||
} |
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 |
---|---|---|
|
@@ -2,9 +2,14 @@ | |
|
||
namespace Aedart\Utils\Random; | ||
|
||
use Aedart\Contracts\Utils\Random\Factory as RandomizerFactory; | ||
use Aedart\Contracts\Utils\Random\Randomizer as RandomizerInterface; | ||
use Aedart\Contracts\Utils\Random\Type; | ||
use Aedart\Utils\Random\Types\ArrayRandomizer; | ||
use Aedart\Utils\Random\Types\NumericRandomizer; | ||
use Aedart\Utils\Random\Types\StringRandomizer; | ||
use Random\Engine; | ||
use Random\Randomizer as NativeRandomizer; | ||
use Random\Randomizer; | ||
|
||
/** | ||
* Randomizer Factory | ||
|
@@ -14,21 +19,34 @@ | |
* @author Alin Eugen Deac <[email protected]> | ||
* @package Aedart\Utils\Random | ||
*/ | ||
class Factory | ||
class Factory implements RandomizerFactory | ||
{ | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function make(Type $type, Engine|null $engine = null): RandomizerInterface | ||
{ | ||
$driver = static::makeDriver($engine); | ||
|
||
return match($type) { | ||
Type::Array => new ArrayRandomizer($driver), | ||
Type::String => new StringRandomizer($driver), | ||
Type::Numeric => new NumericRandomizer($driver) | ||
}; | ||
} | ||
|
||
/** | ||
* Returns a new Randomizer instance, using given engine | ||
* Returns a new driver instance | ||
* | ||
* @see https://www.php.net/manual/en/random-randomizer.construct.php | ||
* @see https://www.php.net/manual/en/class.random-randomizer.php | ||
* | ||
* @param Engine|null $engine [optional] | ||
* | ||
* @return RandomizerInterface | ||
* @return Randomizer | ||
*/ | ||
public static function make(Engine|null $engine = null): RandomizerInterface | ||
protected static function makeDriver(Engine|null $engine = null): Randomizer | ||
{ | ||
$driver = new NativeRandomizer($engine); | ||
|
||
return new Randomizer($driver); | ||
return new Randomizer($engine); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,30 @@ | ||
<?php | ||
|
||
namespace Aedart\Utils\Random\Types; | ||
|
||
use Aedart\Contracts\Utils\Random\ArrayRandomizer as ArrayRandomizerInterface; | ||
|
||
/** | ||
* Array Randomizer | ||
* | ||
* @author Alin Eugen Deac <[email protected]> | ||
* @package Aedart\Utils\Random\Types | ||
*/ | ||
class ArrayRandomizer extends BaseRandomizer implements ArrayRandomizerInterface | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function pickKeys(array $arr, int $amount): array | ||
{ | ||
return $this->driver()->pickArrayKeys($arr, $amount); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function shuffle(array $arr): array | ||
{ | ||
return $this->driver()->shuffleArray($arr); | ||
} | ||
} |
Oops, something went wrong.