Validate the ISBN-13 $isbn

@param string $isbn
@return boolean
@throws Exception
764738
public function isbn13($isbn)
    {
        if(is_string($isbn) === false) {
            throw new Exception('Invalid parameter type.');
        }
        
        //Verify ISBN-13 scheme
        $isbn = $this->hyphens->removeHyphens($isbn);
        if (strlen($isbn) != 13) {
            return false;
        }
        if (preg_match('/\d{13}/i',$isbn) == false) {
            return false;
        }

        //Verify checksum
        $check = 0;
        for ($i = 0; $i < 13; $i += 2) {
            $check += substr($isbn, $i, 1);
        }
        for ($i = 1; $i < 12; $i += 2) {
            $check += 3 * substr($isbn, $i, 1);
        }
        return $check % 10 === 0;
    }
1262712
public function getPublisherIdentifier() : string
    {
        if ($this->rangeInfo === null) {
            throw IsbnException::unknownGroup($this->isbn);
        }

        if ($this->rangeInfo->parts === null) {
            throw IsbnException::unknownRange($this->isbn);
        }

        return $this->rangeInfo->parts[$this->is13 ? 2 : 1];
    }
16150
def _postprocess(self):
        
        # validate series ISBN
        self._POST["P0601010__a"] = self._validate_isbn(
            self._POST["P0601010__a"],
            accept_blank=True
        )
        if self._POST["P0601010__a"] != "":
            self._POST["P0601010__b"] = "soubor : " + self._POST["P0601010__a"]

        # validate ISBN of the book
        self._POST["P0501010__a"] = self._validate_isbn(
            self._POST["P0501010__a"],
            accept_blank=False
        )
        self._POST["P1601ISB__a"] = self._POST["P0501010__a"]
1274426
private function validate()
    {
        if (!stristr(self::CHARACTER_LIST, $this->value[0])) {
            throw new \Exception('Invalid first character: ' . $this->value[0]);
        }

        if (!in_array($this->value, self::listForCharacter($this->value[0]))) {
            throw new \Exception('Word not in known list of words: ' . $this->value);
        }
    }
1035307
protected function _validateMethod($method) {
        if (in_array(strtoupper($method), $this->_validMethods)) {
            return true;
        }
        $exception = new Garp_Content_Api_Rest_Exception('Method not allowed');
        $exception->setHttpStatusCode(405);
        throw $exception;
    }
907705
public function fixIsbnNumber( $isbn_number ) {

		// Regex to split a string only by the last whitespace character
		@list( $isbn_number, $addon ) = preg_split( '/\s+(?=\S*+$)/', trim( $isbn_number ) ); // @codingStandardsIgnoreLine

		$isbn_number = ( new \Isbn\Isbn() )->hyphens->fixHyphens( $isbn_number );

		if ( $addon ) {
			$isbn_number .= " $addon";
		}

		return $isbn_number;
	}
1192960
private function validateISBN13Checksum($isbnNr, &$error)
    {
        if (!$isbnNr) {
            return false;
        }

        if (strlen($isbnNr) !== self::ISBN13_LENGTH) {
            $error = 'ISBN-13 must be 13 digit, digit count is: ' . strlen($isbnNr);

            return false;
        }

        if (substr($isbnNr, 0, self::ISBN13_PREFIX_LENGTH) !== self::ISBN13_PREFIX_978 &&
             substr($isbnNr, 0, self::ISBN13_PREFIX_LENGTH) !== self::ISBN13_PREFIX_979) {
            $error = 'ISBN-13 value must start with 978 or 979, got: ' . substr($isbnNr, 0, self::ISBN13_PREFIX_LENGTH);

            return false;
        }

        $checksum13 = 0;
        $weight13 = 1;
        //compute checksum
        $val = 0;
        for ($i = 0; $i < self::ISBN13_LENGTH; ++$i) {
            $val = $isbnNr[$i];
            if (!is_numeric($isbnNr[$i])) {
                $error = 'All ISBN-13 characters need to be numeric';

                return false;
            }
            $checksum13 = $checksum13 + $weight13 * $val;
            $weight13 = ($weight13 + 2) % 4;
        }
        if (($checksum13 % 10) !== 0) {
            // Calculate the last digit from the 12 first numbers.
            $checkDigit = (10 - (($checksum13 - (($weight13 + 2) % 4) * $val) % 10)) % 10;
            //bad checksum
            $error = 'Bad checksum, last digit of ISBN-13 should be ' . $checkDigit;

            return false;
        }

        return true;
    }
71623
public function format($format = 'EAN')
    {
        if (!$this->isValid()) {
            throw new \Exception('Cannot format invalid ISBN: '.$this->getErrors());
        }

        $this->calculateChecksum($format);

        $A = $this->getProduct();
        $B = $this->getCountry();
        $C = $this->getPublisher();
        $D = $this->getPublication();
        $E = $this->getChecksum();

        if ($format == 'ISBN-10') {
            return $B.'-'.$C.'-'.$D.'-'.$E;
        } elseif ($format == 'ISBN-13' || $format == 'ISBN') {
            return $A.'-'.$B.'-'.$C.'-'.$D.'-'.$E;
        } else {
            return $A.$B.$C.$D.$E;
        }
    }
269150
private function validateAuto(): bool
    {
        if (\preg_match($this->patterns['isbn13-basic'], $this->value)) {
            return $this->validateIsbn13();
        }
        if (\preg_match($this->patterns['isbn13-separated'], $this->value) && \strlen($this->value) == 17) {
            return $this->validateIsbn13();
        }
        if (\preg_match($this->patterns['isbn10-basic'], $this->value)) {
            return $this->validateIsbn10();
        }
        if (\preg_match($this->patterns['isbn10-separated'], $this->value) && \strlen($this->value) == 13) {
            return $this->validateIsbn10();
        }

        $this->setError(self::VALIDATOR_ERROR_ISBN_INCORRECT_FORMAT);
        return false;
    }
