-
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_isbn' validation rule (#142)
Introduced a new cell validation rule named `is_isbn` to validate if a value is a valid International Standard Book Number (ISBN). The rule has been added to the schema-examples and implemented in a new class file. The README.md file and relevant test files have been updated to reflect this change.
- Loading branch information
Showing
8 changed files
with
137 additions
and
5 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 |
---|---|---|
|
@@ -179,6 +179,7 @@ columns: | |
is_bic: true | ||
postal_code: US | ||
is_imei: true | ||
is_isbn: true | ||
|
||
is_version: true | ||
is_punct: 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,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 IsIsbn extends AbstractCellRule | ||
{ | ||
public function getHelpMeta(): array | ||
{ | ||
return [ | ||
[], | ||
[ | ||
self::DEFAULT => [ | ||
'true', | ||
'Validates an International Standard Book Number (ISBN). ' . | ||
'See: https://www.isbn-international.org/content/what-isbn', | ||
], | ||
], | ||
]; | ||
} | ||
|
||
public function validateRule(string $cellValue): ?string | ||
{ | ||
if ($cellValue === '') { | ||
return null; | ||
} | ||
|
||
if (!Validator::isbn()->validate($cellValue)) { | ||
return "Value \"<c>{$cellValue}</c>\" is not a valid ISBN number."; | ||
} | ||
|
||
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,79 @@ | ||
<?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\IsIsbn; | ||
use JBZoo\PHPUnit\Rules\TestAbstractCellRule; | ||
|
||
use function JBZoo\PHPUnit\isSame; | ||
|
||
final class IsIsbnTest extends TestAbstractCellRule | ||
{ | ||
protected string $ruleClass = IsIsbn::class; | ||
|
||
public function testPositive(): void | ||
{ | ||
$rule = $this->create(true); | ||
isSame(null, $rule->validate('')); | ||
|
||
$valid = [ | ||
'', | ||
'ISBN-13: 978-0-596-52068-7', | ||
'978 0 596 52068 7', | ||
'9780596520687', | ||
'0-596-52068-9', | ||
'0 512 52068 9', | ||
'ISBN-10 0-596-52068-9', | ||
'ISBN-10: 0-596-52068-9', | ||
]; | ||
|
||
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 = [ | ||
';', | ||
'!@#$%^&*()', | ||
'ISBN 11978-0-596-52068-7', | ||
'ISBN-12: 978-0-596-52068-7', | ||
'978 10 596 52068 7', | ||
'119780596520687', | ||
'0-5961-52068-9', | ||
'11 5122 52068 9', | ||
'ISBN-11 0-596-52068-9', | ||
'ISBN-10- 0-596-52068-9', | ||
'Defiatly no ISBN', | ||
'Neither ISBN-13: 978-0-596-52068-7', | ||
]; | ||
|
||
foreach ($invalid as $value) { | ||
isSame( | ||
"Value \"{$value}\" is not a valid ISBN number.", | ||
$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