-
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 hexadecimal check to schema rules (#125)
A new hexadecimal validation check, `is_hex`, has been added to the rules in the schema. This check validates values against hexadecimal number formats, being able to handle those with or without a '0x' prefix. As a result, the functionality of validating inputs as hexadecimal values has been added to the system's validation checks.
- Loading branch information
Showing
8 changed files
with
118 additions
and
4 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,45 @@ | ||
<?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 IsHex extends AbstractCellRule | ||
{ | ||
public function getHelpMeta(): array | ||
{ | ||
return [ | ||
[], | ||
[ | ||
self::DEFAULT => [ | ||
'true', | ||
'Both: with or without "0x" prefix. Example: "0x1A" or "1A"', | ||
], | ||
], | ||
]; | ||
} | ||
|
||
public function validateRule(string $cellValue): ?string | ||
{ | ||
if ( | ||
\preg_match('/^[0-9a-fA-F]+$/i', $cellValue) === 0 | ||
&& \preg_match('/^0x[0-9a-fA-F]+$/i', $cellValue) === 0 | ||
) { | ||
return "Value \"<c>{$cellValue}</c>\" is not a valid hexadecimal number. Example: \"0x1A\" or \"1A\""; | ||
} | ||
|
||
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,65 @@ | ||
<?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\IsHex; | ||
use JBZoo\PHPUnit\Rules\TestAbstractCellRule; | ||
|
||
use function JBZoo\PHPUnit\isSame; | ||
|
||
final class IsHexTest extends TestAbstractCellRule | ||
{ | ||
protected string $ruleClass = IsHex::class; | ||
|
||
public function testPositive(): void | ||
{ | ||
$rule = $this->create(true); | ||
isSame(null, $rule->validate('')); | ||
isSame('', $rule->test('0')); | ||
isSame('', $rule->test('1')); | ||
isSame('', $rule->test('11')); | ||
isSame('', $rule->test('1F')); | ||
isSame('', $rule->test('ff')); | ||
isSame('', $rule->test('fa')); | ||
|
||
isSame('', $rule->test('0x1')); | ||
isSame('', $rule->test('0x0')); | ||
isSame('', $rule->test('0x11')); | ||
isSame('', $rule->test('0x1F')); | ||
isSame('', $rule->test('0xff')); | ||
isSame('', $rule->test('0xfa')); | ||
|
||
$rule = $this->create(false); | ||
isSame(null, $rule->validate('1')); | ||
} | ||
|
||
public function testNegative(): void | ||
{ | ||
$rule = $this->create(true); | ||
isSame( | ||
'Value "qwerty" is not a valid hexadecimal number. Example: "0x1A" or "1A"', | ||
$rule->test('qwerty'), | ||
); | ||
|
||
$rule = $this->create(true); | ||
isSame( | ||
'"is_hex" at line <red>1</red>, column "prop". ' . | ||
'Value "<c>qwerty</c>" is not a valid hexadecimal number. Example: "0x1A" or "1A".', | ||
(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