1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PragmaRX\Random; |
4
|
|
|
|
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) |
34
|
|
|
{ |
35
|
11 |
|
if (!is_null($this->prefix) || !is_null($this->suffix)) { |
36
|
3 |
|
return (string) $this->prefix . $value . (string) $this->suffix; |
37
|
|
|
} |
38
|
|
|
|
39
|
9 |
|
return $value; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set the string suffix. |
44
|
|
|
* |
45
|
|
|
* @param $string |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
1 |
|
public function suffix($string) |
49
|
|
|
{ |
50
|
1 |
|
$this->suffix = $string; |
51
|
|
|
|
52
|
1 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Extract a string pattern from a string. |
57
|
|
|
* |
58
|
|
|
* @param $string |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
10 |
|
protected function extractPattern($string) |
62
|
|
|
{ |
63
|
10 |
|
if (is_null($pattern = $this->getPattern())) { |
64
|
1 |
|
return $string; |
65
|
|
|
} |
66
|
|
|
|
67
|
9 |
|
preg_match_all("/$pattern/", $string, $matches); |
68
|
|
|
|
69
|
9 |
|
return implode('', $matches[0]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get numeric end. |
74
|
|
|
* |
75
|
|
|
* @return int |
76
|
|
|
*/ |
77
|
1 |
|
public function getEnd() |
78
|
|
|
{ |
79
|
1 |
|
return $this->end; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get string pattern. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
10 |
|
public function getPattern() |
88
|
|
|
{ |
89
|
10 |
|
return $this->pattern; |
90
|
|
|
} |
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() |
110
|
|
|
{ |
111
|
10 |
|
return $this->size ?: static::DEFAULT_STRING_SIZE; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get numeric start. |
116
|
|
|
* |
117
|
|
|
* @return int |
118
|
|
|
*/ |
119
|
1 |
|
public function getStart() |
120
|
|
|
{ |
121
|
1 |
|
return $this->start; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Generate a random hex. |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
3 |
|
public function hex() |
130
|
|
|
{ |
131
|
3 |
|
$this->pattern('[a-f0-9]')->uppercase(); |
132
|
|
|
|
133
|
3 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set string pattern. |
138
|
|
|
* |
139
|
|
|
* @param string $pattern |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
4 |
|
public function pattern($pattern) |
143
|
|
|
{ |
144
|
4 |
|
$this->pattern = $pattern; |
145
|
|
|
|
146
|
4 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Set the string prefix. |
151
|
|
|
* |
152
|
|
|
* @param $string |
153
|
|
|
* @return $this |
154
|
|
|
*/ |
155
|
2 |
|
public function prefix($string) |
156
|
|
|
{ |
157
|
2 |
|
$this->prefix = $string; |
158
|
|
|
|
159
|
2 |
|
return $this; |
160
|
|
|
} |
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) |
170
|
|
|
{ |
171
|
10 |
|
return substr($string, 0, $size ?: $this->getSize()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Set result to numeric. |
176
|
|
|
* |
177
|
|
|
* @param bool $state |
178
|
|
|
* @return $this |
179
|
|
|
*/ |
180
|
3 |
|
public function numeric($state = true) |
181
|
|
|
{ |
182
|
3 |
|
$this->numeric = $state; |
183
|
|
|
|
184
|
3 |
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Set result to alpha. |
189
|
|
|
* |
190
|
|
|
* @param bool $state |
191
|
|
|
* @return $this |
192
|
|
|
*/ |
193
|
1 |
|
public function alpha($state = true) |
194
|
|
|
{ |
195
|
1 |
|
$this->numeric = !$state; |
196
|
|
|
|
197
|
1 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Set numeric end. |
202
|
|
|
* |
203
|
|
|
* @param int $end |
204
|
|
|
* @return $this |
205
|
|
|
*/ |
206
|
1 |
|
public function end($end) |
207
|
|
|
{ |
208
|
1 |
|
$this->end = $end; |
209
|
|
|
|
210
|
1 |
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Reset one-time values. |
215
|
|
|
* |
216
|
|
|
* @return $this |
217
|
|
|
*/ |
218
|
11 |
|
public function resetOneTimeValues() |
219
|
|
|
{ |
220
|
11 |
|
$this->prefix = null; |
221
|
|
|
|
222
|
11 |
|
$this->suffix = null; |
223
|
|
|
|
224
|
11 |
|
$this->fakerString = null; |
225
|
|
|
|
226
|
11 |
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Set numeric start. |
231
|
|
|
* |
232
|
|
|
* @param int $start |
233
|
|
|
* @return $this |
234
|
|
|
*/ |
235
|
1 |
|
public function start($start) |
236
|
|
|
{ |
237
|
1 |
|
$this->start = $start; |
238
|
|
|
|
239
|
1 |
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Set the return string size. |
244
|
|
|
* |
245
|
|
|
* @param $size |
246
|
|
|
* @return $this |
247
|
|
|
*/ |
248
|
10 |
|
public function size($size) |
249
|
|
|
{ |
250
|
10 |
|
$this->size = $size; |
251
|
|
|
|
252
|
10 |
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get the generated random string/number. |
257
|
|
|
* |
258
|
|
|
* @return string|int |
259
|
|
|
*/ |
260
|
11 |
|
public function get() |
261
|
|
|
{ |
262
|
11 |
|
$result = $this->addPrefixSuffix( |
263
|
11 |
|
$this->changeCase( |
264
|
11 |
|
$this->generate() |
265
|
|
|
) |
266
|
|
|
); |
267
|
|
|
|
268
|
11 |
|
$this->resetOneTimeValues(); |
269
|
|
|
|
270
|
11 |
|
return $result; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|