4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator;
15 * Default implementation of {@ConstraintViolationInterface}.
17 * @author Bernhard Schussek <bschussek@gmail.com>
19 class ConstraintViolation implements ConstraintViolationInterface
29 private $messageTemplate;
49 private $propertyPath;
54 private $invalidValue;
57 * @var Constraint|null
72 * Creates a new constraint violation.
74 * @param string $message The violation message
75 * @param string $messageTemplate The raw violation message
76 * @param array $parameters The parameters to substitute in the
77 * raw violation message
78 * @param mixed $root The value originally passed to the
80 * @param string $propertyPath The property path from the root
81 * value to the invalid value
82 * @param mixed $invalidValue The invalid value that caused this
84 * @param int|null $plural The number for determining the plural
85 * form when translating the message
86 * @param mixed $code The error code of the violation
87 * @param Constraint|null $constraint The constraint whose validation
88 * caused the violation
89 * @param mixed $cause The cause of the violation
91 public function __construct($message, $messageTemplate, array $parameters, $root, $propertyPath, $invalidValue, $plural = null, $code = null, Constraint $constraint = null, $cause = null)
93 $this->message = $message;
94 $this->messageTemplate = $messageTemplate;
95 $this->parameters = $parameters;
96 $this->plural = $plural;
98 $this->propertyPath = $propertyPath;
99 $this->invalidValue = $invalidValue;
100 $this->constraint = $constraint;
102 $this->cause = $cause;
106 * Converts the violation into a string for debugging purposes.
108 * @return string The violation as string
110 public function __toString()
112 if (is_object($this->root)) {
113 $class = 'Object('.get_class($this->root).')';
114 } elseif (is_array($this->root)) {
117 $class = (string) $this->root;
120 $propertyPath = (string) $this->propertyPath;
123 if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) {
128 $code = ' (code '.$code.')';
131 return $class.$propertyPath.":\n ".$this->getMessage().$code;
137 public function getMessageTemplate()
139 return $this->messageTemplate;
145 public function getParameters()
147 return $this->parameters;
153 public function getPlural()
155 return $this->plural;
161 public function getMessage()
163 return $this->message;
169 public function getRoot()
177 public function getPropertyPath()
179 return $this->propertyPath;
185 public function getInvalidValue()
187 return $this->invalidValue;
191 * Returns the constraint whose validation caused the violation.
193 * @return Constraint|null The constraint or null if it is not known
195 public function getConstraint()
197 return $this->constraint;
201 * Returns the cause of the violation.
205 public function getCause()
213 public function getCode()