Skip to content

[Serializer] ObjectNormalizer #13257

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 1 commit into from
Mar 6, 2015
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 @@ -15,6 +15,8 @@ CHANGELOG
`PropertyNormalizer::setCamelizedAttributes()` are replaced by
`CamelCaseToSnakeCaseNameConverter`
* [DEPRECATION] the `Exception` interface has been renamed to `ExceptionInterface`
* added `ObjectNormalizer` leveraging the `PropertyAccess` component to normalize
objects containing both properties and getters / setters / issers / hassers methods.

2.6.0
-----
Expand Down
162 changes: 162 additions & 0 deletions src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Serializer\Exception\CircularReferenceException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;

/**
* Converts between objects and arrays using the PropertyAccess component.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class ObjectNormalizer extends AbstractNormalizer
{
/**
* @var PropertyAccessorInterface
*/
protected $propertyAccessor;

public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null)
{
parent::__construct($classMetadataFactory, $nameConverter);

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

/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return is_object($data);
}

/**
* {@inheritdoc}
*
* @throws CircularReferenceException
*/
public function normalize($object, $format = null, array $context = array())
{
if ($this->isCircularReference($object, $context)) {
return $this->handleCircularReference($object);
}

$data = array();
$attributes = $this->getAllowedAttributes($object, $context, true);

// If not using groups, detect manually
if (false === $attributes) {
$attributes = array();

// methods
$reflClass = new \ReflectionClass($object);
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
if (
!$reflMethod->isConstructor() &&
!$reflMethod->isDestructor() &&
Copy link
Member

Choose a reason for hiding this comment

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

you should also avoid __clone

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe the best thing to do is to ignore "jQuery style" getsetters and loop only over standard getters, hassers and issers.
At least __toString() should be avoided too IMO. static methods to.

Copy link
Member

Choose a reason for hiding this comment

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

@dunglas I agree that jQuery style getters/setters should not be supported.

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

0 === $reflMethod->getNumberOfRequiredParameters()
) {
$name = $reflMethod->getName();

if (strpos($name, 'get') === 0 || strpos($name, 'has') === 0) {
// getters and hassers
$attributes[lcfirst(substr($name, 3))] = true;
} elseif (strpos($name, 'is') === 0) {
// issers
$attributes[lcfirst(substr($name, 2))] = true;
}
}
}

// properties
foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
$attributes[$reflProperty->getName()] = true;
}

$attributes = array_keys($attributes);
}

foreach ($attributes as $attribute) {
if (in_array($attribute, $this->ignoredAttributes)) {
continue;
}

$attributeValue = $this->propertyAccessor->getValue($object, $attribute);
Copy link
Member

Choose a reason for hiding this comment

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

Is it on purpose you don't use an else block on the next statement? It can avoid a call to the this->propertyAccessor->getValue

Copy link
Member Author

Choose a reason for hiding this comment

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

@jeremy-derusse I don't understand what you mean.

Copy link
Member

Choose a reason for hiding this comment

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

You can replace

$attributeValue = $this->propertyAccessor->getValue($object, $attribute);
if (array_key_exists($attribute, $this->callbacks)) {
    $attributeValue = call_user_func($this->callbacks[$attribute], $attributeValue);
}

by

if (array_key_exists($attribute, $this->callbacks)) {
    $attributeValue = call_user_func($this->callbacks[$attribute], $attributeValue);
} else {
    $attributeValue = $this->propertyAccessor->getValue($object, $attribute);
}

and avoid to call $this->propertyAccessor->getValue($object, $attribute); when array_key_exists($attribute, $this->callbacks) is false.

Copy link
Member Author

Choose a reason for hiding this comment

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

No because $attributeValue in $attributeValue = call_user_func($this->callbacks[$attribute], $attributeValue); will be undefined.

Copy link
Member Author

Choose a reason for hiding this comment

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

Or worse, the value of the previous loop iteration.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok I got it. But still no because the attribute value will never be retrieved. We need it even if a callback is not registered.


if (isset($this->callbacks[$attribute])) {
$attributeValue = call_user_func($this->callbacks[$attribute], $attributeValue);
}

if (null !== $attributeValue && !is_scalar($attributeValue)) {
if (!$this->serializer instanceof NormalizerInterface) {
throw new LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attribute));
}

$attributeValue = $this->serializer->normalize($attributeValue, $format, $context);
}

if ($this->nameConverter) {
$attribute = $this->nameConverter->normalize($attribute);
}

$data[$attribute] = $attributeValue;
}

return $data;
}

/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return class_exists($type);
}

/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
$allowedAttributes = $this->getAllowedAttributes($class, $context, true);
$normalizedData = $this->prepareForDenormalization($data);

$reflectionClass = new \ReflectionClass($class);
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);

foreach ($normalizedData as $attribute => $value) {
$allowed = $allowedAttributes === false || in_array($attribute, $allowedAttributes);
$ignored = in_array($attribute, $this->ignoredAttributes);

if ($allowed && !$ignored) {
if ($this->nameConverter) {
$attribute = $this->nameConverter->normalize($attribute);
}

try {
$this->propertyAccessor->setValue($object, $attribute, $value);
} catch (NoSuchPropertyException $exception) {
// Properties not found are ignored
}
}
}

return $object;
}
}
Loading