Exemplo n.º 1
0
 /**
  * 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);
 }
 /**
  * checkCode
  *
  * Check Code will attempt to smartly detect the code type,
  * IE UPC, EAN or ISBN10/13.
  *
  * Since most 13 DIGIT EAN's also validate as an ISBN, the type will be ISBN
  * if the code validates as both.
  *
  * @param string $code Code to check.
  * @return Returns an array with the code type, check digit and if
  * the code is an ISBN, the ISBN parts. FALSE otherwise.
  */
 public static function checkCode($code)
 {
     $isbn = array();
     $upcValid = false;
     $upcEValid = false;
     $eanValid = false;
     $isbnValid = false;
     try {
         $upcValid = ProductValidator::checkUpcA($code);
     } catch (UpcException\UpcException $e) {
     }
     try {
         $upcEValid = ProductValidator::checkUpcE($code);
     } catch (UpcException\UpcException $e) {
     }
     try {
         $eanValid = ProductValidator::checkEan($code);
     } catch (EanException\EanException $e) {
     }
     try {
         $isbnValid = ProductValidator::checkIsbn($code, $isbn);
     } catch (IsbnException\IsbnException $e) {
     }
     $type = '';
     $checkDigit = 0;
     // UPC Code
     if ($upcValid) {
         $type = 'UPC-A';
         $checkDigit = UpcValidator\UpcValidator::getCheckDigit($code);
     } else {
         if ($upcEValid) {
             $type = 'UPC-E - ' . strlen($code);
             $code = UpcEValidator\UpcEExpander::expand($code);
             $checkDigit = UpcEValidator\UpcEValidator::getCheckDigit($code);
         } else {
             if ($eanValid) {
                 $type = 'EAN';
                 $checkDigit = EanValidator\EanValidator::getCheckDigit($code);
             } else {
                 if ($isbnValid && !$eanValid) {
                     $type = 'ISBN';
                     $checkDigit = IsbnValidator\IsbnValidator::getCheckDigit($code);
                 } else {
                     return false;
                 }
             }
         }
     }
     // Return array of values
     return array('type' => $type, 'checkDigit' => $checkDigit, 'isbn' => $isbn);
 }