Download the PHP package janisvepris/gs1-decoder without Composer

On this page you can find all versions of the php package janisvepris/gs1-decoder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package gs1-decoder

GitHub Actions Workflow Status GitHub Tag codecov

GS1 Barcode decoder

A zero-dependency PHP library that decodes GS1 barcode strings into typed application identifier objects.

Every application identifier in the GS1 General Specifications is supported — see supported identifiers for the full list, which is generated from the code.

Installation

This package requires PHP ^8.2 and has no runtime dependencies.

Usage

Application identifier codes are cases of the Gs1ApplicationIdentifier enum rather than strings, so a typo is a compile-time problem rather than a silent null. If you are handed a code as a string, tryFrom() turns it into a case, or null when the package does not know it:

Set the delimiter to match your input

Variable length identifiers are terminated by an FNC1 separator. The default delimiter is the literal string [FNC1], which is not what a barcode scanner produces — scanners emit FNC1 as the ASCII group separator, 0x1D. If you are decoding scanner or raw symbol data, set the delimiter first:

Getting this wrong does not raise an error, it silently mis-decodes: the variable length field runs on past its real end and swallows the identifiers that follow it.

Reading the result

decode() returns a Barcode:

Every identifier exposes:

A repeated identifier code overwrites the earlier one, so a Barcode holds at most one identifier per code.

Typed values

getValue() is typed per identifier, and some shapes expose extra accessors for the parts of a compound field:

Identifiers getValue() Also available
Most identifiers string
Measures and amounts (31nn–36nn, 390, 392, 394, 395) float, already shifted by the decimal point position getDecimalPosition()
Dates (11–17, 4326, 7006) DateTime at the end of the day
Date and time (4324, 4325, 7003, 7250, 7251, 8008) DateTime as given
Harvest date (7007) DateTime opening the range at midnight getEndDate(): ?DateTime
Test by date (7011) DateTime hasTime(): bool
Amount with currency (391, 393) float amount getCurrencyCode()
Postal code / processor with country (421, 7030–7039) string without the country code getCountryCode()
Certification reference (7230–7239) string without the scheme getSchemeId()
GRAI (8003) string serial, empty when absent getAssetType()
ITIP (8006, 8026) string, the whole 18 digit field getGtin(), getPieceNumber(), getTotalPieces()
Temperature (4330–4333) float, negative when the field is signed isNegative()

What the decoder does not do

It does not validate. There is no check digit verification, no character set enforcement, and no check that an identifier is allowed to appear where it does. The decoder reads what it can and hands it back.

It does not throw for bad input. Decoding always returns a Barcode, possibly a partial one:

If you need to know that a barcode was fully understood, compare getIdentifierCount() against what you expect, or check for the identifiers you require.

Undelimited variable length fields will over-consume. This is inherent to the format rather than a limitation of the decoder: a variable length identifier reads until the delimiter or its maximum length, so if it is neither last in the barcode nor followed by an FNC1, it will take the following identifier's characters with it. Setting the right delimiter is what avoids this.

Customising

Restrict or replace the identifier map

The decoder is built with a map of every identifier that ships with the package. Passing your own replaces it entirely, which is a cheap way to decode only the identifiers you care about:

The map is keyed by ->value rather than by the case itself, because PHP arrays cannot be keyed by an enum instance. Its methods take the case:

Define your own identifier class

Extend the abstract that matches the field's shape, assign the enum case, and declare the lengths:

The abstracts in ApplicationIdentifier\Abstract\ cover the shapes the specification uses:

Abstract Field Value
SimpleIdentifier fixed length string
VariableLengthIdentifier min to max length string
DecimalIdentifier fixed length, decimal point position float
VariableLengthDecimalIdentifier variable length, decimal point position float
CurrencyAmountIdentifier ISO 4217 currency code plus amount float
DateIdentifier YYMMDD DateTime
DateTimeIdentifier YYMMDDHHMM DateTime
LongDateIdentifier YYYYMMDD or YYYYMMDDHHMM DateTime
OptionalTimeDateIdentifier YYMMDD with optional HHMM DateTime
DateRangeIdentifier start date with optional end date DateTime
ProductionDateTimeIdentifier YYMMDDHH with optional minutes and seconds DateTime
PrefixedIdentifier leading characters that identify rather than describe string
TemperatureIdentifier six digits with an optional sign float
ItipIdentifier GTIN plus piece number plus total pieces string

PrefixedIdentifier is the base for CountryPrefixedIdentifier, CertificationReferenceIdentifier and AssetTypePrefixedIdentifier; extend it directly if you need a different prefix length.

Anything implementing ApplicationIdentifierInterface works, and implementing DecimalIdentifierInterface or VariableLengthIdentifierInterface is what makes the decoder consume a decimal point position or read up to a delimiter — the decoder branches on the contracts in ApplicationIdentifier\Contract\, not on the abstracts.

Because a code is an enum case rather than an arbitrary string, an identifier class can only carry a code the enum already lists. Overriding a shipped identifier works, but decoding a code this package does not know needs a case added to Gs1ApplicationIdentifier first — a map entry on its own is never reached.

Development

docs/SupportedIdentifiers.md is generated; run composer generate-ai-list after adding an identifier rather than editing it by hand.

Changelog

Notable changes are recorded in CHANGELOG.md.

License

MIT. See LICENSE.


All versions of gs1-decoder with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package janisvepris/gs1-decoder contains the following files

Loading the files please wait ...