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