Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1d1dcf8

Browse files
committed
bug #45733 [Validator] fix #43345 @Assert\DivisibleBy (CharlyPoppins)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Validator] fix #43345 @Assert\DivisibleBy | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #43345| License | MIT `@Assert\DivisibleBy` was not working with 0.01 and smaller divider. Commits ------- d774fe3 [Validator] fix #43345 @Assert\DivisibleBy
2 parents 4efd3ce + d774fe3 commit 1d1dcf8

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/Symfony/Component/Validator/Constraints/DivisibleByValidator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ protected function compareValues($value1, $value2)
4242
if (!$remainder = fmod($value1, $value2)) {
4343
return true;
4444
}
45+
if (\is_float($value2) && \INF !== $value2) {
46+
$quotient = $value1 / $value2;
47+
$rounded = round($quotient);
48+
49+
return sprintf('%.12e', $quotient) === sprintf('%.12e', $rounded);
50+
}
4551

4652
return sprintf('%.12e', $value2) === sprintf('%.12e', $remainder);
4753
}

src/Symfony/Component/Validator/Tests/Constraints/DivisibleByValidatorTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ public function provideValidComparisons(): array
4646
[0, 3.1415],
4747
[42, 42],
4848
[42, 21],
49+
[10.12, 0.01],
50+
[10.12, 0.001],
51+
[1.133, 0.001],
52+
[1.1331, 0.0001],
53+
[1.13331, 0.00001],
54+
[1.13331, 0.000001],
55+
[1, 0.1],
56+
[1, 0.01],
57+
[1, 0.001],
58+
[1, 0.0001],
59+
[1, 0.00001],
60+
[1, 0.000001],
4961
[3.25, 0.25],
5062
['100', '10'],
5163
[4.1, 0.1],
@@ -74,6 +86,7 @@ public function provideInvalidComparisons(): array
7486
[10, '10', 0, '0', 'int'],
7587
[42, '42', \INF, 'INF', 'float'],
7688
[4.15, '4.15', 0.1, '0.1', 'float'],
89+
[10.123, '10.123', 0.01, '0.01', 'float'],
7790
['22', '"22"', '10', '"10"', 'string'],
7891
];
7992
}

0 commit comments

Comments
 (0)