定制 janisvepris/gs1-decoder 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

janisvepris/gs1-decoder

最新稳定版本:1.1.2

Composer 安装命令:

composer require janisvepris/gs1-decoder

包简介

A library for parsing GS1 codes in PHP

README 文档

README

GitHub Actions Workflow Status GitHub Tag codecov

GS1 Barcode decoder

A simple library that decodes GS1 barcodes. For a full list of supported application identifiers that ship with this package, see supported identifiers.

Instalation

This package requires PHP ^8.2

composer require janisvepris/gs1-decoder

Usage

Decode GS1 barcode

<?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();

Custom application identifier code -> class map

Normally, the decoder gets initialized with a default identifier class map which includes all the identifiers that ship with this package. You can override it with your own.

<?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);

Define your own application identifier class

Multiple abstract identifier classes are available for extension:

  • SimpleIdentifier - string value, set length
  • DateIdentifier - DateTime value, set length
  • DecimalIdentifier - float value, set length
  • VariableLengthIdentifier - string value, variable length (min-max)

You can define your own as long as they implement 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)

Note

This package does not validate the barcode. It tries to decode it as is.

统计信息

  • 总下载量: 7.97k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-28