Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stricter regex for validating FR, DE, RO postal codes #16

Closed
wants to merge 10 commits into from
4 changes: 3 additions & 1 deletion src/Formatter/DEFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class DEFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
if (preg_match('/^[0-9]{5}$/', $postcode) !== 1) {

if (preg_match('/^((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:4[0-24-9]\d{3})|(?:6[013-9]\d{3}))$/', $postcode) !== 1) {

return null;
}

Expand Down
7 changes: 7 additions & 0 deletions src/Formatter/FRFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@
*
* Postcodes consist of 5 digits, without separator.
*
* First two digits indicate the department (region)
* 01–95 for metropolitan areas
* 96–98 for overseas territories
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_France
*/
class FRFormatter implements CountryPostcodeFormatter
{

public function format(string $postcode) : ?string
{

if (preg_match('/^[0-9]{5}$/', $postcode) !== 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you removed the updated regex for FR by mistake?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, nitpick: please try to keep formatting intact (not adding newlines), we don't have a CS fixer on this project yet, so let's try to keep it tidy manually!

return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/ROFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ROFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
if (preg_match('/^[0-9]{6}$/', $postcode) !== 1) {
if (preg_match('/^(01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|50|51|52|53|54|55|60|61|62|70|71|72|73|80|81|82|90|91|92)[0-9]{4}$/', $postcode) !== 1) {
return null;
}

Expand Down
45 changes: 45 additions & 0 deletions tests/Formatter/DEFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,51 @@ public function providerFormat() : array
['ABCD', null],
['ABCDE', null],
['ABCDEF', null],

// invalid
['010101', null],
['000000', null],
['(123456)', null],
['!23456', null],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data passed to CountryPostcodeFormatter::format() can only contain uppercase alphanumeric characters, so testing with other characters is redundant here.

(we may revisit this in #17 though!)

['412345', null],
['567890', null],
['4A1234', null],
['132045', null],
['00001', null],
['05234', null],
['62000', null],
['43000', null],

// valid
['01234','01234'],
['04123','04123'],
['06123','06123'],
['07123', '07123'],
['08123', '08123'],
['09123', '09123'],
['18888', '18888'],
['38888', '38888'],
['58888', '58888'],
['78888', '78888'],
['88888', '88888'],
['99999', '99999'],
['40123', '40123'],
['42123', '42123'],
['44123', '44123'],
['46123', '46123'],
['47123', '47123'],
['48123', '48123'],
['49123', '49123'],
['60123', '60123'],
['61123', '61123'],
['63123', '63123'],
['64123', '64123'],
['65123', '65123'],
['66123', '66123'],
['67123', '67123'],
['68123', '68123'],
['69123', '69123'],

];
}
}
23 changes: 23 additions & 0 deletions tests/Formatter/ROFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ public function providerFormat() : array
['ABCD', null],
['ABCDE', null],
['ABCDEF', null],

// valid
['010101', '010101'],
['123456', '123456'],
['090123', '090123'],
['080456', '080456'],
['070789', '070789'],
['123450', '123450'],
['012345', '012345'],
['202020', '202020'],

// invalid
['000123', null],
['01111', null],
['000000', null],
['12ABCD', null],
['12345678', null],
['01 234 56', null],
['0300!', null],
['999999', null],
['12 345', null],
['07-089', null]

];
}
}