8000 [2.2][Validator] add support of arrays and Countables to Size Validator. by makasim · Pull Request #3918 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[2.2][Validator] add support of arrays and Countables to Size Validator. #3918

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
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
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/SizeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function isValid($value, Constraint $constraint)
return true;
}

if (is_array($value) || $value instanceof \Countable) {
$value = count($value);
}

if (!is_numeric($value)) {
$this->context->addViolation($constraint->invalidMessage, array(
'{{ value }}' => $value,
Expand Down
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public function testValidValues($value)

public function getValidValues()
{
$array = range(1, 15);
$countableMock = $this->getMock('Countable');
$countableMock
->expects($this->any())
->method('count')
->will($this->returnValue(15))
;

return array(
array(10.00001),
array(19.99999),
Expand All @@ -57,6 +65,8 @@ public function getValidValues()
array(20),
array(10.0),
array(20.0),
array($array),
array($countableMock)
);
}

Expand All @@ -74,12 +84,31 @@ public function testInvalidValues($value)

public function getInvalidValues()
{
$smallerArray = range(1, 9);
$biggerArray = range(1, 21);
$smallerCountableMock = $this->getMock('Countable');
$smallerCountableMock
->expects($this->any())
->method('count')
->will($this->returnValue(9))
;
$biggerCountableMock = $this->getMock('Countable');
$biggerCountableMock
->expects($this->any())
->method('count')
->will($this->returnValue(21))
;

return array(
array(9.999999),
array(20.000001),
array('9.999999'),
array('20.000001'),
array(new \stdClass()),
array($smallerArray),
array($biggerArray),
array($smallerCountableMock),
array($biggerCountableMock)
);
}

Expand Down
0