@@ -61,7 +61,7 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
61
61
parent ::__construct ($ classMetadataFactory , $ nameConverter , $ defaultContext );
62
62
63
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 ));
64
+ throw new InvalidArgumentException (sprintf ('The "%s" given in the default context is not callable. ' , self ::MAX_DEPTH_HANDLER ));
65
65
}
66
66
67
67
$ this ->defaultContext [self ::EXCLUDE_FROM_CACHE_KEY ] = [self ::CIRCULAR_REFERENCE_LIMIT_COUNTERS ];
@@ -91,22 +91,15 @@ public function normalize($object, $format = null, array $context = [])
91
91
if (!isset ($ context ['cache_key ' ])) {
92
92
$ context ['cache_key ' ] = $ this ->getCacheKey ($ format , $ context );
93
93
}
94
- if (\array_key_exists (self ::CALLBACKS , $ context )) {
95
- foreach ($ context [self ::CALLBACKS ] as $ attribute => $ callback ) {
96
- if (!\is_callable ($ callback )) {
97
- throw new InvalidArgumentException (sprintf ('The callback given in the context for attribute "%s" is not callable. ' , $ attribute ));
98
- }
99
- }
100
- }
101
94
102
95
if (isset ($ context [self ::CALLBACKS ])) {
103
96
if (!\is_array ($ context [self ::CALLBACKS ])) {
104
- throw new InvalidArgumentException ('The callbacks context must be a array of callable. ' );
97
+ throw new InvalidArgumentException (sprintf ( 'The "%s" context option must be a array of callables. ' , self :: CALLBACKS ) );
105
98
}
106
99
107
100
foreach ($ context [self ::CALLBACKS ] as $ attribute => $ callback ) {
108
101
if (!\is_callable ($ callback )) {
109
- throw new InvalidArgumentException (sprintf ('The callback given in the context for attribute "%s" is not callable . ' , $ attribute ));
102
+ throw new InvalidArgumentException (sprintf ('Invalid callback found for attribute "%s" in the "%s" context option . ' , $ attribute, self :: CALLBACKS ));
110
103
}
111
104
}
112
105
}
@@ -120,10 +113,13 @@ public function normalize($object, $format = null, array $context = [])
120
113
$ attributes = $ this ->getAttributes ($ object , $ format , $ context );
121
114
$ class = $ this ->objectClassResolver ? ($ this ->objectClassResolver )($ object ) : \get_class ($ object );
122
115
$ attributesMetadata = $ this ->classMetadataFactory ? $ this ->classMetadataFactory ->getMetadataFor ($ class )->getAttributesMetadata () : null ;
123
- $ maxDepthHandler = $ context [self ::MAX_DEPTH_HANDLER ] ?? $ this ->defaultContext [self ::MAX_DEPTH_HANDLER ] ?? $ this ->maxDepthHandler ;
124
-
125
- if (null !== $ maxDepthHandler && !\is_callable ($ maxDepthHandler )) {
126
- throw new InvalidArgumentException (sprintf ('The %s given in the context is not callable. ' , self ::MAX_DEPTH_HANDLER ));
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
+ $ maxDepthHandler = $ this ->defaultContext [self ::MAX_DEPTH_HANDLER ] ?? $ this ->maxDepthHandler ;
127
123
}
128
124
129
125
foreach ($ attributes as $ attribute ) {
0 commit comments