1 | <?php |
||
5 | class Random |
||
6 | { |
||
7 | use CharCase; |
||
8 | |||
9 | const DEFAULT_STRING_SIZE = 16; |
||
10 | |||
11 | const DEFAULT_PATTERN = '[A-Za-z0-9]'; |
||
12 | |||
13 | protected $numeric = false; |
||
14 | |||
15 | protected $start = 0; |
||
16 | |||
17 | protected $end = PHP_INT_MAX; |
||
18 | |||
19 | protected $size = null; |
||
20 | |||
21 | protected $pattern = '[A-Za-z0-9]'; |
||
22 | |||
23 | /** |
||
24 | * Extract a string pattern from a string. |
||
25 | * |
||
26 | * @param $string |
||
27 | * @return string |
||
28 | */ |
||
29 | 8 | protected function extractPattern($string) |
|
39 | |||
40 | /** |
||
41 | * Generate a random string. |
||
42 | * |
||
43 | * @return string |
||
44 | */ |
||
45 | 8 | protected function generate() |
|
51 | |||
52 | 1 | protected function generateInteger() |
|
56 | |||
57 | /** |
||
58 | * Generate a random string. |
||
59 | * |
||
60 | * @param \Closure $generator |
||
61 | * @return mixed |
||
62 | */ |
||
63 | 8 | protected function generateString($generator) |
|
73 | |||
74 | /** |
||
75 | * Get the alpha generator. |
||
76 | * |
||
77 | * @return mixed |
||
78 | */ |
||
79 | protected function getAlphaGenerator() |
||
85 | |||
86 | /** |
||
87 | * Get the alpha generator. |
||
88 | * |
||
89 | * @return mixed |
||
90 | */ |
||
91 | protected function getNumericGenerator() |
||
97 | |||
98 | /** |
||
99 | * Generate a random string. |
||
100 | * |
||
101 | * @return int|string |
||
102 | */ |
||
103 | 7 | protected function generateAlpha() |
|
107 | |||
108 | /** |
||
109 | * Generate a numeric random value. |
||
110 | * |
||
111 | * @return int|string |
||
112 | */ |
||
113 | 3 | protected function generateNumeric() |
|
121 | |||
122 | /** |
||
123 | * Get numeric end. |
||
124 | * |
||
125 | * @return int |
||
126 | */ |
||
127 | 1 | public function getEnd() |
|
131 | |||
132 | /** |
||
133 | * Get string pattern. |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | 8 | public function getPattern() |
|
141 | |||
142 | /** |
||
143 | * Get string pattern. |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | 1 | public function noPattern() |
|
153 | |||
154 | /** |
||
155 | * Get the final string size. |
||
156 | * |
||
157 | * @return integer |
||
158 | */ |
||
159 | 8 | public function getSize() |
|
163 | |||
164 | /** |
||
165 | * Get numeric start. |
||
166 | * |
||
167 | * @return int |
||
168 | */ |
||
169 | 1 | public function getStart() |
|
173 | |||
174 | /** |
||
175 | * Generate a random hex. |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | 1 | public function hex() |
|
185 | |||
186 | /** |
||
187 | * Set string pattern. |
||
188 | * |
||
189 | * @param string $pattern |
||
190 | * @return $this |
||
191 | */ |
||
192 | 2 | public function pattern($pattern) |
|
198 | |||
199 | /** |
||
200 | * Trim string to expected size. |
||
201 | * |
||
202 | * @param $string |
||
203 | * @param int|null $size |
||
204 | * @return string |
||
205 | */ |
||
206 | 8 | protected function trimToExpectedSize($string, $size = null) |
|
210 | |||
211 | /** |
||
212 | * Set result to numeric. |
||
213 | * |
||
214 | * @param bool $state |
||
215 | * @return $this |
||
216 | */ |
||
217 | 3 | public function numeric($state = true) |
|
223 | |||
224 | /** |
||
225 | * Set result to alpha. |
||
226 | * |
||
227 | * @param bool $state |
||
228 | * @return $this |
||
229 | */ |
||
230 | 1 | public function alpha($state = true) |
|
236 | |||
237 | /** |
||
238 | * Set numeric end. |
||
239 | * |
||
240 | * @param int $end |
||
241 | * @return $this |
||
242 | */ |
||
243 | 1 | public function end($end) |
|
249 | |||
250 | /** |
||
251 | * Set numeric start. |
||
252 | * |
||
253 | * @param int $start |
||
254 | * @return $this |
||
255 | */ |
||
256 | 1 | public function start($start) |
|
262 | |||
263 | |||
264 | /** |
||
265 | * Set the return string size. |
||
266 | * |
||
267 | * @param $size |
||
268 | * @return $this |
||
269 | */ |
||
270 | 8 | public function size($size) |
|
276 | |||
277 | /** |
||
278 | * Generate a more truly "random" alpha-numeric string. |
||
279 | * |
||
280 | * Extracted from Laravel Framework: Illuminate\Support\Str |
||
281 | * |
||
282 | * @return string|int |
||
283 | */ |
||
284 | 8 | public function get() |
|
290 | } |
||
291 |