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