-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRandomStringUtils.php
More file actions
250 lines (226 loc) · 7.66 KB
/
RandomStringUtils.php
File metadata and controls
250 lines (226 loc) · 7.66 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
<?php
/**
* FlorianWolters\Component\Core\RandomStringUtils
*
* PHP Version 5.3
*
* @author Florian Wolters <wolters.fl@gmail.com>
* @copyright 2010-2014 Florian Wolters (http://blog.florianwolters.de)
* @license https://gnu.org/licenses/lgpl.txt LGPL-3.0+
* @link https://github.com/FlorianWolters/PHP-Component-Core-StringUtils
*/
namespace FlorianWolters\Component\Core;
use \InvalidArgumentException;
/**
* The class {@see RandomStringUtils} offers operations for random `string`s.
*
* This class is inspired by the Java class {@link
* https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/RandomStringUtils.html
* RandomStringUtils} from the {@link https://commons.apache.org/lang Apache
* Commons Lang Application Programming Interface (API)}.
*
* @since Class available since Release 0.2.0
*/
final class RandomStringUtils
{
// @codeCoverageIgnoreStart
/**
* {@see RandomStringUtils} instances can **NOT** be constructed in standard
* programming.
*
* Instead, the class should be used as:
*
* RandomStringUtils::random(42);
*
*/
protected function __construct()
{
// NOOP
}
// @codeCoverageIgnoreEnd
/**
* Creates a random string whose length is the number of characters
* specified.
*
* Characters will be chosen from the set of alphabetic characters.
*
* @param integer $count The length of the random string to create.
*
* @return string The random string.
*/
public static function randomAlphabetic($count)
{
return self::random($count, 0, 0, true, false);
}
/**
* Creates a random string whose length is the number of characters
* specified.
*
* Characters will be chosen from the set of lower cased alphabetic
* characters.
*
* @param integer $count The length of the random string to create.
*
* @return string The random string.
*/
public static function randomAlphabeticLower($count)
{
return self::random($count, 'a', 'z', true, false);
}
/**
* Creates a random string whose length is the number of characters
* specified.
*
* Characters will be chosen from the set of upper cased alphabetic
* characters.
*
* @param integer $count The length of the random string to create.
*
* @return string The random string.
*/
public static function randomAlphabeticUpper($count)
{
return self::random($count, 'A', 'Z', true, false);
}
/**
* Creates a random string whose length is the number of characters
* specified.
*
* Characters will be chosen from the set of alpha-numeric characters.
*
* @param integer $count The length of the random string to create.
*
* @return string The random string.
*/
public static function randomAlphanumeric($count)
{
return self::random($count);
}
/**
* Creates a random string whose length is the number of characters
* specified.
*
* Characters will be chosen from the set of characters whose ASCII value is
* between `32` and `126` (inclusive).
*
* @param integer $count The length of the random string to create.
*
* @return string The random string
*/
public static function randomAscii($count)
{
return self::random($count, 32, 127, true, false);
}
/**
* Creates a random string whose length is the number of characters
* specified.
*
* Characters will be chosen from the set of numeric characters.
*
* @param integer $count The length of the random string to create.
*
* @return string The random string.
*/
public static function randomNumeric($count)
{
return self::random($count, 0, 0, false);
}
/**
* Creates a random string based on a variety of options.
*
* If start and end are both `0`, `$startPosition` is set to `' '` and
* `$endPosition` is set to `'z'`, the ASCII printable characters, will be
* used, unless letters and numbers are both `false`, in which case, all
* printable characters are used that are *not* alphanumeric.
*
* If `$characters` is not an empty array, characters will be chosen from
* the set of characters specified.
*
* @param integer $count The length of the random string to
* create.
* @param integer|string $startPosition The position in the set of
* characters to start at.
* @param integer|string $endPosition The position in the set of
* characters to end before.
* @param boolean $letters `true` if only letters are allowed.
* @param boolean $numbers `true` if only numbers are allowed.
* @param array $characters The set of characters to choose
* randoms from.
*
* @return string The random string.
* @throws InvalidArgumentException If `$count` is less than `0`.
*/
public static function random(
$count,
$startPosition = 0,
$endPosition = 0,
$letters = true,
$numbers = true,
array $characters = array()
) {
if (0 === $count) {
return StringUtils::EMPTY_STR;
}
if (0 > $count) {
throw new InvalidArgumentException(
'Requested random string length ' . $count . ' is less than 0.'
);
}
foreach ($characters as $intValue) {
if (false === CharUtils::isAscii($intValue)) {
throw new InvalidArgumentException(
'Requested set of characters does not contain characters only.'
);
}
}
if (false === \is_int($startPosition)) {
$startPosition = CharUtils::fromAsciiCharToValue($startPosition);
}
if (false === \is_int($endPosition)) {
$endPosition = CharUtils::fromAsciiCharToValue($endPosition);
}
if (0 > $startPosition) {
throw new InvalidArgumentException(
'Requested start position ' . $startPosition . ' is less than 0.'
);
}
if (0 > $endPosition) {
throw new InvalidArgumentException(
'Requested end position ' . $endPosition . ' is less than 0.'
);
}
if ((0 === $startPosition) && (0 === $endPosition)) {
$startPosition = 32;
$endPosition = 123;
if ((false === $letters) && (false === $numbers)) {
$characters = \array_values(
\array_diff(
CharUtils::charAsciiPrintableArray(),
CharUtils::charAsciiAlphanumericArray()
)
);
}
}
$charactersUpperBound = (\count($characters) - 1);
$result = '';
while (0 !== $count--) {
if (!$characters) {
$intValue = \rand($startPosition, $endPosition);
} else {
$intValue = CharUtils::fromAsciiCharToValue(
$characters[\rand(0, $charactersUpperBound)]
);
}
$charValue = CharUtils::fromAsciiValueToChar($intValue);
if ((true === $letters) && (true === CharUtils::isAsciiAlpha($charValue))
|| (true === $numbers) && (true === CharUtils::isAsciiNumeric($charValue))
|| (false === $letters) && (false === $numbers)
) {
$result .= $charValue;
} else {
++$count;
}
}
return $result;
}
}