2013-01-12 13:38:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Validator\Constraints;
|
|
|
|
|
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
|
|
use Symfony\Component\Validator\ConstraintValidator;
|
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
|
|
|
|
|
|
|
/**
|
2013-04-21 12:14:35 +01:00
|
|
|
* Validates whether the value is a valid ISBN-10 or ISBN-13.
|
2013-04-20 14:37:23 +01:00
|
|
|
*
|
2013-01-12 13:38:26 +00:00
|
|
|
* @author The Whole Life To Learn <thewholelifetolearn@gmail.com>
|
2014-04-10 17:57:38 +01:00
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
2013-01-12 13:38:26 +00:00
|
|
|
*
|
2013-04-20 14:37:23 +01:00
|
|
|
* @see https://en.wikipedia.org/wiki/Isbn
|
2013-01-12 13:38:26 +00:00
|
|
|
*/
|
|
|
|
class IsbnValidator extends ConstraintValidator
|
|
|
|
{
|
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2013-01-12 13:38:26 +00:00
|
|
|
*/
|
|
|
|
public function validate($value, Constraint $constraint)
|
|
|
|
{
|
|
|
|
if (null === $value || '' === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
|
|
|
|
throw new UnexpectedTypeException($value, 'string');
|
|
|
|
}
|
|
|
|
|
2014-04-10 17:57:38 +01:00
|
|
|
$value = (string) $value;
|
|
|
|
$canonical = strtoupper(str_replace('-', '', $value));
|
2013-04-20 14:37:23 +01:00
|
|
|
|
2014-04-10 17:57:38 +01:00
|
|
|
if ($constraint->isbn10 && $this->isValidIsbn10($canonical)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-01-12 13:38:26 +00:00
|
|
|
|
2014-04-10 17:57:38 +01:00
|
|
|
if ($constraint->isbn13 && $this->isValidIsbn13($canonical)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-01-12 13:38:26 +00:00
|
|
|
|
2014-04-10 17:57:38 +01:00
|
|
|
if ($constraint->isbn10 && $constraint->isbn13) {
|
|
|
|
$this->context->addViolation($constraint->bothIsbnMessage, array(
|
2014-07-24 13:14:19 +01:00
|
|
|
'{{ value }}' => $this->formatValue($value),
|
2014-04-10 17:57:38 +01:00
|
|
|
));
|
|
|
|
} elseif ($constraint->isbn10) {
|
|
|
|
$this->context->addViolation($constraint->isbn10Message, array(
|
2014-07-24 13:14:19 +01:00
|
|
|
'{{ value }}' => $this->formatValue($value),
|
2014-04-10 17:57:38 +01:00
|
|
|
));
|
2013-01-12 13:38:26 +00:00
|
|
|
} else {
|
2014-04-10 17:57:38 +01:00
|
|
|
$this->context->addViolation($constraint->isbn13Message, array(
|
2014-07-24 13:14:19 +01:00
|
|
|
'{{ value }}' => $this->formatValue($value),
|
2014-04-10 17:57:38 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function isValidIsbn10($isbn)
|
|
|
|
{
|
|
|
|
if (10 !== strlen($isbn)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$checkSum = 0;
|
|
|
|
|
|
|
|
for ($i = 0; $i < 10; ++$i) {
|
|
|
|
if ('X' === $isbn{$i}) {
|
|
|
|
$digit = 10;
|
|
|
|
} elseif (ctype_digit($isbn{$i})) {
|
|
|
|
$digit = $isbn{$i};
|
2013-01-12 13:38:26 +00:00
|
|
|
} else {
|
2014-04-10 17:57:38 +01:00
|
|
|
return false;
|
2013-01-12 13:38:26 +00:00
|
|
|
}
|
2014-04-10 17:57:38 +01:00
|
|
|
|
|
|
|
$checkSum += $digit * intval(10 - $i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0 === $checkSum % 11;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function isValidIsbn13($isbn)
|
|
|
|
{
|
|
|
|
if (13 !== strlen($isbn) || !ctype_digit($isbn)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$checkSum = 0;
|
|
|
|
|
|
|
|
for ($i = 0; $i < 13; $i += 2) {
|
|
|
|
$checkSum += $isbn{$i};
|
2013-01-12 13:38:26 +00:00
|
|
|
}
|
2014-04-10 17:57:38 +01:00
|
|
|
|
|
|
|
for ($i = 1; $i < 12; $i += 2) {
|
2014-09-21 19:53:12 +01:00
|
|
|
$checkSum += $isbn{$i}
|
|
|
|
* 3;
|
2014-04-10 17:57:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0 === $checkSum % 10;
|
2013-01-12 13:38:26 +00:00
|
|
|
}
|
|
|
|
}
|