@@ -59,6 +59,11 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
59
59
public function __construct (ClassMetadataFactoryInterface $ classMetadataFactory = null , NameConverterInterface $ nameConverter = null , PropertyTypeExtractorInterface $ propertyTypeExtractor = null , ClassDiscriminatorResolverInterface $ classDiscriminatorResolver = null , callable $ objectClassResolver = null , array $ defaultContext = [])
60
60
{
61
61
parent ::__construct ($ classMetadataFactory , $ nameConverter , $ defaultContext );
62
+
63
+ if (isset ($ this ->defaultContext [self ::MAX_DEPTH_HANDLER ]) && !\is_callable ($ this ->defaultContext [self ::MAX_DEPTH_HANDLER ])) {
64
+ throw new InvalidArgumentException (sprintf ('The "%s" given in the default context is not callable. ' , self ::MAX_DEPTH_HANDLER ));
65
+ }
66
+
62
67
$ this ->defaultContext [self ::EXCLUDE_FROM_CACHE_KEY ] = [self ::CIRCULAR_REFERENCE_LIMIT_COUNTERS ];
63
68
64
69
$ this ->propertyTypeExtractor = $ propertyTypeExtractor ;
@@ -87,6 +92,18 @@ public function normalize($object, $format = null, array $context = [])
87
92
$ context ['cache_key ' ] = $ this ->getCacheKey ($ format , $ context );
88
93
}
89
94
95
+ if (isset ($ context [self ::CALLBACKS ])) {
96
+ if (!\is_array ($ context [self ::CALLBACKS ])) {
97
+ throw new InvalidArgumentException (sprintf ('The "%s" context option must be an array of callables. ' , self ::CALLBACKS ));
98
+ }
99
+
100
+ foreach ($ context [self ::CALLBACKS ] as $ attribute => $ callback ) {
101
+ if (!\is_callable ($ callback )) {
102
+ throw new InvalidArgumentException (sprintf ('Invalid callback found for attribute "%s" in the "%s" context option. ' , $ attribute , self ::CALLBACKS ));
103
+ }
104
+ }
105
+ }
106
+
90
107
if ($ this ->isCircularReference ($ object , $ context )) {
91
108
return $ this ->handleCircularReference ($ object , $ format , $ context );
92
109
}
@@ -96,7 +113,15 @@ public function normalize($object, $format = null, array $context = [])
96
113
$ attributes = $ this ->getAttributes ($ object , $ format , $ context );
97
114
$ class = $ this ->objectClassResolver ? ($ this ->objectClassResolver )($ object ) : \get_class ($ object );
98
115
$ attributesMetadata = $ this ->classMetadataFactory ? $ this ->classMetadataFactory ->getMetadataFor ($ class )->getAttributesMetadata () : null ;
99
- $ maxDepthHandler = $ context [self ::MAX_DEPTH_HANDLER ] ?? $ this ->defaultContext [self ::MAX_DEPTH_HANDLER ] ?? $ this ->maxDepthHandler ;
<
8000
/code>
116
+ if (isset ($ context [self ::MAX_DEPTH_HANDLER ])) {
117
+ $ maxDepthHandler = $ context [self ::MAX_DEPTH_HANDLER ];
118
+ if (!\is_callable ($ maxDepthHandler )) {
119
+ throw new InvalidArgumentException (sprintf ('The "%s" given in the context is not callable. ' , self ::MAX_DEPTH_HANDLER ));
120
+ }
121
+ } else {
122
+ // already validated in constructor resp by type declaration of setMaxDepthHandler
123
+ $ maxDepthHandler = $ this ->defaultContext [self ::MAX_DEPTH_HANDLER ] ?? $ this ->maxDepthHandler ;
124
+ }
100
125
101
126
foreach ($ attributes as $ attribute ) {
102
127
$ maxDepthReached = false ;
0 commit comments