diff --git a/src/CantonManager.php b/src/CantonManager.php index 96ebcdc..06eb8f7 100644 --- a/src/CantonManager.php +++ b/src/CantonManager.php @@ -3,6 +3,7 @@ namespace Wnx\SwissCantons; use Wnx\SwissCantons\Exceptions\CantonNotFoundException; +use Wnx\SwissCantons\Exceptions\NotUniqueCantonException; class CantonManager { @@ -81,29 +82,30 @@ public function getByZipcode(int $zipcode): array * @param int $zipcode * @param ?string $cityName * @return Canton - * @throws CantonNotFoundException + * @throws CantonNotFoundException|NotUniqueCantonException */ public function getByZipcodeAndCity(int $zipcode, ?string $cityName = null): Canton { - $cities = $this->citySearch->findByZipcode($zipcode); + $cantons = $this->getByZipcode($zipcode); - if (1 === count($cities)) { - return $this->search->findByAbbreviation($cities[0]['canton']) - ?? throw CantonNotFoundException::notFoundForZipcode($zipcode); + if (1 === count($cantons)) { + return $cantons[0]; } if (null !== $cityName) { + $cities = $this->citySearch->findByZipcode($zipcode); + foreach ($cities as $city) { if ($city['city'] === $cityName) { return $this->search->findByAbbreviation($city['canton']) - ?? throw CantonNotFoundException::notFoundForZipcodeAndCity($zipcode, $cityName); + ?? throw CantonNotFoundException::notFoundForAbbreviation($city['canton']); } } - throw CantonNotFoundException::notFoundForZipcodeAndCity($zipcode, $cityName); + throw NotUniqueCantonException::notUniqueForZipcodeAndCity($zipcode, $cityName); } - throw CantonNotFoundException::notFoundForZipcode($zipcode); + throw NotUniqueCantonException::notUniqueForZipcode($zipcode); } } diff --git a/src/Exceptions/CantonNotFoundException.php b/src/Exceptions/CantonNotFoundException.php index a398669..929feaf 100644 --- a/src/Exceptions/CantonNotFoundException.php +++ b/src/Exceptions/CantonNotFoundException.php @@ -23,10 +23,4 @@ public static function notFoundForZipcode(int $zipcode): self /** @phpstan-ignore-next-line */ return new static("Couldn't find Canton for given zipcode: {$zipcode}"); } - - public static function notFoundForZipcodeAndCity(int $zipcode, string $city): self - { - /** @phpstan-ignore-next-line */ - return new static("Couldn't find Canton for given zipcode and city name: {$zipcode}, {$city}"); - } } diff --git a/src/Exceptions/NotUniqueCantonException.php b/src/Exceptions/NotUniqueCantonException.php new file mode 100644 index 0000000..e9d7cc0 --- /dev/null +++ b/src/Exceptions/NotUniqueCantonException.php @@ -0,0 +1,20 @@ +assertEquals('VD', $canton->getAbbreviation()); } + #[Test] + public function it_finds_single_canton_with_zipcode_for_several_cities_in_same_canton(): void + { + $cantonManager = new CantonManager(); + + $canton = $cantonManager->getByZipcodeAndCity(8914); + + $this->assertEquals('ZH', $canton->getAbbreviation()); + } + #[Test] public function it_throws_exception_if_no_single_canton_for_zipcode_could_be_found(): void { @@ -154,10 +165,20 @@ public function it_throws_exception_if_no_single_canton_for_zipcode_could_be_fou #[Test] public function it_throws_exception_if_no_canton_for_zipcode_and_city_could_be_found(): void { - $this->expectException(CantonNotFoundException::class); + $this->expectException(NotUniqueCantonException::class); $canton = new CantonManager(); $canton->getByZipcodeAndCity(1290, 'Lausanne'); } + + #[Test] + public function it_throws_exception_if_no_unique_canton_for_zipcode(): void + { + $this->expectException(NotUniqueCantonException::class); + + $canton = new CantonManager(); + + $canton->getByZipcodeAndCity(1290); + } }