Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c472eaa

Browse filesBrowse files
committed
Move some logic up to the AbstractObjectNormalizer
1 parent 91c69c6 commit c472eaa
Copy full SHA for c472eaa

File tree

2 files changed

+39
-46
lines changed
Filter options

2 files changed

+39
-46
lines changed

‎src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+38-2Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1919
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
2020
use Symfony\Component\PropertyInfo\Type;
21+
use Symfony\Component\Serializer\Exception\RuntimeException;
2122
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
23+
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolver;
24+
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
2225
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
2326
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
2427

@@ -38,11 +41,17 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
3841
private $attributesCache = array();
3942
private $cache = array();
4043

41-
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null)
44+
/**
45+
* @var ClassDiscriminatorResolverInterface
46+
*/
47+
protected $classDiscriminatorResolver;
48+
49+
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null)
4250
{
4351
parent::__construct($classMetadataFactory, $nameConverter);
4452

4553
$this->propertyTypeExtractor = $propertyTypeExtractor;
54+
$this->classDiscriminatorResolver = $classDiscriminatorResolver ?: new ClassDiscriminatorResolver();
4655
}
4756

4857
/**
@@ -101,6 +110,27 @@ public function normalize($object, $format = null, array $context = array())
101110
return $data;
102111
}
103112

113+
/**
114+
* {@inheritdoc}
115+
*/
116+
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, string $format = null)
117+
{
118+
if ($mapping = $this->classDiscriminatorResolver->getMappingForClass($class)) {
119+
if (!isset($data[$mapping->getTypeProperty()])) {
120+
throw new RuntimeException(sprintf('Type property "%s" not found for the abstract object "%s"', $mapping['typeProperty'], $class));
121+
}
122+
123+
$type = $data[$mapping->getTypeProperty()];
124+
if (null === ($class = $mapping->getClassForType($type))) {
125+
throw new RuntimeException(sprintf('The type "%s" has no mapped class for the abstract object "%s"', $type, $class));
126+
}
127+
128+
$reflectionClass = new \ReflectionClass($class);
129+
}
130+
131+
return parent::instantiateObject($data, $class, $context, $reflectionClass, $allowedAttributes, $format);
132+
}
133+
104134
/**
105135
* Gets and caches attributes for the given object, format and context.
106136
*
@@ -133,7 +163,13 @@ protected function getAttributes($object, $format = null, array $context)
133163
return $this->attributesCache[$class];
134164
}
135165

136-
return $this->attributesCache[$class] = $this->extractAttributes($object, $format, $context);
166+
$attributes = $this->extractAttributes($object, $format, $context);
167+
168+
if ($mapping = $this->classDiscriminatorResolver->getMappingForMappedObject($object)) {
169+
array_unshift($attributes, $mapping->getTypeProperty());
170+
}
171+
172+
return $this->attributesCache[$class] = $attributes;
137173
}
138174

139175
/**

‎src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php
+1-44Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
use Symfony\Component\PropertyAccess\PropertyAccess;
1616
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1717
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
18-
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolver;
1918
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
2019
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
2120
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
22-
use Symfony\Component\Serializer\Exception\RuntimeException;
2321

2422
/**
2523
* Converts between objects and arrays using the PropertyAccess component.
@@ -33,52 +31,11 @@ class ObjectNormalizer extends AbstractObjectNormalizer
3331
*/
3432
protected $propertyAccessor;
3533

36-
/**
37-
* @var ClassDiscriminatorResolverInterface
38-
*/
39-
protected $classDiscriminatorResolver;
40-
4134
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null)
4235
{
43-
parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor);
36+
parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver);
4437

4538
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
46-
$this->classDiscriminatorResolver = $classDiscriminatorResolver ?: new ClassDiscriminatorResolver();
47-
}
48-
49-
/**
50-
* {@inheritdoc}
51-
*/
52-
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, string $format = null)
53-
{
54-
if ($mapping = $this->classDiscriminatorResolver->getMappingForClass($class)) {
55-
if (!isset($data[$mapping->getTypeProperty()])) {
56-
throw new RuntimeException(sprintf('Type property "%s" not found for the abstract object "%s"', $mapping['typeProperty'], $class));
57-
}
58-
59-
$type = $data[$mapping->getTypeProperty()];
60-
if (null === ($class = $mapping->getClassForType($type))) {
61-
throw new RuntimeException(sprintf('The type "%s" has no mapped class for the abstract object "%s"', $type, $class));
62-
}
63-
64-
$reflectionClass = new \ReflectionClass($class);
65-
}
66-
67-
return parent::instantiateObject($data, $class, $context, $reflectionClass, $allowedAttributes, $format);
68-
}
69-
70-
/**
71-
* {@inheritdoc}
72-
*/
73-
protected function getAttributes($object, $format = null, array $context)
74-
{
75-
$attributes = parent::getAttributes($object, $format, $context);
76-
77-
if ($mapping = $this->classDiscriminatorResolver->getMappingForMappedObject($object)) {
78-
array_unshift($attributes, $mapping->getTypeProperty());
79-
}
80-
81-
return $attributes;
8239
}
8340

8441
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.