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\Violation;
14 use Symfony\Component\Translation\TranslatorInterface;
15 use Symfony\Component\Validator\Constraint;
16 use Symfony\Component\Validator\ConstraintViolation;
17 use Symfony\Component\Validator\ConstraintViolationList;
18 use Symfony\Component\Validator\Util\PropertyPath;
21 * Default implementation of {@link ConstraintViolationBuilderInterface}.
23 * @author Bernhard Schussek <bschussek@gmail.com>
25 * @internal You should not instantiate or use this class. Code against
26 * {@link ConstraintViolationBuilderInterface} instead.
28 class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
31 * @var ConstraintViolationList
53 private $invalidValue;
58 private $propertyPath;
61 * @var TranslatorInterface
68 private $translationDomain;
90 public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, TranslatorInterface $translator, $translationDomain = null)
92 $this->violations = $violations;
93 $this->message = $message;
94 $this->parameters = $parameters;
96 $this->propertyPath = $propertyPath;
97 $this->invalidValue = $invalidValue;
98 $this->translator = $translator;
99 $this->translationDomain = $translationDomain;
100 $this->constraint = $constraint;
106 public function atPath($path)
108 $this->propertyPath = PropertyPath::append($this->propertyPath, $path);
116 public function setParameter($key, $value)
118 $this->parameters[$key] = $value;
126 public function setParameters(array $parameters)
128 $this->parameters = $parameters;
136 public function setTranslationDomain($translationDomain)
138 $this->translationDomain = $translationDomain;
146 public function setInvalidValue($invalidValue)
148 $this->invalidValue = $invalidValue;
156 public function setPlural($number)
158 $this->plural = $number;
166 public function setCode($code)
176 public function setCause($cause)
178 $this->cause = $cause;
186 public function addViolation()
188 if (null === $this->plural) {
189 $translatedMessage = $this->translator->trans(
192 $this->translationDomain
196 $translatedMessage = $this->translator->transChoice(
200 $this->translationDomain
202 } catch (\InvalidArgumentException $e) {
203 $translatedMessage = $this->translator->trans(
206 $this->translationDomain
211 $this->violations->add(new ConstraintViolation(