contextDefinition = $context_definition; $this->contextValue = $context_value; } /** * {@inheritdoc} */ public function getContextValue() { // Support optional contexts. if (!isset($this->contextValue)) { $definition = $this->getContextDefinition(); $default_value = $definition->getDefaultValue(); if (!isset($default_value) && $definition->isRequired()) { $type = $definition->getDataType(); throw new ContextException(sprintf("The %s context is required and not present.", $type)); } // Keep the default value here so that subsequent calls don't have to look // it up again. $this->contextValue = $default_value; } return $this->contextValue; } /** * {@inheritdoc} */ public function hasContextValue() { return (bool) $this->contextValue || (bool) $this->getContextDefinition()->getDefaultValue(); } /** * {@inheritdoc} */ public function getContextDefinition() { return $this->contextDefinition; } /** * {@inheritdoc} */ public function getConstraints() { if (empty($this->contextDefinition['class'])) { throw new ContextException("An error was encountered while trying to validate the context."); } return [new Type($this->contextDefinition['class'])]; } /** * {@inheritdoc} */ public function validate() { $validator = Validation::createValidatorBuilder() ->getValidator(); return $validator->validateValue($this->getContextValue(), $this->getConstraints()); } }