pmigut / gtin-validator
This library is a set of static methods for GTIN validation. Methods return true if given string is publicly available GTIN-8, GTIN-12, GTIN-13 or GTIN-14 code. Checks include format, check digit and prefix.
Installs: 132 081
Dependents: 2
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.1
- ext-ctype: *
Requires (Dev)
- phpunit/phpunit: ^7.4|^9.5
This package is auto-updated.
Last update: 2025-03-12 20:30:44 UTC
README
This library is a set of static methods for GTIN validation.
The library is best suited for quick validation of many codes without instantiating new object for each code.
Simple true
or false
is returned and no additional details are provided on failure. Only trade product codes return
true
, codes with restricted or special use prefix return false
.
Supported codes
- GTIN-8 (former EAN-8)
- GTIN-12 (former UPC)
- GTIN-13 (former EAN-13)
- GTIN-14
Checks performed
- format
- check digit
- GS1 prefix
Implementation is based on GS1 General Specifications ver. 18, Jan 2018. Prefix list last updated on 12 Jan 2025.
Installation
Via Composer
$ composer require pmigut/gtin-validator
Via Git
$ git clone git@github.com:pmigut/gtin-validator.git
Examples
Basic usage
<?php use Pmigut\GtinValidator\Gtin8; var_dump(Gtin8::isValid('12312312')); // Output: false
Strict types
isValid
method expects string
argument. If strict_types=1
is declared, isValid
may throw TypeError
.
<?php declare(strict_types=1); use Pmigut\GtinValidator\Gtin8; var_dump(Gtin8::isValid(12312312)); // Output: PHP Fatal error: Uncaught TypeError: Argument 1 passed to // Pmigut\GtinValidator\Gtin8::isValid() must be of the type string, integer given
Leading zeros
Codes are validated as is, no leading zeros are added.
<?php use Pmigut\GtinValidator\Gtin13; var_dump(Gtin13::isValid('0000906332847')); // Output: true var_dump(Gtin13::isValid('906332847')); // Output: false
Testing
$ composer test
License
This library is licensed under the MIT License, see LICENSE for details.