Skip to content
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
9 changes: 8 additions & 1 deletion src/Formatter/FRFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

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

Please avoid mixing \d with ranges, I think it hurts readability. Let's keep [0-9] everywhere.

Copy link
Author

Choose a reason for hiding this comment

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

I agree with this perspective, as even if the given numbers are not in a valid range, using [0-9] enhances clarity.

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