1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PragmaRX\Random; |
4
|
|
|
|
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) |
30
|
|
|
{ |
31
|
8 |
|
if (is_null($pattern = $this->getPattern())) { |
32
|
1 |
|
return $string; |
33
|
|
|
} |
34
|
|
|
|
35
|
7 |
|
preg_match_all("/$pattern/", $string, $matches); |
36
|
|
|
|
37
|
7 |
|
return implode('', $matches[0]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Generate a random string. |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
8 |
|
protected function generate() |
46
|
|
|
{ |
47
|
8 |
|
return $this->numeric |
48
|
3 |
|
? $this->generateNumeric() |
49
|
8 |
|
: $this->generateAlpha(); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
protected function generateInteger() |
53
|
|
|
{ |
54
|
1 |
|
return random_int($this->getStart(), $this->getEnd()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Generate a random string. |
59
|
|
|
* |
60
|
|
|
* @param \Closure $generator |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
8 |
|
protected function generateString($generator) |
64
|
|
|
{ |
65
|
8 |
|
$string = ''; |
66
|
|
|
|
67
|
8 |
|
while (strlen($string) < $size = $this->getSize()) { |
68
|
8 |
|
$string .= $this->extractPattern($generator($size)); |
69
|
|
|
} |
70
|
|
|
|
71
|
8 |
|
return $this->trimToExpectedSize($string, $size); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the alpha generator. |
76
|
|
|
* |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
protected function getAlphaGenerator() |
80
|
|
|
{ |
81
|
7 |
|
return function ($size) { |
82
|
7 |
|
return random_bytes($size); |
83
|
7 |
|
}; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the alpha generator. |
88
|
|
|
* |
89
|
|
|
* @return mixed |
90
|
|
|
*/ |
91
|
|
|
protected function getNumericGenerator() |
92
|
|
|
{ |
93
|
3 |
|
return function () { |
94
|
3 |
|
return random_int(0, PHP_INT_MAX); |
95
|
3 |
|
}; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Generate a random string. |
100
|
|
|
* |
101
|
|
|
* @return int|string |
102
|
|
|
*/ |
103
|
7 |
|
protected function generateAlpha() |
104
|
|
|
{ |
105
|
7 |
|
return $this->generateString($this->getAlphaGenerator()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Generate a numeric random value. |
110
|
|
|
* |
111
|
|
|
* @return int|string |
112
|
|
|
*/ |
113
|
3 |
|
protected function generateNumeric() |
114
|
|
|
{ |
115
|
3 |
|
if (is_null($this->size) && $this->pattern == static::DEFAULT_PATTERN) { |
116
|
1 |
|
return $this->generateInteger(); |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
return $this->generateString($this->getNumericGenerator()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get numeric end. |
124
|
|
|
* |
125
|
|
|
* @return int |
126
|
|
|
*/ |
127
|
1 |
|
public function getEnd() |
128
|
|
|
{ |
129
|
1 |
|
return $this->end; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get string pattern. |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
8 |
|
public function getPattern() |
138
|
|
|
{ |
139
|
8 |
|
return $this->pattern; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get string pattern. |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
1 |
|
public function noPattern() |
148
|
|
|
{ |
149
|
1 |
|
$this->pattern = null; |
150
|
|
|
|
151
|
1 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get the final string size. |
156
|
|
|
* |
157
|
|
|
* @return integer |
158
|
|
|
*/ |
159
|
8 |
|
public function getSize() |
160
|
|
|
{ |
161
|
8 |
|
return $this->size ?: static::DEFAULT_STRING_SIZE; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get numeric start. |
166
|
|
|
* |
167
|
|
|
* @return int |
168
|
|
|
*/ |
169
|
1 |
|
public function getStart() |
170
|
|
|
{ |
171
|
1 |
|
return $this->start; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Generate a random hex. |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
1 |
|
public function hex() |
180
|
|
|
{ |
181
|
1 |
|
$this->pattern('[a-f0-9]')->uppercase(); |
182
|
|
|
|
183
|
1 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Set string pattern. |
188
|
|
|
* |
189
|
|
|
* @param string $pattern |
190
|
|
|
* @return $this |
191
|
|
|
*/ |
192
|
2 |
|
public function pattern($pattern) |
193
|
|
|
{ |
194
|
2 |
|
$this->pattern = $pattern; |
195
|
|
|
|
196
|
2 |
|
return $this; |
197
|
|
|
} |
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) |
207
|
|
|
{ |
208
|
8 |
|
return substr($string, 0, $size ?: $this->getSize()); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Set result to numeric. |
213
|
|
|
* |
214
|
|
|
* @param bool $state |
215
|
|
|
* @return $this |
216
|
|
|
*/ |
217
|
3 |
|
public function numeric($state = true) |
218
|
|
|
{ |
219
|
3 |
|
$this->numeric = $state; |
220
|
|
|
|
221
|
3 |
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Set result to alpha. |
226
|
|
|
* |
227
|
|
|
* @param bool $state |
228
|
|
|
* @return $this |
229
|
|
|
*/ |
230
|
1 |
|
public function alpha($state = true) |
231
|
|
|
{ |
232
|
1 |
|
$this->numeric = !$state; |
233
|
|
|
|
234
|
1 |
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Set numeric end. |
239
|
|
|
* |
240
|
|
|
* @param int $end |
241
|
|
|
* @return $this |
242
|
|
|
*/ |
243
|
1 |
|
public function end($end) |
244
|
|
|
{ |
245
|
1 |
|
$this->end = $end; |
246
|
|
|
|
247
|
1 |
|
return $this; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Set numeric start. |
252
|
|
|
* |
253
|
|
|
* @param int $start |
254
|
|
|
* @return $this |
255
|
|
|
*/ |
256
|
1 |
|
public function start($start) |
257
|
|
|
{ |
258
|
1 |
|
$this->start = $start; |
259
|
|
|
|
260
|
1 |
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Set the return string size. |
266
|
|
|
* |
267
|
|
|
* @param $size |
268
|
|
|
* @return $this |
269
|
|
|
*/ |
270
|
8 |
|
public function size($size) |
271
|
|
|
{ |
272
|
8 |
|
$this->size = $size; |
273
|
|
|
|
274
|
8 |
|
return $this; |
275
|
|
|
} |
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() |
285
|
|
|
{ |
286
|
8 |
|
return $this->changeCase( |
287
|
8 |
|
$this->generate() |
288
|
|
|
); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|