diff --git a/src/AbstractRule.php b/src/AbstractRule.php index e3ec532..90cd2c8 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 isOnlyNumericCharacters(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..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 is_numeric($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 bce0068..7b94714 100644 --- a/src/Rules/Ean.php +++ b/src/Rules/Ean.php @@ -26,7 +26,11 @@ public function __construct(private array $lengths = [8, 13]) */ public function isValid(mixed $value): bool { - return is_numeric($value) && $this->hasAllowedLength($value) && $this->checksumMatches($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 fabbbc7..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 (!is_numeric($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), 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