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
22 private $messageTemplate;
26 private $propertyPath;
27 private $invalidValue;
33 * Creates a new constraint violation.
35 * @param string $message The violation message
36 * @param string $messageTemplate The raw violation message
37 * @param array $parameters The parameters to substitute in the
38 * raw violation message
39 * @param mixed $root The value originally passed to the
41 * @param string $propertyPath The property path from the root
42 * value to the invalid value
43 * @param mixed $invalidValue The invalid value that caused this
45 * @param int|null $plural The number for determining the plural
46 * form when translating the message
47 * @param mixed $code The error code of the violation
48 * @param Constraint|null $constraint The constraint whose validation
49 * caused the violation
50 * @param mixed $cause The cause of the violation
52 public function __construct($message, $messageTemplate, array $parameters, $root, $propertyPath, $invalidValue, $plural = null, $code = null, Constraint $constraint = null, $cause = null)
54 $this->message = $message;
55 $this->messageTemplate = $messageTemplate;
56 $this->parameters = $parameters;
57 $this->plural = $plural;
59 $this->propertyPath = $propertyPath;
60 $this->invalidValue = $invalidValue;
61 $this->constraint = $constraint;
63 $this->cause = $cause;
67 * Converts the violation into a string for debugging purposes.
69 * @return string The violation as string
71 public function __toString()
73 if (\is_object($this->root)) {
74 $class = 'Object('.\get_class($this->root).')';
75 } elseif (\is_array($this->root)) {
78 $class = (string) $this->root;
81 $propertyPath = (string) $this->propertyPath;
84 if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) {
89 $code = ' (code '.$code.')';
92 return $class.$propertyPath.":\n ".$this->getMessage().$code;
98 public function getMessageTemplate()
100 return $this->messageTemplate;
106 public function getParameters()
108 return $this->parameters;
114 public function getPlural()
116 return $this->plural;
122 public function getMessage()
124 return $this->message;
130 public function getRoot()
138 public function getPropertyPath()
140 return $this->propertyPath;
146 public function getInvalidValue()
148 return $this->invalidValue;
152 * Returns the constraint whose validation caused the violation.
154 * @return Constraint|null The constraint or null if it is not known
156 public function getConstraint()
158 return $this->constraint;
162 * Returns the cause of the violation.
166 public function getCause()
174 public function getCode()