Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/AbstractRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/AustrianInsuranceNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion src/Rules/Ean.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Rules/Gtin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ 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;
}

if (!$this->hasAllowedLength($value)) {
return false;
}

$value = strval($value);

return match (strlen($value)) {
8, 13 => parent::isValid($value),
12 => parent::checksumMatches('0' . $value),
Expand Down
1 change: 1 addition & 0 deletions tests/Rules/EanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down