Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Formatter/ESFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}
}
6 changes: 6 additions & 0 deletions tests/Formatter/ESFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
Loading