-
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
country_code
validation rule (#45)
Implemented a new CountryCode rule which validates the input against the ISO 3166-1 standard. This new rule is applied in full.json, full.php, and full.yml files, and is supported by a new source code file, CountryCode.php, and its corresponding test file, CountryCodeTest.php. Composer dependencies were updated to accommodate this enhancement.
- Loading branch information
Showing
9 changed files
with
281 additions
and
17 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?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\Rules\CountryCode as RespectCountryCode; | ||
use Respect\Validation\Validator; | ||
|
||
class CountryCode extends AbstractCellRule | ||
{ | ||
protected const HELP_TOP = [ | ||
'Validates whether the input is a country code in ISO 3166-1 standard.', | ||
'Available options: "alpha-2" (Ex: "US"), "alpha-3" (Ex: "USA"), "numeric" (Ex: "840").', | ||
'The rule uses data from iso-codes: https://salsa.debian.org/iso-codes-team/iso-codes.', | ||
]; | ||
|
||
protected const HELP_OPTIONS = [ | ||
self::DEFAULT => ['alpha-2', 'Country code in ISO 3166-1 standard. Examples: "US", "USA", "840"'], | ||
]; | ||
|
||
public function validateRule(string $cellValue): ?string | ||
{ | ||
if ($cellValue === '') { | ||
return null; | ||
} | ||
|
||
$validSets = [ | ||
RespectCountryCode::ALPHA2, | ||
RespectCountryCode::ALPHA3, | ||
RespectCountryCode::NUMERIC, | ||
]; | ||
|
||
$set = $this->getOptionAsString(); | ||
|
||
if (!\in_array($set, $validSets, true)) { | ||
return "Unknown country set: \"<c>{$set}</c>\". " . | ||
'Available options: [<green>' . \implode(', ', $validSets) . '</green>]'; | ||
} | ||
|
||
if (!Validator::countryCode($set)->validate($cellValue)) { | ||
return "Value \"<c>{$cellValue}</c>\" is not a valid {$set} country code."; | ||
} | ||
|
||
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,71 @@ | ||
<?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\CountryCode; | ||
use JBZoo\PHPUnit\Rules\AbstractCellRule; | ||
use Respect\Validation\Rules\CountryCode as RespectCountryCode; | ||
|
||
use function JBZoo\PHPUnit\isSame; | ||
|
||
final class CountryCodeTest extends AbstractCellRule | ||
{ | ||
protected string $ruleClass = CountryCode::class; | ||
|
||
public function testPositive(): void | ||
{ | ||
$rule = $this->create(RespectCountryCode::ALPHA2); | ||
isSame('', $rule->test('')); | ||
isSame('', $rule->test('US')); | ||
|
||
$rule = $this->create(RespectCountryCode::ALPHA3); | ||
isSame('', $rule->test('USA')); | ||
|
||
$rule = $this->create(RespectCountryCode::NUMERIC); | ||
isSame('', $rule->test('840')); | ||
} | ||
|
||
public function testNegative(): void | ||
{ | ||
$rule = $this->create(RespectCountryCode::ALPHA2); | ||
isSame( | ||
'Value "qq" is not a valid alpha-2 country code.', | ||
$rule->test('qq'), | ||
); | ||
|
||
$rule = $this->create(RespectCountryCode::ALPHA3); | ||
isSame( | ||
'Value "QQQ" is not a valid alpha-3 country code.', | ||
$rule->test('QQQ'), | ||
); | ||
|
||
$rule = $this->create(RespectCountryCode::NUMERIC); | ||
isSame( | ||
'Value "101010101" is not a valid numeric country code.', | ||
$rule->test('101010101'), | ||
); | ||
} | ||
|
||
public function testInvalidOption(): void | ||
{ | ||
$rule = $this->create('qwerty'); | ||
isSame( | ||
'Unknown country set: "qwerty". Available options: [alpha-2, alpha-3, numeric]', | ||
$rule->test('US'), | ||
); | ||
} | ||
} |