Updates for IntVal changes

This commit is contained in:
Alexandre Gomes Gaigalas 2023-02-13 19:21:42 -03:00
commit 9ae39de44e
5 changed files with 25 additions and 2 deletions

View file

@ -23,6 +23,7 @@
- Fixed `v::decimal()` for float values (thanks @scruwi)
- Added `v::portugueseNif()` to validate _Número de Identificação Fiscal_ in Portugal (thanks @goncalo-andrade).
- Allow 5-digit and 6-digit postal codes for Cambodia (thanks @omega3000)
- `v::intval()` now handles negative values with trailing zeroes better (thanks @l-x)
# Changes in Respect\Validation 1.0

View file

@ -33,6 +33,7 @@ consider them as valid.
Version | Description
---------|-------------
2.2.4 | Improved support for negative values with trailing zeroes
2.0.14 | Allow leading zeros
1.0.0 | Renamed from `Int` to `IntVal`
0.3.9 | Created as `Int`

View file

@ -13,9 +13,9 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use function ctype_digit;
use function is_int;
use function is_string;
use function preg_match;
/**
* Validates if the input is an integer.
@ -37,6 +37,10 @@ final class IntVal extends AbstractRule
return true;
}
return is_string($input) && ctype_digit($input);
if (!is_string($input)) {
return false;
}
return preg_match('/^-?\d+$/', $input) === 1;
}
}

View file

@ -35,9 +35,23 @@ try {
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage() . PHP_EOL;
}
try {
v::not(v::intVal())->assert(-42);
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage() . PHP_EOL;
}
try {
v::not(v::intVal())->assert('-42');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage() . PHP_EOL;
}
?>
--EXPECT--
"42.33" must be an integer number
2 must not be an integer number
- "Foo" must be an integer number
- 3 must not be an integer number
- -42 must not be an integer number
- "-42" must not be an integer number

View file

@ -49,6 +49,9 @@ final class IntValTest extends RuleTestCase
[$rule, 0b101010],
[$rule, 0x2a],
[$rule, '089'],
[$rule, -42],
[$rule, '-42'],
[$rule, '-042'],
];
}