From 0e5f74071a41a1c14e7522f9ea221493273b7935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Mon, 23 Apr 2018 16:28:06 +0200 Subject: [PATCH] [Serializer] Allow to access to the context and various other infos in callbacks and max depth handler --- .../Normalizer/AbstractObjectNormalizer.php | 4 ++-- .../Tests/Normalizer/ObjectNormalizerTest.php | 22 +++++++++++++++++-- .../Normalizer/PropertyNormalizerTest.php | 1 - 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php index c9a7da9b813d8..abac37770e333 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php @@ -97,11 +97,11 @@ public function normalize($object, $format = null, array $context = array()) $attributeValue = $this->getAttributeValue($object, $attribute, $format, $context); 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])) { - $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)) { diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php index d5d9885f7f679..62338af339f41 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -426,6 +426,8 @@ public function provideCallbacks() array( array( 'bar' => function ($bar) { + $this->assertEquals('baz', $bar); + return 'baz'; }, ), @@ -435,8 +437,12 @@ public function provideCallbacks() ), array( array( - 'bar' => function ($bar) { - return; + 'bar' => function ($value, $object, $attributeName, $format, $context) { + $this->assertSame('baz', $value); + $this->assertInstanceOf(ObjectConstructorDummy::class, $object); + $this->assertSame('bar', $attributeName); + $this->assertSame('any', $format); + $this->assertArrayHasKey('circular_reference_limit', $context); }, ), 'baz', @@ -634,6 +640,18 @@ public function testMaxDepth() $result = $serializer->normalize($level1, null, array(ObjectNormalizer::ENABLE_MAX_DEPTH => true)); $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)); } /** diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php index 29c2127a27973..ffcbacd063d0c 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php @@ -276,7 +276,6 @@ public function provideCallbacks() array( array( 'bar' => function ($bar) { - return; }, ), 'baz', pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy