| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Framework\Randomizer; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use FluentCommunity\Framework\Support\InvalidArgumentException; |
| 7 |
|
| 8 |
/** |
| 9 |
* An implementation of the Randomizer class. |
| 10 |
* @see https://www.php.net/manual/en/class.random-randomizer.php |
| 11 |
*/ |
| 12 |
if (class_exists('Random\Randomizer')) { |
| 13 |
final class Randomizer |
| 14 |
{ |
| 15 |
use GetStringTrait; |
| 16 |
|
| 17 |
private $randomizer; |
| 18 |
|
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
// @phpstan-ignore-next-line |
| 22 |
$this->randomizer = new \Random\Randomizer(); |
| 23 |
} |
| 24 |
|
| 25 |
public function getBytesFromString(string $string, int $length): string |
| 26 |
{ |
| 27 |
return $this->randomizer->getBytesFromString($string, $length); |
| 28 |
} |
| 29 |
|
| 30 |
public function __call($method, $args = []) |
| 31 |
{ |
| 32 |
return $this->randomizer->{$method}(...$args); |
| 33 |
} |
| 34 |
} |
| 35 |
} else { |
| 36 |
final class Randomizer |
| 37 |
{ |
| 38 |
use GetStringTrait; |
| 39 |
|
| 40 |
private $engine; |
| 41 |
|
| 42 |
public function __construct() |
| 43 |
{ |
| 44 |
$this->engine = $this->makeEngine(); |
| 45 |
} |
| 46 |
|
| 47 |
public function getBytes(int $length) |
| 48 |
{ |
| 49 |
$result = ''; |
| 50 |
|
| 51 |
while (strlen($result) < $length) { |
| 52 |
$result .= $this->engine->generate(); |
| 53 |
} |
| 54 |
|
| 55 |
return substr($result, 0, $length); |
| 56 |
} |
| 57 |
|
| 58 |
public function getBytesFromString(string $string, int $length) |
| 59 |
{ |
| 60 |
if ($length === 0) { |
| 61 |
throw new InvalidArgumentException('Length cannot be zero.'); |
| 62 |
} |
| 63 |
|
| 64 |
$sourceLength = strlen($string); |
| 65 |
|
| 66 |
if ($sourceLength === 0) { |
| 67 |
throw new InvalidArgumentException( |
| 68 |
'Source string cannot be empty.' |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
$result = []; |
| 73 |
|
| 74 |
for ($i = 0; $i < $length; $i++) { |
| 75 |
$index = $this->getInt(0, $sourceLength - 1); |
| 76 |
$result[] = $string[$index]; |
| 77 |
} |
| 78 |
|
| 79 |
return implode('', $result); |
| 80 |
} |
| 81 |
|
| 82 |
public function getFloat(float $min, float $max) |
| 83 |
{ |
| 84 |
if ($min >= $max) { |
| 85 |
throw new InvalidArgumentException( |
| 86 |
'The minimum value must be less than the maximum value.' |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
$randomFraction = $this->nextFloat(); |
| 91 |
|
| 92 |
return $min + ($randomFraction * ($max - $min)); |
| 93 |
} |
| 94 |
|
| 95 |
public function getInt(int $min, int $max) |
| 96 |
{ |
| 97 |
$range = $max - $min + 1; |
| 98 |
return $min + random_int(0, $range - 1); |
| 99 |
} |
| 100 |
|
| 101 |
public function nextFloat() |
| 102 |
{ |
| 103 |
return random_int(0, PHP_INT_MAX) / (PHP_INT_MAX + 1); |
| 104 |
} |
| 105 |
|
| 106 |
public function nextInt() |
| 107 |
{ |
| 108 |
return random_int(0, PHP_INT_MAX); |
| 109 |
} |
| 110 |
|
| 111 |
public function pickArrayKeys(array $array, int $num) |
| 112 |
{ |
| 113 |
if ($num > count($array)) { |
| 114 |
throw new InvalidArgumentException( |
| 115 |
'Cannot pick more keys than the array size.' |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
$pickedKeys = []; |
| 120 |
|
| 121 |
$keys = array_keys($array); |
| 122 |
|
| 123 |
while (count($pickedKeys) < $num) { |
| 124 |
$index = random_int(0, count($keys) - 1); |
| 125 |
$pickedKeys[] = $keys[$index]; |
| 126 |
array_splice($keys, $index, 1); |
| 127 |
} |
| 128 |
|
| 129 |
return $pickedKeys; |
| 130 |
} |
| 131 |
|
| 132 |
public function shuffleArray(array $array) |
| 133 |
{ |
| 134 |
$count = count($array); |
| 135 |
|
| 136 |
if ($count < 2) { |
| 137 |
return $array; |
| 138 |
} |
| 139 |
|
| 140 |
$originalArray = $array; |
| 141 |
|
| 142 |
// Shuffle and check if the result is different |
| 143 |
do { |
| 144 |
// Perform the Fisher-Yates shuffle |
| 145 |
for ($i = $count - 1; $i > 0; $i--) { |
| 146 |
$j = random_int(0, $i); |
| 147 |
[$array[$i], $array[$j]] = [$array[$j], $array[$i]]; |
| 148 |
} |
| 149 |
} while ($array === $originalArray); |
| 150 |
|
| 151 |
return $array; |
| 152 |
} |
| 153 |
|
| 154 |
public function shuffleBytes(string $bytes) |
| 155 |
{ |
| 156 |
$array = str_split($bytes); |
| 157 |
$shuffled = $this->shuffleArray($array); |
| 158 |
return implode('', $shuffled); |
| 159 |
} |
| 160 |
|
| 161 |
private function makeEngine() |
| 162 |
{ |
| 163 |
return new class { |
| 164 |
public function generate() { |
| 165 |
$length = 32; |
| 166 |
|
| 167 |
if (function_exists('random_bytes')) { |
| 168 |
return random_bytes($length); |
| 169 |
} |
| 170 |
|
| 171 |
if (function_exists('openssl_random_pseudo_bytes')) { |
| 172 |
return openssl_random_pseudo_bytes($length); |
| 173 |
} |
| 174 |
|
| 175 |
throw new Exception('No secure random source available.'); |
| 176 |
} |
| 177 |
}; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|