Skip to content

Commit 4b91f14

Browse files
authored
Correctly validate colombian postal codes. (#11)
1 parent 16f7ce2 commit 4b91f14

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

src/Formatter/COFormatter.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,35 @@
1313
* The first 2 digits represent the department and can range from 00 to 32.
1414
*
1515
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
16-
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Colombia
16+
* @see https://es.wikipedia.org/wiki/Anexo:C%C3%B3digos_postales_de_Colombia
1717
*/
1818
class COFormatter implements CountryPostcodeFormatter
1919
{
20+
protected $departments = [
21+
'05', '08', '11', '13',
22+
'15', '17', '18', '19',
23+
'20', '23', '25', '27',
24+
'41', '44', '47', '50',
25+
'52', '54', '63', '66',
26+
'68', '70', '73', '76',
27+
'81', '85', '86', '88',
28+
'91', '94', '95', '97',
29+
'99'
30+
];
31+
2032
/**
2133
* {@inheritdoc}
2234
*/
2335
public function format(string $postcode) : ?string
2436
{
25-
if (preg_match('/^[0-9]{6}$/', $postcode) !== 1) {
37+
if (preg_match('/^\d{2}(?!0000)\d{4}$/', $postcode) !== 1) {
2638
return null;
2739
}
2840

41+
2942
$department = substr($postcode, 0, 2);
3043

31-
if ($department > '32') {
44+
if (!in_array($department, $this->departments, true)) {
3245
return null;
3346
}
3447

tests/Formatter/COFormatterTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ public function providerFormat() : array
3434
['123', null],
3535
['1234', null],
3636
['12345', null],
37-
['000000', '000000'],
38-
['123456', '123456'],
39-
['320000', '320000'],
40-
['330000', null],
37+
['000000', null],
38+
['123456', null],
39+
['661001', '661001'],
40+
['760002', '760002'],
41+
['850000', null],
42+
['850001', '850001'],
4143
['990000', null],
4244
['1234567', null],
4345

0 commit comments

Comments
 (0)