added ability to handle parent classes for PropertyNormalizer
This commit is contained in:
parent
8136fa5050
commit
5ecafc5e25
@ -79,7 +79,7 @@ class PropertyNormalizer extends AbstractObjectNormalizer
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$reflectionProperty = new \ReflectionProperty(is_string($classOrObject) ? $classOrObject : get_class($classOrObject), $attribute);
|
$reflectionProperty = $this->getReflectionProperty($classOrObject, $attribute);
|
||||||
if ($reflectionProperty->isStatic()) {
|
if ($reflectionProperty->isStatic()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -98,13 +98,15 @@ class PropertyNormalizer extends AbstractObjectNormalizer
|
|||||||
$reflectionObject = new \ReflectionObject($object);
|
$reflectionObject = new \ReflectionObject($object);
|
||||||
$attributes = array();
|
$attributes = array();
|
||||||
|
|
||||||
foreach ($reflectionObject->getProperties() as $property) {
|
do {
|
||||||
if (!$this->isAllowedAttribute($object, $property->name)) {
|
foreach ($reflectionObject->getProperties() as $property) {
|
||||||
continue;
|
if (!$this->isAllowedAttribute($reflectionObject->getName(), $property->name)) {
|
||||||
}
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$attributes[] = $property->name;
|
$attributes[] = $property->name;
|
||||||
}
|
}
|
||||||
|
} while ($reflectionObject = $reflectionObject->getParentClass());
|
||||||
|
|
||||||
return $attributes;
|
return $attributes;
|
||||||
}
|
}
|
||||||
@ -115,7 +117,7 @@ class PropertyNormalizer extends AbstractObjectNormalizer
|
|||||||
protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
|
protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$reflectionProperty = new \ReflectionProperty(get_class($object), $attribute);
|
$reflectionProperty = $this->getReflectionProperty($object, $attribute);
|
||||||
} catch (\ReflectionException $reflectionException) {
|
} catch (\ReflectionException $reflectionException) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -134,7 +136,7 @@ class PropertyNormalizer extends AbstractObjectNormalizer
|
|||||||
protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
|
protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$reflectionProperty = new \ReflectionProperty(get_class($object), $attribute);
|
$reflectionProperty = $this->getReflectionProperty($object, $attribute);
|
||||||
} catch (\ReflectionException $reflectionException) {
|
} catch (\ReflectionException $reflectionException) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -150,4 +152,26 @@ class PropertyNormalizer extends AbstractObjectNormalizer
|
|||||||
|
|
||||||
$reflectionProperty->setValue($object, $value);
|
$reflectionProperty->setValue($object, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string|object $classOrObject
|
||||||
|
* @param string $attribute
|
||||||
|
*
|
||||||
|
* @return \ReflectionProperty
|
||||||
|
*
|
||||||
|
* @throws \ReflectionException
|
||||||
|
*/
|
||||||
|
private function getReflectionProperty($classOrObject, $attribute)
|
||||||
|
{
|
||||||
|
$reflectionClass = new \ReflectionClass($classOrObject);
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
return $reflectionClass->getProperty($attribute);
|
||||||
|
} catch (\ReflectionException $e) {
|
||||||
|
if (!$reflectionClass = $reflectionClass->getParentClass()) {
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
<?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\Tests\Fixtures;
|
||||||
|
|
||||||
|
class GroupDummyChild extends GroupDummy
|
||||||
|
{
|
||||||
|
private $baz;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getBaz()
|
||||||
|
{
|
||||||
|
return $this->baz;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $baz
|
||||||
|
*/
|
||||||
|
public function setBaz($baz)
|
||||||
|
{
|
||||||
|
$this->baz = $baz;
|
||||||
|
}
|
||||||
|
}
|
@ -20,6 +20,7 @@ use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
|
|||||||
use Symfony\Component\Serializer\Serializer;
|
use Symfony\Component\Serializer\Serializer;
|
||||||
use Symfony\Component\Serializer\SerializerInterface;
|
use Symfony\Component\Serializer\SerializerInterface;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
|
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
|
||||||
|
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyChild;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
|
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy;
|
use Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\PropertySiblingHolder;
|
use Symfony\Component\Serializer\Tests\Fixtures\PropertySiblingHolder;
|
||||||
@ -65,6 +66,35 @@ class PropertyNormalizerTest extends TestCase
|
|||||||
$this->assertEquals('bar', $obj->getBar());
|
$this->assertEquals('bar', $obj->getBar());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testNormalizeWithParentClass()
|
||||||
|
{
|
||||||
|
$group = new GroupDummyChild();
|
||||||
|
$group->setBaz('baz');
|
||||||
|
$group->setFoo('foo');
|
||||||
|
$group->setBar('bar');
|
||||||
|
$group->setKevin('Kevin');
|
||||||
|
$group->setCoopTilleuls('coop');
|
||||||
|
$this->assertEquals(
|
||||||
|
array('foo' => 'foo', 'bar' => 'bar', 'kevin' => 'Kevin',
|
||||||
|
'coopTilleuls' => 'coop', 'fooBar' => null, 'symfony' => null, 'baz' => 'baz', ),
|
||||||
|
$this->normalizer->normalize($group, 'any')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDenormalizeWithParentClass()
|
||||||
|
{
|
||||||
|
$obj = $this->normalizer->denormalize(
|
||||||
|
array('foo' => 'foo', 'bar' => 'bar', 'kevin' => 'Kevin', 'baz' => 'baz'),
|
||||||
|
GroupDummyChild::class,
|
||||||
|
'any'
|
||||||
|
);
|
||||||
|
$this->assertEquals('foo', $obj->getFoo());
|
||||||
|
$this->assertEquals('bar', $obj->getBar());
|
||||||
|
$this->assertEquals('Kevin', $obj->getKevin());
|
||||||
|
$this->assertEquals('baz', $obj->getBaz());
|
||||||
|
$this->assertNull($obj->getSymfony());
|
||||||
|
}
|
||||||
|
|
||||||
public function testConstructorDenormalize()
|
public function testConstructorDenormalize()
|
||||||
{
|
{
|
||||||
$obj = $this->normalizer->denormalize(
|
$obj = $this->normalizer->denormalize(
|
||||||
@ -147,12 +177,14 @@ class PropertyNormalizerTest extends TestCase
|
|||||||
'bar' => 'bar',
|
'bar' => 'bar',
|
||||||
), $this->normalizer->normalize($obj, null, array(PropertyNormalizer::GROUPS => array('c'))));
|
), $this->normalizer->normalize($obj, null, array(PropertyNormalizer::GROUPS => array('c'))));
|
||||||
|
|
||||||
// The PropertyNormalizer is not able to hydrate properties from parent classes
|
// The PropertyNormalizer is also able to hydrate properties from parent classes
|
||||||
$this->assertEquals(array(
|
$this->assertEquals(array(
|
||||||
'symfony' => 'symfony',
|
'symfony' => 'symfony',
|
||||||
'foo' => 'foo',
|
'foo' => 'foo',
|
||||||
'fooBar' => 'fooBar',
|
'fooBar' => 'fooBar',
|
||||||
'bar' => 'bar',
|
'bar' => 'bar',
|
||||||
|
'kevin' => 'kevin',
|
||||||
|
'coopTilleuls' => 'coopTilleuls',
|
||||||
), $this->normalizer->normalize($obj, null, array(PropertyNormalizer::GROUPS => array('a', 'c'))));
|
), $this->normalizer->normalize($obj, null, array(PropertyNormalizer::GROUPS => array('a', 'c'))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user