-
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_hex_rgb_color' validation rule (#143)
A new validation rule called 'is_hex_rgb_color' has been added to the CSV Blueprint parsing tool. This update allows for RGB color codes in hexadecimal format to be validated. Changes have also been made to the README tutorial and enforcement logic for this validation rule. Tests have been created to ensure its proper functionality.
- Loading branch information
Showing
8 changed files
with
146 additions
and
11 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 |
---|---|---|
|
@@ -187,6 +187,7 @@ columns: | |
is_consonant: true | ||
is_alnum: true | ||
is_alpha: true | ||
is_hex_rgb_color: true | ||
|
||
hash: set_algo | ||
|
||
|
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,49 @@ | ||
<?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; | ||
|
||
final class IsHexRgbColor extends AbstractCellRule | ||
{ | ||
public function getHelpMeta(): array | ||
{ | ||
return [ | ||
[], | ||
[ | ||
self::DEFAULT => [ | ||
'true', | ||
'Validates weather the input is a hex RGB color or not. ' . | ||
'Examples: "#FF0000", "#123", "ffffff", "fff".', | ||
], | ||
], | ||
]; | ||
} | ||
|
||
public function validateRule(string $cellValue): ?string | ||
{ | ||
if ($cellValue === '') { | ||
return null; | ||
} | ||
|
||
if (!Validator::hexRgbColor()->validate($cellValue)) { | ||
return "Value \"<c>{$cellValue}</c>\" is not a valid hex RGB color."; | ||
} | ||
|
||
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,81 @@ | ||
<?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\IsHexRgbColor; | ||
use JBZoo\PHPUnit\Rules\TestAbstractCellRule; | ||
|
||
use function JBZoo\PHPUnit\isSame; | ||
|
||
final class IsHexRgbColorTest extends TestAbstractCellRule | ||
{ | ||
protected string $ruleClass = IsHexRgbColor::class; | ||
|
||
public function testPositive(): void | ||
{ | ||
$rule = $this->create(true); | ||
isSame(null, $rule->validate('')); | ||
|
||
$valid = [ | ||
'', | ||
'#000', | ||
'#00000F', | ||
'#00000f', | ||
'#123', | ||
'#123456', | ||
'#FFFFFF', | ||
'#ffffff', | ||
'123123', | ||
'FFFFFF', | ||
'ffffff', | ||
'443', | ||
]; | ||
|
||
foreach ($valid as $value) { | ||
isSame('', $rule->test($value), "\"{$value}\""); | ||
} | ||
|
||
$rule = $this->create(false); | ||
isSame(null, $rule->validate(' 1')); | ||
} | ||
|
||
public function testNegative(): void | ||
{ | ||
$rule = $this->create(true); | ||
|
||
$invalid = [ | ||
';', | ||
'!@#$%^&*()', | ||
'#0', | ||
'#0000G0', | ||
'#0FG', | ||
'#1234', | ||
'#AAAAAA1', | ||
'#S', | ||
'1234', | ||
'foo', | ||
]; | ||
|
||
foreach ($invalid as $value) { | ||
isSame( | ||
"Value \"{$value}\" is not a valid hex RGB color.", | ||
$rule->test($value), | ||
$value, | ||
); | ||
} | ||
} | ||
} |
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