Skip to content

Commit

Permalink
Redesign Randomizer, extract into small self-contained "drivers"
Browse files Browse the repository at this point in the history
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
aedart committed Mar 15, 2024
1 parent 6d2c467 commit ccaf926
Show file tree
Hide file tree
Showing 20 changed files with 468 additions and 251 deletions.
2 changes: 1 addition & 1 deletion packages/Contracts/src/Utils/Random/ArrayRandomizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions packages/Contracts/src/Utils/Random/Factory.php
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;
}
2 changes: 1 addition & 1 deletion packages/Contracts/src/Utils/Random/NumericRandomizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 3 additions & 10 deletions packages/Contracts/src/Utils/Random/Randomizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 3 additions & 3 deletions packages/Contracts/src/Utils/Random/StringRandomizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,5 +32,5 @@ public function bytes(int $length): string;
*
* @throws Throwable
*/
public function shuffleBytes(string $bytes): string;
public function shuffle(string $bytes): string;
}
27 changes: 27 additions & 0 deletions packages/Contracts/src/Utils/Random/Type.php
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;
}
3 changes: 2 additions & 1 deletion packages/Utils/src/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Aedart\Utils;

use Aedart\Contracts\Utils\Random\ArrayRandomizer;
use Aedart\Contracts\Utils\Random\Type;
use Aedart\Utils\Random\Factory;
use Illuminate\Support\Arr as ArrBase;
use InvalidArgumentException;
Expand All @@ -26,7 +27,7 @@ class Arr extends ArrBase
*/
public static function randomizer(Engine|null $engine = null): ArrayRandomizer
{
return Factory::make($engine);
return Factory::make(Type::Array, $engine);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/Utils/src/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Aedart\Utils;

use Aedart\Contracts\Utils\Random\NumericRandomizer;
use Aedart\Contracts\Utils\Random\Type;
use Aedart\Utils\Random\Factory;
use Random\Engine;
use RuntimeException;
Expand All @@ -25,7 +26,7 @@ class Math
*/
public static function randomizer(Engine|null $engine = null): NumericRandomizer
{
return Factory::make($engine);
return Factory::make(Type::Numeric, $engine);
}

/**
Expand Down
36 changes: 27 additions & 9 deletions packages/Utils/src/Random/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
102 changes: 0 additions & 102 deletions packages/Utils/src/Random/Randomizer.php

This file was deleted.

30 changes: 30 additions & 0 deletions packages/Utils/src/Random/Types/ArrayRandomizer.php
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);
}
}
Loading

0 comments on commit ccaf926

Please sign in to comment.