[Serializer] ObjectNormalizer: don't serialize static methods and props
This commit is contained in:
parent
0bd8b58d9b
commit
1fab27b58c
@ -68,6 +68,7 @@ class ObjectNormalizer extends AbstractNormalizer
|
|||||||
$reflClass = new \ReflectionClass($object);
|
$reflClass = new \ReflectionClass($object);
|
||||||
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
|
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
|
||||||
if (
|
if (
|
||||||
|
!$reflMethod->isStatic() &&
|
||||||
!$reflMethod->isConstructor() &&
|
!$reflMethod->isConstructor() &&
|
||||||
!$reflMethod->isDestructor() &&
|
!$reflMethod->isDestructor() &&
|
||||||
0 === $reflMethod->getNumberOfRequiredParameters()
|
0 === $reflMethod->getNumberOfRequiredParameters()
|
||||||
@ -86,7 +87,9 @@ class ObjectNormalizer extends AbstractNormalizer
|
|||||||
|
|
||||||
// properties
|
// properties
|
||||||
foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
|
foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
|
||||||
$attributes[$reflProperty->getName()] = true;
|
if (!$reflProperty->isStatic()) {
|
||||||
|
$attributes[$reflProperty->getName()] = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$attributes = array_keys($attributes);
|
$attributes = array_keys($attributes);
|
||||||
|
@ -456,6 +456,11 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
|
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testNormalizeStatic()
|
||||||
|
{
|
||||||
|
$this->assertEquals(array('foo' => 'K'), $this->normalizer->normalize(new ObjectWithStaticPropertiesAndMethods()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ObjectDummy
|
class ObjectDummy
|
||||||
@ -605,3 +610,14 @@ class ObjectConstructorArgsWithDefaultValueDummy
|
|||||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ObjectWithStaticPropertiesAndMethods
|
||||||
|
{
|
||||||
|
public $foo = 'K';
|
||||||
|
public static $bar = 'A';
|
||||||
|
|
||||||
|
public static function getBaz()
|
||||||
|
{
|
||||||
|
return 'L';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user