From 0cbaa12b13e97da219c9be519eb351cfb1d1c3ea Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Wed, 24 Jun 2026 15:37:18 +0200 Subject: [PATCH 1/3] Fix bug in Ean rule --- src/AbstractRule.php | 8 ++++++++ src/Rules/AustrianInsuranceNumber.php | 2 +- src/Rules/Ean.php | 4 +++- src/Rules/Gtin.php | 2 +- tests/Rules/EanTest.php | 1 + 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/AbstractRule.php b/src/AbstractRule.php index e3ec532..b87d4aa 100644 --- a/src/AbstractRule.php +++ b/src/AbstractRule.php @@ -37,6 +37,14 @@ protected function shortname(): string return strtolower((new ReflectionClass($this))->getShortName()); } + /** + * Determine if input chars are only numbers. + */ + protected function isOnlyNumbers(string $value): bool + { + return preg_match("/^[0-9]+$/", $value) === 1; + } + /** * Return localized error message. */ diff --git a/src/Rules/AustrianInsuranceNumber.php b/src/Rules/AustrianInsuranceNumber.php index 3a1b094..9310678 100644 --- a/src/Rules/AustrianInsuranceNumber.php +++ b/src/Rules/AustrianInsuranceNumber.php @@ -33,7 +33,7 @@ public function isValid(mixed $value): bool { $value = str_replace(' ', '', (string) $value); - return is_numeric($value) + return $this->isOnlyNumbers(strval($value)) && $this->startsNotWithZero($value) && $this->hasValidLength($value) && $this->hasValidBirthday($value) diff --git a/src/Rules/Ean.php b/src/Rules/Ean.php index bce0068..f2ada02 100644 --- a/src/Rules/Ean.php +++ b/src/Rules/Ean.php @@ -26,7 +26,9 @@ public function __construct(private array $lengths = [8, 13]) */ public function isValid(mixed $value): bool { - return is_numeric($value) && $this->hasAllowedLength($value) && $this->checksumMatches($value); + return $this->isOnlyNumbers(strval($value)) + && $this->hasAllowedLength($value) + && $this->checksumMatches($value); } /** diff --git a/src/Rules/Gtin.php b/src/Rules/Gtin.php index fabbbc7..cf088a4 100644 --- a/src/Rules/Gtin.php +++ b/src/Rules/Gtin.php @@ -31,7 +31,7 @@ public function __construct(private array $lengths = [8, 12, 13, 14]) */ public function isValid(mixed $value): bool { - if (!is_numeric($value)) { + if (!$this->isOnlyNumbers(strval($value))) { return false; } diff --git a/tests/Rules/EanTest.php b/tests/Rules/EanTest.php index c343ed6..477e839 100644 --- a/tests/Rules/EanTest.php +++ b/tests/Rules/EanTest.php @@ -68,6 +68,7 @@ public static function dataProviderEan13(): Generator yield [false, '40123455']; yield [false, '96385074']; yield [false, '65833254']; + yield [false, '004.03996E+12']; } public static function dataProviderEan8(): Generator From ca792224823cd4c278f38a5a76f7d4f87f703c5f Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Wed, 24 Jun 2026 15:58:25 +0200 Subject: [PATCH 2/3] Rename method --- src/AbstractRule.php | 2 +- src/Rules/AustrianInsuranceNumber.php | 2 +- src/Rules/Ean.php | 2 +- src/Rules/Gtin.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/AbstractRule.php b/src/AbstractRule.php index b87d4aa..90cd2c8 100644 --- a/src/AbstractRule.php +++ b/src/AbstractRule.php @@ -40,7 +40,7 @@ protected function shortname(): string /** * Determine if input chars are only numbers. */ - protected function isOnlyNumbers(string $value): bool + protected function isOnlyNumericCharacters(string $value): bool { return preg_match("/^[0-9]+$/", $value) === 1; } diff --git a/src/Rules/AustrianInsuranceNumber.php b/src/Rules/AustrianInsuranceNumber.php index 9310678..0c9429d 100644 --- a/src/Rules/AustrianInsuranceNumber.php +++ b/src/Rules/AustrianInsuranceNumber.php @@ -33,7 +33,7 @@ public function isValid(mixed $value): bool { $value = str_replace(' ', '', (string) $value); - return $this->isOnlyNumbers(strval($value)) + return $this->isOnlyNumericCharacters(strval($value)) && $this->startsNotWithZero($value) && $this->hasValidLength($value) && $this->hasValidBirthday($value) diff --git a/src/Rules/Ean.php b/src/Rules/Ean.php index f2ada02..fe4ebc6 100644 --- a/src/Rules/Ean.php +++ b/src/Rules/Ean.php @@ -26,7 +26,7 @@ public function __construct(private array $lengths = [8, 13]) */ public function isValid(mixed $value): bool { - return $this->isOnlyNumbers(strval($value)) + return $this->isOnlyNumericCharacters(strval($value)) && $this->hasAllowedLength($value) && $this->checksumMatches($value); } diff --git a/src/Rules/Gtin.php b/src/Rules/Gtin.php index cf088a4..b001148 100644 --- a/src/Rules/Gtin.php +++ b/src/Rules/Gtin.php @@ -31,7 +31,7 @@ public function __construct(private array $lengths = [8, 12, 13, 14]) */ public function isValid(mixed $value): bool { - if (!$this->isOnlyNumbers(strval($value))) { + if (!$this->isOnlyNumericCharacters(strval($value))) { return false; } From 6774fd2d8e6e01c63717615e44887edf2c8362f1 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Wed, 24 Jun 2026 16:01:43 +0200 Subject: [PATCH 3/3] Remove unnecessary call --- src/Rules/AustrianInsuranceNumber.php | 2 +- src/Rules/Ean.php | 4 +++- src/Rules/Gtin.php | 6 +++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Rules/AustrianInsuranceNumber.php b/src/Rules/AustrianInsuranceNumber.php index 0c9429d..a17a165 100644 --- a/src/Rules/AustrianInsuranceNumber.php +++ b/src/Rules/AustrianInsuranceNumber.php @@ -33,7 +33,7 @@ public function isValid(mixed $value): bool { $value = str_replace(' ', '', (string) $value); - return $this->isOnlyNumericCharacters(strval($value)) + return $this->isOnlyNumericCharacters($value) && $this->startsNotWithZero($value) && $this->hasValidLength($value) && $this->hasValidBirthday($value) diff --git a/src/Rules/Ean.php b/src/Rules/Ean.php index fe4ebc6..7b94714 100644 --- a/src/Rules/Ean.php +++ b/src/Rules/Ean.php @@ -26,7 +26,9 @@ public function __construct(private array $lengths = [8, 13]) */ public function isValid(mixed $value): bool { - return $this->isOnlyNumericCharacters(strval($value)) + $value = strval($value); + + return $this->isOnlyNumericCharacters($value) && $this->hasAllowedLength($value) && $this->checksumMatches($value); } diff --git a/src/Rules/Gtin.php b/src/Rules/Gtin.php index b001148..2164f8f 100644 --- a/src/Rules/Gtin.php +++ b/src/Rules/Gtin.php @@ -31,7 +31,9 @@ public function __construct(private array $lengths = [8, 12, 13, 14]) */ public function isValid(mixed $value): bool { - if (!$this->isOnlyNumericCharacters(strval($value))) { + $value = strval($value); + + if (!$this->isOnlyNumericCharacters($value)) { return false; } @@ -39,8 +41,6 @@ public function isValid(mixed $value): bool return false; } - $value = strval($value); - return match (strlen($value)) { 8, 13 => parent::isValid($value), 12 => parent::checksumMatches('0' . $value),