-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathASFormatter.php
More file actions
55 lines (42 loc) · 1.27 KB
/
ASFormatter.php
File metadata and controls
55 lines (42 loc) · 1.27 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 preg_match;
use function strlen;
use function substr;
/**
* Validates and formats postcodes in American Samoa.
*
* Mail service in American Samoa is fully integrated with the United States Postal Service.
* All points in the territory use the same ZIP code, 96799.
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_American_Samoa
*/
final class ASFormatter implements CountryPostcodeFormatter
{
public function hint(): string
{
return 'Mail service in American Samoa is fully integrated with the United States Postal Service.';
}
public function format(string $postcode): ?string
{
$length = strlen($postcode);
if ($length === 5) {
if ($postcode !== '96799') {
return null;
}
return $postcode;
}
if (preg_match('/^[0-9]{9}$/', $postcode) !== 1) {
return null;
}
$zip = substr($postcode, 0, 5);
if ($zip !== '96799') {
return null;
}
$plusFour = substr($postcode, 5);
return $zip . '-' . $plusFour;
}
}