[Validator] Fixed and simplified IsbnValidator
This commit is contained in:
parent
fd58870ac0
commit
224e70f709
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
));
|
||||||
|
} elseif ($constraint->isbn10) {
|
||||||
|
$this->context->addViolation($constraint->isbn10Message, array(
|
||||||
|
'{{ value }}' => $value,
|
||||||
|
));
|
||||||
} else {
|
} else {
|
||||||
$validation += intval($value[$i]) * intval(10 - $i);
|
$this->context->addViolation($constraint->isbn13Message, array(
|
||||||
|
'{{ value }}' => $value,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($validation % 11 != 0) {
|
private function isValidIsbn10($isbn)
|
||||||
if (null !== $constraint->isbn13) {
|
{
|
||||||
$this->context->addViolation($constraint->bothIsbnMessage);
|
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 {
|
} else {
|
||||||
$this->context->addViolation($constraint->isbn10Message);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$checkSum += $digit * intval(10 - $i);
|
||||||
}
|
}
|
||||||
} elseif (13 === $valueLength && null !== $constraint->isbn13) {
|
|
||||||
|
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) {
|
for ($i = 0; $i < 13; $i += 2) {
|
||||||
$validation += intval($value[$i]);
|
$checkSum += $isbn{$i};
|
||||||
}
|
|
||||||
for ($i = 1; $i < 12; $i += 2) {
|
|
||||||
$validation += intval($value[$i]) * 3;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($validation % 10 != 0) {
|
for ($i = 1; $i < 12; $i += 2) {
|
||||||
if (null !== $constraint->isbn10) {
|
$checkSum += $isbn{$i} * 3;
|
||||||
$this->context->addViolation($constraint->bothIsbnMessage);
|
|
||||||
} else {
|
|
||||||
$this->context->addViolation($constraint->isbn13Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (null !== $constraint->isbn10 && null !== $constraint->isbn13) {
|
|
||||||
$this->context->addViolation($constraint->bothIsbnMessage);
|
|
||||||
} elseif (null !== $constraint->isbn10) {
|
|
||||||
$this->context->addViolation($constraint->isbn10Message);
|
|
||||||
} else {
|
|
||||||
$this->context->addViolation($constraint->isbn13Message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0 === $checkSum % 10;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user