class ExecutionContext

Same name in this branch
  1. 10 core/lib/Drupal/Core/TypedData/Validation/ExecutionContext.php \Drupal\Core\TypedData\Validation\ExecutionContext
Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/TypedData/Validation/ExecutionContext.php \Drupal\Core\TypedData\Validation\ExecutionContext
  2. 8.9.x core/lib/Drupal/Core/TypedData/Validation/ExecutionContext.php \Drupal\Core\TypedData\Validation\ExecutionContext
  3. 11.x core/lib/Drupal/Core/TypedData/Validation/ExecutionContext.php \Drupal\Core\TypedData\Validation\ExecutionContext
  4. 11.x core/lib/Drupal/Core/Validation/ExecutionContext.php \Drupal\Core\Validation\ExecutionContext

Defines an execution context class.

We do not use the context provided by Symfony as it is marked internal, so this class is pretty much the same, but has some code style changes as well as exceptions for methods we don't support.

Hierarchy

  • class \Drupal\Core\Validation\ExecutionContext implements \Symfony\Component\Validator\Context\ExecutionContextInterface

Expanded class hierarchy of ExecutionContext

2 files declare their use of ExecutionContext
ExecutionContext.php in core/lib/Drupal/Core/TypedData/Validation/ExecutionContext.php
PrecedingConstraintAwareValidatorTrait.php in core/modules/ckeditor5/src/Plugin/Validation/Constraint/PrecedingConstraintAwareValidatorTrait.php

File

core/lib/Drupal/Core/Validation/ExecutionContext.php, line 23

Namespace

Drupal\Core\Validation
View source
class ExecutionContext implements ExecutionContextInterface {
    
    /**
     * The violations generated in the current context.
     */
    protected ConstraintViolationList $violations;
    
    /**
     * The currently validated value.
     */
    protected mixed $value = NULL;
    
    /**
     * The currently validated object.
     */
    protected ?object $object = NULL;
    
    /**
     * The property path leading to the current value.
     */
    protected string $propertyPath = '';
    
    /**
     * The current validation metadata.
     */
    protected ?MetadataInterface $metadata = NULL;
    
    /**
     * The currently validated group.
     */
    protected ?string $group;
    
    /**
     * The currently validated constraint.
     */
    protected ?Constraint $constraint;
    
    /**
     * Stores which objects have been validated in which group.
     */
    protected array $validatedObjects = [];
    
    /**
     * Stores which class constraint has been validated for which object.
     */
    protected array $validatedConstraints = [];
    
    /**
     * Creates a new ExecutionContext.
     *
     * @param \Symfony\Component\Validator\Validator\ValidatorInterface $validator
     *   The validator.
     * @param mixed $root
     *   The root.
     * @param \Drupal\Core\Validation\TranslatorInterface $translator
     *   The translator.
     * @param string|null $translationDomain
     *   (optional) The translation domain.
     *
     * @internal Called by \Drupal\Core\Validation\ExecutionContextFactory.
     *    Should not be used in user code.
     */
    public function __construct(ValidatorInterface $validator, mixed $root, TranslatorInterface $translator, ?string $translationDomain = NULL) {
        $this->violations = new ConstraintViolationList();
    }
    
