|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace marcuspi\RandomFactor; |
| 5 | + |
| 6 | +class Words |
| 7 | +{ |
| 8 | + private static $languages = [ |
| 9 | + 'no' => 'no-nb', |
| 10 | + 'en' => 'en' |
| 11 | + ]; |
| 12 | + |
| 13 | + private $language; |
| 14 | + private $spacing; |
| 15 | + private $nouns; |
| 16 | + private $adjectives; |
| 17 | + |
| 18 | + /** |
| 19 | + * Creates new Words. |
| 20 | + * |
| 21 | + * @access public |
| 22 | + * @param string $language The language to use (default: "en") |
| 23 | + * @param string $spacing Character(s) to used to split the words (default: " ") |
| 24 | + * @param int $nouns Number of nouns (default: 1) |
| 25 | + * @param int $adjectives Number of adjectives (default: 1) |
| 26 | + */ |
| 27 | + public function __construct($language = "en", $spacing = " ", $nouns = 1, $adjectives = 1) |
| 28 | + { |
| 29 | + $this->language($language); |
| 30 | + $this->spacing($spacing); |
| 31 | + $this->nouns($nouns); |
| 32 | + $this->adjectives($adjectives); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Sets which language that should be used |
| 37 | + * |
| 38 | + * @access public |
| 39 | + * @param string $language One of "no", "en". |
| 40 | + * @return self |
| 41 | + */ |
| 42 | + public function language(string $language): Words |
| 43 | + { |
| 44 | + if (!array_key_exists(mb_strtolower($language), static::$languages)) { |
| 45 | + throw new \Exception('Language \'' . $language . '\' not found'); |
| 46 | + } |
| 47 | + |
| 48 | + $this->language = mb_strtolower($language); |
| 49 | + |
| 50 | + return $this; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Sets the character(s) to used to split the words |
| 55 | + * |
| 56 | + * @access public |
| 57 | + * @param string $spacing |
| 58 | + * @return self |
| 59 | + */ |
| 60 | + public function spacing(string $spacing): Words |
| 61 | + { |
| 62 | + $this->spacing = $spacing; |
| 63 | + |
| 64 | + return $this; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Sets the number of nouns |
| 69 | + * |
| 70 | + * @access public |
| 71 | + * @param int $nouns A positive integer |
| 72 | + * @return self |
| 73 | + */ |
| 74 | + public function nouns(int $nouns): Words |
| 75 | + { |
| 76 | + if ($nouns < 0) { |
| 77 | + throw new \Exception('Number of nouns must be a positive integer'); |
| 78 | + } |
| 79 | + |
| 80 | + $this->nouns = $nouns; |
| 81 | + |
| 82 | + return $this; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Sets the number of adjectives |
| 87 | + * |
| 88 | + * @access public |
| 89 | + * @param int $adjectives A positive integer |
| 90 | + * @return self |
| 91 | + */ |
| 92 | + public function adjectives(int $adjectives): Words |
| 93 | + { |
| 94 | + if ($adjectives < 0) { |
| 95 | + throw new \Exception('Number of adjectives must be a positive integer'); |
| 96 | + } |
| 97 | + |
| 98 | + $this->adjectives = $adjectives; |
| 99 | + |
| 100 | + return $this; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Static creation. |
| 105 | + * |
| 106 | + * @access public |
| 107 | + * @static |
| 108 | + * @return self |
| 109 | + */ |
| 110 | + public static function create() |
| 111 | + { |
| 112 | + return new static; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Generates the random words. |
| 117 | + * |
| 118 | + * @access public |
| 119 | + * @return string |
| 120 | + */ |
| 121 | + public function generate(): string |
| 122 | + { |
| 123 | + |
| 124 | + $wordlist = $this->readWordList(); |
| 125 | + |
| 126 | + $words = []; |
| 127 | + |
| 128 | + for ($i = 0; $i < $this->adjectives; $i++) { |
| 129 | + $words[] = $wordlist['adjectives'][random_int(0, count($wordlist['adjectives']) - 1)]; |
| 130 | + } |
| 131 | + |
| 132 | + for ($i = 0; $i < $this->nouns; $i++) { |
| 133 | + $words[] = $wordlist['nouns'][random_int(0, count($wordlist['nouns']) - 1)]; |
| 134 | + } |
| 135 | + |
| 136 | + return implode($this->spacing, $words); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Alias for self::generate() |
| 141 | + * |
| 142 | + * @access public |
| 143 | + * @return string |
| 144 | + */ |
| 145 | + public function __toString(): string |
| 146 | + { |
| 147 | + return $this->generate(); |
| 148 | + } |
| 149 | + |
| 150 | + private function readWordList(): array |
| 151 | + { |
| 152 | + $wordfile = __DIR__ . '/lang/' . static::$languages[$this->language] . '.json'; |
| 153 | + |
| 154 | + if (!file_exists($wordfile)) { |
| 155 | + throw new \Exception('Language file \'' . $wordfile . '\' not found'); |
| 156 | + } |
| 157 | + |
| 158 | + return json_decode(file_get_contents($wordfile), true); |
| 159 | + } |
| 160 | +} |
0 commit comments