-
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
is_usa_state
validation rule (#184)
This commit introduces a new validation rule to check if a value represents a valid USA state name or code.
- Loading branch information
Showing
8 changed files
with
164 additions
and
2 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
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,109 @@ | ||
<?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; | ||
|
||
final class IsUsaState extends AbstractCellRule | ||
{ | ||
public function getHelpMeta(): array | ||
{ | ||
return [ | ||
[], | ||
[ | ||
self::DEFAULT => [ | ||
'true', | ||
'Name or code of USA state name. Case-insensitive. ' . | ||
'Example: "CA" or "California".', | ||
], | ||
], | ||
]; | ||
} | ||
|
||
public function validateRule(string $cellValue): ?string | ||
{ | ||
if ($cellValue === '') { | ||
return null; | ||
} | ||
|
||
if (!self::testValue($cellValue)) { | ||
return "Value \"<c>{$cellValue}</c>\" is not a valid USA state name or code"; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static function testValue(string $cellValue): bool | ||
{ | ||
return \in_array(\strtoupper($cellValue), self::getOptions(), true) | ||
|| \in_array(\strtoupper($cellValue), \array_keys(self::getOptions()), true); | ||
} | ||
|
||
private static function getOptions(): array | ||
{ | ||
return [ | ||
'AL' => 'ALABAMA', | ||
'AK' => 'ALASKA', | ||
'AZ' => 'ARIZONA', | ||
'AR' => 'ARKANSAS', | ||
'CA' => 'CALIFORNIA', | ||
'CO' => 'COLORADO', | ||
'CT' => 'CONNECTICUT', | ||
'DE' => 'DELAWARE', | ||
'FL' => 'FLORIDA', | ||
'GA' => 'GEORGIA', | ||
'HI' => 'HAWAII', | ||
'ID' => 'IDAHO', | ||
'IL' => 'ILLINOIS', | ||
'IN' => 'INDIANA', | ||
'IA' => 'IOWA', | ||
'KS' => 'KANSAS', | ||
'KY' => 'KENTUCKY', | ||
'LA' => 'LOUISIANA', | ||
'ME' => 'MAINE', | ||
'MD' => 'MARYLAND', | ||
'MA' => 'MASSACHUSETTS', | ||
'MI' => 'MICHIGAN', | ||
'MN' => 'MINNESOTA', | ||
'MS' => 'MISSISSIPPI', | ||
'MO' => 'MISSOURI', | ||
'MT' => 'MONTANA', | ||
'NE' => 'NEBRASKA', | ||
'NV' => 'NEVADA', | ||
'NH' => 'NEW HAMPSHIRE', | ||
'NJ' => 'NEW JERSEY', | ||
'NM' => 'NEW MEXICO', | ||
'NY' => 'NEW YORK', | ||
'NC' => 'NORTH CAROLINA', | ||
'ND' => 'NORTH DAKOTA', | ||
'OH' => 'OHIO', | ||
'OK' => 'OKLAHOMA', | ||
'OR' => 'OREGON', | ||
'PA' => 'PENNSYLVANIA', | ||
'RI' => 'RHODE ISLAND', | ||
'SC' => 'SOUTH CAROLINA', | ||
'SD' => 'SOUTH DAKOTA', | ||
'TN' => 'TENNESSEE', | ||
'TX' => 'TEXAS', | ||
'UT' => 'UTAH', | ||
'VT' => 'VERMONT', | ||
'VA' => 'VIRGINIA', | ||
'WA' => 'WASHINGTON', | ||
'WV' => 'WEST VIRGINIA', | ||
'WI' => 'WISCONSIN', | ||
'WY' => 'WYOMING', | ||
]; | ||
} | ||
} |
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,47 @@ | ||
<?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\IsUsaState; | ||
use JBZoo\PHPUnit\Rules\TestAbstractCellRule; | ||
|
||
use function JBZoo\PHPUnit\isSame; | ||
|
||
final class IsUsaStateTest extends TestAbstractCellRule | ||
{ | ||
protected string $ruleClass = IsUsaState::class; | ||
|
||
public function testPositive(): void | ||
{ | ||
$rule = $this->create(true); | ||
isSame('', $rule->test('')); | ||
isSame('', $rule->test('NY')); | ||
isSame('', $rule->test('New York')); | ||
|
||
isSame('', $rule->test('ny')); | ||
isSame('', $rule->test('new york')); | ||
} | ||
|
||
public function testNegative(): void | ||
{ | ||
$rule = $this->create(true); | ||
isSame( | ||
'Value "qwe" is not a valid USA state name or code', | ||
$rule->test('qwe'), | ||
); | ||
} | ||
} |