Skip to content

Commit

Permalink
fix(Rules): Improve NumberRule range of supported int/float values
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna authored and pionl committed Jun 1, 2023
1 parent 3ce4861 commit 4292a8a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
28 changes: 18 additions & 10 deletions src/Validation/Rules/NumberRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,32 @@ class NumberRule implements Rule
{
public function passes($attribute, $value): bool
{
if (is_string($value)) {
$value = str_replace(',', '.', $value);
}

if ((is_float($value)) || (is_numeric($value) && str_contains((string) $value, '.'))) {
$floatVal = (float) $value;
$stringVal = (string) $floatVal;
return is_finite($floatVal) && $stringVal === (string) $value;
} elseif (is_numeric($value)) {
if (self::isNumericInt($value)) {
$intVal = (int) $value;
return $intVal !== PHP_INT_MAX && $intVal !== PHP_INT_MIN;
}

return false;
$value = strtr((string) $value, [
',' => '.',
]);

if (is_numeric($value) === false) {
return false;
}

return str_contains((string) (float) $value, 'E+') === false;
}

public function message(): string
{
return 'Given :attribute is not a valid number or it exceeds int/float limits.';
}

/**
* @return ($value is non-empty-string ? bool : ($value is int ? true : false))
*/
private static function isNumericInt(mixed $value): bool
{
return is_int($value) || (is_string($value) && preg_match('#^[+-]?\d+$#D', $value));
}
}
25 changes: 23 additions & 2 deletions tests/Unit/Validation/Rules/NumberRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,34 @@ protected function testData(): array
return [
new RuleExpectation('test', false),
new RuleExpectation('234', true),
new RuleExpectation(234, true),
new RuleExpectation('-234', true),
new RuleExpectation(-245, true),
new RuleExpectation(234, true),
new RuleExpectation(-234, true),
new RuleExpectation(.0, true),
new RuleExpectation(-.0, true),
new RuleExpectation(0., true),
new RuleExpectation(-0., true),
new RuleExpectation('245.5', true),
new RuleExpectation('-245.5', true),
new RuleExpectation(245.50, true),
new RuleExpectation(-245.50, true),
new RuleExpectation('-01.50', true),
new RuleExpectation('01.50', true),
new RuleExpectation('01', true),
new RuleExpectation('-01', true),
new RuleExpectation('.50', true),
new RuleExpectation('+.50', true),
new RuleExpectation('-.50', true),
new RuleExpectation('0', true),
new RuleExpectation('.0', true),
new RuleExpectation('0.', true),
new RuleExpectation('0.0', true),
new RuleExpectation('0,0', true),
new RuleExpectation('0,50', true),
new RuleExpectation(',', false),
new RuleExpectation('-,', false),
new RuleExpectation('.', false),
new RuleExpectation('', false),
new RuleExpectation('9223372036854775807', false),
new RuleExpectation('9223372036854775807.5', false),
new RuleExpectation('922337203685477580703434355.5', false),
Expand Down

0 comments on commit 4292a8a

Please sign in to comment.