From f8897823d6e1f6aafd96ec86a492e2ae15ae3b72 Mon Sep 17 00:00:00 2001 From: Peter Slapansky Date: Tue, 9 Sep 2025 11:39:47 +0100 Subject: [PATCH 1/7] Extracted barcode types into their own value class, so they can be used without the main Barcode class if desired. --- src/BarcodeType.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/BarcodeType.php diff --git a/src/BarcodeType.php b/src/BarcodeType.php new file mode 100644 index 0000000..85b81f7 --- /dev/null +++ b/src/BarcodeType.php @@ -0,0 +1,34 @@ +. + * + * @link http://www.phpclasses.org/package/8560-PHP-Detect-type-and-check-EAN-and-UPC-barcodes.html + * @author Ferry Bouwhuis + */ + +/** + * @package Ced\Validator + */ +class BarcodeType +{ + public const TYPE_ASIN = 'ASIN'; + public const TYPE_EAN = 'EAN'; // 13 digits + public const TYPE_EAN_8 = 'EAN-8'; + public const TYPE_GTIN = 'GTIN'; // 14 digits + public const TYPE_ISBN_10 = 'ISBN'; // 10 digits excluding dashes + public const TYPE_ISBN_13 = 'ISBN'; // 13 digits excluding dashes + public const TYPE_UPC = 'UPC'; // 12 digits + public const TYPE_UPC_COUPON_CODE = 'UPC Coupon Code'; +} From bb63b57c807fe6b89d48e89548a8a803c36edbdf Mon Sep 17 00:00:00 2001 From: Peter Slapansky Date: Tue, 9 Sep 2025 12:02:44 +0100 Subject: [PATCH 2/7] Upgrading PHPUnit 9.6, which is the last version supported for PHP 7.x. Adding type hints where missing and using constants from the new value class BarcodeType. --- .gitignore | 6 +- composer.json | 4 +- phpunit.xml.dist | 30 +++---- src/Barcode.php | 190 ++++++++++++++++++++++-------------------- tests/BarcodeTest.php | 74 ++++++++-------- 5 files changed, 155 insertions(+), 149 deletions(-) diff --git a/.gitignore b/.gitignore index 723ef36..fed1eea 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ -.idea \ No newline at end of file +.idea +vendor +composer.lock +build +.phpunit.result.cache diff --git a/composer.json b/composer.json index a7e848c..00eba40 100644 --- a/composer.json +++ b/composer.json @@ -3,10 +3,10 @@ "description": "A collection of PHP classes for managing barcodes.", "license": "GPL-3.0+", "require": { - "php": "^5.0 || ^7.0" + "php": "^7.4 || ^8.0" }, "require-dev": { - "phpunit/phpunit": "^4.8" + "phpunit/phpunit": "^9.6" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 9194498..c108409 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -2,14 +2,11 @@ - - - - @@ -18,22 +15,19 @@ - - - + + ./vendor/ ./tests/ - - + - - + src - - + + - - + + - \ No newline at end of file + diff --git a/src/Barcode.php b/src/Barcode.php index cf8168c..125f4b6 100644 --- a/src/Barcode.php +++ b/src/Barcode.php @@ -7,12 +7,10 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * @@ -24,131 +22,141 @@ /** * Class Barcode + * * @package Ced\Validator */ class Barcode { - const TYPE_GTIN = 'GTIN'; // 14 digits - const TYPE_EAN = 'EAN'; // 13 digits - const TYPE_UPC = 'UPC'; // 12 digits - const TYPE_ISBN_10 = 'ISBN'; // 10 digits excluding dashes - const TYPE_ISBN_13 = 'ISBN'; // 13 digits excluding dashes - const TYPE_EAN_8 = 'EAN-8'; - const TYPE_ASIN = 'ASIN'; - const TYPE_UPC_COUPON_CODE = 'UPC Coupon Code'; - public $barcode; - public $type; - public $gtin; - public $valid; - - public $allowedIdentifiers = array( - self::TYPE_GTIN, - self::TYPE_EAN, - self::TYPE_EAN_8, - self::TYPE_UPC, - self::TYPE_ASIN, - self::TYPE_ISBN_10, - self::TYPE_ISBN_13, - ); - - public function getBarcode() - { - // For Walmart - /*if (($this->type == self::TYPE_EAN || $this->type == self::TYPE_EAN_8) && strlen($this->barcode) < 14) { - $zeros = 14 - strlen($this->barcode); - $prefix = ''; - for ($i = $zeros; $i <= $zeros; $i++) { - $prefix .= '0'; - } - return $prefix . $this->barcode; - }*/ + public const TYPE_ASIN = BarcodeType::TYPE_ASIN; + public const TYPE_EAN = BarcodeType::TYPE_EAN; // 13 digits + public const TYPE_EAN_8 = BarcodeType::TYPE_EAN_8; + public const TYPE_GTIN = BarcodeType::TYPE_GTIN; // 14 digits + public const TYPE_ISBN_10 = BarcodeType::TYPE_ISBN_10; // 10 digits excluding dashes + public const TYPE_ISBN_13 = BarcodeType::TYPE_ISBN_13; // 13 digits excluding dashes + public const TYPE_UPC = BarcodeType::TYPE_UPC; // 12 digits + public const TYPE_UPC_COUPON_CODE = BarcodeType::TYPE_UPC_COUPON_CODE; + + public string $barcode; + public ?string $type = null; + public string $gtin; + public bool $valid; + + public array $allowedIdentifiers = [ + BarcodeType::TYPE_GTIN, + BarcodeType::TYPE_EAN, + BarcodeType::TYPE_EAN_8, + BarcodeType::TYPE_UPC, + BarcodeType::TYPE_ASIN, + BarcodeType::TYPE_ISBN_10, + BarcodeType::TYPE_ISBN_13, + ]; + public function getBarcode(): string + { return $this->barcode; } - public function setBarcode($barcode) + public function setBarcode(string $barcode): self { - $this->barcode = (string)$barcode; // Trims parsed string to remove unwanted whitespace or characters - $this->barcode = trim($this->barcode); + $this->barcode = trim((string)$barcode); + if (preg_match('/[^0-9]/', $this->barcode)) { - $isbn = $this->isIsbn($this->barcode); - if ($isbn == false) { - $asin = $this->isAsin($this->barcode); + if (false === $this->isIsbn($this->barcode)) { + $this->isAsin($this->barcode); } - } else { - $this->gtin = $this->barcode; - $length = strlen($this->gtin); - if (($length > 11 && $length <= 14) || $length == 8) { - $zeros = 18 - $length; - $length = null; - $fill = ''; - for ($i = 0; $i < $zeros; $i++) { - $fill .= '0'; - } - $this->gtin = $fill . $this->gtin; - $fill = null; - $this->valid = true; - if (!$this->checkDigitValid()) { - $this->valid = false; - } elseif (substr($this->gtin, 5, 1) > 2) { - // EAN / JAN / EAN-13 code - $this->type = self::TYPE_EAN; - } elseif (substr($this->gtin, 6, 1) == 0 && substr($this->gtin, 0, 10) == 0) { - // EAN-8 / GTIN-8 code - $this->type = self::TYPE_EAN_8; - } elseif (substr($this->gtin, 5, 1) <= 0) { - // UPC / UCC-12 GTIN-12 code - if (substr($this->gtin, 6, 1) == 5) { - $this->type = self::TYPE_UPC_COUPON_CODE; - } else { - $this->type = self::TYPE_UPC; - } - } elseif (substr($this->gtin, 0, 6) == 0) { - // GTIN-14 code - $this->type = self::TYPE_GTIN; - } else { - // EAN code - $this->type = self::TYPE_EAN; - } + + return $this; + } + + $this->gtin = $this->barcode; + $this->detectTypeAndValidate($this->barcode); + + return $this; + } + + /** + * Refactor this class later so that validation and detection aren't done as side effects. + */ + public function detectTypeAndValidate(string $barcode): self + { + $length = strlen($this->gtin); + $hasRequiredLength = ($length > 11 && $length <= 14) || $length == 8; + + if (!$hasRequiredLength) { + return $this; + } + + $zeros = 18 - $length; + $this->gtin = str_repeat('0', $zeros) . $this->gtin; + $this->valid = true; + + if (!$this->checkDigitValid()) { + $this->valid = false; + } elseif (substr($this->gtin, 5, 1) > 2) { + // EAN / JAN / EAN-13 code + $this->type = BarcodeType::TYPE_EAN; + } elseif (substr($this->gtin, 6, 1) == 0 && substr($this->gtin, 0, 10) == 0) { + // EAN-8 / GTIN-8 code + $this->type = BarcodeType::TYPE_EAN_8; + } elseif (substr($this->gtin, 5, 1) <= 0) { + // UPC / UCC-12 GTIN-12 code + if (substr($this->gtin, 6, 1) == 5) { + $this->type = BarcodeType::TYPE_UPC_COUPON_CODE; + } else { + $this->type = BarcodeType::TYPE_UPC; } + } elseif (substr($this->gtin, 0, 6) == 0) { + // GTIN-14 code + $this->type = BarcodeType::TYPE_GTIN; + } else { + // EAN code + $this->type = BarcodeType::TYPE_EAN; } + return $this; } - public function isIsbn($barcode) + public function isIsbn(string $barcode): bool { $regex = '/\b(?:ISBN(?:: ?| ))?((?:97[89])?\d{9}[\dx])\b/i'; if (preg_match($regex, str_replace('-', '', $barcode), $matches)) { if (strlen($matches[1]) === 10) { $this->valid = true; - $this->type = self::TYPE_ISBN_10; + $this->type = BarcodeType::TYPE_ISBN_10; } else { $this->valid = true; - $this->type = self::TYPE_ISBN_13; + $this->type = BarcodeType::TYPE_ISBN_13; } + return true; } + return false; // No valid ISBN found } - public function isAsin($barcode) + public function isAsin(string $barcode): bool { $ptn = "/B[0-9]{2}[0-9A-Z]{7}|[0-9]{9}(X|0-9])/"; + if (preg_match($ptn, $barcode, $matches)) { $this->valid = true; - $this->type = self::TYPE_ASIN; + $this->type = BarcodeType::TYPE_ASIN; + return true; } + return false; } - public function checkDigitValid() + public function checkDigitValid(): bool { $calculation = 0; + for ($i = 0; $i < (strlen($this->gtin) - 1); $i++) { $calculation += $i % 2 ? $this->gtin[$i] * 1 : $this->gtin[$i] * 3; } + if (substr(10 - (substr($calculation, -1)), -1) != substr($this->gtin, -1)) { return false; } else { @@ -156,26 +164,28 @@ public function checkDigitValid() } } - public function getType() + public function getType(): ?string { - // For Walmart - /*if ($this->type == self::TYPE_EAN || $this->type == self::TYPE_EAN_8) { - return self::TYPE_GTIN; - }*/ return $this->type; } - public function getGTIN14() + public function getGTIN14(): string { return (string)substr($this->gtin, -14); } - public function isValid() + public function isValid(): bool { + if (empty($this->type)) { + return false; + } + + // TODO: This should be handled by injecting a list of valid types, so everyone can set their own. // For Walmart if (!in_array($this->type, $this->allowedIdentifiers)) { $this->valid = false; } + return $this->valid; } } diff --git a/tests/BarcodeTest.php b/tests/BarcodeTest.php index 4a281a8..5a3bb83 100644 --- a/tests/BarcodeTest.php +++ b/tests/BarcodeTest.php @@ -3,65 +3,64 @@ namespace Ced\Barcodes\Tests; use Ced\Validator\Barcode; +use Ced\Validator\BarcodeType; +use PHPUnit\Framework\TestCase; -// Nasty work around for testing over multiple PHPUnit versions -if (!class_exists('\PHPUnit_Framework_TestCase') && class_exists('\PHPUnit\Framework\TestCase')) { - class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase'); -} - -class BarcodeTest extends \PHPUnit_Framework_TestCase +class BarcodeTest extends TestCase { + private function getTestedInstanceForBarcode(string $barcode): Barcode + { + return (new Barcode())->setBarcode($barcode); + } - public function testInit() + public function testInit(): void { - $validator = new Barcode(); - $validator->setBarcode('12345123'); - $this->assertInstanceOf('\Ced\Validator\Barcode', $validator); + $this->assertInstanceOf( + Barcode::class, + $this->getTestedInstanceForBarcode('12345123'), + ); } - public function testInvalid() + public function testInvalid(): void { - $validator = new Barcode(); - $validator->setBarcode('string123'); - $this->assertFalse($validator->isValid()); + $this->assertFalse( + $this->getTestedInstanceForBarcode('string123')->isValid() + ); } - public function testInvalidLooksLikeBarcode() + public function testInvalidLooksLikeBarcode(): void { - $validator = new Barcode(); - $validator->setBarcode('5060411950138'); - $this->assertFalse($validator->isValid()); + $this->assertFalse( + $this->getTestedInstanceForBarcode('5060411950138')->isValid() + ); } - public function testValid() + public function testValid(): void { - $validator = new Barcode(); - $validator->setBarcode('001303050100'); - $this->assertFalse($validator->isValid()); + $this->assertFalse( + $this->getTestedInstanceForBarcode('001303050100')->isValid() + ); } - public function testEanRestricted() + public function testEanRestricted(): void { - $validator = new Barcode(); - $validator->setBarcode('2100000030019'); + $validator = $this->getTestedInstanceForBarcode('2100000030019'); $this->assertTrue($validator->isValid()); - $this->assertSame(Barcode::TYPE_EAN, $validator->getType()); + $this->assertSame(BarcodeType::TYPE_EAN, $validator->getType()); } - public function testEan() + public function testEan(): void { - $validator = new Barcode(); - $validator->setBarcode('8838108476586'); + $validator = $this->getTestedInstanceForBarcode('8838108476586'); $this->assertTrue($validator->isValid()); - $this->assertSame(Barcode::TYPE_EAN, $validator->getType()); + $this->assertSame(BarcodeType::TYPE_EAN, $validator->getType()); } - public function testEan2() + public function testEan2(): void { - $validator = new Barcode(); - $validator->setBarcode('5060411950139'); + $validator = $this->getTestedInstanceForBarcode('5060411950139'); $this->assertTrue($validator->isValid()); - $this->assertSame(Barcode::TYPE_EAN, $validator->getType()); + $this->assertSame(BarcodeType::TYPE_EAN, $validator->getType()); } // public function testEan3() @@ -72,11 +71,10 @@ public function testEan2() // $this->assertSame(Barcode::TYPE_EAN, $validator->getType()); // } - public function testUpc() + public function testUpc(): void { - $validator = new Barcode(); - $validator->setBarcode('700867967774'); + $validator = $this->getTestedInstanceForBarcode('700867967774'); $this->assertTrue($validator->isValid()); - $this->assertSame(Barcode::TYPE_UPC, $validator->getType()); + $this->assertSame(BarcodeType::TYPE_UPC, $validator->getType()); } } From 18e2c0190b62b6b68ce40863bd433eab46b42b3e Mon Sep 17 00:00:00 2001 From: Peter Slapansky Date: Tue, 9 Sep 2025 12:15:03 +0100 Subject: [PATCH 3/7] Switching to strict comparisons and exlicit type casts to remove ambiguity and more clearly demonstrate comparison intent. --- src/Barcode.php | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/Barcode.php b/src/Barcode.php index 125f4b6..c52f46c 100644 --- a/src/Barcode.php +++ b/src/Barcode.php @@ -41,6 +41,8 @@ class Barcode public string $gtin; public bool $valid; + protected int $paddedBarcodeLength = 18; + public array $allowedIdentifiers = [ BarcodeType::TYPE_GTIN, BarcodeType::TYPE_EAN, @@ -58,7 +60,7 @@ public function getBarcode(): string public function setBarcode(string $barcode): self { - // Trims parsed string to remove unwanted whitespace or characters + // Trims parsed string to remove unwanted whitespace or characters. $this->barcode = trim((string)$barcode); if (preg_match('/[^0-9]/', $this->barcode)) { @@ -76,7 +78,7 @@ public function setBarcode(string $barcode): self } /** - * Refactor this class later so that validation and detection aren't done as side effects. + * TODO: Refactor this class later so that validation and detection aren't done as side effects. */ public function detectTypeAndValidate(string $barcode): self { @@ -87,26 +89,25 @@ public function detectTypeAndValidate(string $barcode): self return $this; } - $zeros = 18 - $length; - $this->gtin = str_repeat('0', $zeros) . $this->gtin; + $this->gtin = str_repeat('0', $this->paddedBarcodeLength - $length) . $this->gtin; $this->valid = true; if (!$this->checkDigitValid()) { $this->valid = false; - } elseif (substr($this->gtin, 5, 1) > 2) { + } elseif ((int)substr($this->gtin, 5, 1) > 2) { // EAN / JAN / EAN-13 code $this->type = BarcodeType::TYPE_EAN; - } elseif (substr($this->gtin, 6, 1) == 0 && substr($this->gtin, 0, 10) == 0) { + } elseif ((int)substr($this->gtin, 6, 1) === 0 && (int)substr($this->gtin, 0, 10) === 0) { // EAN-8 / GTIN-8 code $this->type = BarcodeType::TYPE_EAN_8; - } elseif (substr($this->gtin, 5, 1) <= 0) { + } elseif ((int)substr($this->gtin, 5, 1) <= 0) { // UPC / UCC-12 GTIN-12 code - if (substr($this->gtin, 6, 1) == 5) { + if ((int)substr($this->gtin, 6, 1) === 5) { $this->type = BarcodeType::TYPE_UPC_COUPON_CODE; } else { $this->type = BarcodeType::TYPE_UPC; } - } elseif (substr($this->gtin, 0, 6) == 0) { + } elseif ((int)substr($this->gtin, 0, 6) === 0) { // GTIN-14 code $this->type = BarcodeType::TYPE_GTIN; } else { @@ -121,11 +122,11 @@ public function isIsbn(string $barcode): bool { $regex = '/\b(?:ISBN(?:: ?| ))?((?:97[89])?\d{9}[\dx])\b/i'; if (preg_match($regex, str_replace('-', '', $barcode), $matches)) { + $this->valid = true; + if (strlen($matches[1]) === 10) { - $this->valid = true; $this->type = BarcodeType::TYPE_ISBN_10; } else { - $this->valid = true; $this->type = BarcodeType::TYPE_ISBN_13; } @@ -151,17 +152,13 @@ public function isAsin(string $barcode): bool public function checkDigitValid(): bool { - $calculation = 0; + $calculated = 0; for ($i = 0; $i < (strlen($this->gtin) - 1); $i++) { - $calculation += $i % 2 ? $this->gtin[$i] * 1 : $this->gtin[$i] * 3; + $calculated += $i % 2 ? $this->gtin[$i] * 1 : $this->gtin[$i] * 3; } - if (substr(10 - (substr($calculation, -1)), -1) != substr($this->gtin, -1)) { - return false; - } else { - return true; - } + return substr(10 - (substr($calculated, -1)), -1) === substr($this->gtin, -1); } public function getType(): ?string From 3db406a706adcb76dd7835b2b222685826521b74 Mon Sep 17 00:00:00 2001 From: Peter Slapansky Date: Tue, 9 Sep 2025 12:19:54 +0100 Subject: [PATCH 4/7] Removing a now antiquated PHP 7 readiness badge and changing note about current implementation to Overview and its content for improved clarity. --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9b1090b..e244f7b 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,11 @@ A PHP class for checking EAN8, EAN13, UPC and GTIN barcodes are valid (based on [![Packagist](https://img.shields.io/packagist/dt/milindsingh/validator.svg?maxAge=3600)](https://github.com/milindsingh/validator) [![Packagist](https://img.shields.io/packagist/dm/milindsingh/validator.svg?maxAge=3600)](https://github.com/milindsingh/validator) [![Packagist](https://img.shields.io/packagist/l/milindsingh/validator.svg?maxAge=3600)](https://github.com/milindsingh/validator) -[![PHP 7 ready](http://php7ready.timesplinter.ch/milindsingh/validator/master/badge.svg)](https://travis-ci.org/milindsingh/validator) - https://packagist.org/packages/milindsingh/validator -_Note: This project currently does nothing other than have some validation functions. I expect to add additional functionality in the future._ +## Overview +This project currently does nothing other than have some validation functions. I expect to add additional functionality in the future. ## Installation (with composer) ``` From 98ca5043564edcea775080999554213b51bf2937 Mon Sep 17 00:00:00 2001 From: Peter Slapansky Date: Tue, 9 Sep 2025 12:23:11 +0100 Subject: [PATCH 5/7] Moving more information into Overview in the readme file. --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e244f7b..12e8a54 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ # PHP Barcode -A PHP class for checking EAN8, EAN13, UPC and GTIN barcodes are valid (based on check digit). [![Travis CI](https://travis-ci.org/milindsingh/validator.svg?branch=master)](https://travis-ci.org/milindsingh/validator) [![Packagist](https://img.shields.io/packagist/v/milindsingh/validator.svg?maxAge=3600)](https://github.com/milindsingh/validator) @@ -10,7 +9,9 @@ A PHP class for checking EAN8, EAN13, UPC and GTIN barcodes are valid (based on https://packagist.org/packages/milindsingh/validator ## Overview -This project currently does nothing other than have some validation functions. I expect to add additional functionality in the future. +A PHP class for checking EAN8, EAN13, UPC and GTIN barcodes are valid (based on check digit). + +This project currently only offers validation functionality but additional functionality might be added in the future. ## Installation (with composer) ``` From c9fa6a67f11d9365d8ca841e4903c56cfff7077a Mon Sep 17 00:00:00 2001 From: Peter Slapansky Date: Tue, 9 Sep 2025 12:25:46 +0100 Subject: [PATCH 6/7] Reprecated constants in the Barcode class in favour of those in BarcodeType. --- README.md | 12 ++++++------ src/Barcode.php | 8 ++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 12e8a54..cd6a96e 100644 --- a/README.md +++ b/README.md @@ -36,12 +36,12 @@ if ($validator->isValid()) { // Get the barcode type echo 'Barcode is in format of ' . $validator->getType(); // Possible formats returned are: -// (string) "GTIN" which equals constant \Ced\Validator\Barcode::TYPE_GTIN -// (string) "EAN-8" which equals constant \Ced\Validator\Barcode::TYPE_EAN_8 -// (string) "EAN" which equals constant \Ced\Validator\Barcode::TYPE_EAN -// (string) "EAN Restricted" which equals constant \Ced\Validator\Barcode::TYPE_EAN_RESTRICTED -// (string) "UPC" which equals constant \Ced\Validator\Barcode::TYPE_UPC -// (string) "UPC Coupon Code" which equals constant \Ced\Validator\Barcode::TYPE_UPC_COUPON_CODE +// (string) "GTIN" which equals constant \Ced\Validator\BarcodeType::TYPE_GTIN +// (string) "EAN-8" which equals constant \Ced\Validator\BarcodeType::TYPE_EAN_8 +// (string) "EAN" which equals constant \Ced\Validator\BarcodeType::TYPE_EAN +// (string) "EAN Restricted" which equals constant \Ced\Validator\BarcodeType::TYPE_EAN_RESTRICTED +// (string) "UPC" which equals constant \Ced\Validator\BarcodeType::TYPE_UPC +// (string) "UPC Coupon Code" which equals constant \Ced\Validator\BarcodeType::TYPE_UPC_COUPON_CODE // Returns the barcode in GTIN-14 format diff --git a/src/Barcode.php b/src/Barcode.php index c52f46c..645c19c 100644 --- a/src/Barcode.php +++ b/src/Barcode.php @@ -27,13 +27,21 @@ */ class Barcode { + /** @deprecated Use same-name equivalent in Ced\Validator\BarcodeType */ public const TYPE_ASIN = BarcodeType::TYPE_ASIN; + /** @deprecated Use same-name equivalent in Ced\Validator\BarcodeType */ public const TYPE_EAN = BarcodeType::TYPE_EAN; // 13 digits + /** @deprecated Use same-name equivalent in Ced\Validator\BarcodeType */ public const TYPE_EAN_8 = BarcodeType::TYPE_EAN_8; + /** @deprecated Use same-name equivalent in Ced\Validator\BarcodeType */ public const TYPE_GTIN = BarcodeType::TYPE_GTIN; // 14 digits + /** @deprecated Use same-name equivalent in Ced\Validator\BarcodeType */ public const TYPE_ISBN_10 = BarcodeType::TYPE_ISBN_10; // 10 digits excluding dashes + /** @deprecated Use same-name equivalent in Ced\Validator\BarcodeType */ public const TYPE_ISBN_13 = BarcodeType::TYPE_ISBN_13; // 13 digits excluding dashes + /** @deprecated Use same-name equivalent in Ced\Validator\BarcodeType */ public const TYPE_UPC = BarcodeType::TYPE_UPC; // 12 digits + /** @deprecated Use same-name equivalent in Ced\Validator\BarcodeType */ public const TYPE_UPC_COUPON_CODE = BarcodeType::TYPE_UPC_COUPON_CODE; public string $barcode; From 546befd46a2cbd4d40da09efc1bb0efeaaa481a4 Mon Sep 17 00:00:00 2001 From: Peter Slapansky Date: Tue, 9 Sep 2025 12:47:33 +0100 Subject: [PATCH 7/7] Added strict type declaration and updated code to match. --- src/Barcode.php | 8 ++++++-- src/BarcodeType.php | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Barcode.php b/src/Barcode.php index 645c19c..809747f 100644 --- a/src/Barcode.php +++ b/src/Barcode.php @@ -1,5 +1,7 @@ gtin) - 1); $i++) { - $calculated += $i % 2 ? $this->gtin[$i] * 1 : $this->gtin[$i] * 3; + $relevantPart = (int)$this->gtin[$i]; + $calculated += $i % 2 ? $relevantPart : ($relevantPart * 3); } - return substr(10 - (substr($calculated, -1)), -1) === substr($this->gtin, -1); + return substr((string)(10 - (int)(substr((string)$calculated, -1))), -1) + === substr($this->gtin, -1); } public function getType(): ?string diff --git a/src/BarcodeType.php b/src/BarcodeType.php index 85b81f7..4392b63 100644 --- a/src/BarcodeType.php +++ b/src/BarcodeType.php @@ -1,5 +1,7 @@