@@ -40,6 +40,8 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
40
40
protected $ encoders = array ();
41
41
protected $ normalizerCache = array ();
42
42
protected $ denormalizerCache = array ();
43
+ protected $ encoderByFormat = array ();
44
+ protected $ decoderByFormat = array ();
43
45
44
46
public function __construct (array $ normalizers = array (), array $ encoders = array ())
45
47
{
@@ -267,12 +269,42 @@ public function supportsDecoding($format)
267
269
/**
268
270
* {@inheritdoc}
269
271
*/
270
- public function getEncoder ($ format )
272
+ private function getEncoder ($ format )
271
273
{
272
- if (!isset ($ this ->encoders [$ format ])) {
273
- throw new RuntimeException (sprintf ('No encoder found for format "%s". ' , $ format ));
274
+ if (isset ($ this ->encoderByFormat [$ format ])
275
+ && isset ($ this ->encoderByFormat [$ format ])
276
+ ) {
277
+ return $ this ->encoders [$ this ->encoderByFormat [$ format ]];
274
278
}
275
279
276
- return $ this ->encoders [$ format ];
280
+ foreach ($ this ->encoders as $ i => $ encoder ) {
281
+ if ($ encoder ->supportsEncoding ($ format )) {
282
+ $ this ->encoderByFormat [$ format ] = $ i ;
283
+ return $ encoder ;
284
+ }
285
+ }
286
+
287
+ throw new RuntimeException (sprintf ('No encoder found for format "%s". ' , $ format ));
288
+ }
289
+
290
+ /**
291
+ * {@inheritdoc}
292
+ */
293
+ private function getDecoder ($ format )
294
+ {
295
+ if (isset ($ this ->decoderByFormat [$ format ])
296
+ && isset ($ this ->decoderByFormat [$ format ])
297
+ ) {
298
+ return $ this ->encoders [$ this ->decoderByFormat [$ format ]];
299
+ }
300
+
301
+ foreach ($ this ->encoders as $ i => $ encoder ) {
302
+ if ($ encoder ->supportsDecoding ($ format )) {
303
+ $ this ->decoderByFormat [$ format ] = $ i ;
304
+ return $ encoder ;
305
+ }
306
+ }
307
+
308
+ throw new RuntimeException (sprintf ('No decoder found for format "%s". ' , $ format ));
277
309
}
278
310
}
0 commit comments