-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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); | ||||
|
||||
|
@@ -60,6 +61,7 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory | |||
$classDiscriminatorResolver = new ClassDiscriminatorFromClassMetadata($classMetadataFactory); | ||||
} | ||||
$this->classDiscriminatorResolver = $classDiscriminatorResolver; | ||||
$this->objectClassResolver = $objectClassResolver; | ||||
} | ||||
|
||||
/** | ||||
|
@@ -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); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could be written as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've copied what was done for the symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php Line 100 in 9610d10
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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
$attributesMetadata = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null; | ||||
|
||||
foreach ($attributes as $attribute) { | ||||
|
@@ -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])) { | ||||
|
There was a problem hiding this comment.
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 callThere was a problem hiding this comment.
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 :)