/** * Parses a GTIN object to Entity * * @param Gtin $gtin GTIN to parse * @return AbstractEntity Entity object */ public static function parse(Gtin $gtin) { $code = $gtin->getCode(); if ($gtin instanceof Gtin13) { switch ($code[0]) { case 2: // Weight or price if ($code[1] >= 0 && $code[1] <= 2) { $divider = self::$price_dividers[intval($code[1])]; $price = intval(substr($code, 8, 4)); return new PriceProduct(substr($code, 2, 6), $divider == 0 ? $price : $price / $divider); } else { if ($code[1] >= 3 && $code[1] <= 5) { $divider = self::$weight_dividers[intval($code[1])]; $weight = intval(substr($code, 8, 4)); return new WeightProduct(substr($code, 2, 6), $weight / $divider); } } break; case 7: if (substr($code, 0, 4) == '7388') { // Publication return new Publication(substr($code, 4, 4), intval(substr($code, 8, 4), 10) / 10); } break; case 9: // Coupon return new Coupon(substr($code, 2, 6), intval(substr($code, 8, 4), 10) / 10); break; } return new Product(substr($code, 6, 6), substr($code, 0, 6)); } return new Product(substr($code, 6, 1), substr($code, 0, 6)); }
/** * Sets code * * @param string $code GTIN-13 code */ public function setCode($code) { $len = strlen($code); if ($len > 13) { throw new ArgumentException('Gtin13: $code can not be longer than 13 characters, ' . $len . ' given'); } if ($len < 12) { throw new ArgumentException('Gtin13: $code can not be shorter than 12 characters, ' . $len . ' given'); } parent::setCode($code); }