-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandString.php
More file actions
355 lines (311 loc) · 8.91 KB
/
RandString.php
File metadata and controls
355 lines (311 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
* RandString Class for generating random strings
* @package RandString
* @author B-and-P
* @link https://github.com/b-and-p/ GitHub repo
* @date 2016-03-10
* PHP 5.4
*/
namespace b_and_p\RandStrNS;
/**
* Random String Generator Class
*
* Utility class to generate random strings.
*
* @example
* Code example below:
*
* include ('RandString.php');
* $oRS = new RandStrNS\RandString();
*
* $oRS->part_length = 2; // INT
* $oRS->parts = 2; // INT
* $oRS->delim = '-'; // STR
* $oRS->prefix = ''; // STR
* $oRS->suffix = ''; // STR
* $oRS->count = 1; // STR
* $oRS->unique = false; //BOOL
* $oRS->flags = 3; // INT Bitmask (000011)
*
* $result = $oRS->generate();
* var_dump_result();
*
* **Will Output:**
*
* Array('gR-Fc')
*
* @version 1.0
* @since 2016-03-15
* @author andrei_roslovtsev <andrei@bytes-and-pixels.com>
*/
class RandString {
/**
* Length of a string part/segment [default value: 3]
* @var int
*/
private $part_length;
/**
* Delimiter to use for concatenating parts/segments [default value: '-']
* @var string
*/
private $delim;
/**
* Number of parts/segments [default value: 2]
* @var int
*/
private $parts;
/**
* Number of strings to generate [default value: 1]
* @var int
*/
private $count;
/**
* Bitmask for character sets to include in random string generation [default value: 2 (000010)]
* @var int
*/
private $flags;
/**
* Generate unique values [default value: false]
* @var bool
*/
private $unique;
/**
* Prefix for random string [default value: empty]
* @var str
*/
private $prefix;
/**
* Suffix for random string [default value: empty]
* @var str
*/
private $suffix;
/**
* Store of generated strings (needed for uniqueness check) [default value: empty]
* @var array
*/
private $history;
/**
* Store of found duplicates during generation cycle (for stats and debugging) [default value: empty]
* @var array
*/
private $duplicates;
/**
* Working character set [default value: alpha lower case + alpha upper case]
* @var string
*/
private $working_charset;
// const CHARSET_ALPHA_LOWER = 1;
// const CHARSET_ALPHA_UPPER = 2;
// const CHARSET_NUMERIC = 4;
// const CHARSET_SPECIAL = 8;
/**
* Character sets serialized array of subsets. Constant
* @var array
*/
private $charsets;
/**
* Map of settable class properties. Constant
* @var array
*/
private $settable;
/**
* Map of gettable class properties. Constant
* @var array
*/
private $gettable;
/**
* Error message
* @var boolean / string
*/
private $error;
/**
* Constructor. Returns Generator object
*
* @return object \RandStrNS\RandString
*/
function __construct() {
/* Set Defaults */
$this->part_length = 3;
$this->delim = '-';
$this->prefix = '';
$this->suffix = '';
$this->parts = 2;
$this->count = 1;
$this->flags = 2;
$this->unique = false;
$this->charsets = array(
0 => 'abcdefghijklmnopqrstuvyxyz',
1 => 'ABCDEFGHIJKLMNOPQRSTUVYXYZ',
2 => '0123456789',
3 => '~!@#$%^&*()_+=<>?/\\[]{}'
);
$this->working_charset = '';
$this->settable = array('part_length',
'delim',
'parts',
'flags',
'unique',
'count',
'prefix',
'suffix');
$this->gettable = array('part_length',
'delim',
'parts',
'flags',
'unique',
'count',
'prefix',
'suffix',
'duplicates',
'history',
'error');
$this->history = array();
$this->duplicates = array();
return $this;
}
/**
* Setter for properties, handles validation
* @param string $prop Property name
* @param variant $val Property value
* @return boolean
*/
public function __set($prop, $val) {
try {
$this->validate($prop, $val);
$this->$prop = $val;
} catch (\Exception $e) {
$this->error = $e->getMessage();
}
return true;
}
/**
* Getter for properties, handles validation
* @param string $prop Property name
* @return variant
*/
public function __get($prop) {
try {
$this->validate($prop);
} catch (\Exception $e) {
$this->error = $e->getMessage();
}
return $this->$prop;
}
/**
* Generates *n* random strings
* @return array
* @throws \Exception
*/
public function generate() {
foreach ($this->charsets as $k => $charset) {
if ($this->flags & (1 << $k)) {
$this->working_charset .= $charset;
}
}
/* check parameter validity */
try {
$check = $this->unique_possibility_check();
} catch (\Exception $e) {
$this->error = $e->getMessage();
$this->count = 0;
}
$count = $this->count;
$result = array();
for ($i = 0; $i < $count; $i++) {
$result[] = $this->make_string();
}
return $result;
}
/**
* Checks whether it is possible to produce required number of unique results with specified parameters
*
* @return boolean
* @throws \Exception
*/
private function unique_possibility_check() {
$variations = pow(strlen($this->working_charset), pow($this->part_length, $this->parts));
if ($this->count > $variations && $this->unique) {
throw new \Exception($this->exception_message('Not enough possible combinations for unique results', ''), 4);
} else {
return true;
}
}
/**
* handles generation of a single string
*
* @return string
*/
private function make_string() {
$aParts = array();
$charset_length = strlen($this->working_charset);
for ($i = 0; $i < $this->parts; $i++) {
$aParts[$i] = '';
for ($j = 0; $j < $this->part_length; $j++) {
$aParts[$i] .= substr($this->working_charset, mt_rand(0, $charset_length - 1), 1);
}
}
$result = implode($this->delim, $aParts);
$result = $this->prefix . $result . $this->suffix;
if ($this->unique) {
if (!in_array($result, $this->history)) {
$this->history[] = $result;
return $result;
} else {
$this->duplicates[] = $result;
return $this->make_string();
}
} else {
return $result;
}
}
/**
*
* Validation for getter and setter methods
*
* @param string $prop Property name
* @param string|int|bool $val Property value
* @return boolean
* @throws \Exception
*/
private function validate($prop, $val = false) {
if (!property_exists($this, $prop)) {
throw new \Exception($this->exception_message('Unknown parameter requested', $prop), 1);
}
if ($val && !in_array($prop, $this->settable)) {
throw new \Exception($this->exception_message('Setting value disallowed', $prop), 2);
}
if (!$val && !in_array($prop, $this->gettable)) {
throw new \Exception($this->exception_message('Getting value disallowed', $prop), 5);
}
if ($val) {
switch ($prop) {
case 'part_length':
case 'parts':
if (!is_numeric($val)) {
throw new \Exception($this->exception_message('Bad parameter value passed for', $prop), 2);
}
break;
}
}
return true;
}
/**
* Produces Exception message string
*
*
* @param string $msg Message body
* @param string $prop property name
* @return string
*/
private function exception_message($msg, $prop) {
if ($prop) {
return 'Cought Exception: ' . $msg . ' [' . $prop . ']';
} else {
return 'Cought Exception: ' . $msg;
}
}
}
/*
* File: RandString.php
*/