Skip to content
Permalink
Browse files

Validator:: accepts the plus character as a valid option in numeric s…

…trings (#184)
  • Loading branch information...
Amunak authored and dg committed Mar 6, 2019
1 parent ed27db4 commit e93dd1adc1d085dad7aa093e2950ea81522023a4
Showing with 8 additions and 2 deletions.
  1. +2 −2 src/Utils/Validators.php
  2. +6 −0 tests/Utils/Validators.is().phpt
@@ -178,7 +178,7 @@ public static function isNumber($value): bool
*/
public static function isNumericInt($value): bool
{
return is_int($value) || is_string($value) && preg_match('#^-?[0-9]+\z#', $value);
return is_int($value) || is_string($value) && preg_match('#^[+-]?[0-9]+\z#', $value);
}
@@ -187,7 +187,7 @@ public static function isNumericInt($value): bool
*/
public static function isNumeric($value): bool
{
return is_float($value) || is_int($value) || is_string($value) && preg_match('#^-?[0-9]*[.]?[0-9]+\z#', $value);
return is_float($value) || is_int($value) || is_string($value) && preg_match('#^[+-]?[0-9]*[.]?[0-9]+\z#', $value);
}
@@ -55,6 +55,9 @@ test(function () {
Assert::true(Validators::is('-1', 'numeric'));
Assert::true(Validators::is('-1.5', 'numeric'));
Assert::true(Validators::is('-.5', 'numeric'));
Assert::true(Validators::is('+1', 'numeric'));
Assert::true(Validators::is('+1.5', 'numeric'));
Assert::true(Validators::is('+.5', 'numeric'));
Assert::false(Validators::is('1e6', 'numeric'));
Assert::true(Validators::is(1, 'numeric'));
Assert::true(Validators::is(1.0, 'numeric'));
@@ -67,6 +70,9 @@ test(function () {
Assert::true(Validators::is('-1', 'numericint'));
Assert::false(Validators::is('-1.5', 'numericint'));
Assert::false(Validators::is('-.5', 'numericint'));
Assert::true(Validators::is('+1', 'numericint'));
Assert::false(Validators::is('+1.5', 'numericint'));
Assert::false(Validators::is('+.5', 'numericint'));
Assert::false(Validators::is('1e6', 'numericint'));
Assert::true(Validators::is(1, 'numericint'));
Assert::false(Validators::is(1.0, 'numericint'));

0 comments on commit e93dd1a

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.