cfd8855bb81508a5de5983a78d36c1c5b2db2c95
[yaffs-website] / vendor / symfony / serializer / Encoder / ChainEncoder.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Serializer\Encoder;
13
14 use Symfony\Component\Serializer\Exception\RuntimeException;
15
16 /**
17  * Encoder delegating the decoding to a chain of encoders.
18  *
19  * @author Jordi Boggiano <j.boggiano@seld.be>
20  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
21  * @author Lukas Kahwe Smith <smith@pooteeweet.org>
22  *
23  * @final since version 3.3.
24  */
25 class ChainEncoder implements EncoderInterface /*, ContextAwareEncoderInterface*/
26 {
27     protected $encoders = array();
28     protected $encoderByFormat = array();
29
30     public function __construct(array $encoders = array())
31     {
32         $this->encoders = $encoders;
33     }
34
35     /**
36      * {@inheritdoc}
37      */
38     final public function encode($data, $format, array $context = array())
39     {
40         return $this->getEncoder($format, $context)->encode($data, $format, $context);
41     }
42
43     /**
44      * {@inheritdoc}
45      */
46     public function supportsEncoding($format/*, array $context = array()*/)
47     {
48         $context = func_num_args() > 1 ? func_get_arg(1) : array();
49
50         try {
51             $this->getEncoder($format, $context);
52         } catch (RuntimeException $e) {
53             return false;
54         }
55
56         return true;
57     }
58
59     /**
60      * Checks whether the normalization is needed for the given format.
61      *
62      * @param string $format
63      * @param array  $context
64      *
65      * @return bool
66      */
67     public function needsNormalization($format/*, array $context = array()*/)
68     {
69         $context = func_num_args() > 1 ? func_get_arg(1) : array();
70         $encoder = $this->getEncoder($format, $context);
71
72         if (!$encoder instanceof NormalizationAwareInterface) {
73             return true;
74         }
75
76         if ($encoder instanceof self) {
77             return $encoder->needsNormalization($format, $context);
78         }
79
80         return false;
81     }
82
83     /**
84      * Gets the encoder supporting the format.
85      *
86      * @param string $format
87      * @param array  $context
88      *
89      * @return EncoderInterface
90      *
91      * @throws RuntimeException if no encoder is found
92      */
93     private function getEncoder($format, array $context)
94     {
95         if (isset($this->encoderByFormat[$format])
96             && isset($this->encoders[$this->encoderByFormat[$format]])
97         ) {
98             return $this->encoders[$this->encoderByFormat[$format]];
99         }
100
101         foreach ($this->encoders as $i => $encoder) {
102             if ($encoder->supportsEncoding($format, $context)) {
103                 $this->encoderByFormat[$format] = $i;
104
105                 return $encoder;
106             }
107         }
108
109         throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
110     }
111 }