Security update for permissions_by_term
[yaffs-website] / vendor / drupal / drupal-driver / src / Drupal / Component / Utility / Random.php
1 <?php
2
3 namespace Drupal\Component\Utility;
4
5 /**
6  * Defines a utility class for creating random data.
7  */
8 class Random {
9
10   /**
11    * The maximum number of times name() and string() can loop.
12    *
13    * This prevents infinite loops if the length of the random value is very
14    * small.
15    *
16    * @see \Drupal\Tests\Component\Utility\RandomTest
17    */
18   const MAXIMUM_TRIES = 100;
19
20   /**
21    * A list of unique strings generated by string().
22    *
23    * @var array
24    */
25   protected $strings = array();
26
27   /**
28    * A list of unique names generated by name().
29    *
30    * @var array
31    */
32   protected $names = array();
33
34   /**
35    * Generates a random string of ASCII characters of codes 32 to 126.
36    *
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.
40    *
41    * @param int $length
42    *   Length of random string to generate.
43    * @param bool $unique
44    *   (optional) If TRUE ensures that the random string returned is unique.
45    *   Defaults to FALSE.
46    * @param callable $validator
47    *   (optional) A callable to validate the the string. Defaults to NULL.
48    *
49    * @return string
50    *   Randomly generated string.
51    *
52    * @see \Drupal\Component\Utility\Random::name()
53    */
54   public function string($length = 8, $unique = FALSE, callable $validator = NULL) {
55     $counter = 0;
56
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.
60     do {
61       if ($counter == static::MAXIMUM_TRIES) {
62         throw new \RuntimeException('Unable to generate a unique random name');
63       }
64       $str = '';
65       for ($i = 0; $i < $length; $i++) {
66         $str .= chr(mt_rand(32, 126));
67       }
68       $counter++;
69
70       $continue = FALSE;
71       if ($unique) {
72         $continue = isset($this->strings[$str]);
73       }
74       if (!$continue && is_callable($validator)) {
75         // If the validator callback returns FALSE generate another random
76         // string.
77         $continue = !call_user_func($validator, $str);
78       }
79     } while ($continue);
80
81     if ($unique) {
82       $this->strings[$str] = TRUE;
83     }
84
85     return $str;
86   }
87
88   /**
89    * Generates a random string containing letters and numbers.
90    *
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.
96    *
97    * @param int $length
98    *   Length of random string to generate.
99    * @param bool $unique
100    *   (optional) If TRUE ensures that the random string returned is unique.
101    *   Defaults to FALSE.
102    *
103    * @return string
104    *   Randomly generated string.
105    *
106    * @see \Drupal\Component\Utility\Random::string()
107    */
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;
111     $counter = 0;
112
113     do {
114       if ($counter == static::MAXIMUM_TRIES) {
115         throw new \RuntimeException('Unable to generate a unique random name');
116       }
117       $str = chr(mt_rand(97, 122));
118       for ($i = 1; $i < $length; $i++) {
119         $str .= chr($values[mt_rand(0, $max)]);
120       }
121       $counter++;
122     } while ($unique && isset($this->names[$str]));
123
124     if ($unique) {
125       $this->names[$str] = TRUE;
126     }
127
128     return $str;
129   }
130
131   /**
132    * Generates a random PHP object.
133    *
134    * @param int $size
135    *   The number of random keys to add to the object.
136    *
137    * @return \stdClass
138    *   The generated object, with the specified number of random keys. Each key
139    *   has a random string value.
140    */
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;
147     }
148     return $object;
149   }
150
151 }