-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathCOFormatter.php
More file actions
55 lines (45 loc) · 1.31 KB
/
COFormatter.php
File metadata and controls
55 lines (45 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
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
46
47
48
49
50
51
52
53
54
55
<?php
declare(strict_types=1);
namespace Brick\Postcode\Formatter;
use Brick\Postcode\CountryPostcodeFormatter;
use function in_array;
use function preg_match;
use function substr;
/**
* Validates and formats postcodes in Colombia.
*
* Postal codes in Colombia are 6 digit numeric.
* 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://es.wikipedia.org/wiki/Anexo:C%C3%B3digos_postales_de_Colombia
*/
final class COFormatter implements CountryPostcodeFormatter
{
private const 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',
];
public function hint(): string
{
return 'Postal codes in Colombia are 6 digit numeric.';
}
public function format(string $postcode): ?string
{
if (preg_match('/^\d{2}(?!0000)\d{4}$/', $postcode) !== 1) {
return null;
}
$department = substr($postcode, 0, 2);
if (! in_array($department, self::DEPARTMENTS, true)) {
return null;
}
return $postcode;
}
}