Chris@0: strings[$str]); Chris@0: } Chris@0: if (!$continue && is_callable($validator)) { Chris@0: // If the validator callback returns FALSE generate another random Chris@0: // string. Chris@0: $continue = !call_user_func($validator, $str); Chris@0: } Chris@0: } while ($continue); Chris@0: Chris@0: if ($unique) { Chris@0: $this->strings[$str] = TRUE; Chris@0: } Chris@0: Chris@0: return $str; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates a random string containing letters and numbers. Chris@0: * Chris@0: * The string will always start with a letter. The letters may be upper or Chris@0: * lower case. This method is better for restricted inputs that do not Chris@0: * accept certain characters. For example, when testing input fields that Chris@0: * require machine readable values (i.e. without spaces and non-standard Chris@0: * characters) this method is best. Chris@0: * Chris@0: * @param int $length Chris@0: * Length of random string to generate. Chris@0: * @param bool $unique Chris@0: * (optional) If TRUE ensures that the random string returned is unique. Chris@0: * Defaults to FALSE. Chris@0: * Chris@0: * @return string Chris@0: * Randomly generated string. Chris@0: * Chris@0: * @see \Drupal\Component\Utility\Random::string() Chris@0: */ Chris@0: public function name($length = 8, $unique = FALSE) { Chris@0: $values = array_merge(range(65, 90), range(97, 122), range(48, 57)); Chris@0: $max = count($values) - 1; Chris@0: $counter = 0; Chris@0: Chris@0: do { Chris@0: if ($counter == static::MAXIMUM_TRIES) { Chris@0: throw new \RuntimeException('Unable to generate a unique random name'); Chris@0: } Chris@0: $str = chr(mt_rand(97, 122)); Chris@0: for ($i = 1; $i < $length; $i++) { Chris@0: $str .= chr($values[mt_rand(0, $max)]); Chris@0: } Chris@0: $counter++; Chris@0: } while ($unique && isset($this->names[$str])); Chris@0: Chris@0: if ($unique) { Chris@0: $this->names[$str] = TRUE; Chris@0: } Chris@0: Chris@0: return $str; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generate a string that looks like a word (letters only, alternating consonants and vowels). Chris@0: * Chris@0: * @param int $length Chris@0: * The desired word length. Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function word($length) { Chris@0: mt_srand((double) microtime() * 1000000); Chris@0: Chris@0: $vowels = ["a", "e", "i", "o", "u"]; Chris@0: $cons = ["b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "u", "v", "w", "tr", Chris@0: "cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "st", "sp", "sw", "pr", Chris@0: "sl", "cl", "sh", Chris@0: ]; Chris@0: Chris@0: $num_vowels = count($vowels); Chris@0: $num_cons = count($cons); Chris@0: $word = ''; Chris@0: Chris@0: while (strlen($word) < $length) { Chris@0: $word .= $cons[mt_rand(0, $num_cons - 1)] . $vowels[mt_rand(0, $num_vowels - 1)]; Chris@0: } Chris@0: Chris@0: return substr($word, 0, $length); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates a random PHP object. Chris@0: * Chris@0: * @param int $size Chris@0: * The number of random keys to add to the object. Chris@0: * Chris@0: * @return \stdClass Chris@0: * The generated object, with the specified number of random keys. Each key Chris@0: * has a random string value. Chris@0: */ Chris@0: public function object($size = 4) { Chris@0: $object = new \stdClass(); Chris@0: for ($i = 0; $i < $size; $i++) { Chris@0: $random_key = $this->name(); Chris@0: $random_value = $this->string(); Chris@0: $object->{$random_key} = $random_value; Chris@0: } Chris@0: return $object; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates sentences Latin words, often used as placeholder text. Chris@0: * Chris@0: * @param int $min_word_count Chris@0: * The minimum number of words in the return string. Total word count Chris@0: * can slightly exceed provided this value in order to deliver Chris@0: * sentences of random length. Chris@0: * @param bool $capitalize Chris@0: * Uppercase all the words in the string. Chris@0: * Chris@0: * @return string Chris@0: * Nonsense latin words which form sentence(s). Chris@0: */ Chris@0: public function sentences($min_word_count, $capitalize = FALSE) { Chris@0: $dictionary = ["abbas", "abdo", "abico", "abigo", "abluo", "accumsan", Chris@0: "acsi", "ad", "adipiscing", "aliquam", "aliquip", "amet", "antehabeo", Chris@0: "appellatio", "aptent", "at", "augue", "autem", "bene", "blandit", Chris@0: "brevitas", "caecus", "camur", "capto", "causa", "cogo", "comis", Chris@0: "commodo", "commoveo", "consectetuer", "consequat", "conventio", "cui", Chris@0: "damnum", "decet", "defui", "diam", "dignissim", "distineo", "dolor", Chris@0: "dolore", "dolus", "duis", "ea", "eligo", "elit", "enim", "erat", Chris@0: "eros", "esca", "esse", "et", "eu", "euismod", "eum", "ex", "exerci", Chris@0: "exputo", "facilisi", "facilisis", "fere", "feugiat", "gemino", Chris@0: "genitus", "gilvus", "gravis", "haero", "hendrerit", "hos", "huic", Chris@0: "humo", "iaceo", "ibidem", "ideo", "ille", "illum", "immitto", Chris@0: "importunus", "imputo", "in", "incassum", "inhibeo", "interdico", Chris@0: "iriure", "iusto", "iustum", "jugis", "jumentum", "jus", "laoreet", Chris@0: "lenis", "letalis", "lobortis", "loquor", "lucidus", "luctus", "ludus", Chris@0: "luptatum", "macto", "magna", "mauris", "melior", "metuo", "meus", Chris@0: "minim", "modo", "molior", "mos", "natu", "neo", "neque", "nibh", Chris@0: "nimis", "nisl", "nobis", "nostrud", "nulla", "nunc", "nutus", "obruo", Chris@0: "occuro", "odio", "olim", "oppeto", "os", "pagus", "pala", "paratus", Chris@0: "patria", "paulatim", "pecus", "persto", "pertineo", "plaga", "pneum", Chris@0: "populus", "praemitto", "praesent", "premo", "probo", "proprius", Chris@0: "quadrum", "quae", "qui", "quia", "quibus", "quidem", "quidne", "quis", Chris@0: "ratis", "refero", "refoveo", "roto", "rusticus", "saepius", Chris@0: "sagaciter", "saluto", "scisco", "secundum", "sed", "si", "similis", Chris@0: "singularis", "sino", "sit", "sudo", "suscipere", "suscipit", "tamen", Chris@0: "tation", "te", "tego", "tincidunt", "torqueo", "tum", "turpis", Chris@0: "typicus", "ulciscor", "ullamcorper", "usitas", "ut", "utinam", Chris@0: "utrum", "uxor", "valde", "valetudo", "validus", "vel", "velit", Chris@0: "veniam", "venio", "vereor", "vero", "verto", "vicis", "vindico", Chris@0: "virtus", "voco", "volutpat", "vulpes", "vulputate", "wisi", "ymo", Chris@0: "zelus", Chris@0: ]; Chris@0: $dictionary_flipped = array_flip($dictionary); Chris@0: $greeking = ''; Chris@0: Chris@0: if (!$capitalize) { Chris@0: $words_remaining = $min_word_count; Chris@0: while ($words_remaining > 0) { Chris@0: $sentence_length = mt_rand(3, 10); Chris@0: $words = array_rand($dictionary_flipped, $sentence_length); Chris@0: $sentence = implode(' ', $words); Chris@0: $greeking .= ucfirst($sentence) . '. '; Chris@0: $words_remaining -= $sentence_length; Chris@0: } Chris@0: } Chris@0: else { Chris@0: // Use slightly different method for titles. Chris@0: $words = array_rand($dictionary_flipped, $min_word_count); Chris@0: $words = is_array($words) ? implode(' ', $words) : $words; Chris@0: $greeking = ucwords($words); Chris@0: } Chris@0: return trim($greeking); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generate paragraphs separated by double new line. Chris@0: * Chris@0: * @param int $paragraph_count Chris@0: * @return string Chris@0: */ Chris@0: public function paragraphs($paragraph_count = 12) { Chris@0: $output = ''; Chris@0: for ($i = 1; $i <= $paragraph_count; $i++) { Chris@0: $output .= $this->sentences(mt_rand(20, 60)) . "\n\n"; Chris@0: } Chris@0: return $output; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Create a placeholder image. Chris@0: * Chris@0: * @param string $destination Chris@0: * The absolute file path where the image should be stored. Chris@0: * @param int $min_resolution Chris@0: * @param int $max_resolution Chris@0: * Chris@0: * @return string Chris@0: * Path to image file. Chris@0: */ Chris@0: public function image($destination, $min_resolution, $max_resolution) { Chris@0: $extension = pathinfo($destination, PATHINFO_EXTENSION); Chris@0: $min = explode('x', $min_resolution); Chris@0: $max = explode('x', $max_resolution); Chris@0: Chris@0: $width = rand((int) $min[0], (int) $max[0]); Chris@0: $height = rand((int) $min[1], (int) $max[1]); Chris@0: Chris@0: // Make an image split into 4 sections with random colors. Chris@0: $im = imagecreate($width, $height); Chris@0: for ($n = 0; $n < 4; $n++) { Chris@0: $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); Chris@0: $x = $width / 2 * ($n % 2); Chris@0: $y = $height / 2 * (int) ($n >= 2); Chris@0: imagefilledrectangle($im, $x, $y, $x + $width / 2, $y + $height / 2, $color); Chris@0: } Chris@0: Chris@0: // Make a perfect circle in the image middle. Chris@0: $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); Chris@0: $smaller_dimension = min($width, $height); Chris@0: imageellipse($im, $width / 2, $height / 2, $smaller_dimension, $smaller_dimension, $color); Chris@0: Chris@0: $save_function = 'image' . ($extension == 'jpg' ? 'jpeg' : $extension); Chris@0: $save_function($im, $destination); Chris@0: return $destination; Chris@0: } Chris@0: Chris@0: }