PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.40
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.40
2.1.0 2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 All 42 releases
fluent-boards / vendor / wpfluent / framework / src / WPFluent / Randomizer / Randomizer.php

Randomizer.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.40, at vendor/wpfluent/framework/src/WPFluent/Randomizer/Randomizer.php

180 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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