Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Validator] Only handle numeric values in DivisibleBy #33435

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

Merged
Merged
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 @@ -11,6 +11,8 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* Validates that values are a multiple of the given number.
*
Expand All @@ -23,6 +25,14 @@ class DivisibleByValidator extends AbstractComparisonValidator
*/
protected function compareValues($value1, $value2)
{
if (!is_numeric($value1)) {
throw new UnexpectedValueException($value1, 'numeric');
}

if (!is_numeric($value2)) {
throw new UnexpectedValueException($value2, 'numeric');
}

if (!$value2 = abs($value2)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Validator\Constraints\DivisibleBy;
use Symfony\Component\Validator\Constraints\DivisibleByValidator;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* @author Colin O'Dell <colinodell@gmail.com>
Expand Down Expand Up @@ -75,4 +76,25 @@ public function provideInvalidComparisons()
['22', '"22"', '10', '"10"', 'string'],
];
}

/**
* @dataProvider throwsOnNonNumericValuesProvider
*/
public function testThrowsOnNonNumericValues(string $expectedGivenType, $value, $comparedValue)
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage(sprintf('Expected argument of type "numeric", "%s" given', $expectedGivenType));

$this->validator->validate($value, $this->createConstraint([
'value' => $comparedValue,
]));
}

public function throwsOnNonNumericValuesProvider()
{
return [
[\stdClass::class, 2, new \stdClass()],
[\ArrayIterator::class, new \ArrayIterator(), 12],
];
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.