Skip to content

Commit

Permalink
Merge pull request #37 from PhantPHP/get-code-departement
Browse files Browse the repository at this point in the history
Get code departement
  • Loading branch information
lennyrouanet authored Oct 4, 2024
2 parents 6e13ec7 + f6a9ce7 commit 078b8ce
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
16 changes: 16 additions & 0 deletions component/Geography/Fr/CodePostal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 11 additions & 3 deletions test/Geography/Fr/CodeCommuneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
}
23 changes: 23 additions & 0 deletions test/Geography/Fr/CodePostalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 078b8ce

Please sign in to comment.