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
-------
0e5f74071a
[Serializer] Allow to access to the context and various other infos in callbacks and max depth handler
This commit is contained in:
commit
0a83b17fb6
@ -97,11 +97,11 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
|
|||||||
|
|
||||||
$attributeValue = $this->getAttributeValue($object, $attribute, $format, $context);
|
$attributeValue = $this->getAttributeValue($object, $attribute, $format, $context);
|
||||||
if ($maxDepthReached) {
|
if ($maxDepthReached) {
|
||||||
$attributeValue = \call_user_func($this->maxDepthHandler, $attributeValue);
|
$attributeValue = \call_user_func($this->maxDepthHandler, $attributeValue, $object, $attribute, $format, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($this->callbacks[$attribute])) {
|
if (isset($this->callbacks[$attribute])) {
|
||||||
$attributeValue = call_user_func($this->callbacks[$attribute], $attributeValue);
|
$attributeValue = \call_user_func($this->callbacks[$attribute], $attributeValue, $object, $attribute, $format, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null !== $attributeValue && !is_scalar($attributeValue)) {
|
if (null !== $attributeValue && !is_scalar($attributeValue)) {
|
||||||
|
@ -426,6 +426,8 @@ class ObjectNormalizerTest extends TestCase
|
|||||||
array(
|
array(
|
||||||
array(
|
array(
|
||||||
'bar' => function ($bar) {
|
'bar' => function ($bar) {
|
||||||
|
$this->assertEquals('baz', $bar);
|
||||||
|
|
||||||
return 'baz';
|
return 'baz';
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@ -435,8 +437,12 @@ class ObjectNormalizerTest extends TestCase
|
|||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
array(
|
array(
|
||||||
'bar' => function ($bar) {
|
'bar' => function ($value, $object, $attributeName, $format, $context) {
|
||||||
return;
|
$this->assertSame('baz', $value);
|
||||||
|
$this->assertInstanceOf(ObjectConstructorDummy::class, $object);
|
||||||
|
$this->assertSame('bar', $attributeName);
|
||||||
|
$this->assertSame('any', $format);
|
||||||
|
$this->assertArrayHasKey('circular_reference_limit', $context);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
'baz',
|
'baz',
|
||||||
@ -634,6 +640,18 @@ class ObjectNormalizerTest extends TestCase
|
|||||||
|
|
||||||
$result = $serializer->normalize($level1, null, array(ObjectNormalizer::ENABLE_MAX_DEPTH => true));
|
$result = $serializer->normalize($level1, null, array(ObjectNormalizer::ENABLE_MAX_DEPTH => true));
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
|
|
||||||
|
$this->normalizer->setMaxDepthHandler(function ($object, $parentObject, $attributeName, $format, $context) {
|
||||||
|
$this->assertSame('level3', $object);
|
||||||
|
$this->assertInstanceOf(MaxDepthDummy::class, $parentObject);
|
||||||
|
$this->assertSame('foo', $attributeName);
|
||||||
|
$this->assertSame('test', $format);
|
||||||
|
$this->assertArrayHasKey(ObjectNormalizer::ENABLE_MAX_DEPTH, $context);
|
||||||
|
|
||||||
|
return 'handler';
|
||||||
|
});
|
||||||
|
|
||||||
|
$serializer->normalize($level1, 'test', array(ObjectNormalizer::ENABLE_MAX_DEPTH => true));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -276,7 +276,6 @@ class PropertyNormalizerTest extends TestCase
|
|||||||
array(
|
array(
|
||||||
array(
|
array(
|
||||||
'bar' => function ($bar) {
|
'bar' => function ($bar) {
|
||||||
return;
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
'baz',
|
'baz',
|
||||||
|
Reference in New Issue
Block a user