Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/Formatter/NGFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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));
}
}
11 changes: 11 additions & 0 deletions tests/Formatter/NGFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
];
}

Expand Down
Loading