3 namespace Drupal\Component\Utility;
6 * Defines a utility class for creating random data.
11 * The maximum number of times name() and string() can loop.
13 * This prevents infinite loops if the length of the random value is very
16 * @see \Drupal\Tests\Component\Utility\RandomTest
18 const MAXIMUM_TRIES = 100;
21 * A list of unique strings generated by string().
25 protected $strings = array();
28 * A list of unique names generated by name().
32 protected $names = array();
35 * Generates a random string of ASCII characters of codes 32 to 126.
37 * The generated string includes alpha-numeric characters and common
38 * miscellaneous characters. Use this method when testing general input
39 * where the content is not restricted.
42 * Length of random string to generate.
44 * (optional) If TRUE ensures that the random string returned is unique.
46 * @param callable $validator
47 * (optional) A callable to validate the the string. Defaults to NULL.
50 * Randomly generated string.
52 * @see \Drupal\Component\Utility\Random::name()
54 public function string($length = 8, $unique = FALSE, callable $validator = NULL) {
57 // Continue to loop if $unique is TRUE and the generated string is not
58 // unique or if $validator is a callable that returns FALSE. To generate a
59 // random string this loop must be carried out at least once.
61 if ($counter == static::MAXIMUM_TRIES) {
62 throw new \RuntimeException('Unable to generate a unique random name');
65 for ($i = 0; $i < $length; $i++) {
66 $str .= chr(mt_rand(32, 126));
72 $continue = isset($this->strings[$str]);
74 if (!$continue && is_callable($validator)) {
75 // If the validator callback returns FALSE generate another random
77 $continue = !call_user_func($validator, $str);
82 $this->strings[$str] = TRUE;
89 * Generates a random string containing letters and numbers.
91 * The string will always start with a letter. The letters may be upper or
92 * lower case. This method is better for restricted inputs that do not
93 * accept certain characters. For example, when testing input fields that
94 * require machine readable values (i.e. without spaces and non-standard
95 * characters) this method is best.
98 * Length of random string to generate.
100 * (optional) If TRUE ensures that the random string returned is unique.
104 * Randomly generated string.
106 * @see \Drupal\Component\Utility\Random::string()
108 public function name($length = 8, $unique = FALSE) {
109 $values = array_merge(range(65, 90), range(97, 122), range(48, 57));
110 $max = count($values) - 1;
114 if ($counter == static::MAXIMUM_TRIES) {
115 throw new \RuntimeException('Unable to generate a unique random name');
117 $str = chr(mt_rand(97, 122));
118 for ($i = 1; $i < $length; $i++) {
119 $str .= chr($values[mt_rand(0, $max)]);
122 } while ($unique && isset($this->names[$str]));
125 $this->names[$str] = TRUE;
132 * Generates a random PHP object.
135 * The number of random keys to add to the object.
138 * The generated object, with the specified number of random keys. Each key
139 * has a random string value.
141 public function object($size = 4) {
142 $object = new \stdClass();
143 for ($i = 0; $i < $size; $i++) {
144 $random_key = $this->name();
145 $random_value = $this->string();
146 $object->{$random_key} = $random_value;