@@ -96,6 +96,7 @@ class Schema extends MagicMap
96
96
public $ oneOf ;
97
97
98
98
public $ objectItemClass ;
99
+ private $ useObjectAsArray = false ;
99
100
100
101
public function import ($ data , DataPreProcessor $ preProcessor = null )
101
102
{
@@ -112,6 +113,10 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
112
113
if (!$ import && $ data instanceof ObjectItem) {
113
114
$ data = $ data ->jsonSerialize ();
114
115
}
116
+ if (!$ import && is_array ($ data ) && $ this ->useObjectAsArray ) {
117
+ $ data = (object )$ data ;
118
+ }
119
+
115
120
if (null !== $ preProcessor ) {
116
121
$ data = $ preProcessor ->process ($ data , $ this , $ import );
117
122
}
@@ -254,8 +259,6 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
254
259
}
255
260
}
256
261
}
257
-
258
-
259
262
}
260
263
261
264
if ($ data instanceof \stdClass) {
@@ -267,16 +270,21 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
267
270
}
268
271
}
269
272
270
- if ($ import && !$ result instanceof ObjectItem) {
271
- $ result = $ this ->makeObjectItem ();
272
273
273
- if ($ result instanceof ClassStructure) {
274
- if ($ result ->__validateOnSet ) {
275
- $ result ->__validateOnSet = false ;
276
- /** @noinspection PhpUnusedLocalVariableInspection */
277
- $ validateOnSetHandler = new ScopeExit (function () use ($ result ) {
278
- $ result ->__validateOnSet = true ;
279
- });
274
+ if ($ import ) {
275
+ if ($ this ->useObjectAsArray ) {
276
+ $ result = array ();
277
+ } elseif (!$ result instanceof ObjectItem) {
278
+ $ result = $ this ->makeObjectItem ();
279
+
280
+ if ($ result instanceof ClassStructure) {
281
+ if ($ result ->__validateOnSet ) {
282
+ $ result ->__validateOnSet = false ;
283
+ /** @noinspection PhpUnusedLocalVariableInspection */
284
+ $ validateOnSetHandler = new ScopeExit (function () use ($ result ) {
285
+ $ result ->__validateOnSet = true ;
286
+ });
287
+ }
280
288
}
281
289
}
282
290
}
@@ -344,7 +352,7 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
344
352
}
345
353
346
354
$ value = $ this ->additionalProperties ->process ($ value , $ import , $ preProcessor , $ path . '->additionalProperties ' );
347
- if ($ import ) {
355
+ if ($ import && ! $ this -> useObjectAsArray ) {
348
356
$ result ->addAdditionalPropertyName ($ key );
349
357
}
350
358
}
@@ -357,7 +365,11 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
357
365
$ result ->$ key = $ value ;
358
366
}
359
367
} else {
360
- $ result ->$ key = $ value ;
368
+ if ($ this ->useObjectAsArray && $ import ) {
369
+ $ result [$ key ] = $ value ;
370
+ } else {
371
+ $ result ->$ key = $ value ;
372
+ }
361
373
}
362
374
363
375
}
@@ -418,6 +430,16 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
418
430
return $ result ;
419
431
}
420
432
433
+ /**
434
+ * @param boolean $useObjectAsArray
435
+ * @return Schema
436
+ */
437
+ public function setUseObjectAsArray ($ useObjectAsArray )
438
+ {
439
+ $ this ->useObjectAsArray = $ useObjectAsArray ;
440
+ return $ this ;
441
+ }
442
+
421
443
/**
422
444
* @param bool|Schema $additionalProperties
423
445
* @return Schema
0 commit comments