/** * 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 6 digits or more than 12 * @return Returns TRUE if the UPC matches the UPC-E (GTIN12) standard, * FALSE otherwise. */ public static function validate($upc) { $upc = trim($upc); if (!is_numeric($upc)) { throw new UpcException\UpcException('UPC Value can only contain numbers. UPC: ' . $upc, UpcException\UpcException::CODE_CONTAINS_CHARACTERS); } // Expand UPC $upc = UpcEExpander::expand($upc); $length = strlen($upc); if (!$upc) { return false; } $checkDigit = UpcEValidator::getCheckDigit($upc); return intval($upc[$length - 1]) === intval($checkDigit); }
/** * Get Parity * * Gets the parity pattern number (0 or 1) and the associated check digit. * * When creating an expanded UPC-A value, the parity number is prepended * to the expanded value and the check digit is appended. * * @param string $upc UPC to Match * */ private static function getParity($upc) { $upcCheckDigit = null; $upcParityDigit = null; $checkDigitParityPattern = array(0 => array('EEEOOO' => 0, 'OOOEEE' => 1), 1 => array('EEOEOO' => 0, 'OOEOEE' => 1), 2 => array('EEOOEO' => 0, 'OOEEOE' => 1), 3 => array('EEOOOE' => 0, 'OOEEEO' => 1), 4 => array('EOEEOO' => 0, 'OEOOEE' => 1), 5 => array('EOOEEO' => 0, 'OEEOOE' => 1), 6 => array('EOOOEE' => 0, 'OEEEOO' => 1), 7 => array('EOEOEO' => 0, 'OEOEOE' => 1), 8 => array('EOEOOE' => 0, 'OEOEEO' => 1), 9 => array('EOOEOE' => 0, 'OEEOEO' => 1)); // Loop through each check digit pattern // If our UPC matches either parity pattern, set the check // digit value and the parity value foreach ($checkDigitParityPattern as $checkDigit => $patterns) { foreach ($patterns as $pattern => $parity) { if (UpcEExpander::matchParityPattern($upc, $pattern)) { $upcCheckDigit = $checkDigit; $upcParityDigit = $parity; } } } return array('checkDigit' => $upcCheckDigit, 'parity' => $upcParityDigit); }
/** * 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); }
<?php // Load Product Validator // Recommended: Use Auto Loader require_once __DIR__ . '/../ProductValidator.php'; use ProductValidator\UpcEValidator; $upcE6 = '654321'; $upcE7 = '654321'; $upcE8 = '10055564'; // Expand the 6 digit code. //$code = UpcEValidator\UpcEExpander::expand($upcE6); //var_dump($code); $code = UpcEValidator\UpcEExpander::expand('049871'); var_dump($code);