/** * Validate * * Determines if the EAN value is valid, IE the check digit * matches. * * @param string $ean EAN to be inspected * @throws UpcException If the EAN value is less than 12 digits. 12 * Digit values will be padded to 13. * @return Returns TRUE if the UPC matches the UPC standard, * FALSE otherwise. */ public static function validate($ean) { $ean = trim($ean); $length = strlen($ean); if ($length < 13) { throw new EanException\EanException('EAN is less than 13 digits in length. EAN: ' . $ean . ' ( length = ' . strlen($ean) . ')', EanException\EanException::CODE_INVALID); } else { if ($length > 13) { throw new EanException\EanException('EAN is more than 13 digits in length. EAN: ' . $ean . ' ( length = ' . strlen($ean) . ')', EanException\EanException::CODE_INVALID); } } if (!is_numeric($ean)) { throw new EanException\EanException('EAN can only contain numbers. EAN: ' . $ean, EanException\EanException::CODE_CONTAINS_CHARACTERS); } $originalCheck = substr($ean, -1); $checkDigit = EanValidator::getCheckDigit($ean); return intval($checkDigit) === intval($originalCheck); }
/** * Check EAN Code * * @param string $code EAN Code to validate * @return Returns TRUE if the EAN validates, otherwise FALSE */ public static function checkEan($code) { return EanValidator\EanValidator::validate($code); }