_randomizer = $randomizer; $this->_adjectives = file(__DIR__ . '/adjectives.txt', FILE_IGNORE_NEW_LINES); $this->_nouns = file(__DIR__ . '/nouns.txt', FILE_IGNORE_NEW_LINES); } /** * Gets a randomly generated alliterative name. * * @api * @return string A random alliterative name. */ public function getName() { $adjective = $this->_getRandomWord($this->_adjectives); $noun = $this->_getRandomWord($this->_nouns, $adjective[0]); return ucwords("{$adjective} {$noun}"); } /** * Get a random word from the list of words, optionally filtering by starting letter. * * @param array $words An array of words to choose from. * @param string $startingLetter The desired starting letter of the word. * @return string The random word. */ protected function _getRandomWord(array $words, $startingLetter = null) { $wordsToSearch = $startingLetter === null ? $words : preg_grep("/^{$startingLetter}/", $words); return $this->_randomizer ? $this->_randomizer->getArrayValue($wordsToSearch) : $wordsToSearch[array_rand($wordsToSearch)]; } }