@@ -140,22 +140,18 @@ private function generateNormalizeChildMethod(Namespace_ $namespace, Class_ $cla
140
140
new Node \Name ('array_map ' ),
141
141
[
142
142
new Node \Arg (
143
- new Node \Expr \Closure ([
143
+ new Node \Expr \ArrowFunction ([
144
144
'params ' => [new Node \Param (new Node \Expr \Variable ('item ' ))],
145
- 'stmts ' => [
146
- new Node \Stmt \Return_ (
147
- $ this ->factory ->methodCall (
148
- new Node \Expr \Variable ('this ' ),
149
- 'normalizeChild ' ,
150
- [
151
- new Node \Arg (new Node \Expr \Variable ('item ' )),
152
- new Node \Arg (new Node \Expr \Variable ('format ' )),
153
- new Node \Arg (new Node \Expr \Variable ('context ' )),
154
- new Node \Arg (new Node \Expr \ConstFetch (new Node \Name ('true ' ))),
155
- ]
156
- )
157
- ),
158
- ],
145
+ 'expr ' => $ this ->factory ->methodCall (
146
+ new Node \Expr \Variable ('this ' ),
147
+ 'normalizeChild ' ,
148
+ [
149
+ new Node \Arg (new Node \Expr \Variable ('item ' )),
150
+ new Node \Arg (new Node \Expr \Variable ('format ' )),
151
+ new Node \Arg (new Node \Expr \Variable ('context ' )),
152
+ new Node \Arg (new Node \Expr \ConstFetch (new Node \Name ('true ' ))),
153
+ ]
154
+ )
159
155
])
160
156
),
161
157
new Node \Arg (new Node \Expr \Variable ('object ' )),
@@ -251,23 +247,19 @@ private function generateDenormalizeChildMethod(Namespace_ $namespace, Class_ $c
251
247
new Node \Name ('array_map ' ),
252
248
[
253
249
new Node \Arg (
254
- new Node \Expr \Closure ([
250
+ new Node \Expr \ArrowFunction ([
255
251
'params ' => [new Node \Param (new Node \Expr \Variable ('item ' ))],
256
- 'stmts ' => [
257
- new Node \Stmt \Return_ (
258
- $ this ->factory ->methodCall (
259
- new Node \Expr \Variable ('this ' ),
260
- 'denormalizeChild ' ,
261
- [
262
- new Node \Arg (new Node \Expr \Variable ('item ' )),
263
- new Node \Arg (new Node \Expr \Variable ('type ' )),
264
- new Node \Arg (new Node \Expr \Variable ('format ' )),
265
- new Node \Arg (new Node \Expr \Variable ('context ' )),
266
- new Node \Arg (new Node \Expr \ConstFetch (new Node \Name ('true ' ))),
267
- ]
268
- )
269
- ),
270
- ],
252
+ 'expr ' => $ this ->factory ->methodCall (
253
+ new Node \Expr \Variable ('this ' ),
254
+ 'denormalizeChild ' ,
255
+ [
256
+ new Node \Arg (new Node \Expr \Variable ('item ' )),
257
+ new Node \Arg (new Node \Expr \Variable ('type ' )),
258
+ new Node \Arg (new Node \Expr \Variable ('format ' )),
259
+ new Node \Arg (new Node \Expr \Variable ('context ' )),
260
+ new Node \Arg (new Node \Expr \ConstFetch (new Node \Name ('true ' ))),
261
+ ]
262
+ )
271
263
])
272
264
),
273
265
new Node \Arg (new Node \Expr \Variable ('data ' )),
@@ -376,14 +368,23 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
376
368
$ targetClasses = $ propertyDefinition ->getNonPrimitiveTypes ();
377
369
$ canBeIterable = $ propertyDefinition ->isCollection ();
378
370
371
+ $ defaultValue = $ propertyDefinition ->getConstructorDefaultValue ();
372
+ if (is_object ($ defaultValue )) {
373
+ // public function __construct($foo = new \stdClass());
374
+ // There is no support for parameters to the object.
375
+ $ defaultValue = new Expr \New_ (new Node \Name \FullyQualified (get_class ($ defaultValue )));
376
+ } else {
377
+ $ defaultValue = $ this ->factory ->val ($ defaultValue );
378
+ }
379
+
379
380
if ([] === $ targetClasses && $ propertyDefinition ->hasConstructorDefaultValue ()) {
380
381
$ constructorArguments [] = new Node \Arg (new Node \Expr \BinaryOp \Coalesce (
381
382
$ variable ,
382
- $ this -> factory -> val ( $ propertyDefinition -> getConstructorDefaultValue ())
383
+ $ defaultValue
383
384
));
384
385
continue ;
385
386
} elseif ([] === $ targetClasses ) {
386
- $ constructorArguments [] = new Node \Arg (new Node \ Expr \ Variable ( $ variable) );
387
+ $ constructorArguments [] = new Node \Arg ($ variable );
387
388
continue ;
388
389
}
389
390
@@ -402,7 +403,7 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
402
403
'denormalizeChild ' ,
403
404
[
404
405
new Node \Arg ($ variable ),
405
- new Node \Arg (new Node \Expr \ClassConstFetch (new Node \Name ($ targetClasses [0 ]), 'class ' )),
406
+ new Node \Arg (new Node \Expr \ClassConstFetch (new Node \Name \ FullyQualified ($ targetClasses [0 ]), 'class ' )),
406
407
new Node \Arg (new Node \Expr \Variable ('format ' )),
407
408
new Node \Arg (new Node \Expr \Variable ('context ' )),
408
409
new Node \Arg (new Expr \ConstFetch (new Node \Name ($ canBeIterable ? 'true ' : 'false ' ))),
@@ -423,10 +424,10 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
423
424
'stmts ' => [
424
425
new Node \Stmt \Expression (new Node \Expr \Assign (
425
426
new Node \Expr \Variable ($ tempVariableName ),
426
- $ this -> factory -> val ( $ propertyDefinition -> getConstructorDefaultValue ())
427
+ $ defaultValue
427
428
)),
428
429
],
429
- 'else ' => $ variableOutput
430
+ 'else ' => new Node \ Stmt \ Else_ ( $ variableOutput)
430
431
]
431
432
)];
432
433
}
@@ -475,7 +476,7 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
475
476
'denormalizeChild ' ,
476
477
[
477
478
new Node \Arg ($ variable ),
478
- new Node \Arg (new Node \Expr \ClassConstFetch (new Node \Name ($ targetClasses [0 ]), 'class ' )),
479
+ new Node \Arg (new Node \Expr \ClassConstFetch (new Node \Name \ FullyQualified ($ targetClasses [0 ]), 'class ' )),
479
480
new Node \Arg (new Node \Expr \Variable ('format ' )),
480
481
new Node \Arg (new Node \Expr \Variable ('context ' )),
481
482
new Node \Arg (new Expr \ConstFetch (new Node \Name ($ propertyDefinition ->isCollection () ? 'true ' : 'false ' ))),
@@ -519,7 +520,7 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
519
520
->addParam ($ this ->factory ->param ('format ' )->setType ('?string ' )->setDefault (null ))
520
521
->addParam ($ this ->factory ->param ('context ' )->setType ('array ' )->setDefault ([]))
521
522
->setReturnType ('mixed ' )
522
- ->addStmts ([] )
523
+ ->addStmts ($ body )
523
524
->addStmt (new Node \Stmt \Return_ (new Node \Expr \Variable ('output ' )))
524
525
);
525
526
@@ -566,7 +567,7 @@ private function addNormailizeMethod(ClassDefinition $definition, Namespace_ $na
566
567
->addParam ($ this ->factory ->param ('format ' )->setType ('string ' )->setDefault (null ))
567
568
->addParam ($ this ->factory ->param ('context ' )->setType ('array ' )->setDefault ([]))
568
569
->setReturnType ('array|string|int|float|bool|\ArrayObject|null ' )
569
- ->setDocComment (sprintf ('/* ' .PHP_EOL .'* @param %s $object ' .PHP_EOL .'*/ ' , $ definition ->getSourceClassName ()))
570
+ ->setDocComment (sprintf ('/** ' .PHP_EOL .'* @param %s $object ' .PHP_EOL .'*/ ' , $ definition ->getSourceClassName ()))
570
571
->addStmt (new Node \Stmt \Return_ (new Node \Expr \Array_ ($ bodyArrayItems ))));
571
572
572
573
if ($ needsChildNormalizer ) {
@@ -584,7 +585,7 @@ private function generateCodeToDeserializeMultiplePossibleClasses(array $targetC
584
585
{
585
586
$ arrayItems = [];
586
587
foreach ($ targetClasses as $ class ) {
587
- $ arrayItems [] = new Node \ArrayItem (new Node \Scalar \ String_ ($ class ));
588
+ $ arrayItems [] = new Node \ArrayItem (new Expr \ ClassConstFetch ( new Node \Name \ FullyQualified ($ class), ' class ' ));
588
589
}
589
590
590
591
return [
@@ -655,15 +656,17 @@ private function generateCodeToDeserializeMultiplePossibleClasses(array $targetC
655
656
new Node \Expr \BooleanNot (new Node \Expr \Variable ($ tempVariableName .'HasValue ' )),
656
657
[
657
658
'stmts ' => [
658
- new Node \Expr \Throw_ (
659
- new Node \Expr \New_ (
660
- new Node \Name \FullyQualified (DenormalizingUnionFailedException::class),
661
- [
662
- new Node \Arg (new Node \Scalar \String_ ('Failed to denormalize key " ' .$ keyName .'" of class " ' .$ classNs .'". ' )),
663
- new Node \Arg (new Node \Expr \Variable ('exceptions ' )),
664
- ]
665
- )
666
- ),
659
+ new Node \Stmt \Expression (
660
+ new Node \Expr \Throw_ (
661
+ new Node \Expr \New_ (
662
+ new Node \Name ('DenormalizingUnionFailedException ' ),
663
+ [
664
+ new Node \Arg (new Node \Scalar \String_ ('Failed to denormalize key " ' .$ keyName .'" of class " ' .$ classNs .'". ' )),
665
+ new Node \Arg (new Node \Expr \Variable ('exceptions ' )),
666
+ ]
667
+ )
668
+ ),
669
+ )
667
670
],
668
671
]
669
672
),
0 commit comments