1 | <?php |
||
5 | class Random |
||
6 | { |
||
7 | use Generators, CharCase, Faker; |
||
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 | private $prefix; |
||
24 | |||
25 | private $suffix; |
||
26 | |||
27 | /** |
||
28 | * Get a prefixed and/or suffixed string. |
||
29 | * |
||
30 | * @param $value |
||
31 | * @return string |
||
32 | */ |
||
33 | 11 | private function addPrefixSuffix($value) |
|
41 | |||
42 | /** |
||
43 | * Set the string suffix. |
||
44 | * |
||
45 | * @param $string |
||
46 | * @return $this |
||
47 | */ |
||
48 | 1 | public function suffix($string) |
|
54 | |||
55 | /** |
||
56 | * Extract a string pattern from a string. |
||
57 | * |
||
58 | * @param $string |
||
59 | * @return string |
||
60 | */ |
||
61 | 10 | protected function extractPattern($string) |
|
71 | |||
72 | /** |
||
73 | * Get numeric end. |
||
74 | * |
||
75 | * @return int |
||
76 | */ |
||
77 | 1 | public function getEnd() |
|
81 | |||
82 | /** |
||
83 | * Get string pattern. |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | 10 | public function getPattern() |
|
91 | |||
92 | /** |
||
93 | * Configure to get a raw. |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | 1 | public function raw() |
|
98 | { |
||
99 | 1 | $this->pattern = null; |
|
100 | |||
101 | 1 | return $this; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Get the final string size. |
||
106 | * |
||
107 | * @return integer |
||
108 | */ |
||
109 | 10 | public function getSize() |
|
113 | |||
114 | /** |
||
115 | * Get numeric start. |
||
116 | * |
||
117 | * @return int |
||
118 | */ |
||
119 | 1 | public function getStart() |
|
123 | |||
124 | /** |
||
125 | * Generate a random hex. |
||
126 | * |
||
127 | * @return string |
||
128 | */ |
||
129 | 3 | public function hex() |
|
135 | |||
136 | /** |
||
137 | * Set string pattern. |
||
138 | * |
||
139 | * @param string $pattern |
||
140 | * @return $this |
||
141 | */ |
||
142 | 4 | public function pattern($pattern) |
|
148 | |||
149 | /** |
||
150 | * Set the string prefix. |
||
151 | * |
||
152 | * @param $string |
||
153 | * @return $this |
||
154 | */ |
||
155 | 2 | public function prefix($string) |
|
161 | |||
162 | /** |
||
163 | * Trim string to expected size. |
||
164 | * |
||
165 | * @param $string |
||
166 | * @param int|null $size |
||
167 | * @return string |
||
168 | */ |
||
169 | 10 | protected function trimToExpectedSize($string, $size = null) |
|
173 | |||
174 | /** |
||
175 | * Set result to numeric. |
||
176 | * |
||
177 | * @param bool $state |
||
178 | * @return $this |
||
179 | */ |
||
180 | 3 | public function numeric($state = true) |
|
186 | |||
187 | /** |
||
188 | * Set result to alpha. |
||
189 | * |
||
190 | * @param bool $state |
||
191 | * @return $this |
||
192 | */ |
||
193 | 1 | public function alpha($state = true) |
|
199 | |||
200 | /** |
||
201 | * Set numeric end. |
||
202 | * |
||
203 | * @param int $end |
||
204 | * @return $this |
||
205 | */ |
||
206 | 1 | public function end($end) |
|
212 | |||
213 | /** |
||
214 | * Reset one-time values. |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | 11 | public function resetOneTimeValues() |
|
228 | |||
229 | /** |
||
230 | * Set numeric start. |
||
231 | * |
||
232 | * @param int $start |
||
233 | * @return $this |
||
234 | */ |
||
235 | 1 | public function start($start) |
|
241 | |||
242 | /** |
||
243 | * Set the return string size. |
||
244 | * |
||
245 | * @param $size |
||
246 | * @return $this |
||
247 | */ |
||
248 | 10 | public function size($size) |
|
254 | |||
255 | /** |
||
256 | * Get the generated random string/number. |
||
257 | * |
||
258 | * @return string|int |
||
259 | */ |
||
260 | 11 | public function get() |
|
272 | } |
||
273 |