From ca71ffeff09248c728758de5ae403295afc62d17 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Tue, 18 Aug 2020 19:30:43 +0200 Subject: [PATCH 1/2] a float that is forcible to int without loss of information is valid. --- src/Constraint/Type/IntegerNumberValidator.php | 5 +++++ tests/Unit/Constraint/Type/IntegerNumberValidatorTest.php | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Constraint/Type/IntegerNumberValidator.php b/src/Constraint/Type/IntegerNumberValidator.php index 29065af..b4c9bfa 100644 --- a/src/Constraint/Type/IntegerNumberValidator.php +++ b/src/Constraint/Type/IntegerNumberValidator.php @@ -22,6 +22,11 @@ public function validate($value, Constraint $constraint): void return; } + // if value is float, force to string. This will cast 5.0 => '5', and 5.5 => '5.5' + if (is_float($value)) { + $value = (string)$value; + } + // value should be either int or string if (is_string($value) === false) { $this->context->buildViolation($constraint->message) diff --git a/tests/Unit/Constraint/Type/IntegerNumberValidatorTest.php b/tests/Unit/Constraint/Type/IntegerNumberValidatorTest.php index f1d974a..199cebb 100644 --- a/tests/Unit/Constraint/Type/IntegerNumberValidatorTest.php +++ b/tests/Unit/Constraint/Type/IntegerNumberValidatorTest.php @@ -13,7 +13,6 @@ use Symfony\Component\Validator\Context\ExecutionContext; use Symfony\Component\Validator\Exception\UnexpectedTypeException; use Symfony\Component\Validator\Validation; -use Symfony\Contracts\Translation\TranslatorInterface; /** * @coversDefaultClass \DigitalRevolution\SymfonyValidationShorthand\Constraint\Type\IntegerNumberValidator @@ -72,11 +71,13 @@ public function dataProvider(): array '-1' => ['-1', 0], 'int 1' => [1, 0], 'int 0' => [0, 0], + 'float 5.0' => [5.0, 0], // failures '' => ['', 1], 'a' => ['a', 1], '0 prefix' => ['01', 1], 'true' => ['true', 1], + 'float 5.5' => [5.5, 1], 'bool true' => [true, 1], '-' => ['-', 1], 'max int' => [PHP_INT_MAX . '2', 1] From 58a9103ff63d794a783516a61bd6bd9ae6b080aa Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Tue, 18 Aug 2020 21:39:07 +0200 Subject: [PATCH 2/2] a float that is forcible to int without loss of information is valid. --- src/Constraint/Type/IntegerNumberValidator.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/Constraint/Type/IntegerNumberValidator.php b/src/Constraint/Type/IntegerNumberValidator.php index b4c9bfa..6d752d3 100644 --- a/src/Constraint/Type/IntegerNumberValidator.php +++ b/src/Constraint/Type/IntegerNumberValidator.php @@ -22,13 +22,8 @@ public function validate($value, Constraint $constraint): void return; } - // if value is float, force to string. This will cast 5.0 => '5', and 5.5 => '5.5' - if (is_float($value)) { - $value = (string)$value; - } - - // value should be either int or string - if (is_string($value) === false) { + // value should be either float or string + if (is_string($value) === false && is_float($value) === false) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) ->setCode($constraint::INVALID_VALUE_TYPE) @@ -37,7 +32,7 @@ public function validate($value, Constraint $constraint): void } // value can't be cast to int - if (((string)(int)$value) !== $value) { + if (((string)(int)$value) !== (string)$value) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) ->setCode($constraint::INVALID_NUMBER_ERROR)