|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PragmaRX\Random; |
|
4
|
|
|
|
|
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) |
|
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
|
|
|
* Get numeric end. |
|
42
|
|
|
* |
|
43
|
|
|
* @return int |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function getEnd() |
|
46
|
|
|
{ |
|
47
|
1 |
|
return $this->end; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get string pattern. |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
8 |
|
public function getPattern() |
|
56
|
|
|
{ |
|
57
|
8 |
|
return $this->pattern; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get string pattern. |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function noPattern() |
|
66
|
|
|
{ |
|
67
|
1 |
|
$this->pattern = null; |
|
68
|
|
|
|
|
69
|
1 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get the final string size. |
|
74
|
|
|
* |
|
75
|
|
|
* @return integer |
|
76
|
|
|
*/ |
|
77
|
8 |
|
public function getSize() |
|
78
|
|
|
{ |
|
79
|
8 |
|
return $this->size ?: static::DEFAULT_STRING_SIZE; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get numeric start. |
|
84
|
|
|
* |
|
85
|
|
|
* @return int |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function getStart() |
|
88
|
|
|
{ |
|
89
|
1 |
|
return $this->start; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Generate a random hex. |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function hex() |
|
98
|
|
|
{ |
|
99
|
1 |
|
$this->pattern('[a-f0-9]')->uppercase(); |
|
100
|
|
|
|
|
101
|
1 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Set string pattern. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $pattern |
|
108
|
|
|
* @return $this |
|
109
|
|
|
*/ |
|
110
|
2 |
|
public function pattern($pattern) |
|
111
|
|
|
{ |
|
112
|
2 |
|
$this->pattern = $pattern; |
|
113
|
|
|
|
|
114
|
2 |
|
return $this; |
|
115
|
|
|
} |
|
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) |
|
125
|
|
|
{ |
|
126
|
8 |
|
return substr($string, 0, $size ?: $this->getSize()); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Set result to numeric. |
|
131
|
|
|
* |
|
132
|
|
|
* @param bool $state |
|
133
|
|
|
* @return $this |
|
134
|
|
|
*/ |
|
135
|
3 |
|
public function numeric($state = true) |
|
136
|
|
|
{ |
|
137
|
3 |
|
$this->numeric = $state; |
|
138
|
|
|
|
|
139
|
3 |
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Set result to alpha. |
|
144
|
|
|
* |
|
145
|
|
|
* @param bool $state |
|
146
|
|
|
* @return $this |
|
147
|
|
|
*/ |
|
148
|
1 |
|
public function alpha($state = true) |
|
149
|
|
|
{ |
|
150
|
1 |
|
$this->numeric = !$state; |
|
151
|
|
|
|
|
152
|
1 |
|
return $this; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Set numeric end. |
|
157
|
|
|
* |
|
158
|
|
|
* @param int $end |
|
159
|
|
|
* @return $this |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function end($end) |
|
162
|
|
|
{ |
|
163
|
1 |
|
$this->end = $end; |
|
164
|
|
|
|
|
165
|
1 |
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Set numeric start. |
|
170
|
|
|
* |
|
171
|
|
|
* @param int $start |
|
172
|
|
|
* @return $this |
|
173
|
|
|
*/ |
|
174
|
1 |
|
public function start($start) |
|
175
|
|
|
{ |
|
176
|
1 |
|
$this->start = $start; |
|
177
|
|
|
|
|
178
|
1 |
|
return $this; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Set the return string size. |
|
184
|
|
|
* |
|
185
|
|
|
* @param $size |
|
186
|
|
|
* @return $this |
|
187
|
|
|
*/ |
|
188
|
8 |
|
public function size($size) |
|
189
|
|
|
{ |
|
190
|
8 |
|
$this->size = $size; |
|
191
|
|
|
|
|
192
|
8 |
|
return $this; |
|
193
|
|
|
} |
|
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() |
|
203
|
|
|
{ |
|
204
|
8 |
|
return $this->changeCase( |
|
205
|
8 |
|
$this->generate() |
|
206
|
|
|
); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|