Skip to content

Commit

Permalink
Correctly validate colombian postal codes. (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
strotgen authored Feb 22, 2023
1 parent 16f7ce2 commit 4b91f14
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
19 changes: 16 additions & 3 deletions src/Formatter/COFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,35 @@
* The first 2 digits represent the department and can range from 00 to 32.
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Colombia
* @see https://es.wikipedia.org/wiki/Anexo:C%C3%B3digos_postales_de_Colombia
*/
class COFormatter implements CountryPostcodeFormatter
{
protected $departments = [
'05', '08', '11', '13',
'15', '17', '18', '19',
'20', '23', '25', '27',
'41', '44', '47', '50',
'52', '54', '63', '66',
'68', '70', '73', '76',
'81', '85', '86', '88',
'91', '94', '95', '97',
'99'
];

/**
* {@inheritdoc}
*/
public function format(string $postcode) : ?string
{
if (preg_match('/^[0-9]{6}$/', $postcode) !== 1) {
if (preg_match('/^\d{2}(?!0000)\d{4}$/', $postcode) !== 1) {
return null;
}


$department = substr($postcode, 0, 2);

if ($department > '32') {
if (!in_array($department, $this->departments, true)) {
return null;
}

Expand Down
10 changes: 6 additions & 4 deletions tests/Formatter/COFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ public function providerFormat() : array
['123', null],
['1234', null],
['12345', null],
['000000', '000000'],
['123456', '123456'],
['320000', '320000'],
['330000', null],
['000000', null],
['123456', null],
['661001', '661001'],
['760002', '760002'],
['850000', null],
['850001', '850001'],
['990000', null],
['1234567', null],

Expand Down

0 comments on commit 4b91f14

Please sign in to comment.