Skip to content

Commit

Permalink
Merge pull request #17 from crazyfactory/default_len_and_lv
Browse files Browse the repository at this point in the history
feat: allow LV to have previx "LV-" and default zip code format len must < 10 chars
  • Loading branch information
konthornR authored Sep 14, 2021
2 parents ba64d42 + 832b80e commit b22bc4a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/ZipCode/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Validator
'CY' => '[1-9]\\d{3}',
'CZ' => '\\d{3}\\s\\d{2}',
'DE' => '[0-9]{5}',
'DEFAULT' => '[0-9A-Z]',
'DEFAULT' => '[0-9A-Z]{1,10}',
'DK' => '\\d{4}',
'EE' => '[1-9]{1}[0-9]{4}',
'ES' => '(AD{0,1}\\d{3})|\\d{5}',
Expand All @@ -73,7 +73,7 @@ class Validator
'IT' => '\\d{5}',
'LT' => '\\d{5}',
'LU' => '[0-9]\\d{3}',
'LV' => '[1-9]{1}\\d{3}',
'LV' => '(LV-)?[1-9]{1}\\d{3}',
'MC' => '98[0-9]{3}',
'MT' => '[A-Z]{3}\\s[1-9]{1}[0-9]{3}',
'NL' => '[1-9]\\d{3}\\s[A-Z]{2}',
Expand Down Expand Up @@ -111,13 +111,7 @@ public function validate(string $zipCode, string $countryCode): bool
])) {
return true;
}

if (isset(static::FORMATS[$countryCode])) {
$regex = '/^' . static::FORMATS[$countryCode] . '$/';
}
else {
$regex = '/' . static::FORMATS['DEFAULT'] . '/';
}
$regex = '/^' . (static::FORMATS[$countryCode] ?? static::FORMATS['DEFAULT']) . '$/i';

return preg_match($regex, $zipCode) > 0;
}
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/ZipCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ public function testIsValid()
$this->assertFalse(Validator::isValid('01234', 'LU'), 'LU');
$this->assertFalse(Validator::isValid('01234', 'RO'), 'RO');
$this->assertFalse(Validator::isValid('20207 MLINI', 'HR'), 'HR');
$this->assertFalse(Validator::isValid('12345678901', 'US'));

$this->assertTrue(Validator::isValid('123456', 'RO'), 'RO');
$this->assertTrue(Validator::isValid('01234', 'FR'), 'FR');
$this->assertTrue(Validator::isValid('M6 6SD', 'GB'), 'GB: M6 6SD');
$this->assertTrue(Validator::isValid('11111', 'HR'), 'HR: 11111');
$this->assertTrue(Validator::isValid('0363', 'LU'), 'LU');
$this->assertTrue(Validator::isValid('1234567890', 'US'));
$this->assertTrue(Validator::isValid('LV-1111', 'LV'));
$this->assertTrue(Validator::isValid('1111', 'LV'));

$noPostCodeCountries = ['AE', 'AU', 'BA', 'CO', 'QA'];
foreach ($noPostCodeCountries as $country) {
Expand Down

0 comments on commit b22bc4a

Please sign in to comment.