Skip to content

Commit 18bc5e3

Browse files
author
Rokas Mikalkėnas
committed
[Serializer] Support canners in object normalizer
1 parent 9cb6274 commit 18bc5e3

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
* Deprecate `ContextAwareDecoderInterface`, use `DecoderInterface` instead
1414
* Deprecate supporting denormalization for `AbstractUid` in `UidNormalizer`, use one of `AbstractUid` child class instead
1515
* Deprecate denormalizing to an abstract class in `UidNormalizer`
16+
* Add support for `can*()` methods to `ObjectNormalizer`
1617

1718
6.0
1819
---

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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ObjectDummy
2222
private $baz;
2323
protected $camelCase;
2424
protected $object;
25+
private $go;
2526

2627
public function getFoo()
2728
{
@@ -72,4 +73,14 @@ public function getObject()
7273
{
7374
return $this->object;
7475
}
76+
77+
public function setGo($go)
78+
{
79+
$this->go = $go;
80+
}
81+
82+
public function canGo()
83+
{
84+
return $this->go;
85+
}
7586
}

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

Lines changed: 6 additions & 0 deletions
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
);
@@ -669,6 +671,7 @@ public function testNormalizeNotSerializableContext()
669671
'camelCase' => null,
670672
'object' => null,
671673
'bar' => null,
674+
'go' => null,
672675
];
673676

674677
$this->assertEquals($expected, $this->normalizer->normalize($objectDummy, null, ['not_serializable' => function () {
@@ -805,6 +808,7 @@ public function testDefaultObjectClassResolver()
805808
$obj->setBaz(true);
806809
$obj->setCamelCase('camelcase');
807810
$obj->unwantedProperty = 'notwanted';
811+
$obj->setGo(false);
808812

809813
$this->assertEquals(
810814
[
@@ -814,6 +818,7 @@ public function testDefaultObjectClassResolver()
814818
'fooBar' => 'foobar',
815819
'camelCase' => 'camelcase',
816820
'object' => null,
821+
'go' => false,
817822
],
818823
$normalizer->normalize($obj, 'any')
819824
);
@@ -842,6 +847,7 @@ public function testObjectClassResolver()
842847
'fooBar' => 'foobar',
843848
'camelCase' => 'camelcase',
844849
'object' => null,
850+
'go' => null,
845851
],
846852
$normalizer->normalize($obj, 'any')
847853
);

0 commit comments

Comments
 (0)