Skip to content

Commit d8613c7

Browse files
committed
[Validator] Only handle numeric values in DivisibleBy
1 parent 173b483 commit d8613c7

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Constraints/DivisibleByValidator.php

Lines 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
}

Tests/Constraints/DivisibleByValidatorTest.php

Lines 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 <[email protected]>
@@ -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->validator->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)