handle array root element

An array to string conversion notice was thrown when the root element of
the thing being validated is an array.
This commit is contained in:
Grégoire Paris 2014-01-24 18:40:09 +01:00 committed by Fabien Potencier
parent ca5eea5c19
commit 17ed8bf563
2 changed files with 27 additions and 1 deletions

View File

@ -95,7 +95,14 @@ class ConstraintViolation implements ConstraintViolationInterface
*/
public function __toString()
{
$class = (string) (is_object($this->root) ? get_class($this->root) : $this->root);
if (is_object($this->root)) {
$class = get_class($this->root);
} elseif (is_array($this->root)) {
$class = "Array";
} else {
$class = (string) $this->root;
}
$propertyPath = (string) $this->propertyPath;
$code = $this->code;

View File

@ -33,4 +33,23 @@ EOF;
$this->assertSame($expected, (string) $violation);
}
public function testToStringHandlesArrayRoots()
{
$violation = new ConstraintViolation(
'42 cannot be used here',
'this is the message template',
array(),
array('some_value' => 42),
'some_value',
null
);
$expected = <<<EOF
Array.some_value:
42 cannot be used here
EOF;
$this->assertSame($expected, (string) $violation);
}
}