-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBBFormatter.php
More file actions
41 lines (33 loc) · 1.01 KB
/
BBFormatter.php
File metadata and controls
41 lines (33 loc) · 1.01 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
<?php
declare(strict_types=1);
namespace Brick\Postcode\Formatter;
use Brick\Postcode\CountryPostcodeFormatter;
use function preg_match;
use function str_starts_with;
use function substr;
/**
* Validates and formats postcodes in Barbados.
*
* Postal codes in Barbados are 5 digit numeric, with BB prefix.
* This formatter accepts postcodes with and without the BB prefix, and always outputs the prefix.
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Barbados
*/
final class BBFormatter implements CountryPostcodeFormatter
{
public function hint(): string
{
return 'Postal codes in Barbados are 5 digit numeric, with BB prefix.';
}
public function format(string $postcode): ?string
{
if (str_starts_with($postcode, 'BB')) {
$postcode = substr($postcode, 2);
}
if (preg_match('/^[0-9]{5}$/', $postcode) !== 1) {
return null;
}
return 'BB' . $postcode;
}
}