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,17 +264,13 @@ 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 ) {
274271 $ result ->__validateOnSet = false ;
275272 /** @noinspection PhpUnusedLocalVariableInspection */
276- $ validateOnSetHandler = new ScopeExit (function () use ($ result ){
273+ $ validateOnSetHandler = new ScopeExit (function () use ($ result ) {
277274 $ result ->__validateOnSet = true ;
278275 });
279276 }
@@ -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,17 @@ 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+ }
343+ }
344+
345+ if ($ nestedEgg && $ import ) {
346+ $ result ->setNestedProperty ($ key , $ value , $ nestedEgg );
347+ } else {
348+ $ result ->$ key = $ value ;
330349 }
331- $ result -> $ key = $ value ;
350+
332351 }
333352
334353 }
@@ -455,4 +474,31 @@ public function setProperty($name, Schema $schema)
455474 return $ this ;
456475 }
457476
477+ /** @var Meta[] */
478+ private $ metaItems = array ();
479+ public function meta (Meta $ meta )
480+ {
481+ $ this ->metaItems [get_class ($ meta )] = $ meta ;
482+ return $ this ;
483+ }
484+
485+ public function getMeta ($ className )
486+ {
487+ if (isset ($ this ->metaItems [$ className ])) {
488+ return $ this ->metaItems [$ className ];
489+ }
490+ return null ;
491+ }
492+
493+ /**
494+ * @return ObjectItem
495+ */
496+ public function makeObjectItem ()
497+ {
498+ if (null === $ this ->objectItemClass ) {
499+ return new ObjectItem ();
500+ } else {
501+ return new $ this ->objectItemClass ;
502+ }
503+ }
458504}
0 commit comments