You are here

protected function IsbnValidator::validateIsbn13 in Plug 7

1 call to IsbnValidator::validateIsbn13()
IsbnValidator::validate in lib/Symfony/validator/Symfony/Component/Validator/Constraints/IsbnValidator.php
Checks if the passed value is valid.

File

lib/Symfony/validator/Symfony/Component/Validator/Constraints/IsbnValidator.php, line 145

Class

IsbnValidator
Validates whether the value is a valid ISBN-10 or ISBN-13

Namespace

Symfony\Component\Validator\Constraints

Code

protected function validateIsbn13($isbn) {

  // Error priority:
  // 1. ERROR_INVALID_CHARACTERS
  // 2. ERROR_TOO_SHORT/ERROR_TOO_LONG
  // 3. ERROR_CHECKSUM_FAILED
  if (!ctype_digit($isbn)) {
    return Isbn::INVALID_CHARACTERS_ERROR;
  }
  $length = strlen($isbn);
  if ($length < 13) {
    return Isbn::TOO_SHORT_ERROR;
  }
  if ($length > 13) {
    return Isbn::TOO_LONG_ERROR;
  }
  $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 ? true : Isbn::CHECKSUM_FAILED_ERROR;
}