Skip to content

Commit 9abb93b

Browse files
committed
[Serializer] Add a MaxDepth handler
1 parent 4ef0b3e commit 9abb93b

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
4141
private $attributesCache = array();
4242
private $cache = array();
4343

44+
/**
45+
* @var callable|null
46+
*/
47+
private $maxDepthHandler;
48+
4449
/**
4550
* @var ClassDiscriminatorResolverInterface|null
4651
*/
@@ -86,11 +91,15 @@ public function normalize($object, $format = null, array $context = array())
8691
$attributesMetadata = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null;
8792

8893
foreach ($attributes as $attribute) {
89-
if (null !== $attributesMetadata && $this->isMaxDepthReached($attributesMetadata, $class, $attribute, $context)) {
94+
$maxDepthReached = false;
95+
if (null !== $attributesMetadata && ($maxDepthReached = $this->isMaxDepthReached($attributesMetadata, $class, $attribute, $context)) && !$this->maxDepthHandler) {
9096
continue;
9197
}
9298

9399
$attributeValue = $this->getAttributeValue($object, $attribute, $format, $context);
100+
if ($maxDepthReached) {
101+
$attributeValue = \call_user_func($this->maxDepthHandler, $attributeValue);
102+
}
94103

95104
if (isset($this->callbacks[$attribute])) {
96105
$attributeValue = call_user_func($this->callbacks[$attribute], $attributeValue);
@@ -204,6 +213,14 @@ abstract protected function extractAttributes($object, $format = null, array $co
204213
*/
205214
abstract protected function getAttributeValue($object, $attribute, $format = null, array $context = array());
206215

216+
/**
217+
* Sets an handler function that will be called when the max depth is reached.
218+
*/
219+
public function setMaxDepthHandler(?callable $handler): void
220+
{
221+
$this->maxDepthHandler = $handler;
222+
}
223+
207224
/**
208225
* {@inheritdoc}
209226
*/

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,27 @@ public function testMaxDepth()
596596
);
597597

598598
$this->assertEquals($expected, $result);
599+
600+
$expected = array(
601+
'bar' => null,
602+
'foo' => 'level1',
603+
'child' => array(
604+
'bar' => null,
605+
'foo' => 'level2',
606+
'child' => array(
607+
'bar' => null,
608+
'child' => null,
609+
'foo' => 'handler',
610+
),
611+
),
612+
);
613+
614+
$this->normalizer->setMaxDepthHandler(function ($obj) {
615+
return 'handler';
616+
});
617+
618+
$result = $serializer->normalize($level1, null, array(ObjectNormalizer::ENABLE_MAX_DEPTH => true));
619+
$this->assertEquals($expected, $result);
599620
}
600621

601622
/**

0 commit comments

Comments
 (0)