From d774fe35c31715b3246be29c31271da8dda325c0 Mon Sep 17 00:00:00 2001 From: "charly.terrier" Date: Sun, 13 Mar 2022 20:47:22 +0100 Subject: [PATCH] [Validator] fix #43345 @Assert\DivisibleBy --- .../Validator/Constraints/DivisibleByValidator.php | 6 ++++++ .../Tests/Constraints/DivisibleByValidatorTest.php | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/Symfony/Component/Validator/Constraints/DivisibleByValidator.php b/src/Symfony/Component/Validator/Constraints/DivisibleByValidator.php index 53b8d38930c90..de7743010b354 100644 --- a/src/Symfony/Component/Validator/Constraints/DivisibleByValidator.php +++ b/src/Symfony/Component/Validator/Constraints/DivisibleByValidator.php @@ -42,6 +42,12 @@ protected function compareValues($value1, $value2) if (!$remainder = fmod($value1, $value2)) { return true; } + if (\is_float($value2) && \INF !== $value2) { + $quotient = $value1 / $value2; + $rounded = round($quotient); + + return sprintf('%.12e', $quotient) === sprintf('%.12e', $rounded); + } return sprintf('%.12e', $value2) === sprintf('%.12e', $remainder); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DivisibleByValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DivisibleByValidatorTest.php index 7612ada32b530..4ce2723c0d845 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DivisibleByValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DivisibleByValidatorTest.php @@ -46,6 +46,18 @@ public function provideValidComparisons(): array [0, 3.1415], [42, 42], [42, 21], + [10.12, 0.01], + [10.12, 0.001], + [1.133, 0.001], + [1.1331, 0.0001], + [1.13331, 0.00001], + [1.13331, 0.000001], + [1, 0.1], + [1, 0.01], + [1, 0.001], + [1, 0.0001], + [1, 0.00001], + [1, 0.000001], [3.25, 0.25], ['100', '10'], [4.1, 0.1], @@ -74,6 +86,7 @@ public function provideInvalidComparisons(): array [10, '10', 0, '0', 'int'], [42, '42', \INF, 'INF', 'float'], [4.15, '4.15', 0.1, '0.1', 'float'], + [10.123, '10.123', 0.01, '0.01', 'float'], ['22', '"22"', '10', '"10"', 'string'], ]; }