Skip to content

Commit c2fb9a4

Browse files
committed
Add factory methods to Japan Prefecture
1 parent f454d53 commit c2fb9a4

3 files changed

Lines changed: 90 additions & 0 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
}
1313
],
1414
"require": {
15+
"php": ">=7.0",
1516
"symfony/intl": "^3.3"
1617
},
1718
"require-dev": {

src/Geo/Japan/Prefecture.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tanigami\ValueObjects\Geo\Japan;
44

55
use BadMethodCallException;
6+
use InvalidArgumentException;
67

78
/**
89
* @method static Prefecture hokkaido
@@ -180,6 +181,60 @@ public static function __callStatic(string $name, array $arguments): self
180181
);
181182
}
182183

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+
183238
/**
184239
* @return string
185240
*/

tests/Geo/Japan/PrefectureTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,38 @@ public function testInvalidFactoryMethod()
252252
{
253253
Prefecture::sato();
254254
}
255+
256+
public function testCreationFromKanjiName()
257+
{
258+
$this->assertSame('JP-47', Prefecture::ofKanjiName('沖縄')->isoCode());
259+
$this->assertSame('Okinawa-ken', Prefecture::ofKanjiName('沖縄')->suffixedName());
260+
$this->assertSame('沖縄県', Prefecture::ofKanjiName('沖縄')->suffixedKanjiName());
261+
$this->assertTrue(Prefecture::ofKanjiName('沖縄')->region()->equals(Region::okinawa()));
262+
}
263+
264+
/**
265+
* @expectedException \InvalidArgumentException
266+
* @expectedExceptionMessage There is no prefecture of kanji name: 東都
267+
*/
268+
public function testCreationFromInvalidKanjiName()
269+
{
270+
Prefecture::ofKanjiName('東都');
271+
}
272+
273+
public function testCreationFromSuffixedKanjiName()
274+
{
275+
$this->assertSame('JP-47', Prefecture::ofSuffixedKanjiName('沖縄県')->isoCode());
276+
$this->assertSame('Okinawa-ken', Prefecture::ofSuffixedKanjiName('沖縄県')->suffixedName());
277+
$this->assertSame('沖縄県', Prefecture::ofSuffixedKanjiName('沖縄県')->suffixedKanjiName());
278+
$this->assertTrue(Prefecture::ofSuffixedKanjiName('沖縄県')->region()->equals(Region::okinawa()));
279+
}
280+
281+
/**
282+
* @expectedException \InvalidArgumentException
283+
* @expectedExceptionMessage There is no prefecture of suffixed kanji name: 大阪都
284+
*/
285+
public function testCreationFromInvalidSuffixedKanjiName()
286+
{
287+
Prefecture::ofSuffixedKanjiName('大阪都');
288+
}
255289
}

0 commit comments

Comments
 (0)