@@ -304,27 +304,18 @@ protected function instantiateObject(array &$data, string $class, array &$contex
304
304
305
305
return $ object ;
306
306
}
307
- // clean up even if no match
308
- unset($ context [static ::OBJECT_TO_POPULATE ]);
309
307
310
308
$ class = $ this ->getClassDiscriminatorResolvedClass ($ data , $ class , $ context );
311
309
$ reflectionClass = new \ReflectionClass ($ class );
312
310
313
311
$ constructor = $ this ->getConstructor ($ data , $ class , $ context , $ reflectionClass , $ allowedAttributes );
314
312
if ($ constructor ) {
315
- $ context ['has_constructor ' ] = true ;
316
- if (true !== $ constructor ->isPublic ()) {
317
- return $ reflectionClass ->newInstanceWithoutConstructor ();
318
- }
319
-
320
313
$ constructorParameters = $ constructor ->getParameters ();
321
- $ missingConstructorArguments = [];
322
- $ params = [];
323
- $ unsetKeys = [];
324
314
315
+ $ params = [];
316
+ $ missingConstructorArguments = [];
325
317
foreach ($ constructorParameters as $ constructorParameter ) {
326
318
$ paramName = $ constructorParameter ->name ;
327
- $ attributeContext = $ this ->getAttributeDenormalizationContext ($ class , $ paramName , $ context );
328
319
$ key = $ this ->nameConverter ? $ this ->nameConverter ->normalize ($ paramName , $ class , $ format , $ context ) : $ paramName ;
329
320
330
321
$ allowed = false === $ allowedAttributes || (\is_array ($ allowedAttributes ) && \in_array ($ paramName , $ allowedAttributes , true ));
@@ -335,22 +326,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex
335
326
throw new RuntimeException (\sprintf ('Cannot create an instance of %s from serialized data because the variadic parameter %s can only accept an array. ' , $ class , $ constructorParameter ->name ));
336
327
}
337
328
338
- $ variadicParameters = [];
339
- foreach ($ data [$ key ] as $ parameterKey => $ parameterData ) {
340
- try {
341
- $ variadicParameters [$ parameterKey ] = $ this ->denormalizeParameter ($ reflectionClass , $ constructorParameter , $ paramName , $ parameterData , $ attributeContext , $ format );
342
- } catch (NotNormalizableValueException $ exception ) {
343
- if (!isset ($ context ['not_normalizable_value_exceptions ' ])) {
344
- throw $ exception ;
345
- }
346
-
347
- $ context ['not_normalizable_value_exceptions ' ][] = $ exception ;
348
- $ params [$ paramName ] = $ parameterData ;
349
- }
350
- }
351
-
352
- $ params = array_merge (array_values ($ params ), $ variadicParameters );
353
- $ unsetKeys [] = $ key ;
329
+ $ params [] = $ data [$ paramName ];
354
330
}
355
331
} elseif ($ allowed && !$ ignored && (isset ($ data [$ key ]) || \array_key_exists ($ key , $ data ))) {
356
332
$ constructorContext = $ context ;
@@ -365,21 +341,18 @@ protected function instantiateObject(array &$data, string $class, array &$contex
365
341
}
366
342
367
343
// Don't run set for a parameter passed to the constructor
368
- $ unsetKeys [] = $ key ;
369
- } elseif (\array_key_exists ($ key , $ context [static ::DEFAULT_CONSTRUCTOR_ARGUMENTS ][$ class ] ?? [])) {
370
- $ params [$ paramName ] = $ context [static ::DEFAULT_CONSTRUCTOR_ARGUMENTS ][$ class ][$ key ];
371
- } elseif (\array_key_exists ($ key , $ this ->defaultContext [self ::DEFAULT_CONSTRUCTOR_ARGUMENTS ][$ class ] ?? [])) {
372
- $ params [$ paramName ] = $ this ->defaultContext [self ::DEFAULT_CONSTRUCTOR_ARGUMENTS ][$ class ][$ key ];
344
+ unset($ data [$ key ]);
345
+ } elseif (isset ($ context [static ::DEFAULT_CONSTRUCTOR_ARGUMENTS ][$ class ][$ key ])) {
346
+ $ params [] = $ context [static ::DEFAULT_CONSTRUCTOR_ARGUMENTS ][$ class ][$ key ];
373
347
} elseif ($ constructorParameter ->isDefaultValueAvailable ()) {
374
- $ params [$ paramName ] = $ constructorParameter ->getDefaultValue ();
375
- } elseif (!($ context [self ::REQUIRE_ALL_PROPERTIES ] ?? $ this ->defaultContext [self ::REQUIRE_ALL_PROPERTIES ] ?? false ) && $ constructorParameter ->hasType () && $ constructorParameter ->getType ()->allowsNull ()) {
376
- $ params [$ paramName ] = null ;
348
+ $ params [] = $ constructorParameter ->getDefaultValue ();
377
349
} else {
378
350
if (!isset ($ context ['not_normalizable_value_exceptions ' ])) {
379
351
$ missingConstructorArguments [] = $ constructorParameter ->name ;
380
352
continue ;
381
353
}
382
354
355
+ $ attributeContext = $ this ->getAttributeDenormalizationContext ($ class , $ paramName , $ context );
383
356
$ constructorParameterType = 'unknown ' ;
384
357
$ reflectionType = $ constructorParameter ->getType ();
385
358
if ($ reflectionType instanceof \ReflectionNamedType) {
@@ -401,39 +374,15 @@ protected function instantiateObject(array &$data, string $class, array &$contex
401
374
throw new MissingConstructorArgumentsException (\sprintf ('Cannot create an instance of "%s" from serialized data because its constructor requires the following parameters to be present : "$%s". ' , $ class , implode ('", "$ ' , $ missingConstructorArguments )), 0 , null , $ missingConstructorArguments , $ class );
402
375
}
403
376
404
- if (!$ constructor ->isConstructor ()) {
405
- $ instance = $ constructor ->invokeArgs (null , $ params );
406
-
407
- // do not set a parameter that has been set in the constructor
408
- foreach ($ unsetKeys as $ key ) {
409
- unset($ data [$ key ]);
410
- }
411
-
412
- return $ instance ;
413
- }
414
-
415
- try {
416
- $ instance = $ reflectionClass ->newInstanceArgs ($ params );
417
-
418
- // do not set a parameter that has been set in the constructor
419
- foreach ($ unsetKeys as $ key ) {
420
- unset($ data [$ key ]);
421
- }
422
-
423
- return $ instance ;
424
- } catch (\TypeError $ e ) {
425
- if (!isset ($ context ['not_normalizable_value_exceptions ' ])) {
426
- throw $ e ;
427
- }
428
-
377
+ if (\count ($ context ['not_normalizable_value_exceptions ' ] ?? []) > 0 ) {
429
378
return $ reflectionClass ->newInstanceWithoutConstructor ();
430
379
}
431
- }
432
380
433
- unset($ context ['has_constructor ' ]);
381
+ if ($ constructor ->isConstructor ()) {
382
+ return $ reflectionClass ->newInstanceArgs ($ params );
383
+ }
434
384
435
- if (!$ reflectionClass ->isInstantiable ()) {
436
- throw NotNormalizableValueException::createForUnexpectedDataType (\sprintf ('Failed to create object because the class "%s" is not instantiable. ' , $ class ), $ data , ['unknown ' ], $ context ['deserialization_path ' ] ?? null );
385
+ return $ constructor ->invokeArgs (null , $ params );
437
386
}
438
387
439
388
return new $ class ();
0 commit comments