Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. IsbnValidator.php

function 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 135

Class

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

Namespace

Symfony\Component\Validator\Constraints

Code

protected function validateIsbn13(string $isbn) : string|bool {
    // 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;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal