ExecutionContext.php
1 year ago
ExecutionContextFactory.php
1 year ago
ExecutionContextFactoryInterface.php
4 years ago
ExecutionContextInterface.php
1 year ago
index.php
3 years ago
ExecutionContext.php
153 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Validator\Context; |
| 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\ConstraintViolationListInterface; |
| 8 | use MailPoetVendor\Symfony\Component\Validator\Mapping\ClassMetadataInterface; |
| 9 | use MailPoetVendor\Symfony\Component\Validator\Mapping\MemberMetadata; |
| 10 | use MailPoetVendor\Symfony\Component\Validator\Mapping\MetadataInterface; |
| 11 | use MailPoetVendor\Symfony\Component\Validator\Mapping\PropertyMetadataInterface; |
| 12 | use MailPoetVendor\Symfony\Component\Validator\Util\PropertyPath; |
| 13 | use MailPoetVendor\Symfony\Component\Validator\Validator\LazyProperty; |
| 14 | use MailPoetVendor\Symfony\Component\Validator\Validator\ValidatorInterface; |
| 15 | use MailPoetVendor\Symfony\Component\Validator\Violation\ConstraintViolationBuilder; |
| 16 | use MailPoetVendor\Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface; |
| 17 | use MailPoetVendor\Symfony\Contracts\Translation\TranslatorInterface; |
| 18 | class ExecutionContext implements ExecutionContextInterface |
| 19 | { |
| 20 | private $validator; |
| 21 | private $root; |
| 22 | private $translator; |
| 23 | private $translationDomain; |
| 24 | private $violations; |
| 25 | private $value; |
| 26 | private $object; |
| 27 | private $propertyPath = ''; |
| 28 | private $metadata; |
| 29 | private $group; |
| 30 | private $constraint; |
| 31 | private $validatedObjects = []; |
| 32 | private $validatedConstraints = []; |
| 33 | private $initializedObjects; |
| 34 | private $cachedObjectsRefs; |
| 35 | public function __construct(ValidatorInterface $validator, $root, TranslatorInterface $translator, ?string $translationDomain = null) |
| 36 | { |
| 37 | $this->validator = $validator; |
| 38 | $this->root = $root; |
| 39 | $this->translator = $translator; |
| 40 | $this->translationDomain = $translationDomain; |
| 41 | $this->violations = new ConstraintViolationList(); |
| 42 | $this->cachedObjectsRefs = new \SplObjectStorage(); |
| 43 | } |
| 44 | public function setNode($value, ?object $object, ?MetadataInterface $metadata, string $propertyPath) |
| 45 | { |
| 46 | $this->value = $value; |
| 47 | $this->object = $object; |
| 48 | $this->metadata = $metadata; |
| 49 | $this->propertyPath = $propertyPath; |
| 50 | } |
| 51 | public function setGroup(?string $group) |
| 52 | { |
| 53 | $this->group = $group; |
| 54 | } |
| 55 | public function setConstraint(Constraint $constraint) |
| 56 | { |
| 57 | $this->constraint = $constraint; |
| 58 | } |
| 59 | public function addViolation(string $message, array $parameters = []) |
| 60 | { |
| 61 | $this->violations->add(new ConstraintViolation($this->translator->trans($message, $parameters, $this->translationDomain), $message, $parameters, $this->root, $this->propertyPath, $this->getValue(), null, null, $this->constraint)); |
| 62 | } |
| 63 | public function buildViolation(string $message, array $parameters = []) : ConstraintViolationBuilderInterface |
| 64 | { |
| 65 | return new ConstraintViolationBuilder($this->violations, $this->constraint, $message, $parameters, $this->root, $this->propertyPath, $this->getValue(), $this->translator, $this->translationDomain); |
| 66 | } |
| 67 | public function getViolations() : ConstraintViolationListInterface |
| 68 | { |
| 69 | return $this->violations; |
| 70 | } |
| 71 | public function getValidator() : ValidatorInterface |
| 72 | { |
| 73 | return $this->validator; |
| 74 | } |
| 75 | public function getRoot() |
| 76 | { |
| 77 | return $this->root; |
| 78 | } |
| 79 | public function getValue() |
| 80 | { |
| 81 | if ($this->value instanceof LazyProperty) { |
| 82 | return $this->value->getPropertyValue(); |
| 83 | } |
| 84 | return $this->value; |
| 85 | } |
| 86 | public function getObject() |
| 87 | { |
| 88 | return $this->object; |
| 89 | } |
| 90 | public function getMetadata() : ?MetadataInterface |
| 91 | { |
| 92 | return $this->metadata; |
| 93 | } |
| 94 | public function getGroup() : ?string |
| 95 | { |
| 96 | return $this->group; |
| 97 | } |
| 98 | public function getConstraint() : ?Constraint |
| 99 | { |
| 100 | return $this->constraint; |
| 101 | } |
| 102 | public function getClassName() : ?string |
| 103 | { |
| 104 | return $this->metadata instanceof MemberMetadata || $this->metadata instanceof ClassMetadataInterface ? $this->metadata->getClassName() : null; |
| 105 | } |
| 106 | public function getPropertyName() : ?string |
| 107 | { |
| 108 | return $this->metadata instanceof PropertyMetadataInterface ? $this->metadata->getPropertyName() : null; |
| 109 | } |
| 110 | public function getPropertyPath(string $subPath = '') : string |
| 111 | { |
| 112 | return PropertyPath::append($this->propertyPath, $subPath); |
| 113 | } |
| 114 | public function markGroupAsValidated(string $cacheKey, string $groupHash) |
| 115 | { |
| 116 | if (!isset($this->validatedObjects[$cacheKey])) { |
| 117 | $this->validatedObjects[$cacheKey] = []; |
| 118 | } |
| 119 | $this->validatedObjects[$cacheKey][$groupHash] = \true; |
| 120 | } |
| 121 | public function isGroupValidated(string $cacheKey, string $groupHash) : bool |
| 122 | { |
| 123 | return isset($this->validatedObjects[$cacheKey][$groupHash]); |
| 124 | } |
| 125 | public function markConstraintAsValidated(string $cacheKey, string $constraintHash) |
| 126 | { |
| 127 | $this->validatedConstraints[$cacheKey . ':' . $constraintHash] = \true; |
| 128 | } |
| 129 | public function isConstraintValidated(string $cacheKey, string $constraintHash) : bool |
| 130 | { |
| 131 | return isset($this->validatedConstraints[$cacheKey . ':' . $constraintHash]); |
| 132 | } |
| 133 | public function markObjectAsInitialized(string $cacheKey) |
| 134 | { |
| 135 | $this->initializedObjects[$cacheKey] = \true; |
| 136 | } |
| 137 | public function isObjectInitialized(string $cacheKey) : bool |
| 138 | { |
| 139 | return isset($this->initializedObjects[$cacheKey]); |
| 140 | } |
| 141 | public function generateCacheKey(object $object) : string |
| 142 | { |
| 143 | if (!isset($this->cachedObjectsRefs[$object])) { |
| 144 | $this->cachedObjectsRefs[$object] = \spl_object_hash($object); |
| 145 | } |
| 146 | return $this->cachedObjectsRefs[$object]; |
| 147 | } |
| 148 | public function __clone() |
| 149 | { |
| 150 | $this->violations = clone $this->violations; |
| 151 | } |
| 152 | } |
| 153 |