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
ContainsKeyword.php
143 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\{ |
| 21 | ValidationContext, |
| 22 | Keyword, |
| 23 | Schema |
| 24 | }; |
| 25 | use Opis\JsonSchema\Errors\ValidationError; |
| 26 | |
| 27 | class ContainsKeyword implements Keyword |
| 28 | { |
| 29 | use ErrorTrait; |
| 30 | |
| 31 | /** @var bool|object */ |
| 32 | protected $value; |
| 33 | protected ?int $min = null; |
| 34 | protected ?int $max = null; |
| 35 | |
| 36 | /** |
| 37 | * @param bool|object $value |
| 38 | * @param int|null $min |
| 39 | * @param int|null $max |
| 40 | */ |
| 41 | public function __construct($value, ?int $min = null, ?int $max = null) |
| 42 | { |
| 43 | $this->value = $value; |
| 44 | $this->min = $min; |
| 45 | $this->max = $max; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @inheritDoc |
| 50 | */ |
| 51 | public function validate(ValidationContext $context, Schema $schema): ?ValidationError |
| 52 | { |
| 53 | $data = $context->currentData(); |
| 54 | $count = count($data); |
| 55 | |
| 56 | $context->markAllAsEvaluatedItems(); |
| 57 | |
| 58 | if ($this->min > $count) { |
| 59 | return $this->error($schema, $context, 'minContains', 'Array must have at least {min} items', [ |
| 60 | 'min' => $this->min, |
| 61 | 'count' => $count, |
| 62 | ]); |
| 63 | } |
| 64 | |
| 65 | $isMaxNull = $this->max === null; |
| 66 | |
| 67 | if ($this->value === true) { |
| 68 | if ($count) { |
| 69 | if (!$isMaxNull && $count > $this->max) { |
| 70 | return $this->error($schema, $context, 'maxContains', 'Array must have at most {max} items', [ |
| 71 | 'max' => $this->max, |
| 72 | 'count' => $count, |
| 73 | ]); |
| 74 | } |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | return $this->error($schema, $context, 'contains', 'Array must not be empty'); |
| 79 | } |
| 80 | |
| 81 | if ($this->value === false) { |
| 82 | return $this->error($schema, $context, 'contains', 'Any array is invalid'); |
| 83 | } |
| 84 | |
| 85 | if (is_object($this->value) && !($this->value instanceof Schema)) { |
| 86 | $this->value = $context->loader()->loadObjectSchema($this->value); |
| 87 | } |
| 88 | |
| 89 | $errors = []; |
| 90 | $valid = 0; |
| 91 | |
| 92 | $isMinNull = $this->min === null; |
| 93 | |
| 94 | if ($isMaxNull && $isMinNull) { |
| 95 | foreach ($data as $key => $item) { |
| 96 | $context->pushDataPath($key); |
| 97 | $error = $this->value->validate($context); |
| 98 | $context->popDataPath(); |
| 99 | if ($error) { |
| 100 | $errors[] = $error; |
| 101 | } else { |
| 102 | return null; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return $this->error($schema, $context, 'contains', 'At least one array item must match schema', [], |
| 107 | $errors); |
| 108 | } |
| 109 | |
| 110 | foreach ($data as $key => $item) { |
| 111 | $context->pushDataPath($key); |
| 112 | $error = $this->value->validate($context); |
| 113 | $context->popDataPath(); |
| 114 | |
| 115 | if ($error) { |
| 116 | $errors[] = $error; |
| 117 | } else { |
| 118 | $valid++; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (!$isMinNull && $valid < $this->min) { |
| 123 | return $this->error($schema, $context, 'minContains', 'At least {min} array items must match schema', [ |
| 124 | 'min' => $this->min, |
| 125 | 'count' => $valid, |
| 126 | ]); |
| 127 | } |
| 128 | |
| 129 | if (!$isMaxNull && $valid > $this->max) { |
| 130 | return $this->error($schema, $context, 'maxContains', 'At most {max} array items must match schema', [ |
| 131 | 'max' => $this->max, |
| 132 | 'count' => $valid, |
| 133 | ]); |
| 134 | } |
| 135 | |
| 136 | if ($valid) { |
| 137 | return null; |
| 138 | } |
| 139 | |
| 140 | return $this->error($schema, $context, 'contains', 'At least one array item must match schema', [], |
| 141 | $errors); |
| 142 | } |
| 143 | } |