Skip to content

Commit 0e5f740

Browse files
committed
[Serializer] Allow to access to the context and various other infos in callbacks and max depth handler
1 parent 9ae116f commit 0e5f740

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ public function normalize($object, $format = null, array $context = array())
9797

9898
$attributeValue = $this->getAttributeValue($object, $attribute, $format, $context);
9999
if ($maxDepthReached) {
100-
$attributeValue = \call_user_func($this->maxDepthHandler, $attributeValue);
100+
$attributeValue = \call_user_func($this->maxDepthHandler, $attributeValue, $object, $attribute, $format, $context);
101101
}
102102

103103
if (isset($this->callbacks[$attribute])) {
104-
$attributeValue = call_user_func($this->callbacks[$attribute], $attributeValue);
104+
$attributeValue = \call_user_func($this->callbacks[$attribute], $attributeValue, $object, $attribute, $format, $context);
105105
}
106106

107107
if (null !== $attributeValue && !is_scalar($attributeValue)) {

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,8 @@ public function provideCallbacks()
426426
array(
427427
array(
428428
'bar' => function ($bar) {
429+
$this->assertEquals('baz', $bar);
430+
429431
return 'baz';
430432
},
431433
),
@@ -435,8 +437,12 @@ public function provideCallbacks()
435437
),
436438
array(
437439
array(
438-
'bar' => function ($bar) {
439-
return;
440+
'bar' => function ($value, $object, $attributeName, $format, $context) {
441+
$this->assertSame('baz', $value);
442+
$this->assertInstanceOf(ObjectConstructorDummy::class, $object);
443+
$this->assertSame('bar', $attributeName);
444+
$this->assertSame('any', $format);
445+
$this->assertArrayHasKey('circular_reference_limit', $context);
440446
},
441447
),
442448
'baz',
@@ -634,6 +640,18 @@ public function testMaxDepth()
634640

635641
$result = $serializer->normalize($level1, null, array(ObjectNormalizer::ENABLE_MAX_DEPTH => true));
636642
$this->assertEquals($expected, $result);
643+
644+
$this->normalizer->setMaxDepthHandler(function ($object, $parentObject, $attributeName, $format, $context) {
645+
$this->assertSame('level3', $object);
646+
$this->assertInstanceOf(MaxDepthDummy::class, $parentObject);
647+
$this->assertSame('foo', $attributeName);
648+
$this->assertSame('test', $format);
649+
$this->assertArrayHasKey(ObjectNormalizer::ENABLE_MAX_DEPTH, $context);
650+
651+
return 'handler';
652+
});
653+
654+
$serializer->normalize($level1, 'test', array(ObjectNormalizer::ENABLE_MAX_DEPTH => true));
637655
}
638656

639657
/**

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ public function provideCallbacks()
276276
array(
277277
array(
278278
'bar' => function ($bar) {
279-
return;
280279
},
281280
),
282281
'baz',

0 commit comments

Comments
 (0)