542682
public function convertIsbn10to13(string $isbn) : string
    {
        if ($this->cleanupBeforeValidate) {
            if (preg_match(Internal\Regexp::ASCII, $isbn) === 0) {
                throw Exception\InvalidIsbnException::forIsbn($isbn);
            }

            $isbn = preg_replace(Internal\Regexp::NON_ALNUM, '', $isbn);
        }

        $isbn = strtoupper($isbn);

        if (preg_match(Internal\Regexp::ISBN10, $isbn) === 0) {
            throw Exception\InvalidIsbnException::forIsbn($isbn);
        }

        if ($this->validateCheckDigit) {
            if (! Internal\CheckDigit::validateCheckDigit10($isbn)) {
                throw Exception\InvalidIsbnException::forIsbn($isbn);
            }
        }

        return Internal\Converter::convertIsbn10to13($isbn);
    }
358495
public function isValid()
    {
        $singleAssertion = $this->validateNumAssertions();
        if (!$singleAssertion) {
            throw new Exception('Multiple assertions are not supported');
        }

        $validTimestamps = $this->validateTimestamps();
        if (!$validTimestamps) {
            throw new Exception('Timing issues (please check your clock settings)');
        }

        $objXMLSecDSig = new XMLSecurityDSig();

        $objDSig = $objXMLSecDSig->locateSignature($this->_document);
        if (!$objDSig) {
            throw new Exception('Cannot locate Signature Node');
        }
        $objXMLSecDSig->canonicalizeSignedInfo();
        $objXMLSecDSig->idKeys = array('ID');

        $objKey = $objXMLSecDSig->locateKey();
        if (!$objKey) {
            throw new Exception('We have no idea about the key');
        }

        try {
            $objXMLSecDSig->validateReference();
        } catch (Exception $e) {
            throw new Exception('Reference Validation Failed');
        }

        XMLSecEnc::staticLocateKeyInfo($objKey, $objDSig);

        $objKey->loadKey($this->_settings->idpPublicCertificate, false, true);

        return ($objXMLSecDSig->verify($objKey) === 1);
    }
1112553
public function fixHyphens($isbn, $char = '-')
    {
        $isbn = $this->removeHyphens($isbn);
        return $this->addHyphens($isbn, $char);
    }
75953
public static function calculateCheckDigit10(string $isbn) : string
    {
        for ($sum = 0, $i = 0; $i < 9; $i++) {
            $sum += $isbn[$i] * (1 + $i);
        }

        $sum %= 11;

        return ($sum === 10) ? 'X' : (string) $sum;
    }
668190
def ISBNValidator(raw_isbn):
    
    isbn_to_check = raw_isbn.replace('-', '').replace(' ', '')

    if not isinstance(isbn_to_check, string_types):
        raise ValidationError(_(u'Invalid ISBN: Not a string'))

    if len(isbn_to_check) != 10 and len(isbn_to_check) != 13:
        raise ValidationError(_(u'Invalid ISBN: Wrong length'))
    
    if not isbn.is_valid(isbn_to_check):
        raise ValidationError(_(u'Invalid ISBN: Failed checksum'))

    if isbn_to_check != isbn_to_check.upper():
        raise ValidationError(_(u'Invalid ISBN: Only upper case allowed'))

    return True
91407
public static function checkIsbn10Number($isbn)
    {
        $isbn = trim($isbn);
        if (mb_strlen($isbn) !== 10 || preg_match('/0{10}/', $isbn)) {
            throw new MsgException("ISBN10 형식에 맞지 않습니다.");
        }

        $total = 0;
        for ($i = 0; $i < 9; $i++) {
            $digit = intval(substr($isbn, $i, 1));
            $total += ((10 - $i) * $digit);
        }

        $check_sum = (11 - ($total % 11)) % 11;
        if ($check_sum === 10) {
            $check_sum = 'X';
        }

        if ($check_sum != substr($isbn, 9)) {
            throw new MsgException("ISBN10 형식에 맞지 않습니다.");
        }
    }
632513
public static function checkIsbn13Number($isbn)
    {
        $isbn = trim($isbn);
        if (mb_strlen($isbn) !== 13 || preg_match('/0{13}/', $isbn)) {
            throw new MsgException("ISBN13 형식에 맞지 않습니다.");
        }

        if (!is_numeric($isbn)) {
            throw new MsgException('ISBN13 형식에 맞지 않습니다.');
        }

        $total = 0;

        for ($i = 0; $i < 12; $i++) {
            $digit = intval(substr($isbn, $i, 1));
            $total += ($i % 2 === 0) ? $digit : $digit * 3;
        }

        $check_sum = 10 - ($total % 10);
        if ($check_sum === 10) {
            $check_sum = 0;
        }

        if ($check_sum !== intval(substr($isbn, -1))) {
            throw new MsgException("ISBN13 형식에 맞지 않습니다.");
        }
    }
378563
public function get10()
    {
        // Is it valid?
        if ($this->isValid()) {
            // Is it already an ISBN-10?  If so, return as-is.
            if (strlen($this->raw) == 10) {
                return $this->raw;
            } elseif (strlen($this->raw) == 13
                && substr($this->raw, 0, 3) == '978'
            ) {
                // Is it a Bookland EAN?  If so, we can convert to ISBN-10.
                $start = substr($this->raw, 3, 9);
                return $start . self::getISBN10CheckDigit($start);
            }
        }

        // If we made it this far, conversion was not possible:
        return false;
    }