[Validator] Fixed and simplified IsbnValidator

This commit is contained in:
Bernhard Schussek 2014-04-10 18:57:38 +02:00
parent fd58870ac0
commit 224e70f709
3 changed files with 73 additions and 43 deletions

View File

@ -42,5 +42,8 @@ class Isbn extends Constraint
if (null === $this->isbn10 && null === $this->isbn13) { if (null === $this->isbn10 && null === $this->isbn13) {
throw new MissingOptionsException(sprintf('Either option "isbn10" or "isbn13" must be given for constraint "%s".', __CLASS__), array('isbn10', 'isbn13')); throw new MissingOptionsException(sprintf('Either option "isbn10" or "isbn13" must be given for constraint "%s".', __CLASS__), array('isbn10', 'isbn13'));
} }
$this->isbn10 = (bool) $this->isbn10;
$this->isbn13 = (bool) $this->isbn13;
} }
} }

View File

@ -19,6 +19,7 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException;
* Validates whether the value is a valid ISBN-10 or ISBN-13. * Validates whether the value is a valid ISBN-10 or ISBN-13.
* *
* @author The Whole Life To Learn <thewholelifetolearn@gmail.com> * @author The Whole Life To Learn <thewholelifetolearn@gmail.com>
* @author Bernhard Schussek <bschussek@gmail.com>
* *
* @see https://en.wikipedia.org/wiki/Isbn * @see https://en.wikipedia.org/wiki/Isbn
*/ */
@ -37,53 +38,71 @@ class IsbnValidator extends ConstraintValidator
throw new UnexpectedTypeException($value, 'string'); throw new UnexpectedTypeException($value, 'string');
} }
if (!is_numeric($value)) { $value = (string) $value;
$value = str_replace('-', '', $value); $canonical = strtoupper(str_replace('-', '', $value));
if ($constraint->isbn10 && $this->isValidIsbn10($canonical)) {
return;
} }
$validation = 0; if ($constraint->isbn13 && $this->isValidIsbn13($canonical)) {
$value = strtoupper($value); return;
$valueLength = strlen($value); }
if (10 === $valueLength && null !== $constraint->isbn10) { if ($constraint->isbn10 && $constraint->isbn13) {
for ($i = 0; $i < 10; $i++) { $this->context->addViolation($constraint->bothIsbnMessage, array(
if ($value[$i] == 'X') { '{{ value }}' => $value,
$validation += 10 * intval(10 - $i); ));
} else { } elseif ($constraint->isbn10) {
$validation += intval($value[$i]) * intval(10 - $i); $this->context->addViolation($constraint->isbn10Message, array(
} '{{ value }}' => $value,
} ));
if ($validation % 11 != 0) {
if (null !== $constraint->isbn13) {
$this->context->addViolation($constraint->bothIsbnMessage);
} else {
$this->context->addViolation($constraint->isbn10Message);
}
}
} elseif (13 === $valueLength && null !== $constraint->isbn13) {
for ($i = 0; $i < 13; $i += 2) {
$validation += intval($value[$i]);
}
for ($i = 1; $i < 12; $i += 2) {
$validation += intval($value[$i]) * 3;
}
if ($validation % 10 != 0) {
if (null !== $constraint->isbn10) {
$this->context->addViolation($constraint->bothIsbnMessage);
} else {
$this->context->addViolation($constraint->isbn13Message);
}
}
} else { } else {
if (null !== $constraint->isbn10 && null !== $constraint->isbn13) { $this->context->addViolation($constraint->isbn13Message, array(
$this->context->addViolation($constraint->bothIsbnMessage); '{{ value }}' => $value,
} elseif (null !== $constraint->isbn10) { ));
$this->context->addViolation($constraint->isbn10Message);
} else {
$this->context->addViolation($constraint->isbn13Message);
}
} }
} }
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};
} else {
return false;
}
$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};
}
for ($i = 1; $i < 12; $i += 2) {
$checkSum += $isbn{$i} * 3;
}
return 0 === $checkSum % 10;
}
} }

View File

@ -58,6 +58,10 @@ class IsbnValidatorTest extends \PHPUnit_Framework_TestCase
array('0-4X19-92611'), array('0-4X19-92611'),
array('0_45122_5244'), array('0_45122_5244'),
array('2870#971#648'), array('2870#971#648'),
array('1A34567890'),
// chr(1) evaluates to 0
// 2070546810 is valid
array('2'.chr(1).'70546810'),
); );
} }
@ -92,6 +96,10 @@ class IsbnValidatorTest extends \PHPUnit_Framework_TestCase
array('980-0474292319'), array('980-0474292319'),
array('978_0451225245'), array('978_0451225245'),
array('978#0471292319'), array('978#0471292319'),
array('978-272C442282'),
// chr(1) evaluates to 0
// 978-2070546817 is valid
array('978-2'.chr(1).'70546817'),
); );
} }