diff --git a/src/FormatHelper/StripPrefix.php b/src/FormatHelper/StripPrefix.php new file mode 100644 index 0000000..70644d8 --- /dev/null +++ b/src/FormatHelper/StripPrefix.php @@ -0,0 +1,36 @@ +stripPrefix($postcode, 'A'); + if (preg_match('/^[1-9][0-9]{3}$/', $postcode) !== 1) { return null; } diff --git a/src/Formatter/BEFormatter.php b/src/Formatter/BEFormatter.php index df200e4..03ae5f0 100644 --- a/src/Formatter/BEFormatter.php +++ b/src/Formatter/BEFormatter.php @@ -5,6 +5,7 @@ namespace Brick\Postcode\Formatter; use Brick\Postcode\CountryPostcodeFormatter; +use Brick\Postcode\FormatHelper\StripPrefix; /** * Validates and formats postcodes in Belgium. @@ -16,8 +17,12 @@ */ class BEFormatter implements CountryPostcodeFormatter { + use StripPrefix; + public function format(string $postcode) : ?string { + $postcode = $this->stripPrefix($postcode, 'B'); + if (preg_match('/^[0-9]{4}$/', $postcode) !== 1) { return null; } diff --git a/src/Formatter/LUFormatter.php b/src/Formatter/LUFormatter.php index bdea984..bc6ec4b 100644 --- a/src/Formatter/LUFormatter.php +++ b/src/Formatter/LUFormatter.php @@ -5,6 +5,7 @@ namespace Brick\Postcode\Formatter; use Brick\Postcode\CountryPostcodeFormatter; +use Brick\Postcode\FormatHelper\StripPrefix; /** * Validates and formats postcodes in Luxembourg. @@ -16,8 +17,12 @@ */ class LUFormatter implements CountryPostcodeFormatter { + use StripPrefix; + public function format(string $postcode) : ?string { + $postcode = $this->stripPrefix($postcode, 'L'); + if (preg_match('/^[0-9]{4}$/', $postcode) !== 1) { return null; } diff --git a/src/InvalidPostcodeException.php b/src/InvalidPostcodeException.php index 629f70c..425aa09 100644 --- a/src/InvalidPostcodeException.php +++ b/src/InvalidPostcodeException.php @@ -9,4 +9,39 @@ */ class InvalidPostcodeException extends \Exception { + protected string $postcode; + protected string $country; + + /** + * Construct exception thrown when trying to format an invalid postcode. + * + * @param string $postcode + * @param string $country + */ + public function __construct(string $postcode, string $country) + { + parent::__construct('Invalid postcode: ' . $postcode); + $this->postcode = $postcode; + $this->country = $country; + } + + /** + * Get the invalid postcode associated with this exception. + * + * @return string + */ + public function getPostcode(): string + { + return $this->postcode; + } + + /** + * Get the country ISO2 code associated with this exception. + * + * @return string + */ + public function getCountry(): string + { + return $this->country; + } } diff --git a/src/PostcodeFormatter.php b/src/PostcodeFormatter.php index 1fef565..1cb69fc 100644 --- a/src/PostcodeFormatter.php +++ b/src/PostcodeFormatter.php @@ -38,17 +38,17 @@ public function format(string $country, string $postcode) : string $formatter = $this->getFormatter($country); if ($formatter === null) { - throw new UnknownCountryException('Unknown country: ' . $country); + throw new UnknownCountryException($country); } if (preg_match('/^[A-Z0-9]+$/', $postcode) !== 1) { - throw new InvalidPostcodeException('Invalid postcode: ' . $postcode); + throw new InvalidPostcodeException($postcode, $country); } $formatted = $formatter->format($postcode); if ($formatted === null) { - throw new InvalidPostcodeException('Invalid postcode: ' . $postcode); + throw new InvalidPostcodeException($postcode, $country); } return $formatted; diff --git a/src/UnknownCountryException.php b/src/UnknownCountryException.php index 47cfdd5..cd68685 100644 --- a/src/UnknownCountryException.php +++ b/src/UnknownCountryException.php @@ -9,4 +9,26 @@ */ class UnknownCountryException extends \Exception { + protected string $country; + + /** + * Construct exception thrown when an unknown country code is provided. + * + * @param string $country + */ + public function __construct(string $country) + { + parent::__construct('Unknown country: ' . $country); + $this->country = $country; + } + + /** + * Get the unknown country ISO2 code associated with this exception. + * + * @return string + */ + public function getCountry(): string + { + return $this->country; + } } diff --git a/tests/Formatter/ATFormatterTest.php b/tests/Formatter/ATFormatterTest.php index a510d1c..4a0b35d 100644 --- a/tests/Formatter/ATFormatterTest.php +++ b/tests/Formatter/ATFormatterTest.php @@ -37,6 +37,9 @@ public function providerFormat() : array ['ABCD', null], ['ABCDE', null], ['ABCDEF', null], + + ['A8084', '8084'], + ['X8084', null], ]; } } diff --git a/tests/Formatter/BEFormatterTest.php b/tests/Formatter/BEFormatterTest.php index 74510e1..7741d9a 100644 --- a/tests/Formatter/BEFormatterTest.php +++ b/tests/Formatter/BEFormatterTest.php @@ -36,6 +36,9 @@ public function providerFormat() : array ['ABCD', null], ['ABCDE', null], ['ABCDEF', null], + + ['B8084', '8084'], + ['X8084', null], ]; } } diff --git a/tests/Formatter/LUFormatterTest.php b/tests/Formatter/LUFormatterTest.php index 1137f92..5ddb54a 100644 --- a/tests/Formatter/LUFormatterTest.php +++ b/tests/Formatter/LUFormatterTest.php @@ -34,6 +34,9 @@ public function providerFormat() : array ['ABC', null], ['ABCD', null], ['ABCDE', null], + + ['L8084', '8084'], + ['X8084', null], ]; } }