11
11
12
12
namespace Symfony \Component \Serializer ;
13
13
14
+ use Symfony \Component \Serializer \Encoder \ChainDecoder ;
15
+ use Symfony \Component \Serializer \Encoder \ChainEncoder ;
14
16
use Symfony \Component \Serializer \Encoder \EncoderInterface ;
15
17
use Symfony \Component \Serializer \Encoder \DecoderInterface ;
16
18
use Symfony \Component \Serializer \Encoder \NormalizationAwareInterface ;
36
38
*/
37
39
class Serializer implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface
38
40
{
41
+ protected $ encoder ;
42
+ protected $ decoder ;
39
43
protected $ normalizers = array ();
40
44
protected $ encoders = array ();
41
45
protected $ normalizerCache = array ();
@@ -52,12 +56,21 @@ public function __construct(array $normalizers = array(), array $encoders = arra
52
56
}
53
57
$ this ->normalizers = $ normalizers ;
54
58
59
+ $ decoders = array ();
60
+ $ realEncoders = array ();
55
61
foreach ($ encoders as $ encoder ) {
56
62
if ($ encoder instanceof SerializerAwareInterface) {
57
63
$ encoder ->setSerializer ($ this );
58
64
}
65
+ if ($ encoder instanceof DecoderInterface) {
66
+ $ decoders [] = $ encoder ;
67
+ }
68
+ if ($ encoder instanceof EncoderInterface) {
69
+ $ realEncoders [] = $ encoder ;
70
+ }
59
71
}
60
- $ this ->encoders = $ encoders ;
72
+ $ this ->encoder = new ChainEncoder ($ realEncoders );
73
+ $ this ->decoder = new ChainDecoder ($ decoders );
61
74
}
62
75
63
76
/**
@@ -69,7 +82,7 @@ final public function serialize($data, $format)
69
82
throw new UnexpectedValueException ('Serialization for the format ' .$ format .' is not supported ' );
70
83
}
71
84
72
- $ encoder = $ this ->getEncoder ($ format );
85
+ $ encoder = $ this ->encoder -> getEncoder ($ format );
73
86
74
87
if (!$ encoder instanceof NormalizationAwareInterface) {
75
88
$ data = $ this ->normalize ($ data , $ format );
@@ -197,15 +210,15 @@ private function getDenormalizer($data, $type, $format = null)
197
210
*/
198
211
final public function encode ($ data , $ format )
199
212
{
200
- return $ this ->getEncoder ( $ format ) ->encode ($ data , $ format );
213
+ return $ this ->encoder ->encode ($ data , $ format );
201
214
}
202
215
203
216
/**
204
217
* {@inheritdoc}
205
218
*/
206
219
final public function decode ($ data , $ format )
207
220
{
208
- return $ this ->getEncoder ( $ format ) ->decode ($ data , $ format );
221
+ return $ this ->decoder ->decode ($ data , $ format );
209
222
}
210
223
211
224
/**
@@ -271,74 +284,14 @@ private function denormalizeObject($data, $class, $format = null)
271
284
*/
272
285
public function supportsEncoding ($ format )
273
286
{
274
- try {
275
- $ this ->getEncoder ($ format );
276
- } catch (RuntimeException $ e ) {
277
- return false ;
278
- }
279
-
280
- return true ;
287
+ return $ this ->encoder ->supportsEncoding ($ format );
281
288
}
282
289
283
290
/**
284
291
* {@inheritdoc}
285
292
*/
286
293
public function supportsDecoding ($ format )
287
294
{
288
- try {
289
- $ this ->getDecoder ($ format );
290
- } catch (RuntimeException $ e ) {
291
- return false ;
292
- }
293
-
294
- return true ;
295
- }
296
-
297
- /**
298
- * {@inheritdoc}
299
- */
300
- private function getEncoder ($ format )
301
- {
302
- if (isset ($ this ->encoderByFormat [$ format ])
303
- && isset ($ this ->encoders [$ this ->encoderByFormat [$ format ]])
304
- ) {
305
- return $ this ->encoders [$ this ->encoderByFormat [$ format ]];
306
- }
307
-
308
- foreach ($ this ->encoders as $ i => $ encoder ) {
309
- if ($ encoder instanceof EncoderInterface
310
- && $ encoder ->supportsEncoding ($ format )
311
- ) {
312
- $ this ->encoderByFormat [$ format ] = $ i ;
313
-
314
- return $ encoder ;
315
- }
316
- }
317
-
318
- throw new RuntimeException (sprintf ('No encoder found for format "%s". ' , $ format ));
319
- }
320
-
321
- /**
322
- * {@inheritdoc}
323
- */
324
- private function getDecoder ($ format )
325
- {
326
- if (isset ($ this ->decoderByFormat [$ format ])
327
- && isset ($ this ->encoders [$ this ->decoderByFormat [$ format ]])
328
- ) {
329
- return $ this ->encoders [$ this ->decoderByFormat [$ format ]];
330
- }
331
-
332
- foreach ($ this ->encoders as $ i => $ encoder ) {
333
- if ($ encoder instanceof DecoderInterface
334
- && $ encoder ->supportsDecoding ($ format )
335
- ) {
336
- $ this ->decoderByFormat [$ format ] = $ i ;
337
-
338
- return $ encoder ;
339
- }
340
- }
341
-
342
- throw new RuntimeException (sprintf ('No decoder found for format "%s". ' , $ format ));
295
+ return $ this ->decoder ->supportsDecoding ($ format );
343
296
}
344
297
}
0 commit comments