-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBRFormatter.php
More file actions
36 lines (29 loc) · 872 Bytes
/
BRFormatter.php
File metadata and controls
36 lines (29 loc) · 872 Bytes
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
<?php
declare(strict_types=1);
namespace Brick\Postcode\Formatter;
use Brick\Postcode\CountryPostcodeFormatter;
use function preg_match;
use function substr;
/**
* Validates and formats postcodes in Brazil.
*
* Format is 5 digits, hyphen, 3 digits.
* The old format that used 5 digits is not accepted by this formatter.
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/C%C3%B3digo_de_Endere%C3%A7amento_Postal
*/
final class BRFormatter implements CountryPostcodeFormatter
{
public function hint(): string
{
return 'Format is 5 digits, hyphen, 3 digits.';
}
public function format(string $postcode): ?string
{
if (preg_match('/^[0-9]{8}$/', $postcode) !== 1) {
return null;
}
return substr($postcode, 0, 5) . '-' . substr($postcode, 5);
}
}