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

Commit 13e8b44

Browse filesBrowse files
committed
[Validator] Only handle numeric values in DivisibleBy
1 parent 61dad16 commit 13e8b44
Copy full SHA for 13e8b44

File tree

2 files changed

+32
-0
lines changed
Filter options

2 files changed

+32
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/DivisibleByValidator.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
15+
1416
/**
1517
* Validates that values are a multiple of the given number.
1618
*
@@ -23,6 +25,14 @@ class DivisibleByValidator extends AbstractComparisonValidator
2325
*/
2426
protected function compareValues($value1, $value2)
2527
{
28+
if (!is_numeric($value1)) {
29+
throw new UnexpectedValueException($value1, 'numeric');
30+
}
31+
32+
if (!is_numeric($value2)) {
33+
throw new UnexpectedValueException($value2, 'numeric');
34+
}
35+
2636
if (!$value2 = abs($value2)) {
2737
return false;
2838
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/DivisibleByValidatorTest.php
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Constraints\DivisibleBy;
1515
use Symfony\Component\Validator\Constraints\DivisibleByValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1617

1718
/**
1819
* @author Colin O'Dell <colinodell@gmail.com>
@@ -75,4 +76,25 @@ public function provideInvalidComparisons()
7576
['22', '"22"', '10', '"10"', 'string'],
7677
];
7778
}
79+
80+
/**
81+
* @dataProvider throwsOnNonNumericValuesProvider
82+
*/
83+
public function testThrowsOnNonNumericValues(string $expectedGivenType, $value, $comparedValue)
84+
{
85+
$this->expectException(UnexpectedValueException::class);
86+
$this->expectExceptionMessage(sprintf('Expected argument of type "numeric", "%s" given', $expectedGivenType));
87+
88+
$this->createValidator()->validate($value, $this->createConstraint([
89+
'value' => $comparedValue,
90+
]));
91+
}
92+
93+
public function throwsOnNonNumericValuesProvider()
94+
{
95+
return [
96+
[\stdClass::class, 2, new \stdClass()],
97+
[\ArrayIterator::class, new \ArrayIterator(), 12],
98+
];
99+
}
78100
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.