    /**
     * {@inheritdoc}
     */
    public function setNode(mixed $value, ?object $object, ?MetadataInterface $metadata, string $propertyPath) : void {
        $this->value = $value;
        $this->object = $object;
        $this->metadata = $metadata;
        $this->propertyPath = $propertyPath;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setConstraint(Constraint $constraint) : void {
        $this->constraint = $constraint;
    }
    
    /**
     * {@inheritdoc}
     *
     * phpcs:ignore Drupal.Commenting.FunctionComment.VoidReturn
     *
     * @return void
     */
    public function addViolation(string $message, array $params = []) {
        $this->violations
            ->add(new ConstraintViolation($this->translator
            ->trans($message, $params, $this->translationDomain), $message, $params, $this->root, $this->propertyPath, $this->value, NULL, NULL, $this->constraint));
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildViolation(string $message, array $parameters = []) : ConstraintViolationBuilderInterface {
        return new ConstraintViolationBuilder($this->violations, $this->constraint, $message, $parameters, $this->root, $this->propertyPath, $this->value, $this->translator, $this->translationDomain);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getViolations() : ConstraintViolationListInterface {
        return $this->violations;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getValidator() : ValidatorInterface {
        return $this->validator;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getRoot() : mixed {
        return $this->root;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getValue() : mixed {
        return $this->value;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getObject() : ?object {
        return $this->object;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getMetadata() : ?MetadataInterface {
        return $this->metadata;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getGroup() : ?string {
        return Constraint::DEFAULT_GROUP;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setGroup(?string $group) : void {
        $this->group = $group;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getClassName() : ?string {
        return get_class($this->object);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getPropertyName() : ?string {
        return $this->metadata instanceof PropertyMetadataInterface ? $this->metadata
            ->getPropertyName() : NULL;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getPropertyPath(string $subPath = '') : string {
        return PropertyPath::append($this->propertyPath, $subPath);
    }
    
    /**
     * {@inheritdoc}
     */
    public function markConstraintAsValidated(string $cacheKey, string $constraintHash) : void {
        $this->validatedConstraints[$cacheKey . ':' . $constraintHash] = TRUE;
    }
    
    /**
     * {@inheritdoc}
     */
    public function isConstraintValidated(string $cacheKey, string $constraintHash) : bool {
        return isset($this->validatedConstraints[$cacheKey . ':' . $constraintHash]);
    }
    
    /**
     * {@inheritdoc}
     */
    public function markGroupAsValidated(string $cacheKey, string $groupHash) : void {
        $this->validatedObjects[$cacheKey][$groupHash] = TRUE;
    }
    
    /**
     * {@inheritdoc}
     */
    public function isGroupValidated(string $cacheKey, string $groupHash) : bool {
        return isset($this->validatedObjects[$cacheKey][$groupHash]);
    }
    
    /**
     * {@inheritdoc}
     */
    public function markObjectAsInitialized(string $cacheKey) : void {
        throw new \LogicException(ExecutionContextInterface::class . '::markObjectAsInitialized is unsupported.');
    }
    
    /**
     * {@inheritdoc}
     */
    public function isObjectInitialized(string $cacheKey) : bool {
        throw new \LogicException(ExecutionContextInterface::class . '::isObjectInitialized is unsupported.');
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ExecutionContext::$constraint protected property The currently validated constraint.
ExecutionContext::$group protected property The currently validated group.
ExecutionContext::$metadata protected property The current validation metadata.
ExecutionContext::$object protected property The currently validated object.
ExecutionContext::$propertyPath protected property The property path leading to the current value.
ExecutionContext::$validatedConstraints protected property Stores which class constraint has been validated for which object.
ExecutionContext::$validatedObjects protected property Stores which objects have been validated in which group.
ExecutionContext::$value protected property The currently validated value.
ExecutionContext::$violations protected property The violations generated in the current context.
ExecutionContext::addViolation public function phpcs:ignore Drupal.Commenting.FunctionComment.VoidReturn
ExecutionContext::buildViolation public function
ExecutionContext::getClassName public function
ExecutionContext::getGroup public function
ExecutionContext::getMetadata public function
ExecutionContext::getObject public function
ExecutionContext::getPropertyName public function
ExecutionContext::getPropertyPath public function
ExecutionContext::getRoot public function
ExecutionContext::getValidator public function
ExecutionContext::getValue public function
ExecutionContext::getViolations public function
ExecutionContext::isConstraintValidated public function
ExecutionContext::isGroupValidated public function
ExecutionContext::isObjectInitialized public function
ExecutionContext::markConstraintAsValidated public function
ExecutionContext::markGroupAsValidated public function
ExecutionContext::markObjectAsInitialized public function
ExecutionContext::setConstraint public function
ExecutionContext::setGroup public function
ExecutionContext::setNode public function
ExecutionContext::__construct public function Creates a new ExecutionContext. 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.