/** * Validate * * Determines if the UPC value is valid, IE the check digit * matches. * * @param string $upc UPC to be inspected * @throws UpcException If the UPC value is less than 12 digits. * @return Returns TRUE if the UPC matches the UPC standard, * FALSE otherwise. */ public static function validate($upc) { $upc = trim($upc); $length = strlen($upc); // Attempt to validate a UPC-E Code if ($length > 12) { throw new UpcException\UpcException('UPC Value is more than 12 digits in length. UPC: ' . $upc . ' ( length = ' . strlen($upc) . ')', UpcException\UpcException::CODE_INVALID); } if ($length < 12) { throw new UpcException\UpcException('UPC Value is less than 12 digits in length. Try using UpcEvalidator. UPC: ' . $upc . ' ( length = ' . strlen($upc) . ')', UpcException\UpcException::CODE_INVALID); } if (!is_numeric($upc)) { throw new UpcException\UpcException('UPC Value can only contain numbers. UPC: ' . $upc, UpcException\UpcException::CODE_CONTAINS_CHARACTERS); } // If the UPC is 6, 7 or 8 digits, validate as UPC-E $checkDigit = UpcValidator::getCheckDigit($upc); return intval($upc[$length - 1]) === intval($checkDigit); }
/** * Epand 8 * * Expand a 8-digit UPC-E code to UPC-A 12 digit code. * The first digit becomes the UPC-E Check Number and then the * first and last digits are stripped. The last digit is then * re-appended as the check digit; * * @param string $upc UPC-E Code to expand * @return Returns the expanded UPC code */ private static function expand8($upc) { // Get literal first and last digits $first = substr($upc, 0, 1); $last = substr($upc, 7, 1); // Strip UPC to middle 6 digits $upc = substr($upc, 1, 6); // Create UPC from 6 digit value $upc = UpcEExpander::createUpcA($upc, $last, $last); // Get new check digit and append $checkDigit = \ProductValidator\UpcValidator\UpcValidator::getCheckDigit($upc); $upc = substr($upc, 0, 11) . $checkDigit; return $upc; }
/** * 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); }