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