1 | <?php |
||
5 | class Random |
||
6 | { |
||
7 | use Generators, 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 | * Get numeric end. |
||
42 | * |
||
43 | * @return int |
||
44 | */ |
||
45 | 1 | public function getEnd() |
|
49 | |||
50 | /** |
||
51 | * Get string pattern. |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | 8 | public function getPattern() |
|
59 | |||
60 | /** |
||
61 | * Get string pattern. |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | 1 | public function noPattern() |
|
71 | |||
72 | /** |
||
73 | * Get the final string size. |
||
74 | * |
||
75 | * @return integer |
||
76 | */ |
||
77 | 8 | public function getSize() |
|
81 | |||
82 | /** |
||
83 | * Get numeric start. |
||
84 | * |
||
85 | * @return int |
||
86 | */ |
||
87 | 1 | public function getStart() |
|
91 | |||
92 | /** |
||
93 | * Generate a random hex. |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | 1 | public function hex() |
|
103 | |||
104 | /** |
||
105 | * Set string pattern. |
||
106 | * |
||
107 | * @param string $pattern |
||
108 | * @return $this |
||
109 | */ |
||
110 | 2 | public function pattern($pattern) |
|
116 | |||
117 | /** |
||
118 | * Trim string to expected size. |
||
119 | * |
||
120 | * @param $string |
||
121 | * @param int|null $size |
||
122 | * @return string |
||
123 | */ |
||
124 | 8 | protected function trimToExpectedSize($string, $size = null) |
|
128 | |||
129 | /** |
||
130 | * Set result to numeric. |
||
131 | * |
||
132 | * @param bool $state |
||
133 | * @return $this |
||
134 | */ |
||
135 | 3 | public function numeric($state = true) |
|
141 | |||
142 | /** |
||
143 | * Set result to alpha. |
||
144 | * |
||
145 | * @param bool $state |
||
146 | * @return $this |
||
147 | */ |
||
148 | 1 | public function alpha($state = true) |
|
154 | |||
155 | /** |
||
156 | * Set numeric end. |
||
157 | * |
||
158 | * @param int $end |
||
159 | * @return $this |
||
160 | */ |
||
161 | 1 | public function end($end) |
|
167 | |||
168 | /** |
||
169 | * Set numeric start. |
||
170 | * |
||
171 | * @param int $start |
||
172 | * @return $this |
||
173 | */ |
||
174 | 1 | public function start($start) |
|
180 | |||
181 | |||
182 | /** |
||
183 | * Set the return string size. |
||
184 | * |
||
185 | * @param $size |
||
186 | * @return $this |
||
187 | */ |
||
188 | 8 | public function size($size) |
|
194 | |||
195 | /** |
||
196 | * Generate a more truly "random" alpha-numeric string. |
||
197 | * |
||
198 | * Extracted from Laravel Framework: Illuminate\Support\Str |
||
199 | * |
||
200 | * @return string|int |
||
201 | */ |
||
202 | 8 | public function get() |
|
208 | } |
||
209 |