From 7c11f827b24778d581798b343ab91f819e2f589f Mon Sep 17 00:00:00 2001 From: "Johnny X. Lemonade" Date: Sat, 28 Sep 2024 09:46:28 +0200 Subject: [PATCH 01/10] Add stricter regex for validating French postal codes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Updated the postal code validation regex to enforce the specific structure of French postal codes. The new regex ensures that the first two digits fall within the valid ranges (01–95 for metropolitan areas and 96–98 for overseas territories), while still allowing any valid 5-digit code. This improves accuracy over the previous general 5-digit validation, which allowed invalid codes. --- src/Formatter/FRFormatter.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Formatter/FRFormatter.php b/src/Formatter/FRFormatter.php index 36f8313..c097e1b 100644 --- a/src/Formatter/FRFormatter.php +++ b/src/Formatter/FRFormatter.php @@ -11,13 +11,20 @@ * * 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) { + + if (preg_match('/^(?:0[1-9]|[1-8]\d|9[0-8])\d{3}$/', $postcode) !== 1) { return null; } From 260a95888f1d55da10d63df07b5174d0f8872496 Mon Sep 17 00:00:00 2001 From: "Johnny X. Lemonade" Date: Sat, 28 Sep 2024 10:01:39 +0200 Subject: [PATCH 02/10] Implement strict regex for validating German postal codes Updated the postal code validation logic to enforce the specific structure of German postal codes using a more precise regular expression. This regex ensures that only valid postal codes are accepted, adhering to the defined formats for various regions in Germany. This change improves data integrity and prevents invalid postal code entries. --- src/Formatter/DEFormatter.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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; } From 297baf84c6a456f45eca7b401dd377f91706dfb8 Mon Sep 17 00:00:00 2001 From: "Johnny X. Lemonade" Date: Sun, 29 Sep 2024 18:50:42 +0200 Subject: [PATCH 03/10] refactor: Update Romanian postal code validation with regional code check - Replaced the basic regex `/^[0-9]{6}$/` which only validated a 6-digit format. - Introduced a new regex that ensures the first two digits match valid Romanian regional codes (01-92). - The updated regex now checks both the length (6 digits) and that the postal code belongs to a valid Romanian region. - This improves accuracy by ensuring only valid postal codes for Romanian regions are accepted. --- src/Formatter/ROFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; } From 3084654a9450796fcc5182a0db892619e2857443 Mon Sep 17 00:00:00 2001 From: "Johnny X. Lemonade" Date: Wed, 9 Oct 2024 09:39:03 +0200 Subject: [PATCH 04/10] Refactor postcode validation test cases - Updated the invalid postcode test cases to correctly reflect the expected output after formatting. - Ensured that postcodes with spaces and dashes are processed to remove those characters before validation. - Added test cases for both valid and invalid postcodes according to the specified regex pattern. --- tests/Formatter/ROFormatterTest.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/Formatter/ROFormatterTest.php b/tests/Formatter/ROFormatterTest.php index 719bc8d..860ab04 100644 --- a/tests/Formatter/ROFormatterTest.php +++ b/tests/Formatter/ROFormatterTest.php @@ -37,6 +37,33 @@ public function providerFormat() : array ['ABCD', null], ['ABCDE', null], ['ABCDEF', null], + + // valid + ['123-456', '123456'], + [' 123456 ', '123456'], + ['010101', '010101'], + ['123456', '123456'], + ['090123', '090123'], + ['080456', '080456'], + ['070789', '070789'], + ['123450', '123450'], + ['012345', '012345'], + ['202020', '202020'], + ['999999', '999999'], + + // invalid + ['000123', null], + ['01111', null], + ['000000', null], + ['-1234', null], + ['12ABCD', null], + ['12345678', null], + ['01 234 56', null], + ['0300!', null], + ['999999', null], + ['12 345', null], + ['07-089', null] + ]; } } From 0289c6dcccee92b02208c14b0413e6d68c6731d7 Mon Sep 17 00:00:00 2001 From: "Johnny X. Lemonade" Date: Wed, 9 Oct 2024 09:41:38 +0200 Subject: [PATCH 05/10] Update ROFormatterTest.php I overlooked one condition --- tests/Formatter/ROFormatterTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Formatter/ROFormatterTest.php b/tests/Formatter/ROFormatterTest.php index 860ab04..561c119 100644 --- a/tests/Formatter/ROFormatterTest.php +++ b/tests/Formatter/ROFormatterTest.php @@ -49,7 +49,6 @@ public function providerFormat() : array ['123450', '123450'], ['012345', '012345'], ['202020', '202020'], - ['999999', '999999'], // invalid ['000123', null], From 8266ab9f461eeaa488330f3b1b60382b7d954739 Mon Sep 17 00:00:00 2001 From: "Johnny X. Lemonade" Date: Wed, 9 Oct 2024 09:42:49 +0200 Subject: [PATCH 06/10] Update ROFormatterTest.php --- tests/Formatter/ROFormatterTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Formatter/ROFormatterTest.php b/tests/Formatter/ROFormatterTest.php index 561c119..18a3f81 100644 --- a/tests/Formatter/ROFormatterTest.php +++ b/tests/Formatter/ROFormatterTest.php @@ -40,7 +40,6 @@ public function providerFormat() : array // valid ['123-456', '123456'], - [' 123456 ', '123456'], ['010101', '010101'], ['123456', '123456'], ['090123', '090123'], From e20702863f04c867d3b8a87806747c9cfbf8e1fc Mon Sep 17 00:00:00 2001 From: "Johnny X. Lemonade" Date: Wed, 9 Oct 2024 09:43:41 +0200 Subject: [PATCH 07/10] Update ROFormatterTest.php --- tests/Formatter/ROFormatterTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Formatter/ROFormatterTest.php b/tests/Formatter/ROFormatterTest.php index 18a3f81..71d6a18 100644 --- a/tests/Formatter/ROFormatterTest.php +++ b/tests/Formatter/ROFormatterTest.php @@ -39,7 +39,6 @@ public function providerFormat() : array ['ABCDEF', null], // valid - ['123-456', '123456'], ['010101', '010101'], ['123456', '123456'], ['090123', '090123'], @@ -53,7 +52,6 @@ public function providerFormat() : array ['000123', null], ['01111', null], ['000000', null], - ['-1234', null], ['12ABCD', null], ['12345678', null], ['01 234 56', null], From cd3ec48acac0a0682a3c01086da86ddc0c970a87 Mon Sep 17 00:00:00 2001 From: "Johnny X. Lemonade" Date: Wed, 9 Oct 2024 09:46:00 +0200 Subject: [PATCH 08/10] keep [0-9] everywhere --- src/Formatter/FRFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Formatter/FRFormatter.php b/src/Formatter/FRFormatter.php index c097e1b..8641aae 100644 --- a/src/Formatter/FRFormatter.php +++ b/src/Formatter/FRFormatter.php @@ -24,7 +24,7 @@ class FRFormatter implements CountryPostcodeFormatter public function format(string $postcode) : ?string { - if (preg_match('/^(?:0[1-9]|[1-8]\d|9[0-8])\d{3}$/', $postcode) !== 1) { + if (preg_match('/^[0-9]{5}$/', $postcode) !== 1) { return null; } From 09a9c8ea19c4b7584d2f94061714686969b16b18 Mon Sep 17 00:00:00 2001 From: "Johnny X. Lemonade" Date: Wed, 9 Oct 2024 10:43:26 +0200 Subject: [PATCH 09/10] Update DEFormatterTest.php --- tests/Formatter/DEFormatterTest.php | 46 +++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/Formatter/DEFormatterTest.php b/tests/Formatter/DEFormatterTest.php index 5338dda..1cfeb94 100644 --- a/tests/Formatter/DEFormatterTest.php +++ b/tests/Formatter/DEFormatterTest.php @@ -36,6 +36,52 @@ 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], + ['02131', 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'], + ]; } } From 45f7031381ba9cd9f158814045aecdd618e10513 Mon Sep 17 00:00:00 2001 From: "Johnny X. Lemonade" Date: Wed, 9 Oct 2024 10:46:15 +0200 Subject: [PATCH 10/10] Update DEFormatterTest.php --- tests/Formatter/DEFormatterTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Formatter/DEFormatterTest.php b/tests/Formatter/DEFormatterTest.php index 1cfeb94..25c046a 100644 --- a/tests/Formatter/DEFormatterTest.php +++ b/tests/Formatter/DEFormatterTest.php @@ -48,7 +48,6 @@ public function providerFormat() : array ['132045', null], ['00001', null], ['05234', null], - ['02131', null], ['62000', null], ['43000', null],