-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathAXFormatter.php
More file actions
60 lines (48 loc) · 1.36 KB
/
AXFormatter.php
File metadata and controls
60 lines (48 loc) · 1.36 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
56
57
58
59
60
<?php
declare(strict_types=1);
namespace Brick\Postcode\Formatter;
use Brick\Postcode\CountryPostcodeFormatter;
use function preg_match;
use function str_starts_with;
use function strlen;
use function substr;
/**
* Validates and formats postcodes in Åland Islands.
*
* Postcodes consist of 5 digits, starting with 22.
* Postcodes may optionally start with "AX-" when used from abroad.
* This formatter only outputs the prefix if present in the input.
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
*/
final class AXFormatter implements CountryPostcodeFormatter
{
public function hint(): string
{
return 'Postcodes consist of 5 digits, starting with 22.';
}
public function format(string $postcode): ?string
{
$length = strlen($postcode);
$prefix = false;
if ($length === 7) {
if (! str_starts_with($postcode, 'AX')) {
return null;
}
$postcode = substr($postcode, 2);
$prefix = true;
} elseif (strlen($postcode) !== 5) {
return null;
}
if (preg_match('/^[0-9]+$/', $postcode) !== 1) {
return null;
}
if (! str_starts_with($postcode, '22')) {
return null;
}
if ($prefix) {
return 'AX-' . $postcode;
}
return $postcode;
}
}