Skip to content

[Serializer] Object class resolver #28669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ CHANGELOG
either `NormalizerInterface` or `DenormalizerInterface`
* deprecated creating a `Serializer` with encoders which do not implement
either `EncoderInterface` or `DecoderInterface`
* added the optional `$objectClassResolver` argument in `AbstractObjectNormalizer`
and `ObjectNormalizer` constructor

4.1.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
* @var callable|null
*/
private $maxDepthHandler;
private $objectClassResolver;

/**
* @var ClassDiscriminatorResolverInterface|null
*/
protected $classDiscriminatorResolver;

public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null)
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null)
{
parent::__construct($classMetadataFactory, $nameConverter);

Expand All @@ -60,6 +61,7 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
$classDiscriminatorResolver = new ClassDiscriminatorFromClassMetadata($classMetadataFactory);
}
$this->classDiscriminatorResolver = $classDiscriminatorResolver;
$this->objectClassResolver = $objectClassResolver;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's late, but how about $this->objectClassResolver = $objectClassResolver >: '\get_class'; or something like that ? So that we don't need to test if it is not null on each call

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Taluu : You should provide a PR :)

}

/**
Expand All @@ -86,7 +88,7 @@ public function normalize($object, $format = null, array $context = array())
$data = array();
$stack = array();
$attributes = $this->getAttributes($object, $format, $context);
$class = \get_class($object);
$class = $this->objectClassResolver ? \call_user_func($this->objectClassResolver, $object) : \get_class($object);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be written as ($this->objectClassResolver)($object)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've copied what was done for the maxDepthHandler.

$attributeValue = \call_user_func($this->maxDepthHandler, $attributeValue, $object, $attribute, $format, $context);

It seems to me there is no important difference between the two ways of doing it: https://stackoverflow.com/questions/1596221/php-call-user-func-vs-just-calling-function/35031466

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

($this->objectClassResolver)($object) is more elegant, but indeed being consistent is good

$attributesMetadata = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null;

foreach ($attributes as $attribute) {
Expand Down Expand Up @@ -155,7 +157,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
*/
protected function getAttributes($object, $format = null, array $context)
{
$class = \get_class($object);
$class = $this->objectClassResolver ? \call_user_func($this->objectClassResolver, $object) : \get_class($object);
$key = $class.'-'.$context['cache_key'];

if (isset($this->attributesCache[$key])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ class ObjectNormalizer extends AbstractObjectNormalizer
{
protected $propertyAccessor;

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

parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver);
parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver);

$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
}
Expand Down