AbstractRefKeyword.php
1 year ago
AdditionalItemsKeyword.php
1 year ago
AdditionalPropertiesKeyword.php
1 year ago
AllOfKeyword.php
1 year ago
AnyOfKeyword.php
1 year ago
ConstDataKeyword.php
1 year ago
ConstKeyword.php
1 year ago
ContainsKeyword.php
1 year ago
ContentEncodingKeyword.php
1 year ago
ContentMediaTypeKeyword.php
1 year ago
ContentSchemaKeyword.php
1 year ago
DefaultKeyword.php
1 year ago
DependenciesKeyword.php
1 year ago
DependentRequiredKeyword.php
1 year ago
DependentSchemasKeyword.php
1 year ago
EnumDataKeyword.php
1 year ago
EnumKeyword.php
1 year ago
ErrorTrait.php
1 year ago
ExclusiveMaximumDataKeyword.php
1 year ago
ExclusiveMaximumKeyword.php
1 year ago
ExclusiveMinimumDataKeyword.php
1 year ago
ExclusiveMinimumKeyword.php
1 year ago
FiltersKeyword.php
1 year ago
FormatDataKeyword.php
1 year ago
FormatKeyword.php
1 year ago
IfThenElseKeyword.php
1 year ago
ItemsKeyword.php
1 year ago
IterableDataValidationTrait.php
1 year ago
MaxItemsDataKeyword.php
1 year ago
MaxItemsKeyword.php
1 year ago
MaxLengthDataKeyword.php
1 year ago
MaxLengthKeyword.php
1 year ago
MaxPropertiesDataKeyword.php
1 year ago
MaxPropertiesKeywords.php
1 year ago
MaximumDataKeyword.php
1 year ago
MaximumKeyword.php
1 year ago
MinItemsDataKeyword.php
1 year ago
MinItemsKeyword.php
1 year ago
MinLengthDataKeyword.php
1 year ago
MinLengthKeyword.php
1 year ago
MinPropertiesDataKeyword.php
1 year ago
MinPropertiesKeyword.php
1 year ago
MinimumDataKeyword.php
1 year ago
MinimumKeyword.php
1 year ago
MultipleOfDataKeyword.php
1 year ago
MultipleOfKeyword.php
1 year ago
NotKeyword.php
1 year ago
OfTrait.php
1 year ago
OneOfKeyword.php
1 year ago
PatternDataKeyword.php
1 year ago
PatternKeyword.php
1 year ago
PatternPropertiesKeyword.php
1 year ago
PointerRefKeyword.php
1 year ago
PropertiesKeyword.php
1 year ago
PropertyNamesKeyword.php
1 year ago
RecursiveRefKeyword.php
1 year ago
RequiredDataKeyword.php
1 year ago
RequiredKeyword.php
1 year ago
SlotsKeyword.php
1 year ago
TemplateRefKeyword.php
1 year ago
TypeKeyword.php
1 year ago
URIRefKeyword.php
1 year ago
UnevaluatedItemsKeyword.php
1 year ago
UnevaluatedPropertiesKeyword.php
1 year ago
UniqueItemsDataKeyword.php
1 year ago
UniqueItemsKeyword.php
1 year ago
RecursiveRefKeyword.php
138 lines
| 1 | <?php |
| 2 | /* ============================================================================ |
| 3 | * Copyright 2020 Zindex Software |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * ============================================================================ */ |
| 17 | |
| 18 | namespace Opis\JsonSchema\Keywords; |
| 19 | |
| 20 | use Opis\JsonSchema\Errors\ValidationError; |
| 21 | use Opis\JsonSchema\Exceptions\UnresolvedReferenceException; |
| 22 | use Opis\JsonSchema\{Schema, ValidationContext, Variables, Uri}; |
| 23 | |
| 24 | class RecursiveRefKeyword extends AbstractRefKeyword |
| 25 | { |
| 26 | protected Uri $uri; |
| 27 | /** @var bool|null|Schema */ |
| 28 | protected $resolved = false; |
| 29 | protected string $anchor; |
| 30 | protected $anchorValue; |
| 31 | |
| 32 | public function __construct( |
| 33 | Uri $uri, |
| 34 | ?Variables $mapper, |
| 35 | ?Variables $globals, |
| 36 | ?array $slots = null, |
| 37 | string $keyword = '$recursiveRef', |
| 38 | string $anchor = '$recursiveAnchor', |
| 39 | $anchorValue = true |
| 40 | ) { |
| 41 | parent::__construct($mapper, $globals, $slots, $keyword); |
| 42 | $this->uri = $uri; |
| 43 | $this->anchor = $anchor; |
| 44 | $this->anchorValue = $anchorValue; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @inheritDoc |
| 49 | */ |
| 50 | public function doValidate(ValidationContext $context, Schema $schema): ?ValidationError |
| 51 | { |
| 52 | if ($this->resolved === false) { |
| 53 | $this->resolved = $context->loader()->loadSchemaById($this->uri); |
| 54 | } |
| 55 | |
| 56 | if ($this->resolved === null) { |
| 57 | throw new UnresolvedReferenceException((string)$this->uri, $schema, $context); |
| 58 | } |
| 59 | |
| 60 | $new_context = $this->createContext($context, $schema); |
| 61 | |
| 62 | if (!$this->hasRecursiveAnchor($this->resolved)) { |
| 63 | $this->setLastRefSchema($this->resolved); |
| 64 | return $this->resolved->validate($new_context); |
| 65 | } |
| 66 | |
| 67 | $ok_sender = $this->resolveSchema($context); |
| 68 | |
| 69 | if (!$ok_sender) { |
| 70 | $this->setLastRefSchema($this->resolved); |
| 71 | return $this->resolved->validate($new_context); |
| 72 | } |
| 73 | |
| 74 | $this->setLastRefSchema($ok_sender); |
| 75 | |
| 76 | return $ok_sender->validate($new_context); |
| 77 | } |
| 78 | |
| 79 | protected function resolveSchema(ValidationContext $context): ?Schema |
| 80 | { |
| 81 | $ok = null; |
| 82 | $loader = $context->loader(); |
| 83 | |
| 84 | while ($context) { |
| 85 | $sender = $context->sender(); |
| 86 | |
| 87 | if (!$sender) { |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | if (!$this->hasRecursiveAnchor($sender)) { |
| 92 | if ($sender->info()->id()) { |
| 93 | // id without recursiveAnchor |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | $sender = $loader->loadSchemaById($sender->info()->root()); |
| 98 | if (!$sender || !$this->hasRecursiveAnchor($sender)) { |
| 99 | // root without recursiveAnchor |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if ($sender->info()->id()) { |
| 105 | // id with recursiveAnchor |
| 106 | $ok = $sender; |
| 107 | } else { |
| 108 | // root with recursiveAnchor |
| 109 | $ok = $loader->loadSchemaById($sender->info()->root()); |
| 110 | } |
| 111 | |
| 112 | $context = $context->parent(); |
| 113 | } |
| 114 | |
| 115 | return $ok; |
| 116 | } |
| 117 | |
| 118 | protected function hasRecursiveAnchor(?Schema $schema): bool |
| 119 | { |
| 120 | if (!$schema) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | $info = $schema->info(); |
| 125 | |
| 126 | if (!$info->isObject()) { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | $data = $info->data(); |
| 131 | |
| 132 | if (!property_exists($data, $this->anchor)) { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | return $data->{$this->anchor} === $this->anchorValue; |
| 137 | } |
| 138 | } |