1 | <?php |
||
5 | class Random |
||
6 | { |
||
7 | const DEFAULT_STRING_SIZE = 16; |
||
8 | |||
9 | const DEFAULT_PATTERN = '[A-Za-z0-9]'; |
||
10 | |||
11 | protected $lowercase = false; |
||
12 | |||
13 | protected $uppercase = false; |
||
14 | |||
15 | protected $numeric = false; |
||
16 | |||
17 | protected $start = 0; |
||
18 | |||
19 | protected $end = PHP_INT_MAX; |
||
20 | |||
21 | protected $size = null; |
||
22 | |||
23 | protected $pattern = '[A-Za-z0-9]'; |
||
24 | |||
25 | /** |
||
26 | * Extract a string pattern from a string. |
||
27 | * |
||
28 | * @param $string |
||
29 | * @return string |
||
30 | */ |
||
31 | 8 | private function extractPattern($string) |
|
41 | |||
42 | /** |
||
43 | * Generate a random string. |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | 8 | protected function generate() |
|
53 | |||
54 | 1 | private function generateInteger() |
|
58 | |||
59 | /** |
||
60 | * Generate a random string. |
||
61 | * |
||
62 | * @param \Closure $generator |
||
63 | * @return mixed |
||
64 | */ |
||
65 | 8 | private function generateString($generator) |
|
75 | |||
76 | /** |
||
77 | * Get the alpha generator. |
||
78 | * |
||
79 | * @return mixed |
||
80 | */ |
||
81 | private function getAlphaGenerator() |
||
87 | |||
88 | /** |
||
89 | * Get the alpha generator. |
||
90 | * |
||
91 | * @return mixed |
||
92 | */ |
||
93 | private function getNumericGenerator() |
||
99 | |||
100 | /** |
||
101 | * Generate a random string. |
||
102 | * |
||
103 | * @return int|string |
||
104 | */ |
||
105 | 7 | private function generateAlpha() |
|
109 | |||
110 | /** |
||
111 | * Generate a numeric random value. |
||
112 | * |
||
113 | * @return int|string |
||
114 | */ |
||
115 | 3 | private function generateNumeric() |
|
123 | |||
124 | /** |
||
125 | * Get numeric end. |
||
126 | * |
||
127 | * @return int |
||
128 | */ |
||
129 | 1 | public function getEnd() |
|
133 | |||
134 | /** |
||
135 | * Get string pattern. |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | 8 | public function getPattern() |
|
143 | |||
144 | /** |
||
145 | * Get string pattern. |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | 1 | public function noPattern() |
|
155 | |||
156 | /** |
||
157 | * Get the final string size. |
||
158 | * |
||
159 | * @return integer |
||
160 | */ |
||
161 | 8 | public function getSize() |
|
165 | |||
166 | /** |
||
167 | * Get numeric start. |
||
168 | * |
||
169 | * @return int |
||
170 | */ |
||
171 | 1 | public function getStart() |
|
175 | |||
176 | /** |
||
177 | * Get lowercase state. |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | 8 | public function isLowercase() |
|
185 | |||
186 | /** |
||
187 | * Get uppercase state. |
||
188 | * |
||
189 | * @return bool |
||
190 | */ |
||
191 | 8 | public function isUppercase() |
|
195 | |||
196 | /** |
||
197 | * Generate a random hex. |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | 1 | public function hex() |
|
207 | |||
208 | /** |
||
209 | * Return a string in the proper case. |
||
210 | * |
||
211 | * @param $string |
||
212 | * @return string |
||
213 | */ |
||
214 | 8 | private function changeCase($string) |
|
226 | |||
227 | /** |
||
228 | * Set the lowercase state. |
||
229 | * |
||
230 | * @param $state |
||
231 | * @return $this |
||
232 | */ |
||
233 | 1 | public function lowercase($state = true) |
|
239 | |||
240 | /** |
||
241 | * Set string pattern. |
||
242 | * |
||
243 | * @param string $pattern |
||
244 | * @return $this |
||
245 | */ |
||
246 | 2 | public function pattern($pattern) |
|
252 | |||
253 | /** |
||
254 | * Trim string to expected size. |
||
255 | * |
||
256 | * @param $string |
||
257 | * @param int|null $size |
||
258 | * @return string |
||
259 | */ |
||
260 | 8 | private function trimToExpectedSize($string, $size = null) |
|
264 | |||
265 | /** |
||
266 | * Set case to mixed. |
||
267 | * |
||
268 | * @return $this |
||
269 | */ |
||
270 | 2 | public function mixedcase() |
|
278 | |||
279 | /** |
||
280 | * Set result to numeric. |
||
281 | * |
||
282 | * @param bool $state |
||
283 | * @return $this |
||
284 | */ |
||
285 | 3 | public function numeric($state = true) |
|
291 | |||
292 | /** |
||
293 | * Set result to alpha. |
||
294 | * |
||
295 | * @param bool $state |
||
296 | * @return $this |
||
297 | */ |
||
298 | 1 | public function alpha($state = true) |
|
304 | |||
305 | /** |
||
306 | * Set numeric end. |
||
307 | * |
||
308 | * @param int $end |
||
309 | * @return $this |
||
310 | */ |
||
311 | 1 | public function end($end) |
|
317 | |||
318 | /** |
||
319 | * Set numeric start. |
||
320 | * |
||
321 | * @param int $start |
||
322 | * @return $this |
||
323 | */ |
||
324 | 1 | public function start($start) |
|
330 | |||
331 | /** |
||
332 | * Set the uppercase state. |
||
333 | * |
||
334 | * @param $state |
||
335 | * @return $this |
||
336 | * @internal param bool $uppercase |
||
337 | */ |
||
338 | 2 | public function uppercase($state = true) |
|
344 | |||
345 | /** |
||
346 | * Set the return string size. |
||
347 | * |
||
348 | * @param $size |
||
349 | * @return $this |
||
350 | */ |
||
351 | 8 | public function size($size) |
|
357 | |||
358 | /** |
||
359 | * Generate a more truly "random" alpha-numeric string. |
||
360 | * |
||
361 | * Extracted from Laravel Framework: Illuminate\Support\Str |
||
362 | * |
||
363 | * @return string|int |
||
364 | */ |
||
365 | 8 | public function get() |
|
371 | } |
||
372 |