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

Skip to content

[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

Closed
Closed
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
117 changes: 117 additions & 0 deletions src/Symfony/Component/Serializer/Normalizer/PropertyInfoNormalizer.php
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)
Copy link
Member

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).

{
$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);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn’t we deal with Doctrine proxies here?

Copy link
Contributor Author

@joelwurtz joelwurtz Apr 7, 2019

Choose a reason for hiding this comment

The 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)) {
Copy link
Member

Choose a reason for hiding this comment

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

I'd add null !== $value && before to save some iterations, wdyt?

$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;
}
}