-
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
language_code
validation rule (#51)
Implemented a new rule for validating 'language_code' based on ISO 639 standard in the schema examples for json, php, and yml files. Added unit tests for this new rule and updated the README along with the total count of rules.
- Loading branch information
Showing
6 changed files
with
127 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?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\LanguageCode as RespectLanguageCode; | ||
use Respect\Validation\Validator; | ||
|
||
class LanguageCode extends AbstractCellRule | ||
{ | ||
protected const HELP_TOP = [ | ||
'Validates whether the input is language code based on ISO 639.', | ||
'Available options: "alpha-2" (Ex: "en"), "alpha-3" (Ex: "eng").', | ||
'See: https://en.wikipedia.org/wiki/ISO_639.', | ||
]; | ||
|
||
protected const HELP_OPTIONS = [ | ||
self::DEFAULT => ['alpha-2', 'Examples: "en", "eng"'], | ||
]; | ||
|
||
public function validateRule(string $cellValue): ?string | ||
{ | ||
if ($cellValue === '') { | ||
return null; | ||
} | ||
|
||
$validSets = [RespectLanguageCode::ALPHA2, RespectLanguageCode::ALPHA3]; | ||
|
||
$set = $this->getOptionAsString(); | ||
|
||
if (!\in_array($set, $validSets, true)) { | ||
return "Unknown language set: \"<c>{$set}</c>\". " . | ||
'Available options: [<green>' . \implode(', ', $validSets) . '</green>]'; | ||
} | ||
|
||
if (!Validator::languageCode($set)->validate($cellValue)) { | ||
return "Value \"<c>{$cellValue}</c>\" is not a valid {$set} language 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,57 @@ | ||
<?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\LanguageCode; | ||
use JBZoo\PHPUnit\Rules\AbstractCellRule; | ||
|
||
use function JBZoo\PHPUnit\isSame; | ||
|
||
final class LanguageCodeTest extends AbstractCellRule | ||
{ | ||
protected string $ruleClass = LanguageCode::class; | ||
|
||
public function testPositive(): void | ||
{ | ||
$rule = $this->create('alpha-2'); | ||
isSame('', $rule->test('')); | ||
isSame('', $rule->test('en')); | ||
isSame('', $rule->test('it')); | ||
|
||
$rule = $this->create('alpha-3'); | ||
isSame('', $rule->test('eng')); | ||
isSame('', $rule->test('ita')); | ||
} | ||
|
||
public function testNegative(): void | ||
{ | ||
$rule = $this->create('alpha-2'); | ||
isSame( | ||
'Value "qq" is not a valid alpha-2 language code.', | ||
$rule->test('qq'), | ||
); | ||
} | ||
|
||
public function testInvalidOption(): void | ||
{ | ||
$rule = $this->create('qwerty'); | ||
isSame( | ||
'Unknown language set: "qwerty". Available options: [alpha-2, alpha-3]', | ||
$rule->test('US'), | ||
); | ||
} | ||
} |