8000 [Validator] Improve DivisibleBy validator by apfelbox · Pull Request #28212 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Improve DivisibleBy validator #28212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,26 @@ class DivisibleByValidator extends AbstractComparisonValidator
*/
protected function compareValues($value1, $value2)
{
return (float) 0 === fmod($value1, $value2);
$value1 = \abs($value1);
$value2 = \abs($value2);
$epsilon = 0.0000001;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about making this configurable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I‘m not sure whether this would actually add anything.

Might make sense to add something like FloatUtils to capsiluate some of this logic (comparing floats etc)


// can't divide by 0
if ($value2 < $epsilon) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this trigger an exception when the constraint is configured with such an invalid value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. The previous implementation with fmod just returned NaN (so the validator failed).

We can change it, I just adapted the previous behavior.

return false;
}

// 0 is divisible by everything
if ($value1 < $epsilon) {
return true;
}

// if the divisor is larger than the dividend, it will never cleanly divide
if ($value2 > $value1) {
return false;
}

return \abs($value1 - round($value1 / $value2) * $value2) < $epsilon;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function provideValidComparisons()
array(42, 21),
array(3.25, 0.25),
array('100', '10'),
array(4.1, 0.1),
);
}

Expand All @@ -69,6 +70,7 @@ public function provideInvalidComparisons()
array(10, '10', 3, '3', 'integer'),
array(10, '10', 0, '0', 'integer'),
array(42, '42', INF, 'INF', 'double'),
array(4.15, '4.15', 0.1, '0.1', 'double'),
array('22', '"22"', '10', '"10"', 'string'),
);
}
Expand Down
0