@@ -41,6 +41,11 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
41
41
private $ attributesCache = array ();
42
42
private $ cache = array ();
43
43
44
+ /**
45
+ * @var callable|null
46
+ */
47
+ private $ maxDepthHandler ;
48
+
44
49
/**
45
50
* @var ClassDiscriminatorResolverInterface|null
46
51
*/
@@ -86,11 +91,15 @@ public function normalize($object, $format = null, array $context = array())
86
91
$ attributesMetadata = $ this ->classMetadataFactory ? $ this ->classMetadataFactory ->getMetadataFor ($ class )->getAttributesMetadata () : null ;
87
92
88
93
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 ) {
90
96
continue ;
91
97
}
92
98
93
99
$ attributeValue = $ this ->getAttributeValue ($ object , $ attribute , $ format , $ context );
100
+ if ($ maxDepthReached ) {
101
+ $ attributeValue = \call_user_func ($ this ->maxDepthHandler , $ attributeValue );
102
+ }
94
103
95
104
if (isset ($ this ->callbacks [$ attribute ])) {
96
105
$ attributeValue = call_user_func ($ this ->callbacks [$ attribute ], $ attributeValue );
@@ -204,6 +213,14 @@ abstract protected function extractAttributes($object, $format = null, array $co
204
213
*/
205
214
abstract protected function getAttributeValue ($ object , $ attribute , $ format = null , array $ context = array ());
206
215
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
+
207
224
/**
208
225
* {@inheritdoc}
209
226
*/
0 commit comments