ConstraintViolationBuilder.php
4 years ago
ConstraintViolationBuilderInterface.php
4 years ago
index.php
3 years ago
ConstraintViolationBuilder.php
85 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Validator\Violation; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\Validator\Constraint; |
| 5 | use MailPoetVendor\Symfony\Component\Validator\ConstraintViolation; |
| 6 | use MailPoetVendor\Symfony\Component\Validator\ConstraintViolationList; |
| 7 | use MailPoetVendor\Symfony\Component\Validator\Util\PropertyPath; |
| 8 | use MailPoetVendor\Symfony\Contracts\Translation\TranslatorInterface; |
| 9 | class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface |
| 10 | { |
| 11 | private $violations; |
| 12 | private $message; |
| 13 | private $parameters; |
| 14 | private $root; |
| 15 | private $invalidValue; |
| 16 | private $propertyPath; |
| 17 | private $translator; |
| 18 | private $translationDomain; |
| 19 | private $plural; |
| 20 | private $constraint; |
| 21 | private $code; |
| 22 | private $cause; |
| 23 | public function __construct(ConstraintViolationList $violations, ?Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, TranslatorInterface $translator, $translationDomain = null) |
| 24 | { |
| 25 | $this->violations = $violations; |
| 26 | $this->message = $message; |
| 27 | $this->parameters = $parameters; |
| 28 | $this->root = $root; |
| 29 | $this->propertyPath = $propertyPath; |
| 30 | $this->invalidValue = $invalidValue; |
| 31 | $this->translator = $translator; |
| 32 | $this->translationDomain = $translationDomain; |
| 33 | $this->constraint = $constraint; |
| 34 | } |
| 35 | public function atPath(string $path) |
| 36 | { |
| 37 | $this->propertyPath = PropertyPath::append($this->propertyPath, $path); |
| 38 | return $this; |
| 39 | } |
| 40 | public function setParameter(string $key, string $value) |
| 41 | { |
| 42 | $this->parameters[$key] = $value; |
| 43 | return $this; |
| 44 | } |
| 45 | public function setParameters(array $parameters) |
| 46 | { |
| 47 | $this->parameters = $parameters; |
| 48 | return $this; |
| 49 | } |
| 50 | public function setTranslationDomain(string $translationDomain) |
| 51 | { |
| 52 | $this->translationDomain = $translationDomain; |
| 53 | return $this; |
| 54 | } |
| 55 | public function setInvalidValue($invalidValue) |
| 56 | { |
| 57 | $this->invalidValue = $invalidValue; |
| 58 | return $this; |
| 59 | } |
| 60 | public function setPlural(int $number) |
| 61 | { |
| 62 | $this->plural = $number; |
| 63 | return $this; |
| 64 | } |
| 65 | public function setCode(?string $code) |
| 66 | { |
| 67 | $this->code = $code; |
| 68 | return $this; |
| 69 | } |
| 70 | public function setCause($cause) |
| 71 | { |
| 72 | $this->cause = $cause; |
| 73 | return $this; |
| 74 | } |
| 75 | public function addViolation() |
| 76 | { |
| 77 | if (null === $this->plural) { |
| 78 | $translatedMessage = $this->translator->trans($this->message, $this->parameters, $this->translationDomain); |
| 79 | } else { |
| 80 | $translatedMessage = $this->translator->trans($this->message, ['%count%' => $this->plural] + $this->parameters, $this->translationDomain); |
| 81 | } |
| 82 | $this->violations->add(new ConstraintViolation($translatedMessage, $this->message, $this->parameters, $this->root, $this->propertyPath, $this->invalidValue, $this->plural, $this->code, $this->constraint, $this->cause)); |
| 83 | } |
| 84 | } |
| 85 |