janisvepris / gs1-decoder
一个用于解析PHP中GS1码的库
1.1.0
2024-02-14 15:19 UTC
Requires
- php: ^8.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.48
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10
README
GS1条形码解码器
一个简单的库,用于解码GS1条形码。有关此包支持的完整应用标识符列表,请参阅支持的标识符。
安装
此包需要PHP ^8.2
composer require janisvepris/gs1-decoder
使用方法
解码GS1条形码
<?php use JanisVepris\GS1Decoder\Decoder; use Janisvepris\Gs1Decoder\ApplicationIdentifier\Gtin; $barcode = '0112345678901234[FNC1]2140928049820384[FNC1]'; $decoder = new Decoder(); $decoded = $decoder->decode($barcode); $decoded->hasIdentifier(Gtin::CODE); $decoded->getIdentifier(Gtin::CODE)->getValue();
自定义应用标识符代码 -> 类映射
通常,解码器使用默认的标识符类映射初始化,该映射包括与此包一起提供的所有标识符。您可以用自己的覆盖它。
<?php use JanisVepris\GS1Decoder\Decoder; use Janisvepris\Gs1Decoder\ApplicationIdentifier\Gtin; use Janisvepris\Gs1Decoder\IdentifierMap; $barcode = '0112345678901234'; $decoder = new Decoder(new IdentifierMap([ '01' => Gtin::class, ])); // or $decoder = new Decoder(); $decoder->setIdentifierMap(new IdentifierMap([ '01' => Gtin::class, ])); $decoded = $decoder->decode($barcode);
定义您自己的应用标识符类
提供了多个抽象标识符类用于扩展
SimpleIdentifier
-string
值,设置长度DateIdentifier
-DateTime
值,设置长度DecimalIdentifier
-float
值,设置长度VariableLengthIdentifier
-string
值,可变长度(最小-最大)
只要它们实现 ApplicationIdentifierInterface
,您就可以定义自己的。
<?php use JanisVepris\GS1Decoder\Decoder; class MyGtinIdentifier extends SimpleIdentifier { protected $code = '01'; protected int $length = 99; protected string $englishTitle = 'My awesome title'; } // Replace the default identifier class map with your own $decoder = new Decoder(new IdentifierMap([ '01' => MyGtinIdentifier::class, ])); // or add your own identifier class to the default map $decoder = new Decoder(); $decoder->getIdentifierMap() ->addIdentifierClass('01', MyGtinIdentifier::class)
注意
此包不验证条形码。它尝试按原样解码。