From 17ed8bf56350aa344560abcc1811028593c091d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Fri, 24 Jan 2014 18:40:09 +0100 Subject: [PATCH] handle array root element An array to string conversion notice was thrown when the root element of the thing being validated is an array. --- .../Validator/ConstraintViolation.php | 9 ++++++++- .../Tests/ConstraintViolationTest.php | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/ConstraintViolation.php b/src/Symfony/Component/Validator/ConstraintViolation.php index ca8d525f67..36a42aaec4 100644 --- a/src/Symfony/Component/Validator/ConstraintViolation.php +++ b/src/Symfony/Component/Validator/ConstraintViolation.php @@ -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; diff --git a/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php b/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php index e1f06c2428..2ceb0169a1 100644 --- a/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php +++ b/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php @@ -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 = <<assertSame($expected, (string) $violation); + } }