Skip to content

Commit 60ee9ed

Browse files
author
Rokas Mikalkėnas
committed
[Serializer] Support canners in object normalizer
1 parent abd7a2c commit 60ee9ed

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* Deprecate `ContextAwareDenormalizerInterface`, use `DenormalizerInterface` instead
1111
* Deprecate `ContextAwareEncoderInterface`, use `EncoderInterface` instead
1212
* Deprecate `ContextAwareDecoderInterface`, use `DecoderInterface` instead
13+
* Add support for `can*()` methods to `ObjectNormalizer`
1314

1415
6.0
1516
---

src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ protected function extractAttributes(object $object, string $format = null, arra
8686
$name = $reflMethod->name;
8787
$attributeName = null;
8888

89-
if (str_starts_with($name, 'get') || str_starts_with($name, 'has')) {
90-
// getters and hassers
89+
if (str_starts_with($name, 'get') || str_starts_with($name, 'has') || str_starts_with($name, 'can')) {
90+
// getters, hassers and canners
9191
$attributeName = substr($name, 3);
9292

9393
if (!$reflClass->hasProperty($attributeName)) {

src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectDummy.php

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class ObjectDummy
1212
private $baz;
1313
protected $camelCase;
1414
protected $object;
15+
private $go;
1516

1617
public function getFoo()
1718
{
@@ -62,4 +63,14 @@ public function getObject()
6263
{
6364
return $this->object;
6465
}
66+
67+
public function setGo($go)
68+
{
69+
$this->go = $go;
70+
}
71+
72+
public function canGo()
73+
{
74+
return $this->go;
75+
}
6576
}

src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public function testNormalize()
107107
$obj->setBaz(true);
108108
$obj->setCamelCase('camelcase');
109109
$obj->setObject($object);
110+
$obj->setGo(true);
110111

111112
$this->serializer
112113
->expects($this->once())
@@ -123,6 +124,7 @@ public function testNormalize()
123124
'fooBar' => 'foobar',
124125
'camelCase' => 'camelcase',
125126
'object' => 'string_object',
127+
'go' => true,
126128
],
127129
$this->normalizer->normalize($obj, 'any')
128130
);
@@ -664,6 +666,7 @@ public function testNormalizeNotSerializableContext()
664666
'camelCase' => null,
665667
'object' => null,
666668
'bar' => null,
669+
'go' => null,
667670
];
668671

669672
$this->assertEquals($expected, $this->normalizer->normalize($objectDummy, null, ['not_serializable' => function () {
@@ -800,6 +803,7 @@ public function testDefaultObjectClassResolver()
800803
$obj->setBaz(true);
801804
$obj->setCamelCase('camelcase');
802805
$obj->unwantedProperty = 'notwanted';
806+
$obj->setGo(false);
803807

804808
$this->assertEquals(
805809
[
@@ -809,6 +813,7 @@ public function testDefaultObjectClassResolver()
809813
'fooBar' => 'foobar',
810814
'camelCase' => 'camelcase',
811815
'object' => null,
816+
'go' => false,
812817
],
813818
$normalizer->normalize($obj, 'any')
814819
);
@@ -837,6 +842,7 @@ public function testObjectClassResolver()
837842
'fooBar' => 'foobar',
838843
'camelCase' => 'camelcase',
839844
'object' => null,
845+
'go' => null,
840846
],
841847
$normalizer->normalize($obj, 'any')
842848
);

0 commit comments

Comments
 (0)