You are here

protected function IsbnValidator::validateIsbn13 in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Constraints/IsbnValidator.php \Symfony\Component\Validator\Constraints\IsbnValidator::validateIsbn13()
1 call to IsbnValidator::validateIsbn13()
IsbnValidator::validate in vendor/symfony/validator/Constraints/IsbnValidator.php
Checks if the passed value is valid.

File

vendor/symfony/validator/Constraints/IsbnValidator.php, line 169

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;
}