Skip to content

Commit 61d2e0b

Browse files
author
Rokas Mikalkėnas
committed
[Serializer] Support canners in object normalizer
1 parent 1df76df commit 61d2e0b

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
@@ -12,6 +12,7 @@ CHANGELOG
1212
* Deprecate `ContextAwareDecoderInterface`, use `DecoderInterface` instead
1313
* Deprecate supporting denormalization for `AbstractUid` in `UidNormalizer`, use one of `AbstractUid` child class instead
1414
* Deprecate denormalizing to an abstract class in `UidNormalizer`
15+
* Add support for `can*()` methods to `ObjectNormalizer`
1516

1617
6.0
1718
---

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
@@ -13,6 +13,7 @@ class ObjectDummy
1313
private $baz;
1414
protected $camelCase;
1515
protected $object;
16+
private $go;
1617

1718
public function getFoo()
1819
{
@@ -63,4 +64,14 @@ public function getObject()
6364
{
6465
return $this->object;
6566
}
67+
68+
public function setGo($go)
69+
{
70+
$this->go = $go;
71+
}
72+
73+
public function canGo()
74+
{
75+
return $this->go;
76+
}
6677
}

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)