|
3 | 3 | namespace Tanigami\ValueObjects\Geo\Japan; |
4 | 4 |
|
5 | 5 | use BadMethodCallException; |
| 6 | +use InvalidArgumentException; |
6 | 7 |
|
7 | 8 | /** |
8 | 9 | * @method static Prefecture hokkaido |
@@ -180,6 +181,60 @@ public static function __callStatic(string $name, array $arguments): self |
180 | 181 | ); |
181 | 182 | } |
182 | 183 |
|
| 184 | + /** |
| 185 | + * @param string $kanjiName |
| 186 | + * @return Prefecture |
| 187 | + */ |
| 188 | + public static function ofKanjiName(string $kanjiName): self |
| 189 | + { |
| 190 | + $filteredData = array_filter(self::DATA, function (array $prefecture) use ($kanjiName) { |
| 191 | + return $kanjiName === strtolower($prefecture['kanjiName']); |
| 192 | + }); |
| 193 | + if ([] === $filteredData) { |
| 194 | + throw new InvalidArgumentException(sprintf('There is no prefecture of kanji name: %s', $kanjiName)); |
| 195 | + } |
| 196 | + $datum = array_shift($filteredData); |
| 197 | + $suffixFactoryMethod = $datum['suffix']; |
| 198 | + $regionFactoryMethod = $datum['region']; |
| 199 | + |
| 200 | + return new self ( |
| 201 | + $datum['isoCode'], |
| 202 | + $datum['name'], |
| 203 | + $datum['kanjiName'], |
| 204 | + PrefectureSuffix::$suffixFactoryMethod(), |
| 205 | + Region::$regionFactoryMethod() |
| 206 | + ); |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * @param string $suffixedKanjiName |
| 211 | + * @return Prefecture |
| 212 | + */ |
| 213 | + public static function ofSuffixedKanjiName(string $suffixedKanjiName): self |
| 214 | + { |
| 215 | + $filteredData = array_filter(self::DATA, function (array $prefecture) use ($suffixedKanjiName) { |
| 216 | + $suffixFactoryMethod = $prefecture['suffix']; |
| 217 | + |
| 218 | + return $suffixedKanjiName === $prefecture['kanjiName'] . PrefectureSuffix::$suffixFactoryMethod()->kanjiName(); |
| 219 | + }); |
| 220 | + if ([] === $filteredData) { |
| 221 | + throw new InvalidArgumentException( |
| 222 | + sprintf('There is no prefecture of suffixed kanji name: %s', $suffixedKanjiName) |
| 223 | + ); |
| 224 | + } |
| 225 | + $datum = array_shift($filteredData); |
| 226 | + $suffixFactoryMethod = $datum['suffix']; |
| 227 | + $regionFactoryMethod = $datum['region']; |
| 228 | + |
| 229 | + return new self ( |
| 230 | + $datum['isoCode'], |
| 231 | + $datum['name'], |
| 232 | + $datum['kanjiName'], |
| 233 | + PrefectureSuffix::$suffixFactoryMethod(), |
| 234 | + Region::$regionFactoryMethod() |
| 235 | + ); |
| 236 | + } |
| 237 | + |
183 | 238 | /** |
184 | 239 | * @return string |
185 | 240 | */ |
|
0 commit comments