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 since version 2.5. Code against ConstraintViolationBuilderInterface instead.
27 class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
33 private $invalidValue;
34 private $propertyPath;
36 private $translationDomain;
46 public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, TranslatorInterface $translator, $translationDomain = null)
48 $this->violations = $violations;
49 $this->message = $message;
50 $this->parameters = $parameters;
52 $this->propertyPath = $propertyPath;
53 $this->invalidValue = $invalidValue;
54 $this->translator = $translator;
55 $this->translationDomain = $translationDomain;
56 $this->constraint = $constraint;
62 public function atPath($path)
64 $this->propertyPath = PropertyPath::append($this->propertyPath, $path);
72 public function setParameter($key, $value)
74 $this->parameters[$key] = $value;
82 public function setParameters(array $parameters)
84 $this->parameters = $parameters;
92 public function setTranslationDomain($translationDomain)
94 $this->translationDomain = $translationDomain;
102 public function setInvalidValue($invalidValue)
104 $this->invalidValue = $invalidValue;
112 public function setPlural($number)
114 $this->plural = $number;
122 public function setCode($code)
132 public function setCause($cause)
134 $this->cause = $cause;
142 public function addViolation()
144 if (null === $this->plural) {
145 $translatedMessage = $this->translator->trans(
148 $this->translationDomain
152 $translatedMessage = $this->translator->transChoice(
156 $this->translationDomain
158 } catch (\InvalidArgumentException $e) {
159 $translatedMessage = $this->translator->trans(
162 $this->translationDomain
167 $this->violations->add(new ConstraintViolation(