-
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_octal' validation rule (#127)
This new rule will help in validating octal number formats like "0o123" or "0123".
- Loading branch information
Showing
7 changed files
with
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,6 +133,7 @@ columns: | |
|
||
is_bool: true | ||
is_binary: true | ||
is_octal: true | ||
is_hex: true | ||
is_uuid: true | ||
is_slug: true | ||
|
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 IsOctal extends AbstractCellRule | ||
{ | ||
public function getHelpMeta(): array | ||
{ | ||
return [ | ||
[], | ||
[ | ||
self::DEFAULT => [ | ||
'true', | ||
'Validates octal numbers in the format "0o123" or "0123".', | ||
], | ||
], | ||
]; | ||
} | ||
|
||
public function validateRule(string $cellValue): ?string | ||
{ | ||
if ( | ||
\preg_match('/^[0-7]+(_[0-7]+)*$/', $cellValue) === 0 | ||
&& \preg_match('/^0[oO]?[0-7]+(_[0-7]+)*$/', $cellValue) === 0 | ||
) { | ||
return "Value \"<c>{$cellValue}</c>\" is not a valid octal number. Examples: \"0o123\" or \"0123\""; | ||
} | ||
|
||
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,61 @@ | ||
<?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\IsOctal; | ||
use JBZoo\PHPUnit\Rules\TestAbstractCellRule; | ||
|
||
use function JBZoo\PHPUnit\isSame; | ||
|
||
final class IsOctalTest extends TestAbstractCellRule | ||
{ | ||
protected string $ruleClass = IsOctal::class; | ||
|
||
public function testPositive(): void | ||
{ | ||
$rule = $this->create(true); | ||
isSame(null, $rule->validate('')); | ||
isSame('', $rule->test('0')); | ||
isSame('', $rule->test('7')); | ||
isSame('', $rule->test('01')); | ||
|
||
isSame('', $rule->test('0o1')); | ||
isSame('', $rule->test('0o123')); | ||
|
||
isSame('', $rule->test('0O1')); | ||
isSame('', $rule->test('0O123')); | ||
|
||
$rule = $this->create(false); | ||
isSame(null, $rule->validate('1')); | ||
} | ||
|
||
public function testNegative(): void | ||
{ | ||
$rule = $this->create(true); | ||
isSame( | ||
'Value "8" is not a valid octal number. Examples: "0o123" or "0123"', | ||
$rule->test('8'), | ||
); | ||
|
||
$rule = $this->create(true); | ||
isSame( | ||
'"is_octal" at line <red>1</red>, column "prop". Value "<c>qwerty</c>" is not a valid octal number. ' . | ||
'Examples: "0o123" or "0123".', | ||
(string)$rule->validate('qwerty'), | ||
); | ||
} | ||
} |