kekos/gs1

PHP 的 GS1 解析器和生成器

v1.0.3 2016-06-30 19:14 UTC

This package is auto-updated.

Last update: 2024-09-09 22:03:43 UTC


README

编码和解码 GTIN 号码(EAN-8 和 EAN-13),支持变重和变价的产品。

安装

您可以通过 Composer 安装此包

composer require kekos/gs1

文档

区域

GS1 的每个成员国都有自己的产品规格,包括变重和变价的产品、杂志和优惠券。该库通过在 "locales" 中定义编码规则来处理这些。

目前仅实现了 Sweden 区域。

生成 GTIN

首先创建一个实体

$entity = new \Gs1\Entity\WeightProduct($sku, $weight);
$entity = new \Gs1\Entity\PriceProduct($sku, $price);
$entity = new \Gs1\Entity\Publication($sku, $price);
$entity = new \Gs1\Entity\Coupon($id, $discount);

通过指定要使用的区域来使用 GtinFactory

$code = \Gs1\GtinFactory::get('Sweden', $entity);
echo $code;

解析 GTIN

创建 GTIN 实体

$gtin = new \Gs1\Gtin\Gtin13($code);
$gtin = new \Gs1\Gtin\Gtin8($code);

通过指定要使用的区域来使用 EntityFactory

$entity = \Gs1\EntityFactory::get('Sweden', $gtin);
if ($entity instanceof \Gs1\Entity\WeightProduct) {
  echo 'Weight: ' . $entity->getWeight();
}

产品实体

$product = new \Gs1\Entity\Product($sku, $company_prefix);
$product->getSku();
$product->setSku($sku);
$product->getCompanyPrefix();
$product->setCompanyPrefix($company_prefix);

重量产品实体

$product = new \Gs1\Entity\WeightProduct($sku, $weight);
$product->getSku();
$product->setSku($sku);
$product->getWeight();
$product->setWeight($weight);

价格产品实体

$product = new \Gs1\Entity\PriceProduct($sku, $price);
$product->getSku();
$product->setSku($sku);
$product->getPrice();
$product->setPrice($price);

优惠券实体

$coupon = new \Gs1\Entity\Coupon($id, $value);
$coupon->getId();
$coupon->setId($id);
$coupon->getValue();
$coupon->setValue($value);

出版物实体

$publication = new \Gs1\Entity\Publication($sku, $price);
$publication->getSku();
$publication->setSku($sku);
$publication->getPrice();
$publication->setPrice($price);

GTIN-13 实体(EAN-13)和 GTIN-8 实体(EAN-8)

设置代码时,不需要指定校验码(第 8 位或第 13 位)。如果需要,Gtin 类将自动添加校验码。

$gtin = new \Gs1\Gtin\Gtin8($code);
// ...or
$gtin = new \Gs1\Gtin\Gtin13($code);

$gtin->getCode();
$gtin->setCode($code);
$gtin->isValid(); // true or false
$gtin->getChecksum();
$gtin->__toString(); // magic method

错误和改进

在 GitHub 问题中报告错误或随意提交拉取请求 :-)

许可证

MIT