__construct()
__construct() : \BarcodeValidator
Constructor.
A PHP class for validating EAN, IMEI, ISBN, GTIN, SSCC, GSIN, UPC and other similar codes.
__construct() : \BarcodeValidator
Constructor.
IsValidISBN(string $code = '') : boolean
Determine whether an ISBN code is valid (either ISBN 10 or 13 digits). It also validates whether the code has the right length.
For ISBN-10 each of the first nine digits of the ten-digit ISBN (excluding the check digit itself) is multiplied by a number in a sequence from 10 to 2 and the remainder of the sum with (respect to 11) is computed. The resulting remainder, plus the check digit, must equal a multiple of 11 (either 0 or 11). For ISBN-13 the check-digit is calculated using the BarcodeValidator::validateEANCheckDigit() function, making it compatible with EAN codes.
| string | $code | the code to validate |
IsValidEAN8(string $code = '') : boolean
Determine whether a EAN-8 (GTIN-8) code is valid
EAN 8 is the short form of EAN-13. This code is only used if the article is too small for an EAN-13 code.
EAN-8 codes are common throughout the world, and companies may also use them to encode RCN-8s (8-digit Restricted Circulation Numbers) used to identify own-brand products sold only in their stores. These are formatted as 02xx xxxx, 04xx xxxx or 2xxx xxxx.
An EAN-8 always has 8 digits:
| string | $code | the code to validate |
IsValidEAN13(string $code = '') : boolean
Determine whether a EAN-13 (GTIN-13) code is valid
The EAN barcode is primarily used in supermarkets to identify product at the point of sales. The products contain the EAN number or GTIN (Global Trade Item Number) to identify itself.
An EAN-13 always has 13 digits:
| string | $code | the code to validate |
IsValidEAN14(string $code = '') : boolean
Determine whether a EAN-14 code is valid
The EAN 14 code is used for traded goods.
An EAN-14 always has 14 digits:
| string | $code | the code to validate |
IsValidUPCA(string $code = '') : boolean
Determine whether a UPC-A code is valid
The UPC-A code is the standard version of the UPC code and has 12 digits. The UPC code is a numeric code which is able to display digits from 0-9. It is also called UPC-12 and is very similar to the EAN code.
The structure of the UPC-A code is as follows:
| string | $code | the code to validate |
IsValidUPCE(string $code = '') : boolean
Determine whether a UPC-E code is valid
The UPC-E code is a short, 8-digit version of the UPC-A code, always starting with a zero. It is often used for small retail items. The UPC code is a numeric code which is able to display digits from 0-9.
The structure of the full-length UPC-E code is as follows:
| string | $code | the code to validate |
IsValidGSIN(string $code = '') : boolean
Determine whether a GSIN code is valid.
The Global Shipment Identification Number (GSIN) is a globally unique number which is used to identify a grouping of logistic units which are part of the same shipment.
The logistic units keep the same GSIN during all transport stages, from origin to final destination. The GSIN identifies the logical grouping of one or several logistic units, each identified with a separate SSCC (Serial Shipping Container Code).
GSIN also meets the requirements for UCR (Unique Consignment Reference) according to the World Customs Organisation, WCO.
| string | $code | the code to validate |
IsValidSSCC(string $code = '') : boolean
Determine whether a SSCC code is valid.
An SSCC (Serial Shipping Container Code) is used to provide a number logistic units. An SSCC enables manufacturers, suppliers, carriers and buyers to track a logistic unit from production to end customer. The can be used for tracing goods, warehouse management and efficient handling in transport.
The same SSCC may not be used on two different logistic units, neither during transport or when they are handled in the warehouse of the transport buyer or goods recipient. This means that the life span of an SSCC, that is the period until the unit is unpacked or repacked, can be from a few to many years.
The transport industry recommends that an SSCC should be available for re-use after 18 months, but only if it is certain that the SSCC is no longer in use. To estimate how many SSCCs your company needs you can, for example, use the number of logistic units handled in the most recent one or two year period.
| string | $code | the code to validate |
IsValidGLN(string $code = '') : boolean
Determine whether a GLN code is valid
A GLN (Global Location Number) is used to uniquely identify a company or organisation.
A GLN can also be used to number delivery places, invoicing addresses, workplaces, branches as well as functions or roles, such as goods recipient or authorised purchaser.
| string | $code | the code to validate |
IsValidIMEI(string $code = '') : boolean
Determine whether an IMEI number is valid or IMEISV
The International Mobile Equipment Identity (IMEI) is a number, to identify 3GPP (i.e., GSM, UMTS and LTE) and iDEN mobile phones, as well as some satellite phones.
It is usually found printed inside the battery compartment of the phone, but can also be displayed on-screen on most phones by entering *#06# on the dialpad, or alongside other system information in the settings menu on smartphone operating systems.
The IMEI (15 decimal digits: 14 digits plus a check digit) or IMEISV (16 digits) includes information on the origin, model, and serial number of the device.
The structure of the IMEI/SV is specified in 3GPP TS 23.003. The model and origin comprise the initial 8-digit portion of the IMEI/SV, known as the Type Allocation Code (TAC). The remainder of the IMEI is manufacturer-defined, with a Luhn check digit at the end. The IMEISV drops the Luhn check digit in favor of an additional two digits for the Software Version Number (SVN)
| string | $code | the code to validate |
validateEANCheckDigit(string $code, integer $length) : boolean
Determine whether a code is valid using the check-digit algorithm. It also validates whether the code has the right length and that it's composed of only digits.
A check digit is a form of redundancy check used for error detection on identification numbers, such as bank account numbers, which are used in an application where they will at least sometimes be input manually.
It is analogous to a binary parity bit used to check for errors in computer-generated data. It consists of one or more digits computed by an algorithm from the other digits (or letters) in the sequence input.
| string | $code | the code to validate |
| integer | $length | the code's length (e.g. 13 for EAN13, 12 for UPC-A, etc.) |
sumAllDigits(integer $number, integer $index) : array
Replace a number by the sum of all its digits.
This funtion is used by BarcodeValidator::calculateLuhnCheckDigit() to add all the single digits of each number
| integer | $number | the number to get the sum of digits from |
| integer | $index | the array index |
calculateLuhnCheckDigit(string $code) : integer
Calculate a check digit using the Luhn Modulo-10 algorithm used for IMEI, Credit Card validation and others.
The check digit (x) is obtained by computing the sum of the rest of the digits then subtracting the units digit from 10. In algorithm form:
For instance
| Digits | 4 | 9 | 0 | 1 | 5 | 4 | 2 | 0 | 3 | 2 | 3 | 7 | 5 | 1 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Double every other | 4 | 18 | 0 | 2 | 5 | 8 | 2 | 0 | 3 | 4 | 3 | 14 | 5 | 2 | |
| Sum all digits | 4 | + (1+8) | + 0 | + 2 | + 5 | + 8 | + 2 | + 0 | + 3 | + 4 | + 3 | + (1+4) | + 5 | + 2 | = 52 |
Check digit is: 10 - 2 = 8.
| string | $code | the code to calculate the Luhn check digit for |
validateLuhnCheckDigit(string $code, integer $length) : boolean
Determine whether a code is valid using the Luhn algorithm. It also validates whether the code has the right length and that it's composed of only digits.
The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers and National Provider Identifier numbers in US and Canadian Social Insurance Numbers.
| string | $code | the code to validate |
| integer | $length | the code's length (e.g. 15 for IMEI numbers, 16 for credit cards, etc.) |
calculateISBNCheckDigit(string $code) : mixed
Calculate a check digit using the ISBN algorithm. The ISBN is error-detecting, but not error-correcting (unless it is known that only a single digit is erroneous). The ISBN detects any single-digit error, as well as most two-digit error resulting from transposing two digits.
The ISBN (International Standard Book Number) is a unique numeric commercial book identifier. An ISBN is assigned to each edition and variation (except reprintings) of a book. For example, an e-book, a paperback and a hardcover edition of the same book would each have a different ISBN. ISBNs are 13 digits long if assigned on or after 1 January 2007, and 10 digits long if assigned before 2007. The method of assigning an ISBN is nation-based and varies from country to country, often depending on how large the publishing industry is within a country.
The initial ISBN configuration of recognition was generated in 1967 based upon the 9-digit Standard Book Numbering (SBN) created in 1966. The 10-digit ISBN format was developed by the International Organization for Standardization (ISO) and was published in 1970 as international standard ISO 2108 (the SBN code can be converted to a ten digit ISBN by prefixing it with a zero). The last digit is a check digit which may be in the range 0-9 or X (where X is the Roman numeral for 10) for an ISBN-10, or 0-9 for an ISBN-13.
For ISBN-10 each of the first nine digits of the ten-digit ISBN (excluding the check digit itself) is multiplied by a number in a sequence from 10 to 2 and the remainder of the sum with (respect to 11) is computed. The resulting remainder, plus the check digit, must equal a multiple of 11 (either 0 or 11).
For ISBN-13 the check-digit is calculated using the BarcodeValidator::calculateEANCheckDigit() function, making it compatible with EAN codes.
Therefore, the check digit is (11 minus the remainder of the sum of the products modulo 11) modulo 11. Taking the remainder modulo 11 a second time accounts for the possibility that the first remainder is 0 (without the second modulo operation the calculation could end up with 11-0 = 11 which is invalid).
| ISBN | 1 | 8 | 4 | 1 | 4 | 6 | 2 | 0 | 1 | Total |
|---|---|---|---|---|---|---|---|---|---|---|
| Weight | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | |
| Result | 10 | 72 | 32 | 7 | 24 | 30 | 8 | 0 | 2 | 185 |
Calculate (11 - (185 mod 11)) mod 11 = 2 which is the last digit from the original ISBN. Hence, the ISBN is valid.
| string | $code | The code to calculate the check digit for |
(integer or X)
validateISBNCheckDigit(string $code) : boolean
Determine whether a code is valid using the ISBN algorithm (either ISBN 10). It also validates whether the code has the right length.
For ISBN-10 each of the first nine digits of the ten-digit ISBN (excluding the check digit itself) is multiplied by a number in a sequence from 10 to 2 and the remainder of the sum with (respect to 11) is computed. The resulting remainder, plus the check digit, must equal a multiple of 11 (either 0 or 11).
| string | $code | the code to validate |