Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5a0cad2

Browse files
committed
feature #28669 [Serializer] Object class resolver (alanpoulain)
This PR was squashed before being merged into the 4.2-dev branch (closes #28669). Discussion ---------- [Serializer] Object class resolver | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | When normalizing an object, it could be useful to use a custom method to resolve the class name of it instead of using `get_class`. For instance, Doctrine is using proxy classes for lazy-loading and we usually want the real class and not the proxied one. That's why we are using this trait in API Platform: https://github.com/api-platform/core/blob/master/src/Util/ClassInfoTrait.php With this feature, we could solve an issue in API Platform with the JSON-LD normalizer when the eager fetching is disabled. Commits ------- 18d2143 [Serializer] Object class resolver
2 parents 0f653d8 + 18d2143 commit 5a0cad2

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ CHANGELOG
2020
either `NormalizerInterface` or `DenormalizerInterface`
2121
* deprecated creating a `Serializer` with encoders which do not implement
2222
either `EncoderInterface` or `DecoderInterface`
23+
* added the optional `$objectClassResolver` argument in `AbstractObjectNormalizer`
24+
and `ObjectNormalizer` constructor
2325

2426
4.1.0
2527
-----

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
4545
* @var callable|null
4646
*/
4747
private $maxDepthHandler;
48+
private $objectClassResolver;
4849

4950
/**
5051
* @var ClassDiscriminatorResolverInterface|null
5152
*/
5253
protected $classDiscriminatorResolver;
5354

54-
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null)
55+
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null)
5556
{
5657
parent::__construct($classMetadataFactory, $nameConverter);
5758

@@ -61,6 +62,7 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
6162
$classDiscriminatorResolver = new ClassDiscriminatorFromClassMetadata($classMetadataFactory);
6263
}
6364
$this->classDiscriminatorResolver = $classDiscriminatorResolver;
65+
$this->objectClassResolver = $objectClassResolver;
6466
}
6567

6668
/**
@@ -87,7 +89,7 @@ public function normalize($object, $format = null, array $context = array())
8789
$data = array();
8890
$stack = array();
8991
$attributes = $this->getAttributes($object, $format, $context);
90-
$class = \get_class($object);
92+
$class = $this->objectClassResolver ? \call_user_func($this->objectClassResolver, $object) : \get_class($object);
9193
$attributesMetadata = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null;
9294

9395
foreach ($attributes as $attribute) {
@@ -156,7 +158,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
156158
*/
157159
protected function getAttributes($object, $format = null, array $context)
158160
{
159-
$class = \get_class($object);
161+
$class = $this->objectClassResolver ? \call_user_func($this->objectClassResolver, $object) : \get_class($object);
160162
$key = $class.'-'.$context['cache_key'];
161163

162164
if (isset($this->attributesCache[$key])) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ class ObjectNormalizer extends AbstractObjectNormalizer
3030
{
3131
protected $propertyAccessor;
3232

33-
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null)
33+
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null)
3434
{
3535
if (!\class_exists(PropertyAccess::class)) {
3636
throw new LogicException('The ObjectNormalizer class requires the "PropertyAccess" component. Install "symfony/property-access" to use it.');
3737
}
3838

39-
parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver);
39+
parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver);
4040

4141
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
4242
}

0 commit comments

Comments
 (0)