-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'postal_code' validation rule (#140)
Introduced a new validation rule to check the correctness of postal codes based on country codes. This update also includes the addition of PostalCode.php for functionality and PostalCodeTest.php for testing. Schema examples and README file have been updated to reflect this change.
- Loading branch information
Showing
8 changed files
with
116 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -177,6 +177,7 @@ columns: | |
phone: ALL | ||
is_iban: true | ||
is_bic: true | ||
postal_code: US | ||
|
||
is_version: true | ||
is_punct: true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - Csv-Blueprint. | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @see https://github.com/JBZoo/Csv-Blueprint | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\CsvBlueprint\Rules\Cell; | ||
|
||
use Respect\Validation\Validator; | ||
|
||
class PostalCode extends AbstractCellRule | ||
{ | ||
public function getHelpMeta(): array | ||
{ | ||
return [ | ||
[], | ||
[ | ||
self::DEFAULT => [ | ||
'US', | ||
'Validate postal code by country code (alpha-2). ' . | ||
'Example: "02179". Extracted from https://www.geonames.org', | ||
], | ||
], | ||
]; | ||
} | ||
|
||
public function validateRule(string $cellValue): ?string | ||
{ | ||
if ($cellValue === '') { | ||
return null; | ||
} | ||
|
||
$countryCode = $this->getOptionAsString(); | ||
if ($countryCode === '') { | ||
return 'Country code is not defined. Please, set it in the rule options.'; | ||
} | ||
|
||
if (!Validator::postalCode($countryCode)->validate($cellValue)) { | ||
return "Value \"<c>{$cellValue}</c>\" is not a valid postal code for country \"{$countryCode}\"."; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - Csv-Blueprint. | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @see https://github.com/JBZoo/Csv-Blueprint | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\PHPUnit\Rules\Cell; | ||
|
||
use JBZoo\CsvBlueprint\Rules\Cell\PostalCode; | ||
use JBZoo\PHPUnit\Rules\TestAbstractCellRule; | ||
|
||
use function JBZoo\PHPUnit\isSame; | ||
|
||
final class PostalCodeTest extends TestAbstractCellRule | ||
{ | ||
protected string $ruleClass = PostalCode::class; | ||
|
||
public function testPositive(): void | ||
{ | ||
$rule = $this->create('BR'); | ||
isSame('', $rule->test('')); | ||
isSame('', $rule->test('02179000')); | ||
isSame('', $rule->test('02179-000')); | ||
} | ||
|
||
public function testNegative(): void | ||
{ | ||
$rule = $this->create('BR'); | ||
isSame( | ||
'Value "qwerty" is not a valid postal code for country "BR".', | ||
$rule->test('qwerty'), | ||
); | ||
|
||
$rule = $this->create('QQ'); | ||
isSame( | ||
'"postal_code" at line <red>1</red>, column "prop". ' . | ||
'Unexpected error: Cannot validate postal code from "QQ" country.', | ||
(string)$rule->validate('qwerty'), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters