PHP code example of janisvepris / gs1-decoder

1. Go to this page and download the library: Download janisvepris/gs1-decoder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

janisvepris / gs1-decoder example snippets




use Janisvepris\Gs1Decoder\Decoder\Decoder;
use Janisvepris\Gs1Decoder\Enum\Gs1ApplicationIdentifier;

$decoder = new Decoder();

$decoded = $decoder->decode('0109521234543213' . '3103000189' . '17250630');

$decoded->hasIdentifier(Gs1ApplicationIdentifier::Gtin);                    // true
$decoded->getIdentifier(Gs1ApplicationIdentifier::Gtin)->getValue();        // '09521234543213'
$decoded->getIdentifier(Gs1ApplicationIdentifier::NetWeightKg)->getValue(); // 0.189
$decoded->getIdentifier(Gs1ApplicationIdentifier::ExpirationDate)->getValue(); // DateTime

$code = Gs1ApplicationIdentifier::tryFrom('01');   // Gs1ApplicationIdentifier::Gtin
$code = Gs1ApplicationIdentifier::tryFrom('9999'); // null

$decoder = (new Decoder())->setDelimiter("\x1d");

$scanned = "0109521234543213" . "10ABC123" . "\x1d" . "17250630";

(new Decoder())->decode($scanned)->getIdentifierCount();                    // 2 - AI 10 ate the rest
(new Decoder())->setDelimiter("\x1d")->decode($scanned)->getIdentifierCount(); // 3 - correct

$decoded->getRawValue();        // the string that was decoded
$decoded->getIdentifierCount(); // how many identifiers were found
$decoded->getAllIdentifiers();  // ApplicationIdentifierInterface[]
$decoded->toArray();            // plain nested array, safe to json_encode

$identifier->getCode();         // Gs1ApplicationIdentifier case
$identifier->getValue();        // typed value - see below
$identifier->getRawValue();     // the characters as they appeared in the barcode
$identifier->getEnglishTitle(); // e.g. 'Net weight, KG'

$amount = $decoded->getIdentifier(Gs1ApplicationIdentifier::AmountPayableWithIsoCurrency);
$amount->getCurrencyCode(); // '978'
$amount->getValue();        // 1234.56

$itip = $decoded->getIdentifier(Gs1ApplicationIdentifier::Itip);
$itip->getGtin();           // '98410843114508'
$itip->getPieceNumber();    // 2
$itip->getTotalPieces();    // 3



use Janisvepris\Gs1Decoder\ApplicationIdentifier\Gtin;
use Janisvepris\Gs1Decoder\ApplicationIdentifier\ExpirationDate;
use Janisvepris\Gs1Decoder\Decoder\Decoder;
use Janisvepris\Gs1Decoder\Enum\Gs1ApplicationIdentifier;
use Janisvepris\Gs1Decoder\IdentifierMap\IdentifierMap;

$decoder = new Decoder(new IdentifierMap([
    Gs1ApplicationIdentifier::Gtin->value => Gtin::class,
    Gs1ApplicationIdentifier::ExpirationDate->value => ExpirationDate::class,
]));

$map = $decoder->getIdentifierMap();

$map->hasIdentifierClass(Gs1ApplicationIdentifier::Gtin);
$map->getIdentifierClass(Gs1ApplicationIdentifier::Gtin);
$map->removeIdentifierClass(Gs1ApplicationIdentifier::Gtin);
$map->addIdentifierClass(Gs1ApplicationIdentifier::Gtin, Gtin::class);



use Janisvepris\Gs1Decoder\ApplicationIdentifier\Abstract\SimpleIdentifier;
use Janisvepris\Gs1Decoder\Decoder\Decoder;
use Janisvepris\Gs1Decoder\Enum\Gs1ApplicationIdentifier;

class MyGtin extends SimpleIdentifier
{
    protected Gs1ApplicationIdentifier $code = Gs1ApplicationIdentifier::Gtin;
    protected int $length = 14;
    protected string $englishTitle = 'GTIN, our wording';
}

// addIdentifierClass() throws DuplicateIdentifierCodeException if the code is already
// mapped, so remove the shipped class before swapping yours in
$decoder = new Decoder();
$decoder->getIdentifierMap()
    ->removeIdentifierClass(Gs1ApplicationIdentifier::Gtin)
    ->addIdentifierClass(Gs1ApplicationIdentifier::Gtin, MyGtin::class);