15
15
use Symfony \Component \PropertyAccess \Exception \NoSuchPropertyException ;
16
16
use Symfony \Component \PropertyInfo \PropertyTypeExtractorInterface ;
17
17
use Symfony \Component \PropertyInfo \Type ;
18
+ use Symfony \Component \Serializer \Context \ChildContextFactoryInterface ;
19
+ use Symfony \Component \Serializer \Context \ObjectChildContextFactory ;
18
20
use Symfony \Component \Serializer \Encoder \CsvEncoder ;
19
21
use Symfony \Component \Serializer \Encoder \JsonEncoder ;
20
22
use Symfony \Component \Serializer \Encoder \XmlEncoder ;
21
23
use Symfony \Component \Serializer \Exception \ExtraAttributesException ;
22
24
use Symfony \Component \Serializer \Exception \LogicException ;
25
+ use Symfony \Component \Serializer \Exception \MissingConstructorArgumentsException ;
23
26
use Symfony \Component \Serializer \Exception \NotNormalizableValueException ;
24
- use Symfony \Component \Serializer \Exception \ RuntimeException ;
27
+ use Symfony \Component \Serializer \Instantiator \ InstantiatorInterface ;
25
28
use Symfony \Component \Serializer \Mapping \AttributeMetadataInterface ;
26
29
use Symfony \Component \Serializer \Mapping \ClassDiscriminatorFromClassMetadata ;
27
30
use Symfony \Component \Serializer \Mapping \ClassDiscriminatorResolverInterface ;
33
36
*
34
37
* @author Kévin Dunglas <dunglas@gmail.com>
35
38
*/
36
- abstract class AbstractObjectNormalizer extends AbstractNormalizer
39
+ abstract class AbstractObjectNormalizer extends AbstractNormalizer implements DenormalizerAwareInterface
37
40
{
41
+ use DenormalizerAwareTrait;
42
+
38
43
/**
39
44
* Set to true to respect the max depth metadata on fields.
40
45
*/
@@ -93,6 +98,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
93
98
public const PRESERVE_EMPTY_OBJECTS = 'preserve_empty_objects ' ;
94
99
95
100
private $ propertyTypeExtractor ;
101
+ private $ instantiator ;
96
102
private $ typesCache = [];
97
103
private $ attributesCache = [];
98
104
@@ -103,9 +109,10 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
103
109
*/
104
110
protected $ classDiscriminatorResolver ;
105
111
106
- public function __construct (ClassMetadataFactoryInterface $ classMetadataFactory = null , NameConverterInterface $ nameConverter = null , PropertyTypeExtractorInterface $ propertyTypeExtractor = null , ClassDiscriminatorResolverInterface $ classDiscriminatorResolver = null , callable $ objectClassResolver = null , array $ defaultContext = [])
112
+ public function __construct (ClassMetadataFactoryInterface $ classMetadataFactory = null , NameConverterInterface $ nameConverter = null , PropertyTypeExtractorInterface $ propertyTypeExtractor = null , ClassDiscriminatorResolverInterface $ classDiscriminatorResolver = null , callable $ objectClassResolver = null , array $ defaultContext = [], ChildContextFactoryInterface $ childContextFactory = null , InstantiatorInterface $ instantiator = null )
107
113
{
108
- parent ::__construct ($ classMetadataFactory , $ nameConverter , $ defaultContext );
114
+ $ this ->childContextFactory = $ childContextFactory ?? new ObjectChildContextFactory ();
115
+ parent ::__construct ($ classMetadataFactory , $ nameConverter , $ defaultContext , $ this ->childContextFactory );
109
116
110
117
if (isset ($ this ->defaultContext [self ::MAX_DEPTH_HANDLER ]) && !\is_callable ($ this ->defaultContext [self ::MAX_DEPTH_HANDLER ])) {
111
118
throw new InvalidArgumentException (sprintf ('The "%s" given in the default context is not callable. ' , self ::MAX_DEPTH_HANDLER ));
@@ -120,6 +127,11 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
120
127
}
121
128
$ this ->classDiscriminatorResolver = $ classDiscriminatorResolver ;
122
129
$ this ->objectClassResolver = $ objectClassResolver ;
130
+
131
+ if (null === $ instantiator ) {
132
+ throw new InvalidArgumentException (sprintf ('Instantiator parameter is required, please use "%s" to instantiate your Normalizer. ' , get_parent_class ($ this ).'Factory ' ));
133
+ }
134
+ $ this ->instantiator = $ instantiator ;
123
135
}
124
136
125
137
/**
@@ -210,29 +222,6 @@ public function normalize($object, string $format = null, array $context = [])
210
222
return $ data ;
211
223
}
212
224
213
- /**
214
- * {@inheritdoc}
215
- */
216
- protected function instantiateObject (array &$ data , string $ class , array &$ context , \ReflectionClass $ reflectionClass , $ allowedAttributes , string $ format = null )
217
- {
218
- if ($ this ->classDiscriminatorResolver && $ mapping = $ this ->classDiscriminatorResolver ->getMappingForClass ($ class )) {
219
- if (!isset ($ data [$ mapping ->getTypeProperty ()])) {
220
- throw new RuntimeException (sprintf ('Type property "%s" not found for the abstract object "%s". ' , $ mapping ->getTypeProperty (), $ class ));
221
- }
222
-
223
- $ type = $ data [$ mapping ->getTypeProperty ()];
224
- if (null === ($ mappedClass = $ mapping ->getClassForType ($ type ))) {
225
- throw new RuntimeException (sprintf ('The type "%s" has no mapped class for the abstract object "%s". ' , $ type , $ class ));
226
- }
227
-
228
- if ($ mappedClass !== $ class ) {
229
- return $ this ->instantiateObject ($ data , $ mappedClass , $ context , new \ReflectionClass ($ mappedClass ), $ allowedAttributes , $ format );
230
- }
231
- }
232
-
233
- return parent ::instantiateObject ($ data , $ class , $ context , $ reflectionClass , $ allowedAttributes , $ format );
234
- }
235
-
236
225
/**
237
226
* Gets and caches attributes for the given object, format and context.
238
227
*
@@ -307,8 +296,14 @@ public function denormalize($data, string $type, string $format = null, array $c
307
296
$ normalizedData = $ this ->prepareForDenormalization ($ data );
308
297
$ extraAttributes = [];
309
298
310
- $ reflectionClass = new \ReflectionClass ($ type );
311
- $ object = $ this ->instantiateObject ($ normalizedData , $ type , $ context , $ reflectionClass , $ allowedAttributes , $ format );
299
+ $ instantiatorResult = $ this ->instantiator ->instantiate ($ type , $ normalizedData , $ context , $ format );
300
+ if ($ instantiatorResult ->hasFailed ()) {
301
+ throw new MissingConstructorArgumentsException ($ instantiatorResult ->getError ());
302
+ }
303
+ $ object = $ instantiatorResult ->getObject ();
304
+ $ normalizedData = $ instantiatorResult ->getUnusedData ();
305
+ $ context = $ instantiatorResult ->getUnusedContext ();
306
+
312
307
$ resolvedClass = $ this ->objectClassResolver ? ($ this ->objectClassResolver )($ object ) : \get_class ($ object );
313
308
314
309
foreach ($ normalizedData as $ attribute => $ value ) {
@@ -603,13 +598,12 @@ private function isMaxDepthReached(array $attributesMetadata, string $class, str
603
598
* {@inheritdoc}
604
599
*
605
600
* @internal
601
+ *
602
+ * @deprecated the "createChildContext" method is deprecated, use Symfony\Component\Serializer\Context\ObjectChildContextFactory::create() instead
606
603
*/
607
604
protected function createChildContext (array $ parentContext , string $ attribute , ?string $ format ): array
608
605
{
609
- $ context = parent ::createChildContext ($ parentContext , $ attribute , $ format );
610
- $ context ['cache_key ' ] = $ this ->getCacheKey ($ format , $ context );
611
-
612
- return $ context ;
606
+ return $ this ->childContextFactory ->create ($ parentContext , $ attribute , $ format );
613
607
}
614
608
615
609
/**
0 commit comments