aboutsummaryrefslogtreecommitdiffhomepage
path: root/assets/php/vendor/nubs/random-name-generator/src/Alliteration.php
diff options
context:
space:
mode:
authormarvin-borner@live.com2018-04-10 21:50:16 +0200
committermarvin-borner@live.com2018-04-10 21:54:48 +0200
commitfc9401f04a3aca5abb22f87ebc210de8afe11d32 (patch)
treeb0b310f3581764ec3955f4e496a05137a32951c3 /assets/php/vendor/nubs/random-name-generator/src/Alliteration.php
parent286d643180672f20526f3dc3bd19d7b751e2fa97 (diff)
Initial Commit
Diffstat (limited to 'assets/php/vendor/nubs/random-name-generator/src/Alliteration.php')
-rw-r--r--assets/php/vendor/nubs/random-name-generator/src/Alliteration.php59
1 files changed, 59 insertions, 0 deletions
diff --git a/assets/php/vendor/nubs/random-name-generator/src/Alliteration.php b/assets/php/vendor/nubs/random-name-generator/src/Alliteration.php
new file mode 100644
index 0000000..68ef3a2
--- /dev/null
+++ b/assets/php/vendor/nubs/random-name-generator/src/Alliteration.php
@@ -0,0 +1,59 @@
+<?php
+namespace Nubs\RandomNameGenerator;
+
+use Cinam\Randomizer\Randomizer;
+
+/**
+ * Defines an alliterative name generator.
+ */
+class Alliteration extends AbstractGenerator implements Generator
+{
+ /** @type array The definition of the potential adjectives. */
+ protected $_adjectives;
+
+ /** @type array The definition of the potential nouns. */
+ protected $_nouns;
+
+ /** @type Cinam\Randomizer\Randomizer The random number generator. */
+ protected $_randomizer;
+
+ /**
+ * Initializes the Alliteration Generator with the default word lists.
+ *
+ * @api
+ * @param \Cinam\Randomizer\Randomizer $randomizer The random number generator.
+ */
+ public function __construct(Randomizer $randomizer = null)
+ {
+ $this->_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)];
+ }
+}