<?php if (isset($_GET['code'])) { ?> <h2>Result</h2> <p> <?php try { $len = strlen($_GET['code']); if ($len === 8) { $gtin = new Gtin8($_GET['code']); } else { if ($len === 13) { $gtin = new Gtin13($_GET['code']); } else { throw new ArgumentException('GTIN must be 8 or 13 characters'); } } if (!$gtin->isValid()) { throw new ArgumentException('GTIN checksum not valid'); } $entity = EntityFactory::get('Sweden', $gtin); if ($entity instanceof WeightProduct) { echo 'SKU: ' . $entity->getSku() . '<br />'; echo 'Weight: ' . $entity->getWeight() . '<br />'; } else { if ($entity instanceof PriceProduct) { echo 'SKU: ' . $entity->getSku() . '<br />'; echo 'Price: ' . $entity->getPrice() . '<br />';
/** * Generates a GTIN object from given Entity * * @param AbstractEntity $entity Entity to generate GTIN for * @return Gtin GTIN entity object */ public static function generate(AbstractEntity $entity) { if ($entity instanceof Product) { try { $gtin = new Gtin8(); $gtin->setPart(6, $entity->getCompanyPrefix()); $gtin->setPart(1, $entity->getSku()); } catch (ArgumentException $ex) { $gtin = new Gtin13(); $gtin->setPart(9, $entity->getCompanyPrefix()); $gtin->setPart(3, $entity->getSku()); } } else { if ($entity instanceof WeightProduct) { $gtin = new Gtin13(); $weight = self::toFloat($entity->getWeight()); $decimals = self::getDecimalsCount($weight); if ($decimals === 0) { $decimals = 3; $weight .= '.000'; } else { $decimals = max(1, min(3, $decimals)); } $modulator = self::$weight_modulators[$decimals]; $weight = substr(str_replace('.', '', $weight), 0, 4); $gtin->setPart(2, '2' . $modulator); $gtin->setPart(6, $entity->getSku()); $gtin->setPart(4, $weight); } else { if ($entity instanceof PriceProduct) { $gtin = new Gtin13(); $price = self::toFloat($entity->getPrice()); $decimals = self::getDecimalsCount($price); $decimals = max(0, min(2, $decimals)); $modulator = self::$price_modulators[$decimals]; $price = substr(str_replace('.', '', $price), 0, 4); $gtin->setPart(2, '2' . $modulator); $gtin->setPart(6, $entity->getSku()); $gtin->setPart(4, $price); } else { if ($entity instanceof Publication) { $gtin = new Gtin13(); $price = self::toFloat($entity->getPrice()); $decimals = self::getDecimalsCount($price); if ($decimals == 0) { $price = substr($price, 0, 3) . '0'; } else { $price = substr(str_replace('.', '', $price), 0, 4); } $gtin->setPart(4, '7388'); $gtin->setPart(4, $entity->getSku()); $gtin->setPart(4, $price); } else { if ($entity instanceof Coupon) { $gtin = new Gtin13(); $value = self::toFloat($entity->getValue()); $decimals = self::getDecimalsCount($value); if ($decimals == 0) { $value = substr($value, 0, 3) . '0'; } else { $value = substr(str_replace('.', '', $value), 0, 4); } $gtin->setPart(2, '99'); $gtin->setPart(6, $entity->getId()); $gtin->setPart(4, $value); } } } } } return $gtin; }