3 namespace Drupal\Core\TypedData\Validation;
5 use Drupal\Core\Validation\TranslatorInterface;
6 use Symfony\Component\Validator\Constraint;
7 use Symfony\Component\Validator\ConstraintViolation;
8 use Symfony\Component\Validator\ConstraintViolationList;
9 use Symfony\Component\Validator\Util\PropertyPath;
10 use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
13 * Defines a constraint violation builder for the Typed Data validator.
15 * We do not use the builder provided by Symfony as it is marked internal.
17 * @codingStandardsIgnoreStart
19 class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface {
22 * The list of violations.
24 * @var \Symfony\Component\Validator\ConstraintViolationList
26 protected $violations;
29 * The violation message.
36 * The message parameters.
40 protected $parameters;
50 * The invalid value caused the violation.
54 protected $invalidValue;
61 protected $propertyPath;
66 * @var \Drupal\Core\Validation\TranslatorInterface
68 protected $translator;
71 * The translation domain.
75 protected $translationDomain;
86 protected $constraint;
99 * Constructs a new ConstraintViolationBuilder instance.
101 * @param \Symfony\Component\Validator\ConstraintViolationList $violations
102 * The violation list.
103 * @param \Symfony\Component\Validator\Constraint $constraint
105 * @param string $message
107 * @param array $parameters
108 * The message parameters.
111 * @param string $propertyPath
112 * The property string.
113 * @param mixed $invalidValue
115 * @param \Drupal\Core\Validation\TranslatorInterface $translator
117 * @param null $translationDomain
118 * (optional) The translation domain.
120 public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, TranslatorInterface $translator, $translationDomain = null)
122 $this->violations = $violations;
123 $this->message = $message;
124 $this->parameters = $parameters;
126 $this->propertyPath = $propertyPath;
127 $this->invalidValue = $invalidValue;
128 $this->translator = $translator;
129 $this->translationDomain = $translationDomain;
130 $this->constraint = $constraint;
136 public function atPath($path)
138 $this->propertyPath = PropertyPath::append($this->propertyPath, $path);
146 public function setParameter($key, $value)
148 $this->parameters[$key] = $value;
156 public function setParameters(array $parameters)
158 $this->parameters = $parameters;
166 public function setTranslationDomain($translationDomain)
168 $this->translationDomain = $translationDomain;
176 public function setInvalidValue($invalidValue)
178 $this->invalidValue = $invalidValue;
186 public function setPlural($number)
188 $this->plural = $number;
196 public function setCode($code)
206 public function setCause($cause)
208 $this->cause = $cause;
216 public function addViolation()
218 if (null === $this->plural) {
219 $translatedMessage = $this->translator->trans(
222 $this->translationDomain
226 $translatedMessage = $this->translator->transChoice(
230 $this->translationDomain#
232 } catch (\InvalidArgumentException $e) {
233 $translatedMessage = $this->translator->trans(
236 $this->translationDomain
241 $this->violations->add(new ConstraintViolation(