diff --git a/src/Formatter/ESFormatter.php b/src/Formatter/ESFormatter.php index cca9d94..7b8d2be 100644 --- a/src/Formatter/ESFormatter.php +++ b/src/Formatter/ESFormatter.php @@ -8,14 +8,17 @@ use Override; use function preg_match; +use function substr; /** * Validates and formats postcodes in Spain. * * Postcodes consist of 5 digits, without separator. + * The first two digits (ranging from 01–52) correspond to the province or autonomous city. * * @see https://en.wikipedia.org/wiki/List_of_postal_codes * @see https://en.wikipedia.org/wiki/List_of_postal_codes_in_Spain + * @see https://en.wikipedia.org/wiki/Postal_codes_in_Spain */ final class ESFormatter implements CountryPostcodeFormatter { @@ -26,6 +29,12 @@ public function format(string $postcode): ?string return null; } + $province = substr($postcode, 0, 2); + + if ($province < '01' || $province > '52') { + return null; + } + return $postcode; } } diff --git a/tests/Formatter/ESFormatterTest.php b/tests/Formatter/ESFormatterTest.php index 734cf47..70b105a 100644 --- a/tests/Formatter/ESFormatterTest.php +++ b/tests/Formatter/ESFormatterTest.php @@ -23,6 +23,12 @@ public static function providerFormat(): array ['123', null], ['1234', null], ['12345', '12345'], + ['00999', null], + ['01001', '01001'], + ['28039', '28039'], + ['52001', '52001'], + ['53000', null], + ['99999', null], ['123456', null], ['A', null],