Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0a83b17

Browse filesBrowse files
committed
feature #27017 [Serializer] Allow to access to the context and various other infos in callbacks and max depth handler (dunglas)
This PR was merged into the 4.1-dev branch. Discussion ---------- [Serializer] Allow to access to the context and various other infos in callbacks and max depth handler | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes<!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | n/a <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | todo Allows to access to the context, format, parent object and attribute name in max depth handlers and callbacks. ping @meyerbaptiste Commits ------- 0e5f740 [Serializer] Allow to access to the context and various other infos in callbacks and max depth handler
2 parents f7264ed + 0e5f740 commit 0a83b17
Copy full SHA for 0a83b17

File tree

3 files changed

+22
-5
lines changed
Filter options

3 files changed

+22
-5
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+2-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php
+20-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php
-1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.