diff --git a/src/Utils/Validator.php b/src/Utils/Validator.php index db7a1d0..11687a4 100644 --- a/src/Utils/Validator.php +++ b/src/Utils/Validator.php @@ -4,10 +4,20 @@ abstract class Validator { + public static $invalidPostcodes = [ + 'KT185DN', + 'AB154YR', + 'B628RS', + ]; + public static function validatePostcode($postcode) { $postcode = strtoupper($postcode); + if (in_array(str_replace(' ', '', $postcode), self::$invalidPostcodes)) { + return false; + } + $regex = '#^(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW]) ?[0-9][ABD-HJLNP-UW-Z]{2})$#'; $result = preg_match($regex, $postcode); diff --git a/tests/Unit/BasicUsageTest.php b/tests/Unit/BasicUsageTest.php index 80f9e22..a7bf7fe 100644 --- a/tests/Unit/BasicUsageTest.php +++ b/tests/Unit/BasicUsageTest.php @@ -19,7 +19,7 @@ public function testValidation() public function testValidationFailure() { - $postcodes = ['ST163DPA', 'XF2P90', 'Ollie', 'cake', 'ST16 3DPA']; + $postcodes = ['ST163DPA', 'XF2P90', 'Ollie', 'cake', 'ST16 3DPA', 'KT18 5DN', 'AB15 4YR', 'B62 8RS']; foreach ($postcodes as $postcode) { $this->assertFalse(Validator::validatePostcode($postcode));