types = $types; $this->before = $before; $this->after = $after; $this->keywordValidator = $keywordValidator; if ($keywordValidator) { while ($next = $keywordValidator->next()) { $keywordValidator = $next; } $keywordValidator->setNext(new CallbackKeywordValidator([$this, 'doValidate'])); } } /** * @inheritDoc */ public function validate(ValidationContext $context): ?ValidationError { $context->pushSharedObject($this); $error = $this->keywordValidator ? $this->keywordValidator->validate($context) : $this->doValidate($context); $context->popSharedObject(); return $error; } /** * @param ValidationContext $context * @return null|ValidationError *@internal */ public function doValidate(ValidationContext $context): ?ValidationError { if ($this->before && ($error = $this->applyKeywords($this->before, $context))) { return $error; } if ($this->types && ($type = $context->currentDataType())) { if (isset($this->types[$type]) && ($error = $this->applyKeywords($this->types[$type], $context))) { return $error; } if (($type = Helper::getJsonSuperType($type)) && isset($this->types[$type])) { if ($error = $this->applyKeywords($this->types[$type], $context)) { return $error; } } unset($type); } if ($this->after && ($error = $this->applyKeywords($this->after, $context))) { return $error; } return null; } /** * @param Keyword[] $keywords * @param ValidationContext $context * @return ValidationError|null */ protected function applyKeywords(array $keywords, ValidationContext $context): ?ValidationError { foreach ($keywords as $keyword) { if ($error = $keyword->validate($context, $this)) { return $error; } } return null; } }