Create "Isbn" rule

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Moritz 2018-10-02 17:23:35 +02:00 committed by Henrique Moody
parent 4106589e7f
commit 03ea1b75f6
No known key found for this signature in database
GPG key ID: 221E9281655813A6
7 changed files with 214 additions and 0 deletions

View file

@ -190,6 +190,7 @@
- [HexRgbColor](rules/HexRgbColor.md)
- [Imei](rules/Imei.md)
- [Ip](rules/Ip.md)
- [Isbn](rules/Isbn.md)
- [Json](rules/Json.md)
- [Luhn](rules/Luhn.md)
- [MacAddress](rules/MacAddress.md)
@ -279,6 +280,7 @@
- [IntType](rules/IntType.md)
- [IntVal](rules/IntVal.md)
- [Ip](rules/Ip.md)
- [Isbn](rules/Isbn.md)
- [IterableType](rules/IterableType.md)
- [Json](rules/Json.md)
- [Key](rules/Key.md)

26
docs/rules/Isbn.md Normal file
View file

@ -0,0 +1,26 @@
# Isbn
- `v::isbn()`
Validates whether the input is a valid [ISBN][] or not.
```php
v::isbn()->validate('ISBN-13: 978-0-596-52068-7'); // true
v::isbn()->validate('978 0 596 52068 7'); // true
v::isbn()->validate('ISBN-12: 978-0-596-52068-7'); // false
v::isbn()->validate('978 10 596 52068 7'); // false
```
## Changelog
Version | Description
--------|-------------
2.0.0 | Created
***
See also:
- [Imei](Imei.md)
- [Luhn](Luhn.md)
[ISBN]: https://www.isbn-international.org/content/what-isbn "International Standard Book Number"

View file

@ -0,0 +1,33 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Respect\Validation\Exceptions;
/**
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Moritz Fromm <moritzgitfromm@gmail.com>
*/
final class IsbnException extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a ISBN',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be a ISBN',
],
];
}

49
library/Rules/Isbn.php Normal file
View file

@ -0,0 +1,49 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use function implode;
use function is_scalar;
use function preg_match;
use function sprintf;
/**
* Validates whether the input is a valid ISBN (International Standard Book Number) or not.
*
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Moritz Fromm <moritzgitfromm@gmail.com>
*/
final class Isbn extends AbstractRule
{
/**
* @see https://howtodoinjava.com/regex/java-regex-validate-international-standard-book-number-isbns
*/
private const PIECES = [
'^(?:ISBN(?:-1[03])?:? )?(?=[0-9X]{10}$|(?=(?:[0-9]+[- ]){3})',
'[- 0-9X]{13}$|97[89][0-9]{10}$|(?=(?:[0-9]+[- ]){4})[- 0-9]{17}$)',
'(?:97[89][- ]?)?[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X]$',
];
/**
* {@inheritdoc}
*/
public function validate($input): bool
{
if (!is_scalar($input)) {
return false;
}
return preg_match(sprintf('/%s/', implode(self::PIECES)), $input) > 0;
}
}

View file

@ -86,6 +86,7 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
* @method static Validator intVal()
* @method static Validator intType()
* @method static Validator ip($ipOptions = null)
* @method static Validator isbn()
* @method static Validator iterableType()
* @method static Validator json()
* @method static Validator key(string $reference, Validatable $referenceValidator = null, bool $mandatory = true)

View file

@ -0,0 +1,37 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\IsbnException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::isbn()->check('ISBN-12: 978-0-596-52068-7');
} catch (IsbnException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::isbn())->check('ISBN-13: 978-0-596-52068-7');
} catch (IsbnException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::isbn()->assert('978 10 596 52068 7');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::isbn())->assert('978 0 596 52068 7');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECT--
"ISBN-12: 978-0-596-52068-7" must be a ISBN
"ISBN-13: 978-0-596-52068-7" must not be a ISBN
- "978 10 596 52068 7" must be a ISBN
- "978 0 596 52068 7" must not be a ISBN

View file

@ -0,0 +1,66 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Test\RuleTestCase;
/**
* @group rule
*
* @covers \Respect\Validation\Rules\Isbn
*
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Moritz Fromm <moritzgitfromm@gmail.com>
*/
final class IsbnTest extends RuleTestCase
{
/**
* {@inheritdoc}
*/
public function providerForValidInput(): array
{
$sut = new Isbn();
return [
[$sut, 'ISBN-13: 978-0-596-52068-7'],
[$sut, '978 0 596 52068 7'],
[$sut, '9780596520687'],
[$sut, '0-596-52068-9'],
[$sut, '0 512 52068 9'],
[$sut, 'ISBN-10 0-596-52068-9'],
[$sut, 'ISBN-10: 0-596-52068-9'],
];
}
/**
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
$sut = new Isbn();
return [
[$sut, 'ISBN 11978-0-596-52068-7'],
[$sut, 'ISBN-12: 978-0-596-52068-7'],
[$sut, '978 10 596 52068 7'],
[$sut, '119780596520687'],
[$sut, '0-5961-52068-9'],
[$sut, '11 5122 52068 9'],
[$sut, 'ISBN-11 0-596-52068-9'],
[$sut, 'ISBN-10- 0-596-52068-9'],
[$sut, 'Defiatly no ISBN'],
[$sut, 'Neither ISBN-13: 978-0-596-52068-7'],
];
}
}