kankro / simple-gtin-validator
Validates GTIN (Global Trade Item Number) codes by calculating checksums.
Requires
- php: >=7.4
README
Lightweight PHP library for validating GTIN (Global Trade Item Number) values and generating check digits.
GTIN is the umbrella format behind common barcode identifiers such as GTIN-8, UPC, EAN, and GTIN-14. This package normalizes the input, applies prefix filtering, and verifies the checksum used by the GTIN standard.
Contents
- Requirements
- Installation
- What the library does
- Quick start
- Working flow
- API reference
- Usage examples
- Prefix filtering
- Development workflow
- Limitations
- License
- Credits
Requirements
- PHP 7.4 or later
- Composer for installation and autoloading
Installation
Install the package with Composer:
composer require kankro/simple-gtin-validator
Then load Composer's autoloader in your project:
require __DIR__ . '/vendor/autoload.php';
What the library does
The GtinValidator class provides two public operations:
isValidGtin($code)validates a GTIN-style valueaddCheckDigit($code)appends a GTIN check digit to an incomplete code
Supported GTIN lengths for validation:
- GTIN-8
- GTIN-12
- GTIN-13
- GTIN-14
Accepted input styles:
- Numeric strings such as
'036000291452' - Strings containing hyphens such as
'978-0-552-13326-5' - Numeric values such as
884571375091
Inputs are normalized before validation: numeric values are cast to strings, and hyphens are removed from string inputs.
Quick start
<?php require __DIR__ . '/vendor/autoload.php'; use Kankro\SimpleGtinValidator\GtinValidator; $validator = new GtinValidator(); var_dump($validator->isValidGtin('884571375091')); // true var_dump($validator->isValidGtin('9780552133265')); // false var_dump($validator->addCheckDigit('1234567890123')); // "12345678901231"
Working flow
Validation flow
When you call isValidGtin($code), the validator works in this order:
- Normalize the input
- Numeric values are converted to strings
- String inputs are trimmed and hyphens are removed
- Check the prefix
- The validator rejects codes that begin with configured restricted prefixes
- Validate format
- The code must be numeric
- The length must be 8, 12, 13, or 14 digits
- Verify the check digit
- The value is left-padded to 14 digits
- The GTIN checksum is calculated and compared with the last digit
Check digit generation flow
When you call addCheckDigit($code), the validator:
- Normalizes the input
- Left-pads the value to 13 digits
- Calculates the GTIN checksum
- Returns the normalized value with the new check digit appended
This means short inputs are padded with leading zeroes before the check digit is generated.
API reference
isValidGtin($code): bool
Validates a GTIN code after normalization, prefix checks, and checksum verification.
Parameters
$code: string or numeric scalar input (other types are not supported)
Returns
truewhen the code passes prefix, length, numeric, and checksum validationfalsewhen the code is not acceptable after normalization, prefix filtering, and checksum verification
Throws
- An exception or fatal error if
$codeis neither a string nor a numeric value (this occurs during input normalization)
addCheckDigit($code): string
Generates and appends a GTIN check digit.
Parameters
$code: string or numeric input representing a code without its final check digit
Returns
- A string with the computed check digit appended
- The input is padded on the left to 13 digits before the result is produced
Usage examples
Validate a standard GTIN
use Kankro\SimpleGtinValidator\GtinValidator; $validator = new GtinValidator(); var_dump($validator->isValidGtin('036000291452')); // true var_dump($validator->isValidGtin('042100005264')); // true
Validate input with hyphens
var_dump($validator->isValidGtin('978-0-552-13326-5')); // false
Hyphens are removed before validation, so this behaves the same as validating 9780552133265.
Understand a rejected code
var_dump($validator->isValidGtin('9780552133265')); // false
This value is mathematically well-formed as an ISBN-style identifier, but the default prefix filter rejects 978 and 979.
Generate a check digit
var_dump($validator->addCheckDigit('1234567890123')); // "12345678901231" var_dump($validator->addCheckDigit('1234567')); // "00000012345670"
Prefix filtering
The validator includes a public static prefix list that is checked before checksum validation:
\Kankro\SimpleGtinValidator\GtinValidator::$gtinPrefixList;
By default, the list excludes these prefixes:
200-299951952960-969977978-979980981-98499
This is important because some values may have a valid checksum but still return false due to business rules in the prefix list.
If your use case needs different behavior, you can adjust the static list before validating:
use Kankro\SimpleGtinValidator\GtinValidator; GtinValidator::$gtinPrefixList = [ [200, 299], 951, 952, ];
Development workflow
This repository is intentionally minimal and currently does not include a dedicated automated test suite.
Useful local checks:
composer validate --no-check-publish php -l src/GtinValidator.php
Typical workflow for local changes:
- Install dependencies with Composer if needed
- Update the source or documentation
- Run
composer validate --no-check-publish - Run
php -l src/GtinValidator.php - Manually verify usage with a small PHP script or
php -r
Limitations
- Input should be a string or numeric value
- Validation behavior depends on the configured prefix list, not only on checksum correctness
addCheckDigit()left-pads inputs shorter than 13 digits before appending the final digit; inputs longer than 13 digits are not truncated, so the returned value can exceed 14 digits- The package is focused on simple validation logic and does not provide barcode parsing or formatting helpers
License
Apache-2.0
Credits
- Original concept adapted from the Python version by charithe: https://github.com/charithe/gtin-validator
- GTIN and prefix reference material: