@@ -59,6 +59,11 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
5959 public function __construct (ClassMetadataFactoryInterface $ classMetadataFactory = null , NameConverterInterface $ nameConverter = null , PropertyTypeExtractorInterface $ propertyTypeExtractor = null , ClassDiscriminatorResolverInterface $ classDiscriminatorResolver = null , callable $ objectClassResolver = null , array $ defaultContext = [])
6060 {
6161 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+
6267 $ this ->defaultContext [self ::EXCLUDE_FROM_CACHE_KEY ] = [self ::CIRCULAR_REFERENCE_LIMIT_COUNTERS ];
6368
6469 $ this ->propertyTypeExtractor = $ propertyTypeExtractor ;
@@ -87,6 +92,18 @@ public function normalize($object, $format = null, array $context = [])
8792 $ context ['cache_key ' ] = $ this ->getCacheKey ($ format , $ context );
8893 }
8994
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+
90107 if ($ this ->isCircularReference ($ object , $ context )) {
91108 return $ this ->handleCircularReference ($ object , $ format , $ context );
92109 }
@@ -96,7 +113,15 @@ public function normalize($object, $format = null, array $context = [])
96113 $ attributes = $ this ->getAttributes ($ object , $ format , $ context );
97114 $ class = $ this ->objectClassResolver ? ($ this ->objectClassResolver )($ object ) : \get_class ($ object );
98115 $ attributesMetadata = $ this ->classMetadataFactory ? $ this ->classMetadataFactory ->getMetadataFor ($ class )->getAttributesMetadata () : null ;
99- $ maxDepthHandler = $ context [self ::MAX_DEPTH_HANDLER ] ?? $ this ->defaultContext [self ::MAX_DEPTH_HANDLER ] ?? $ this ->maxDepthHandler ;
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+ }
100125
101126 foreach ($ attributes as $ attribute ) {
102127 $ maxDepthReached = false ;
0 commit comments