-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBHFormatter.php
More file actions
44 lines (34 loc) · 1.06 KB
/
BHFormatter.php
File metadata and controls
44 lines (34 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
<?php
declare(strict_types=1);
namespace Brick\Postcode\Formatter;
use Brick\Postcode\CountryPostcodeFormatter;
use function preg_match;
/**
* Validates and formats postcodes in Bahrain.
*
* Valid post code numbers are 101 to 1216 with gaps in the range. Known as block number formally.
* The first 1 or 2 digits refer to one of the 12 municipalities of the country.
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
*/
final class BHFormatter implements CountryPostcodeFormatter
{
public function hint(): string
{
return 'Valid post code numbers are 101 to 1216 with gaps in the range. Known as block number formally.';
}
public function format(string $postcode): ?string
{
if (preg_match('/^(1?[0-9])([0-9]{2})$/', $postcode, $matches) !== 1) {
return null;
}
$municipality = (int) $matches[1];
if ($municipality < 1 || $municipality > 12) {
return null;
}
if ($matches[2] === '00') {
return null;
}
return $postcode;
}
}