-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathCRFormatter.php
More file actions
47 lines (37 loc) · 1.06 KB
/
CRFormatter.php
File metadata and controls
47 lines (37 loc) · 1.06 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
<?php
declare(strict_types=1);
namespace Brick\Postcode\Formatter;
use Brick\Postcode\CountryPostcodeFormatter;
use function preg_match;
use function strlen;
use function substr;
/**
* Validates and formats postcodes in Costa Rica.
*
* Postal codes in Costa Rica are 5 digit numeric.
* According to Wikipedia, street level format is 5 digits, dash, 4 digits.
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/List_of_districts_of_Costa_Rica
*/
final class CRFormatter implements CountryPostcodeFormatter
{
public function hint(): string
{
return 'Postal codes in Costa Rica are 5 digit numeric.';
}
public function format(string $postcode): ?string
{
if (preg_match('/^[0-9]+$/', $postcode) !== 1) {
return null;
}
$length = strlen($postcode);
if ($length === 5) {
return $postcode;
}
if ($length !== 9) {
return null;
}
return substr($postcode, 0, 5) . '-' . substr($postcode, 5);
}
}