Skip to content

Recursive denormalize with property info #17193

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
wants to merge 13 commits into from
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Annotation/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Groups
*/
public function __construct(array $data)
{
if (!isset($data['value']) || !$data['value']) {
if (empty($data['value'])) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', get_class($this)));
}

Expand Down
48 changes: 48 additions & 0 deletions src/Symfony/Component/Serializer/Annotation/MaxDepth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\Annotation;

use Symfony\Component\Serializer\Exception\InvalidArgumentException;

/**
* Annotation class for @MaxDepth().
*
* @Annotation
* @Target({"PROPERTY", "METHOD"})
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class MaxDepth
{
/**
* @var int
*/
private $maxDepth;

public function __construct(array $data)
{
if (empty($data['value'])) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', get_class($this)));
}

if (!is_int($data['value']) || $data['value'] <= 0) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a positive integer.', get_class($this)));
}

$this->maxDepth = $data['value'];
}

public function getMaxDepth()
{
return $this->maxDepth;
}
}
32 changes: 31 additions & 1 deletion src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class AttributeMetadata implements AttributeMetadataInterface
*/
public $groups = array();

/**
* @var int|null
*
* @internal This property is public in order to reduce the size of the
* class' serialized representation. Do not access it. Use
* {@link getMaxDepth()} instead.
*/
public $maxDepth;

/**
* Constructs a metadata for the given attribute.
*
Expand Down Expand Up @@ -72,6 +81,22 @@ public function getGroups()
return $this->groups;
}

/**
* {@inheritdoc}
*/
public function setMaxDepth($maxDepth)
{
$this->maxDepth = $maxDepth;
}

/**
* {@inheritdoc}
*/
public function getMaxDepth()
{
return $this->maxDepth;
}

/**
* {@inheritdoc}
*/
Expand All @@ -80,6 +105,11 @@ public function merge(AttributeMetadataInterface $attributeMetadata)
foreach ($attributeMetadata->getGroups() as $group) {
$this->addGroup($group);
}

// Overwrite only if not defined
if (null === $this->maxDepth) {
$this->maxDepth = $attributeMetadata->getMaxDepth();
}
}

/**
Expand All @@ -89,6 +119,6 @@ public function merge(AttributeMetadataInterface $attributeMetadata)
*/
public function __sleep()
{
return array('name', 'groups');
return array('name', 'groups', 'maxDepth');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ public function addGroup($group);
*/
public function getGroups();

/**
* Sets the serialization max depth for this attribute.
*
* @param int|null $maxDepth
*/
public function setMaxDepth($maxDepth);

/**
* Gets the serialization max depth for this attribute.
*
* @return int|null
*/
public function getMaxDepth();

/**
* Merges an {@see AttributeMetadataInterface} with in the current one.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ClassMetadata implements ClassMetadataInterface
public $name;

/**
* @var AttributeMetadataInterface[]
* @var array
*
* @internal This property is public in order to reduce the size of the
* class' serialized representation. Do not access it. Use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function addAttributeMetadata(AttributeMetadataInterface $attributeMetada
/**
* Gets the list of {@link AttributeMetadataInterface}.
*
* @return AttributeMetadataInterface[]
* @return array An array containing the attribute name as key and the related instance of AttributeMetadataInterface as value.
*/
public function getAttributesMetadata();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Common\Annotations\Reader;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\MaxDepth;
use Symfony\Component\Serializer\Exception\MappingException;
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
Expand Down Expand Up @@ -55,11 +56,13 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
}

if ($property->getDeclaringClass()->name === $className) {
foreach ($this->reader->getPropertyAnnotations($property) as $groups) {
if ($groups instanceof Groups) {
foreach ($groups->getGroups() as $group) {
foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
if ($annotation instanceof Groups) {
foreach ($annotation->getGroups() as $group) {
$attributesMetadata[$property->name]->addGroup($group);
}
} elseif ($annotation instanceof MaxDepth) {
$attributesMetadata[$property->name]->setMaxDepth($annotation->getMaxDepth());
}

$loaded = true;
Expand All @@ -68,29 +71,40 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
}

foreach ($reflectionClass->getMethods() as $method) {
if ($method->getDeclaringClass()->name === $className) {
foreach ($this->reader->getMethodAnnotations($method) as $groups) {
if ($groups instanceof Groups) {
if (preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches)) {
$attributeName = lcfirst($matches[2]);

if (isset($attributesMetadata[$attributeName])) {
$attributeMetadata = $attributesMetadata[$attributeName];
} else {
$attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName);
$classMetadata->addAttributeMetadata($attributeMetadata);
}

foreach ($groups->getGroups() as $group) {
$attributeMetadata->addGroup($group);
}
} else {
throw new MappingException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
}
if ($method->getDeclaringClass()->name !== $className) {
continue;
}

$accessorOrMutator = preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches);
if ($accessorOrMutator) {
$attributeName = lcfirst($matches[2]);

if (isset($attributesMetadata[$attributeName])) {
$attributeMetadata = $attributesMetadata[$attributeName];
} else {
$attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName);
$classMetadata->addAttributeMetadata($attributeMetadata);
}
}

foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
if ($annotation instanceof Groups) {
if (!$accessorOrMutator) {
throw new MappingException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
}

$loaded = true;
foreach ($annotation->getGroups() as $group) {
$attributeMetadata->addGroup($group);
}
} elseif ($annotation instanceof MaxDepth) {
if (!$accessorOrMutator) {
throw new MappingException(sprintf('MaxDepth on "%s::%s" cannot be added. MaxDepth can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
}

$attributeMetadata->setMaxDepth($annotation->getMaxDepth());
}

$loaded = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
foreach ($attribute->group as $group) {
$attributeMetadata->addGroup((string) $group);
}

if (isset($attribute['max-depth'])) {
$attributeMetadata->setMaxDepth((int) $attribute['max-depth']);
}
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)

if (isset($yaml['attributes']) && is_array($yaml['attributes'])) {
$attributesMetadata = $classMetadata->getAttributesMetadata();

foreach ($yaml['attributes'] as $attribute => $data) {
if (isset($attributesMetadata[$attribute])) {
$attributeMetadata = $attributesMetadata[$attribute];
Expand All @@ -75,9 +76,13 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)

if (isset($data['groups'])) {
foreach ($data['groups'] as $group) {
$attributeMetadata->addGroup($group);
$attributeMetadata->addGroup((string) $group);
}
}

if (isset($data['max_depth'])) {
$attributeMetadata->setMaxDepth((int) $data['max_depth']);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@
Contains serialization groups for a attributes. The name of the attribute should be given in the "name" option.
]]></xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:sequence minOccurs="0">
<xsd:element name="group" type="xsd:string" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:attribute name="max-depth" type="xsd:int" />
</xsd:complexType>

</xsd:schema>
Loading