-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Add new normalizer: PropertyInfoNormalizer based on PropertyInfo / PropertyAccess components #30960
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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Normalizer; | ||
|
||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; | ||
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface; | ||
use Symfony\Component\Serializer\Extractor\ObjectPropertyListExtractor; | ||
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface; | ||
|
||
|
||
/** | ||
* Converts between objects and arrays using the PropertyInfo and PropertyAccess component. | ||
* | ||
* @author Kévin Dunglas <[email protected]> | ||
*/ | ||
final class PropertyInfoNormalizer implements NormalizerInterface, DenormalizerInterface, NormalizerAwareInterface, DenormalizerAwareInterface, CacheableSupportsMethodInterface | ||
{ | ||
use NormalizerAwareTrait; | ||
use DenormalizerAwareTrait; | ||
|
||
private $instantiator; | ||
|
||
private $normalizerExtractor; | ||
|
||
private $denormalizerExtractor; | ||
|
||
private $propertyAccessor; | ||
|
||
private $nameConverter; | ||
|
||
private $objectClassResolver; | ||
|
||
public function __construct($instantiator, ObjectPropertyListExtractor $normalizerExtractor, PropertyListExtractorInterface $denormalizerExtractor, PropertyAccessorInterface $propertyAccessor, AdvancedNameConverterInterface $nameConverter = null, callable $objectClassReolver = null) | ||
{ | ||
$this->instantiator = $instantiator; | ||
$this->normalizerExtractor = $normalizerExtractor; | ||
$this->denormalizerExtractor = $denormalizerExtractor; | ||
$this->propertyAccessor = $propertyAccessor; | ||
$this->nameConverter = $nameConverter; | ||
$this->objectClassResolver = $objectClassReolver; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasCacheableSupportsMethod(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function denormalize($data, $class, $format = null, array $context = []) | ||
{ | ||
$object = $this->instantiator->instantiate($class, $data, $format, $context); | ||
$properties = $this->denormalizerExtractor->getProperties($class, $context); | ||
|
||
foreach ($data as $key => $value) { | ||
$key = $this->nameConverter ? $this->nameConverter->denormalize($key, $class, $format, $context) : $key; | ||
|
||
if (\in_array($key, $properties, true)) { | ||
$this->propertyAccessor->setValue($object, $key, $value); | ||
} | ||
} | ||
|
||
return $object; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsDenormalization($data, $type, $format = null) | ||
{ | ||
return \class_exists($type) && \is_array($data); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function normalize($object, $format = null, array $context = []) | ||
{ | ||
$properties = $this->normalizerExtractor->getProperties($object, $context); | ||
$class = $this->objectClassResolver ? ($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. Shouldn’t we deal with Doctrine proxies here? 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. That's what we do isn't it ? Allowing an object class resolver will allow to inject a specific callback to handle this problem. |
||
$data = []; | ||
|
||
foreach ($properties as $property) { | ||
$propertyName = $this->nameConverter ? $this->nameConverter->normalize($property, $class, $format, $context) : $property; | ||
$value = $this->propertyAccessor->getValue($object, $property); | ||
|
||
if (!is_scalar($value)) { | ||
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'd add |
||
$value = $this->normalizer->normalize($value, $format, $context); | ||
} | ||
|
||
$data[$propertyName] = $value; | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsNormalization($data, $format = null) | ||
{ | ||
return \is_object($data) && !$data instanceof \Traversable; | ||
} | ||
} |
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.
We should allow to pass the standard NameConverterInterface here (and pass the extra argument required for the advanced one).