1616use Swaggest \JsonSchema \Exception \StringException ;
1717use Swaggest \JsonSchema \Exception \TypeException ;
1818use Swaggest \JsonSchema \Structure \ClassStructure ;
19+ use Swaggest \JsonSchema \Structure \Egg ;
1920use Swaggest \JsonSchema \Structure \ObjectItem ;
2021
2122class Schema extends MagicMap
@@ -126,7 +127,7 @@ private function process($data, $import = true, $path = '#')
126127 if ($ this ->enum !== null ) {
127128 $ enumOk = false ;
128129 foreach ($ this ->enum as $ item ) {
129- if ($ item === $ data ) {
130+ if ($ item === $ data ) { // todo support complex structures here
130131 $ enumOk = true ;
131132 break ;
132133 }
@@ -263,11 +264,7 @@ private function process($data, $import = true, $path = '#')
263264 }
264265
265266 if ($ import && !$ result instanceof ObjectItem) {
266- if (null === $ this ->objectItemClass ) {
267- $ result = new ObjectItem ();
268- } else {
269- $ result = new $ this ->objectItemClass ;
270- }
267+ $ result = $ this ->makeObjectItem ();
271268
272269 if ($ result instanceof ClassStructure) {
273270 if ($ result ->__validateOnSet ) {
@@ -282,7 +279,8 @@ private function process($data, $import = true, $path = '#')
282279
283280 if ($ this ->properties !== null ) {
284281 /** @var Schema[] $properties */
285- $ properties = &$ this ->properties ->toArray ();
282+ $ properties = &$ this ->properties ->toArray (); // TODO check performance of pointer
283+ $ nestedProperties = $ this ->properties ->getNestedProperties ();
286284 }
287285
288286 $ array = (array )$ data ;
@@ -301,7 +299,8 @@ private function process($data, $import = true, $path = '#')
301299 } else {
302300 foreach ($ dependencies as $ item ) {
303301 if (!property_exists ($ data , $ item )) {
304- $ this ->fail (new ObjectException ('Dependency property missing: ' . $ item , ObjectException::DEPENDENCY_MISSING ), $ path );
302+ $ this ->fail (new ObjectException ('Dependency property missing: ' . $ item ,
303+ ObjectException::DEPENDENCY_MISSING ), $ path );
305304 }
306305 }
307306 }
@@ -312,11 +311,22 @@ private function process($data, $import = true, $path = '#')
312311 $ value = $ properties [$ key ]->process ($ value , $ import , $ path . '->properties: ' . $ key );
313312 }
314313
314+ /** @var Egg $nestedEgg */
315+ $ nestedEgg = null ;
316+ if (!$ found && isset ($ nestedProperties [$ key ])) {
317+ $ found = true ;
318+ $ nestedEgg = $ nestedProperties [$ key ];
319+ $ value = $ nestedEgg ->propertySchema ->process ($ value , $ import , $ path . '->nestedProperties: ' . $ key );
320+ }
321+
315322 if ($ this ->patternProperties !== null ) {
316323 foreach ($ this ->patternProperties as $ pattern => $ propertySchema ) {
317324 if (preg_match ($ pattern , $ key )) {
318325 $ found = true ;
319326 $ value = $ propertySchema ->process ($ value , $ import , $ path . '->patternProperties: ' . $ pattern );
327+ if ($ import ) {
328+ $ result ->addPatternPropertyName ($ pattern , $ key );
329+ }
320330 //break; // todo manage multiple import data properly (pattern accessor)
321331 }
322332 }
@@ -327,8 +337,18 @@ private function process($data, $import = true, $path = '#')
327337 }
328338
329339 $ value = $ this ->additionalProperties ->process ($ value , $ import , $ path . '->additionalProperties ' );
340+ if ($ import ) {
341+ $ result ->addAdditionalPropertyName ($ key );
342+ }
330343 }
331- $ result ->$ key = $ value ;
344+
345+ if ($ nestedEgg ) {
346+ $ result ->setNestedProperty ($ key , $ value , $ nestedEgg );
347+
348+ } else {
349+ $ result ->$ key = $ value ;
350+ }
351+
332352 }
333353
334354 }
@@ -471,4 +491,15 @@ public function getMeta($className)
471491 return null ;
472492 }
473493
494+ /**
495+ * @return ObjectItem
496+ */
497+ public function makeObjectItem ()
498+ {
499+ if (null === $ this ->objectItemClass ) {
500+ return new ObjectItem ();
501+ } else {
502+ return new $ this ->objectItemClass ;
503+ }
504+ }
474505}
0 commit comments