[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);
|
||||
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
|
||||
if (
|
||||
!$reflMethod->isStatic() &&
|
||||
!$reflMethod->isConstructor() &&
|
||||
!$reflMethod->isDestructor() &&
|
||||
0 === $reflMethod->getNumberOfRequiredParameters()
|
||||
@ -86,8 +87,10 @@ class ObjectNormalizer extends AbstractNormalizer
|
||||
|
||||
// properties
|
||||
foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
|
||||
if (!$reflProperty->isStatic()) {
|
||||
$attributes[$reflProperty->getName()] = true;
|
||||
}
|
||||
}
|
||||
|
||||
$attributes = array_keys($attributes);
|
||||
}
|
||||
|
@ -456,6 +456,11 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
|
||||
}
|
||||
|
||||
public function testNormalizeStatic()
|
||||
{
|
||||
$this->assertEquals(array('foo' => 'K'), $this->normalizer->normalize(new ObjectWithStaticPropertiesAndMethods()));
|
||||
}
|
||||
}
|
||||
|
||||
class ObjectDummy
|
||||
@ -605,3 +610,14 @@ class ObjectConstructorArgsWithDefaultValueDummy
|
||||
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