diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 9bee69dbd4fbb..c2b3ebfb3c6c2 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -13,6 +13,7 @@ CHANGELOG * Deprecate `ContextAwareDecoderInterface`, use `DecoderInterface` instead * Deprecate supporting denormalization for `AbstractUid` in `UidNormalizer`, use one of `AbstractUid` child class instead * Deprecate denormalizing to an abstract class in `UidNormalizer` + * Add support for `can*()` methods to `ObjectNormalizer` 6.0 --- diff --git a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php index 0dbc8bc3e7f92..263959c1d6e79 100644 --- a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php @@ -86,8 +86,8 @@ protected function extractAttributes(object $object, string $format = null, arra $name = $reflMethod->name; $attributeName = null; - if (str_starts_with($name, 'get') || str_starts_with($name, 'has')) { - // getters and hassers + if (str_starts_with($name, 'get') || str_starts_with($name, 'has') || str_starts_with($name, 'can')) { + // getters, hassers and canners $attributeName = substr($name, 3); if (!$reflClass->hasProperty($attributeName)) { diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectDummy.php b/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectDummy.php index 69661fd1601c1..39f3c354a8024 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectDummy.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectDummy.php @@ -22,6 +22,7 @@ class ObjectDummy private $baz; protected $camelCase; protected $object; + private $go; public function getFoo() { @@ -72,4 +73,14 @@ public function getObject() { return $this->object; } + + public function setGo($go) + { + $this->go = $go; + } + + public function canGo() + { + return $this->go; + } } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php index 244bf94987d44..f7b5dc88d4108 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -107,6 +107,7 @@ public function testNormalize() $obj->setBaz(true); $obj->setCamelCase('camelcase'); $obj->setObject($object); + $obj->setGo(true); $this->serializer ->expects($this->once()) @@ -123,6 +124,7 @@ public function testNormalize() 'fooBar' => 'foobar', 'camelCase' => 'camelcase', 'object' => 'string_object', + 'go' => true, ], $this->normalizer->normalize($obj, 'any') ); @@ -669,6 +671,7 @@ public function testNormalizeNotSerializableContext() 'camelCase' => null, 'object' => null, 'bar' => null, + 'go' => null, ]; $this->assertEquals($expected, $this->normalizer->normalize($objectDummy, null, ['not_serializable' => function () { @@ -805,6 +808,7 @@ public function testDefaultObjectClassResolver() $obj->setBaz(true); $obj->setCamelCase('camelcase'); $obj->unwantedProperty = 'notwanted'; + $obj->setGo(false); $this->assertEquals( [ @@ -814,6 +818,7 @@ public function testDefaultObjectClassResolver() 'fooBar' => 'foobar', 'camelCase' => 'camelcase', 'object' => null, + 'go' => false, ], $normalizer->normalize($obj, 'any') ); @@ -842,6 +847,7 @@ public function testObjectClassResolver() 'fooBar' => 'foobar', 'camelCase' => 'camelcase', 'object' => null, + 'go' => null, ], $normalizer->normalize($obj, 'any') );