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 * @deprecated since version 2.7, to be removed in 3.0.
146 * Use getParameters() instead
148 public function getMessageParameters()
150 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.7, to be removed in 3.0. Use the ConstraintViolation::getParameters() method instead.', E_USER_DEPRECATED);
152 return $this->parameters;
156 * Alias of {@link getMessageParameters()}.
158 public function getParameters()
160 return $this->parameters;
166 * @deprecated since version 2.7, to be removed in 3.0.
167 * Use getPlural() instead
169 public function getMessagePluralization()
171 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.7, to be removed in 3.0. Use the ConstraintViolation::getPlural() method instead.', E_USER_DEPRECATED);
173 return $this->plural;
177 * Alias of {@link getMessagePluralization()}.
179 public function getPlural()
181 return $this->plural;
187 public function getMessage()
189 return $this->message;
195 public function getRoot()
203 public function getPropertyPath()
205 return $this->propertyPath;
211 public function getInvalidValue()
213 return $this->invalidValue;
217 * Returns the constraint whose validation caused the violation.
219 * @return Constraint|null The constraint or null if it is not known
221 public function getConstraint()
223 return $this->constraint;
227 * Returns the cause of the violation.
231 public function getCause()
239 public function getCode()