diff --git a/src/Formatter/NGFormatter.php b/src/Formatter/NGFormatter.php index d3b7b3e..85b89e9 100644 --- a/src/Formatter/NGFormatter.php +++ b/src/Formatter/NGFormatter.php @@ -7,13 +7,18 @@ use Brick\Postcode\CountryPostcodeFormatter; use Override; +use function array_slice; +use function implode; use function preg_match; /** * Validates and formats postcodes in Nigeria. * - * Postal codes in Nigeria are numeric, consisting of six digits. + * The digital postcode NIPOST launched in October 2026 has 11 characters, AA-NN-XXX-AA-NN, + * where A stands for a letter, N for a digit and X for either. Both numeric segments + * run from 01 to 99. The former six-digit code is still accepted. * + * @see https://docs.postcode.gov.ng/concepts/postcode-format * @see https://en.wikipedia.org/wiki/List_of_postal_codes * @see https://en.wikipedia.org/wiki/Postal_codes_in_Nigeria */ @@ -22,10 +27,16 @@ final class NGFormatter implements CountryPostcodeFormatter #[Override] public function format(string $postcode): ?string { - if (preg_match('/^[0-9]{6}$/', $postcode) !== 1) { + if (preg_match('/^[0-9]{6}$/', $postcode) === 1) { + return $postcode; + } + + $pattern = '/^([A-Z]{2})(0[1-9]|[1-9][0-9])([A-Z0-9]{3})([A-Z]{2})(0[1-9]|[1-9][0-9])$/'; + + if (preg_match($pattern, $postcode, $matches) !== 1) { return null; } - return $postcode; + return implode('-', array_slice($matches, 1)); } } diff --git a/tests/Formatter/NGFormatterTest.php b/tests/Formatter/NGFormatterTest.php index c1c5ba8..8073ff3 100644 --- a/tests/Formatter/NGFormatterTest.php +++ b/tests/Formatter/NGFormatterTest.php @@ -33,6 +33,17 @@ public static function providerFormat(): array ['ABCDE', null], ['ABCDEF', null], ['ABCDEFG', null], + + ['EK01A03FK01', 'EK-01-A03-FK-01'], + ['FC03B06AG12', 'FC-03-B06-AG-12'], + ['LA99ZZZTC99', 'LA-99-ZZZ-TC-99'], + ['EK00A03FK01', null], + ['EK01A03FK00', null], + ['EK01A03FK1', null], + ['EK01A03FK011', null], + ['E101A03FK01', null], + ['EKA1A03FK01', null], + ['EK01A03F101', null], ]; }