diff --git a/src/Formatter/DEFormatter.php b/src/Formatter/DEFormatter.php index 42c40f0..d587166 100644 --- a/src/Formatter/DEFormatter.php +++ b/src/Formatter/DEFormatter.php @@ -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; } diff --git a/src/Formatter/FRFormatter.php b/src/Formatter/FRFormatter.php index 36f8313..8641aae 100644 --- a/src/Formatter/FRFormatter.php +++ b/src/Formatter/FRFormatter.php @@ -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) { return null; } diff --git a/src/Formatter/ROFormatter.php b/src/Formatter/ROFormatter.php index 55eec0a..faadc0d 100644 --- a/src/Formatter/ROFormatter.php +++ b/src/Formatter/ROFormatter.php @@ -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; } diff --git a/tests/Formatter/DEFormatterTest.php b/tests/Formatter/DEFormatterTest.php index 5338dda..25c046a 100644 --- a/tests/Formatter/DEFormatterTest.php +++ b/tests/Formatter/DEFormatterTest.php @@ -36,6 +36,51 @@ public function providerFormat() : array ['ABCD', null], ['ABCDE', null], ['ABCDEF', null], + + // invalid + ['010101', null], + ['000000', null], + ['(123456)', null], + ['!23456', null], + ['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'], + ]; } } diff --git a/tests/Formatter/ROFormatterTest.php b/tests/Formatter/ROFormatterTest.php index 719bc8d..71d6a18 100644 --- a/tests/Formatter/ROFormatterTest.php +++ b/tests/Formatter/ROFormatterTest.php @@ -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] + ]; } }