diff --git a/component/Geography/Fr/CodePostal.php b/component/Geography/Fr/CodePostal.php index 9aac721..f096ca3 100755 --- a/component/Geography/Fr/CodePostal.php +++ b/component/Geography/Fr/CodePostal.php @@ -17,4 +17,20 @@ public function __construct(string $code) parent::__construct($code); } + + public function getNumeroDepartement(): NumeroDepartement + { + $numeroDepartement = substr($this->value, 0, 2); + $numeroDepartement = match($numeroDepartement) { + '20' => match (substr($this->value, 0, 3)) { + '201' => '2A', + '202' => '2B', + }, + '97' => substr($this->value, 0, 3), + '98' => substr($this->value, 0, 3), + default => $numeroDepartement, + }; + + return new NumeroDepartement($numeroDepartement); + } } diff --git a/test/Geography/Fr/CodeCommuneTest.php b/test/Geography/Fr/CodeCommuneTest.php index 75a70fa..df002e9 100644 --- a/test/Geography/Fr/CodeCommuneTest.php +++ b/test/Geography/Fr/CodeCommuneTest.php @@ -18,9 +18,6 @@ public function testInterface(): void $this->assertIsString($codeCommune->value); $this->assertEquals('97421', $codeCommune->value); - - $this->assertIsObject($codeCommune->getNumeroDepartement()); - $this->assertEquals('974', (string)$codeCommune->getNumeroDepartement()); } public function testNotCompliant(): void @@ -29,4 +26,15 @@ public function testNotCompliant(): void new CodeCommune('690I'); } + + public function testGetNumeroDepartement(): void + { + $codeCommune = new CodeCommune('69001'); + $this->assertIsObject($codeCommune->getNumeroDepartement()); + $this->assertEquals('69', (string)$codeCommune->getNumeroDepartement()); + + $codeCommune = new CodeCommune('97421'); + $this->assertIsObject($codeCommune->getNumeroDepartement()); + $this->assertEquals('974', (string)$codeCommune->getNumeroDepartement()); + } } diff --git a/test/Geography/Fr/CodePostalTest.php b/test/Geography/Fr/CodePostalTest.php index f29dff9..469daab 100644 --- a/test/Geography/Fr/CodePostalTest.php +++ b/test/Geography/Fr/CodePostalTest.php @@ -26,4 +26,27 @@ public function testNotCompliant(): void new CodePostal('690I'); } + + public function testGetNumeroDepartement(): void + { + $codePostal = new CodePostal('69001'); + $this->assertIsObject($codePostal->getNumeroDepartement()); + $this->assertEquals('69', (string)$codePostal->getNumeroDepartement()); + + $codePostal = new CodePostal('20169'); + $this->assertIsObject($codePostal->getNumeroDepartement()); + $this->assertEquals('2A', (string)$codePostal->getNumeroDepartement()); + + $codePostal = new CodePostal('20290'); + $this->assertIsObject($codePostal->getNumeroDepartement()); + $this->assertEquals('2B', (string)$codePostal->getNumeroDepartement()); + + $codePostal = new CodePostal('97400'); + $this->assertIsObject($codePostal->getNumeroDepartement()); + $this->assertEquals('974', (string)$codePostal->getNumeroDepartement()); + + $codePostal = new CodePostal('98735'); + $this->assertIsObject($codePostal->getNumeroDepartement()); + $this->assertEquals('987', (string)$codePostal->getNumeroDepartement()); + } }