'UPC-12', 'full_form' => $paddedTo12, ]; } // Try validating EAN-13 if (!$noEan13 && self::isValidEAN13($paddedTo13)) { return [ 'type' => 'EAN-13', 'full_form' => $paddedTo13, ]; } // Try validating ITF-14 if (!$noItf14 && self::isValidITF14($paddedTo14)) { return [ 'type' => 'ITF-14', 'full_form' => $paddedTo14, ]; } // If none match, return null return null; } /** * Validate and convert the given GTIN to EAN-13. Translate ITF-14 bulk items * to EAN-13 singular item codes. * * @param string|null $originalGtin The original GTIN to validate and convert. * @param bool $trimLeadingZeroes Trim leading 0's from result. * @param \Sfinktah\String\GTIN|null $gtinInfo Optionally return validation information from $originalGtin * @return string|null Returns the validated and formatted EAN-13, or null if invalid. */ public static function normalizeAsEan13(?string $originalGtin, bool $trimLeadingZeroes = false, ?GTIN &$gtinInfo = null) : ?string { if (!empty($originalGtin)) { // Identify and validate the GTIN $gtinInfo = self::identifyBarcodeType(ltrim($originalGtin, 0)); if (!$gtinInfo) return null; if ($gtinInfo['type'] === 'ITF-14') { $fixedGtin = self::itf14ToEan13($gtinInfo['full_form']); } else if ($gtinInfo['type'] === 'UPC-12') { // As we trim the result, this step doesn't really do anything useful, // though if you want uniform EAN-13 then leave it in. $fixedGtin = self::upc12ToEan13($gtinInfo['full_form']); } else if ($gtinInfo['type'] === 'EAN-13') { $fixedGtin = $gtinInfo['full_form']; } else { throw new \RuntimeException('Invalid GTIN type: ' . $gtinInfo['type']); } if (!self::isValidEAN13($fixedGtin)) { throw new \RuntimeException('Converted to invalid EAN13: ' . $fixedGtin); } return $trimLeadingZeroes ? ltrim($fixedGtin, 0) : $fixedGtin; } return null; } /** * Normalize and validate the given GTIN to ITF-14. Converts relevant EAN-13 or UPC-12 codes * into ITF-14 (basically just checks they're valid and padds with zeros) * * @param string|null $originalGtin The original GTIN to validate and convert. * @param bool $trimLeadingZeroes Trim leading 0's from result. * @param \Sfinktah\String\GTIN|null $gtinInfo Optionally return validation information from $originalGtin. * @return string|null Returns the validated and formatted ITF-14, or null if invalid. */ public static function normalizeAsItf14(?string $originalGtin, bool $trimLeadingZeroes = false, ?GTIN &$gtinInfo = null) : ?string { if (!empty($originalGtin)) { // Identify and validate the GTIN $gtinInfo = self::identifyBarcodeType(ltrim($originalGtin, 0)); if (!$gtinInfo) return null; if ($gtinInfo['type'] === 'ITF-14') { $fixedGtin = $gtinInfo['full_form']; } else if ($gtinInfo['type'] === 'UPC-12') { $fixedGtin = self::ean13ToItf14(self::upc12ToEan13($gtinInfo['full_form'])); } else if ($gtinInfo['type'] === 'EAN-13') { $fixedGtin = self::ean13ToItf14($gtinInfo['full_form']); } else { throw new \RuntimeException('Invalid GTIN type: ' . $gtinInfo['type']); } if (!self::isValidITF14($fixedGtin)) { throw new \RuntimeException('Converted to invalid EAN13: ' . $fixedGtin); } return $trimLeadingZeroes ? ltrim($fixedGtin, 0) : $fixedGtin; } return null; } public static function test() { // use Sfinktah\String\GTIN; // Example ITF-14 barcode $itf14 = "10855100009555"; if (GTIN::isValidITF14($itf14)) { echo "The ITF-14 barcode is valid.\n"; $ean13 = GTIN::itf14ToEan13($itf14); if ($ean13) { echo "Converted EAN-13: $ean13\n"; } else { echo "Failed to convert to EAN-13.\n"; } } else { echo "The ITF-14 barcode is invalid.\n"; } $ean13 = "4006381333931"; // Example EAN-13 barcode // Validate EAN-13 if (GTIN::isValidEAN13($ean13)) { echo "The EAN-13 barcode is valid.\n"; } else { echo "The EAN-13 barcode is invalid.\n"; } $upc12 = "012345678905"; // Example UPC-12 barcode // Validate UPC-12 if (GTIN::isValidUPC12($upc12)) { echo "The UPC-12 barcode is valid.\n"; // Convert to EAN-13 $gtin13 = GTIN::upc12ToEan13($upc12); if ($gtin13) { echo "The corresponding EAN-13 is: $gtin13\n"; } } else { echo "The UPC-12 barcode is invalid.\n"; } $unknownBarcode = "10315605012"; // Example input: 11 digits $result = GTIN::identifyBarcodeType($unknownBarcode); if ($result) { echo "The barcode is identified as {$result['type']}.\n"; echo "Full form: {$result['full_form']}\n"; } else { echo "The barcode is invalid for UPC-12, EAN-13, and ITF-14.\n"; } } }