forked from acquia/blt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomString.php
85 lines (76 loc) · 2.45 KB
/
RandomString.php
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
<?php
namespace Acquia\Blt\Robo\Common;
/**
* Utility class for generating random strings.
*/
class RandomString {
/**
* The maximum number of times name() and string() can loop.
*
* This prevents infinite loops if the length of the random value is very
* small.
*
* @see \Drupal\Tests\Component\Utility\RandomTest
*/
const MAXIMUM_TRIES = 100;
/**
* Generates a random string of ASCII characters of codes 32 to 126.
*
* The generated string includes alpha-numeric characters and common
* miscellaneous characters. Use this method when testing general input
* where the content is not restricted.
*
* @param int $length
* Length of random string to generate.
* @param bool $unique
* (optional) If TRUE ensures that the random string returned is unique.
* Defaults to FALSE.
* @param callable $validator
* (optional) A callable to validate the string. Defaults to NULL.
* @param string $characters
* (optional) A string containing all possible characters that may be used
* to generate the random string.
*
* @return string
* Randomly generated string.
*
* @see \Drupal\Component\Utility\Random::name()
*/
public static function string($length = 8, $unique = FALSE, callable $validator = NULL, $characters = '') {
$counter = 0;
$strings = [];
$characters_array = $characters ? str_split($characters) : [];
// Continue to loop if $unique is TRUE and the generated string is not
// unique or if $validator is a callable that returns FALSE. To generate a
// random string this loop must be carried out at least once.
do {
if ($counter == static::MAXIMUM_TRIES) {
throw new \RuntimeException('Unable to generate a unique random name');
}
$str = '';
for ($i = 0; $i < $length; $i++) {
if ($characters_array) {
$position = mt_rand(0, count($characters_array) - 1);
$str .= $characters_array[$position];
}
else {
$str .= chr(mt_rand(32, 126));
}
}
$counter++;
$continue = FALSE;
if ($unique) {
$continue = isset($strings[$str]);
}
if (!$continue && is_callable($validator)) {
// If the validator callback returns FALSE generate another random
// string.
$continue = !call_user_func($validator, $str);
}
} while ($continue);
if ($unique) {
$strings[$str] = TRUE;
}
return $str;
}
}