/var/www/html_fr/wp-content/plugins/woocommerce/vendor/opis/json-schema/src/Validator.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/* ============================================================================
 * Copyright 2020 Zindex Software
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============================================================================ */

namespace Opis\JsonSchema;

use 
InvalidArgumentExceptionRuntimeException;
use 
Opis\JsonSchema\Parsers\SchemaParser;
use 
Opis\JsonSchema\Errors\ValidationError;
use 
Opis\JsonSchema\Resolvers\{SchemaResolver};

class 
Validator
{
    protected 
SchemaLoader $loader;
    protected 
int $maxErrors 1;

    
/**
     * @param SchemaLoader|null $loader
     * @param int $max_errors
     */
    
public function __construct(?SchemaLoader $loader nullint $max_errors 1)
    {
        
$this->loader $loader ?? new SchemaLoader(new SchemaParser(), new SchemaResolver(), true);
        
$this->maxErrors $max_errors;
    }

    
/**
     * @param $data
     * @param bool|string|Uri|Schema|object $schema
     * @param array|null $globals
     * @param array|null $slots
     * @return ValidationResult
     */
    
public function validate($data$schema, ?array $globals null, ?array $slots null): ValidationResult
    
{
        if (
is_string($schema)) {
            if (
$uri Uri::parse($schematrue)) {
                
$schema $uri;
            } else {
                
$schema json_decode($schemafalse);
            }
        }

        
$error null;
        if (
is_bool($schema)) {
            
$error $this->dataValidation($data$schema$globals$slots);
        } elseif (
is_object($schema)) {
            if (
$schema instanceof Uri) {
                
$error $this->uriValidation($data$schema$globals$slots);
            } elseif (
$schema instanceof Schema) {
                
$error $this->schemaValidation($data$schema$globals$slots);
            } else {
                
$error $this->dataValidation($data$schema$globals$slots);
            }
        } else {
            throw new 
InvalidArgumentException("Invalid schema");
        }

        return new 
ValidationResult($error);
    }

    
/**
     * @param $data
     * @param Uri|string $uri
     * @param array|null $globals
     * @param array|null $slots
     * @return null|ValidationError
     */
    
public function uriValidation($data$uri, ?array $globals null, ?array $slots null): ?ValidationError
    
{
        if (
is_string($uri)) {
            
$uri Uri::parse($uritrue);
        }

        if (!(
$uri instanceof Uri)) {
            throw new 
InvalidArgumentException("Invalid uri");
        }

        if (
$uri->fragment() === null) {
            
$uri Uri::merge($urinulltrue);
        }

        
$schema $this->loader->loadSchemaById($uri);

        if (
$schema === null) {
            throw new 
RuntimeException("Schema not found: $uri");
        }

        return 
$this->schemaValidation($data$schema$globals$slots);
    }

    
/**
     * @param $data
     * @param string|object|bool $schema
     * @param array|null $globals
     * @param array|null $slots
     * @param string|null $id
     * @param string|null $draft
     * @return ValidationError|null
     */
    
public function dataValidation(
        
$data,
        
$schema,
        ?array 
$globals null,
        ?array 
$slots null,
        ?
string $id null,
        ?
string $draft null
    
): ?ValidationError
    
{
        if (
is_string($schema)) {
            
$schema json_decode($schemafalse);
        }

        if (
$schema === true) {
            return 
null;
        }

        if (
$schema === false) {
            
$schema $this->loader->loadBooleanSchema(false$id$draft);
        } else {
            if (!
is_object($schema)) {
                throw new 
InvalidArgumentException("Invalid schema");
            }

            
$schema $this->loader->loadObjectSchema($schema$id$draft);
        }

        return 
$this->schemaValidation($data$schema$globals$slots);
    }

    
/**
     * @param $data
     * @param Schema $schema
     * @param array|null $globals
     * @param array|null $slots
     * @return null|ValidationError
     */
    
public function schemaValidation(
        
$data,
        
Schema $schema,
        ?array 
$globals null,
        ?array 
$slots null
    
): ?ValidationError
    
{
        return 
$schema->validate($this->createContext($data$globals$slots));
    }

    
/**
     * @param $data
     * @param array|null $globals
     * @param array|null $slots
     * @return ValidationContext
     */
    
public function createContext($data, ?array $globals null, ?array $slots null): ValidationContext
    
{
        if (
$slots) {
            
$slots $this->parseSlots($slots);
        }

        return new 
ValidationContext($data$this->loadernullnull$globals ?? [], $slots$this->maxErrors);
    }

    
/**
     * @return SchemaParser
     */
    
public function parser(): SchemaParser
    
{
        return 
$this->loader->parser();
    }

    
/**
     * @param SchemaParser $parser
     * @return Validator
     */
    
public function setParser(SchemaParser $parser): self
    
{
        
$this->loader->setParser($parser);

        return 
$this;
    }

    
/**
     * @return SchemaResolver|null
     */
    
public function resolver(): ?SchemaResolver
    
{
        return 
$this->loader->resolver();
    }

    
/**
     * @param SchemaResolver|null $resolver
     * @return Validator
     */
    
public function setResolver(?SchemaResolver $resolver): self
    
{
        
$this->loader->setResolver($resolver);

        return 
$this;
    }

    
/**
     * @return SchemaLoader
     */
    
public function loader(): SchemaLoader
    
{
        return 
$this->loader;
    }

    
/**
     * @param SchemaLoader $loader
     * @return Validator
     */
    
public function setLoader(SchemaLoader $loader): self
    
{
        
$this->loader $loader;

        return 
$this;
    }

    
/**
     * @return int
     */
    
public function getMaxErrors(): int
    
{
        return 
$this->maxErrors;
    }

    
/**
     * @param int $max_errors
     * @return Validator
     */
    
public function setMaxErrors(int $max_errors): self
    
{
        
$this->maxErrors $max_errors;

        return 
$this;
    }

    
/**
     * @param array $slots
     * @return array
     */
    
protected function parseSlots(array $slots): array
    {
        foreach (
$slots as $name => &$value) {
            if (!
is_string($name)) {
                unset(
$slots[$name]);
                continue;
            }

            if (
is_string($value)) {
                
$value Uri::parse($valuetrue);
            }

            if (
$value instanceof Uri) {
                
$value $this->loader->loadSchemaById($value);
            } elseif (
is_bool($value)) {
                
$value $this->loader->loadBooleanSchema($value);
            }

            if (!
is_object($value)) {
                unset(
$slots[$name]);
            }

            unset(
$value);
        }

        return 
$slots;
    }
}