24
24
*/
25
25
class Schema extends ObjectItem
26
26
{
27
+ public $ fromRef ;
28
+ public $ originPath ;
29
+
27
30
/*
28
- public $seqId;
31
+ public $__seqId;
32
+
29
33
public function __construct()
30
34
{
31
35
static $seq = 0;
32
36
$seq++;
33
- $this->seqId = $seq;
37
+ $this->__seqId = $seq;
34
38
}
35
39
*/
36
40
@@ -150,6 +154,7 @@ public function export($data, ProcessingOptions $options = null)
150
154
public function process ($ data , ProcessingOptions $ options , $ path = '# ' )
151
155
{
152
156
$ import = $ options ->import ;
157
+ //$pathTrace = explode('->', $path);
153
158
154
159
if (!$ import && $ data instanceof ObjectItem) {
155
160
$ data = $ data ->jsonSerialize ();
@@ -200,6 +205,7 @@ public function process($data, ProcessingOptions $options, $path = '#')
200
205
201
206
if ($ this ->oneOf !== null ) {
202
207
$ successes = 0 ;
208
+ $ failures = '' ;
203
209
foreach ($ this ->oneOf as $ index => $ item ) {
204
210
try {
205
211
$ result = $ item ->process ($ data , $ options , $ path . '->oneOf: ' . $ index );
@@ -208,16 +214,21 @@ public function process($data, ProcessingOptions $options, $path = '#')
208
214
break ;
209
215
}
210
216
} catch (InvalidValue $ exception ) {
217
+ $ failures .= ' ' . $ index . ': ' . $ exception ->getMessage () . "\n" ;
211
218
// Expected exception
212
219
}
213
220
}
214
- if ($ successes !== 1 ) {
215
- $ this ->fail (new LogicException ('Failed due to logical constraint: oneOf ' ), $ path );
221
+ if ($ successes === 0 ) {
222
+ $ this ->fail (new LogicException ('Failed due to logical constraint: no valid results for oneOf { ' . "\n" . substr ($ failures , 0 , -1 ) . "\n} " ), $ path );
223
+ } elseif ($ successes > 1 ) {
224
+ $ this ->fail (new LogicException ('Failed due to logical constraint: '
225
+ . $ successes . '/ ' . count ($ this ->oneOf ) . ' valid results for oneOf ' ), $ path );
216
226
}
217
227
}
218
228
219
229
if ($ this ->anyOf !== null ) {
220
230
$ successes = 0 ;
231
+ $ failures = '' ;
221
232
foreach ($ this ->anyOf as $ index => $ item ) {
222
233
try {
223
234
$ result = $ item ->process ($ data , $ options , $ path . '->anyOf: ' . $ index );
@@ -226,11 +237,12 @@ public function process($data, ProcessingOptions $options, $path = '#')
226
237
break ;
227
238
}
228
239
} catch (InvalidValue $ exception ) {
240
+ $ failures .= ' ' . $ index . ': ' . $ exception ->getMessage () . "\n" ;
229
241
// Expected exception
230
242
}
231
243
}
232
244
if (!$ successes ) {
233
- $ this ->fail (new LogicException ('Failed due to logical constraint: anyOf ' ), $ path );
245
+ $ this ->fail (new LogicException ('Failed due to logical constraint: no valid results for anyOf { ' . "\n" . substr ( $ failures , 0 , - 1 ) . "\n } " ), $ path );
234
246
}
235
247
}
236
248
@@ -319,6 +331,10 @@ public function process($data, ProcessingOptions $options, $path = '#')
319
331
});
320
332
}
321
333
}
334
+
335
+ if ($ result instanceof Schema) {
336
+ $ result ->originPath = $ path ;
337
+ }
322
338
}
323
339
}
324
340
@@ -335,8 +351,23 @@ public function process($data, ProcessingOptions $options, $path = '#')
335
351
return $ ref ->getImported ();
336
352
}
337
353
$ data = $ ref ->getData ();
354
+ if ($ result instanceof Schema) {
355
+ $ result ->fromRef = $ refString ;
356
+
357
+ }
358
+ $ result ->fromPath = $ refString ;
338
359
$ ref ->setImported ($ result );
339
360
$ path .= '->$ref: ' . $ refString ;
361
+
362
+ if ($ ref ->resolver !== $ options ->refResolver ) {
363
+ $ prevResolver = $ options ->refResolver ;
364
+ $ options ->refResolver = $ ref ->resolver ;
365
+ /** @noinspection PhpUnusedLocalVariableInspection */
366
+ $ deferResolver = new ScopeExit (function () use ($ prevResolver , $ options ) {
367
+ $ options ->refResolver = $ prevResolver ;
368
+ });
369
+
370
+ }
340
371
}
341
372
} catch (InvalidValue $ exception ) {
342
373
$ this ->fail ($ exception , $ path );
@@ -438,7 +469,7 @@ public function process($data, ProcessingOptions $options, $path = '#')
438
469
if (preg_match (Helper::toPregPattern ($ pattern ), $ key )) {
439
470
$ found = true ;
440
471
$ value = $ propertySchema ->process ($ value , $ options ,
441
- $ path . '->patternProperties: ' . $ pattern . '( ' . $ key . ' ) ' );
472
+ $ path . '->patternProperties[ ' . $ pattern . ']: ' . $ key );
442
473
if ($ import ) {
443
474
$ result ->addPatternPropertyName ($ pattern , $ key );
444
475
}
@@ -468,8 +499,9 @@ public function process($data, ProcessingOptions $options, $path = '#')
468
499
if ($ this ->useObjectAsArray && $ import ) {
469
500
$ result [$ key ] = $ value ;
470
501
} else {
471
- $ resultValue = $ result ->$ key ;
472
- if (!$ import || !($ resultValue instanceof ObjectItem && $ value instanceof \stdClass)) {
502
+ if ($ found || !$ import ) {
503
+ $ result ->$ key = $ value ;
504
+ } elseif (!isset ($ result ->$ key )) {
473
505
$ result ->$ key = $ value ;
474
506
}
475
507
}
0 commit comments