Skip to content

Commit

Permalink
Add checks against invalid postcodes mentioned in fzaninotto/Faker#1443
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Hall committed Apr 16, 2018
1 parent bf3e41c commit fae337d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Utils/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/BasicUsageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit fae337d

Please sign in to comment.