123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- namespace Drupal\Core\TypedData\Validation;
- use Drupal\Core\Validation\TranslatorInterface;
- use Symfony\Component\Validator\Constraint;
- use Symfony\Component\Validator\ConstraintViolation;
- use Symfony\Component\Validator\ConstraintViolationList;
- use Symfony\Component\Validator\Util\PropertyPath;
- use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
- /**
- * Defines a constraint violation builder for the Typed Data validator.
- *
- * We do not use the builder provided by Symfony as it is marked internal.
- *
- * @codingStandardsIgnoreStart
- */
- class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface {
- /**
- * The list of violations.
- *
- * @var \Symfony\Component\Validator\ConstraintViolationList
- */
- protected $violations;
- /**
- * The violation message.
- *
- * @var string
- */
- protected $message;
- /**
- * The message parameters.
- *
- * @var array
- */
- protected $parameters;
- /**
- * The root path.
- *
- * @var mixed
- */
- protected $root;
- /**
- * The invalid value caused the violation.
- *
- * @var mixed
- */
- protected $invalidValue;
- /**
- * The property path.
- *
- * @var string
- */
- protected $propertyPath;
- /**
- * The translator.
- *
- * @var \Drupal\Core\Validation\TranslatorInterface
- */
- protected $translator;
- /**
- * The translation domain.
- *
- * @var string|null
- */
- protected $translationDomain;
- /**
- * The number used
- * @var int|null
- */
- protected $plural;
- /**
- * @var Constraint
- */
- protected $constraint;
- /**
- * @var mixed
- */
- protected $code;
- /**
- * @var mixed
- */
- protected $cause;
- /**
- * Constructs a new ConstraintViolationBuilder instance.
- *
- * @param \Symfony\Component\Validator\ConstraintViolationList $violations
- * The violation list.
- * @param \Symfony\Component\Validator\Constraint $constraint
- * The constraint.
- * @param string $message
- * The message.
- * @param array $parameters
- * The message parameters.
- * @param mixed $root
- * The root.
- * @param string $propertyPath
- * The property string.
- * @param mixed $invalidValue
- * The invalid value.
- * @param \Drupal\Core\Validation\TranslatorInterface $translator
- * The translator.
- * @param null $translationDomain
- * (optional) The translation domain.
- */
- public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, TranslatorInterface $translator, $translationDomain = null)
- {
- $this->violations = $violations;
- $this->message = $message;
- $this->parameters = $parameters;
- $this->root = $root;
- $this->propertyPath = $propertyPath;
- $this->invalidValue = $invalidValue;
- $this->translator = $translator;
- $this->translationDomain = $translationDomain;
- $this->constraint = $constraint;
- }
- /**
- * {@inheritdoc}
- */
- public function atPath($path)
- {
- $this->propertyPath = PropertyPath::append($this->propertyPath, $path);
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function setParameter($key, $value)
- {
- $this->parameters[$key] = $value;
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function setParameters(array $parameters)
- {
- $this->parameters = $parameters;
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function setTranslationDomain($translationDomain)
- {
- $this->translationDomain = $translationDomain;
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function setInvalidValue($invalidValue)
- {
- $this->invalidValue = $invalidValue;
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function setPlural($number)
- {
- $this->plural = $number;
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function setCode($code)
- {
- $this->code = $code;
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function setCause($cause)
- {
- $this->cause = $cause;
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function addViolation()
- {
- if (null === $this->plural) {
- $translatedMessage = $this->translator->trans(
- $this->message,
- $this->parameters,
- $this->translationDomain
- );
- } else {
- try {
- $translatedMessage = $this->translator->transChoice(
- $this->message,
- $this->plural,
- $this->parameters,
- $this->translationDomain#
- );
- } catch (\InvalidArgumentException $e) {
- $translatedMessage = $this->translator->trans(
- $this->message,
- $this->parameters,
- $this->translationDomain
- );
- }
- }
- $this->violations->add(new ConstraintViolation(
- $translatedMessage,
- $this->message,
- $this->parameters,
- $this->root,
- $this->propertyPath,
- $this->invalidValue,
- $this->plural,
- $this->code,
- $this->constraint,
- $this->cause
- ));
- }
- }
|