26
26
*/
27
27
class ObjectNormalizer extends AbstractNormalizer
28
28
{
29
- private static $ attributesCache = array ();
29
+ private $ attributesCache = array ();
30
30
31
31
/**
32
32
* @var PropertyAccessorInterface
@@ -136,20 +136,39 @@ public function denormalize($data, $class, $format = null, array $context = arra
136
136
* @param object $object
137
137
* @param array $context
138
138
*
139
- * @return array
139
+ * @return string[]
140
140
*/
141
141
private function getAttributes ($ object , array $ context )
142
142
{
143
- $ key = sprintf ('%s-%s ' , get_class ($ object ), serialize ($ context ));
143
+ try {
144
+ $ serializedContext = serialize ($ context );
145
+ } catch (\Exception $ exception ) {
146
+ // The context cannot be serialized, skip the cache
147
+ return $ this ->extractAttributes ($ object , $ context );
148
+ }
149
+
150
+ $ key = sprintf ('%s-%s ' , get_class ($ object ), $ serializedContext );
144
151
145
- if (isset (self :: $ attributesCache [$ key ])) {
146
- return self :: $ attributesCache [$ key ];
152
+ if (isset ($ this -> attributesCache [$ key ])) {
153
+ return $ this -> attributesCache [$ key ];
147
154
}
148
155
149
- $ allowedAttributes = $ this ->getAllowedAttributes ($ object , $ context , true );
156
+ return $ this ->attributesCache [$ key ] = $ this ->extractAttributes ($ object , $ context );
157
+ }
150
158
159
+ /**
160
+ * Extracts attributes for this class and context.
161
+ *
162
+ * @param object $object
163
+ * @param array $context
164
+ *
165
+ * @return string[]
166
+ */
167
+ private function extractAttributes ($ object , array $ context )
168
+ {
169
+ $ allowedAttributes = $ this ->getAllowedAttributes ($ object , $ context , true );
151
170
if (false !== $ allowedAttributes ) {
152
- return self :: $ attributesCache [ $ key ] = $ allowedAttributes ;
171
+ return $ allowedAttributes ;
153
172
}
154
173
155
174
// If not using groups, detect manually
@@ -167,9 +186,9 @@ private function getAttributes($object, array $context)
167
186
continue ;
168
187
}
169
188
170
- $ name = $ reflMethod ->getName () ;
189
+ $ name = $ reflMethod ->name ;
171
190
172
- if (strpos ($ name , 'get ' ) === 0 || strpos ($ name , 'has ' ) === 0 ) {
191
+ if (0 === strpos ($ name , 'get ' ) || 0 === strpos ($ name , 'has ' )) {
173
192
// getters and hassers
174
193
$ attributes [lcfirst (substr ($ name , 3 ))] = true ;
175
194
} elseif (strpos ($ name , 'is ' ) === 0 ) {
@@ -184,9 +203,9 @@ private function getAttributes($object, array $context)
184
203
continue ;
185
204
}
186
205
187
- $ attributes [$ reflProperty ->getName () ] = true ;
206
+ $ attributes [$ reflProperty ->name ] = true ;
188
207
}
189
208
190
- return self :: $ attributesCache [ $ key ] = array_keys ($ attributes );
209
+ return array_keys ($ attributes );
191
210
}
192
211
}
0 commit comments