1 | <?php |
||
5 | class Random |
||
6 | { |
||
7 | use Generators, CharCase, Faker, Trivia; |
||
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 $array = false; |
||
22 | |||
23 | protected $items = []; |
||
24 | |||
25 | protected $count = 1; |
||
26 | |||
27 | protected $pattern = '[A-Za-z0-9]'; |
||
28 | |||
29 | private $prefix; |
||
30 | |||
31 | private $suffix; |
||
32 | |||
33 | /** |
||
34 | * Get a prefixed and/or suffixed string. |
||
35 | * |
||
36 | * @param $value |
||
37 | * @return string |
||
38 | */ |
||
39 | 12 | private function addPrefixSuffix($value) |
|
40 | { |
||
41 | 12 | if (!is_null($this->prefix) || !is_null($this->suffix)) { |
|
42 | 3 | return (string) $this->prefix . $value . (string) $this->suffix; |
|
43 | } |
||
44 | |||
45 | 10 | return $value; |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * Set the array items count. |
||
50 | * |
||
51 | * @param $count |
||
52 | * @return $this |
||
53 | */ |
||
54 | 1 | public function count($count) |
|
55 | { |
||
56 | 1 | $this->count = $count; |
|
57 | |||
58 | 1 | return $this; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * Set the string suffix. |
||
63 | * |
||
64 | * @param $string |
||
65 | * @return $this |
||
66 | */ |
||
67 | 1 | public function suffix($string) |
|
73 | |||
74 | /** |
||
75 | * Extract a string pattern from a string. |
||
76 | * |
||
77 | * @param $string |
||
78 | * @return string |
||
79 | */ |
||
80 | 10 | protected function extractPattern($string) |
|
90 | |||
91 | /** |
||
92 | * Get numeric end. |
||
93 | * |
||
94 | * @return int |
||
95 | */ |
||
96 | 1 | public function getEnd() |
|
100 | |||
101 | /** |
||
102 | * Get string pattern. |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | 10 | public function getPattern() |
|
110 | |||
111 | /** |
||
112 | * Configure to get a raw. |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | 1 | public function raw() |
|
117 | { |
||
118 | 1 | $this->pattern = null; |
|
119 | |||
120 | 1 | return $this; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * Get the final string size. |
||
125 | * |
||
126 | * @return integer |
||
127 | */ |
||
128 | 10 | public function getSize() |
|
132 | |||
133 | /** |
||
134 | * Get numeric start. |
||
135 | * |
||
136 | * @return int |
||
137 | */ |
||
138 | 1 | public function getStart() |
|
142 | |||
143 | /** |
||
144 | * Generate a random hex. |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | 3 | public function hex() |
|
154 | |||
155 | /** |
||
156 | * Set string pattern. |
||
157 | * |
||
158 | * @param string $pattern |
||
159 | * @return $this |
||
160 | */ |
||
161 | 4 | public function pattern($pattern) |
|
167 | |||
168 | /** |
||
169 | * Set the string prefix. |
||
170 | * |
||
171 | * @param $string |
||
172 | * @return $this |
||
173 | */ |
||
174 | 2 | public function prefix($string) |
|
180 | |||
181 | /** |
||
182 | * Trim string to expected size. |
||
183 | * |
||
184 | * @param $string |
||
185 | * @param int|null $size |
||
186 | * @return string |
||
187 | */ |
||
188 | 10 | protected function trimToExpectedSize($string, $size = null) |
|
192 | |||
193 | /** |
||
194 | * Set result to numeric. |
||
195 | * |
||
196 | * @param bool $state |
||
197 | * @return $this |
||
198 | */ |
||
199 | 3 | public function numeric($state = true) |
|
205 | |||
206 | /** |
||
207 | * Set result to alpha. |
||
208 | * |
||
209 | * @param bool $state |
||
210 | * @return $this |
||
211 | */ |
||
212 | 1 | public function alpha($state = true) |
|
218 | |||
219 | /** |
||
220 | * Set numeric end. |
||
221 | * |
||
222 | * @param int $end |
||
223 | * @return $this |
||
224 | */ |
||
225 | 1 | public function end($end) |
|
231 | |||
232 | /** |
||
233 | * Reset one-time values. |
||
234 | * |
||
235 | * @return $this |
||
236 | */ |
||
237 | 12 | public function resetOneTimeValues() |
|
247 | |||
248 | /** |
||
249 | * Set numeric start. |
||
250 | * |
||
251 | * @param int $start |
||
252 | * @return $this |
||
253 | */ |
||
254 | 1 | public function start($start) |
|
260 | |||
261 | /** |
||
262 | * Set the return string size. |
||
263 | * |
||
264 | * @param $size |
||
265 | * @return $this |
||
266 | */ |
||
267 | 10 | public function size($size) |
|
273 | |||
274 | /** |
||
275 | * Get the generated random string/number. |
||
276 | * |
||
277 | * @return string|int |
||
278 | */ |
||
279 | 12 | public function get() |
|
291 | } |
||
292 |