From c3cb4f5f08eee6fb016a0a8e60506dcbfcd7c729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rokas=20Mikalk=C4=97nas?= Date: Wed, 2 Feb 2022 12:17:50 +0200 Subject: [PATCH] [Serializer] Support canners in object normalizer --- src/Symfony/Component/Serializer/CHANGELOG.md | 1 + .../Serializer/Normalizer/ObjectNormalizer.php | 4 ++-- .../Tests/Normalizer/Features/ObjectDummy.php | 11 +++++++++++ .../Tests/Normalizer/ObjectNormalizerTest.php | 6 ++++++ 4 files changed, 20 insertions(+), 2 deletions(-) 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') ); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy