123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- namespace Symfony\Component\Validator\Violation;
- use Symfony\Component\Translation\TranslatorInterface;
- use Symfony\Component\Validator\Constraint;
- use Symfony\Component\Validator\ConstraintViolation;
- use Symfony\Component\Validator\ConstraintViolationList;
- use Symfony\Component\Validator\Util\PropertyPath;
- class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
- {
- private $violations;
- private $message;
- private $parameters;
- private $root;
- private $invalidValue;
- private $propertyPath;
- private $translator;
- private $translationDomain;
- private $plural;
- private $constraint;
- private $code;
-
- private $cause;
- 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;
- }
-
- public function atPath($path)
- {
- $this->propertyPath = PropertyPath::append($this->propertyPath, $path);
- return $this;
- }
-
- public function setParameter($key, $value)
- {
- $this->parameters[$key] = $value;
- return $this;
- }
-
- public function setParameters(array $parameters)
- {
- $this->parameters = $parameters;
- return $this;
- }
-
- public function setTranslationDomain($translationDomain)
- {
- $this->translationDomain = $translationDomain;
- return $this;
- }
-
- public function setInvalidValue($invalidValue)
- {
- $this->invalidValue = $invalidValue;
- return $this;
- }
-
- public function setPlural($number)
- {
- $this->plural = $number;
- return $this;
- }
-
- public function setCode($code)
- {
- $this->code = $code;
- return $this;
- }
-
- public function setCause($cause)
- {
- $this->cause = $cause;
- return $this;
- }
-
- 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
- ));
- }
- }
|