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;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Serializer\Encoder\ChainDecoder;
|
Chris@0
|
15 use Symfony\Component\Serializer\Encoder\ChainEncoder;
|
Chris@0
|
16 use Symfony\Component\Serializer\Encoder\EncoderInterface;
|
Chris@0
|
17 use Symfony\Component\Serializer\Encoder\DecoderInterface;
|
Chris@0
|
18 use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
|
Chris@0
|
19 use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
Chris@0
|
20 use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
Chris@0
|
21 use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
Chris@0
|
22 use Symfony\Component\Serializer\Exception\LogicException;
|
Chris@0
|
23 use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * Serializer serializes and deserializes data.
|
Chris@0
|
27 *
|
Chris@0
|
28 * objects are turned into arrays by normalizers.
|
Chris@0
|
29 * arrays are turned into various output formats by encoders.
|
Chris@0
|
30 *
|
Chris@0
|
31 * $serializer->serialize($obj, 'xml')
|
Chris@0
|
32 * $serializer->decode($data, 'xml')
|
Chris@0
|
33 * $serializer->denormalize($data, 'Class', 'xml')
|
Chris@0
|
34 *
|
Chris@0
|
35 * @author Jordi Boggiano <j.boggiano@seld.be>
|
Chris@0
|
36 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
Chris@0
|
37 * @author Lukas Kahwe Smith <smith@pooteeweet.org>
|
Chris@0
|
38 * @author Kévin Dunglas <dunglas@gmail.com>
|
Chris@0
|
39 */
|
Chris@0
|
40 class Serializer implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface
|
Chris@0
|
41 {
|
Chris@0
|
42 /**
|
Chris@0
|
43 * @var Encoder\ChainEncoder
|
Chris@0
|
44 */
|
Chris@0
|
45 protected $encoder;
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * @var Encoder\ChainDecoder
|
Chris@0
|
49 */
|
Chris@0
|
50 protected $decoder;
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * @var array
|
Chris@0
|
54 */
|
Chris@0
|
55 protected $normalizers = array();
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * @var array
|
Chris@0
|
59 *
|
Chris@0
|
60 * @deprecated since 3.1 will be removed in 4.0
|
Chris@0
|
61 */
|
Chris@0
|
62 protected $normalizerCache = array();
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * @var array
|
Chris@0
|
66 *
|
Chris@0
|
67 * @deprecated since 3.1 will be removed in 4.0
|
Chris@0
|
68 */
|
Chris@0
|
69 protected $denormalizerCache = array();
|
Chris@0
|
70
|
Chris@0
|
71 public function __construct(array $normalizers = array(), array $encoders = array())
|
Chris@0
|
72 {
|
Chris@0
|
73 foreach ($normalizers as $normalizer) {
|
Chris@0
|
74 if ($normalizer instanceof SerializerAwareInterface) {
|
Chris@0
|
75 $normalizer->setSerializer($this);
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 if ($normalizer instanceof DenormalizerAwareInterface) {
|
Chris@0
|
79 $normalizer->setDenormalizer($this);
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 if ($normalizer instanceof NormalizerAwareInterface) {
|
Chris@0
|
83 $normalizer->setNormalizer($this);
|
Chris@0
|
84 }
|
Chris@0
|
85 }
|
Chris@0
|
86 $this->normalizers = $normalizers;
|
Chris@0
|
87
|
Chris@0
|
88 $decoders = array();
|
Chris@0
|
89 $realEncoders = array();
|
Chris@0
|
90 foreach ($encoders as $encoder) {
|
Chris@0
|
91 if ($encoder instanceof SerializerAwareInterface) {
|
Chris@0
|
92 $encoder->setSerializer($this);
|
Chris@0
|
93 }
|
Chris@0
|
94 if ($encoder instanceof DecoderInterface) {
|
Chris@0
|
95 $decoders[] = $encoder;
|
Chris@0
|
96 }
|
Chris@0
|
97 if ($encoder instanceof EncoderInterface) {
|
Chris@0
|
98 $realEncoders[] = $encoder;
|
Chris@0
|
99 }
|
Chris@0
|
100 }
|
Chris@0
|
101 $this->encoder = new ChainEncoder($realEncoders);
|
Chris@0
|
102 $this->decoder = new ChainDecoder($decoders);
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 /**
|
Chris@0
|
106 * {@inheritdoc}
|
Chris@0
|
107 */
|
Chris@0
|
108 final public function serialize($data, $format, array $context = array())
|
Chris@0
|
109 {
|
Chris@0
|
110 if (!$this->supportsEncoding($format)) {
|
Chris@0
|
111 throw new UnexpectedValueException(sprintf('Serialization for the format %s is not supported', $format));
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 if ($this->encoder->needsNormalization($format)) {
|
Chris@0
|
115 $data = $this->normalize($data, $format, $context);
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 return $this->encode($data, $format, $context);
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 /**
|
Chris@0
|
122 * {@inheritdoc}
|
Chris@0
|
123 */
|
Chris@0
|
124 final public function deserialize($data, $type, $format, array $context = array())
|
Chris@0
|
125 {
|
Chris@0
|
126 if (!$this->supportsDecoding($format)) {
|
Chris@0
|
127 throw new UnexpectedValueException(sprintf('Deserialization for the format %s is not supported', $format));
|
Chris@0
|
128 }
|
Chris@0
|
129
|
Chris@0
|
130 $data = $this->decode($data, $format, $context);
|
Chris@0
|
131
|
Chris@0
|
132 return $this->denormalize($data, $type, $format, $context);
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 /**
|
Chris@0
|
136 * {@inheritdoc}
|
Chris@0
|
137 */
|
Chris@0
|
138 public function normalize($data, $format = null, array $context = array())
|
Chris@0
|
139 {
|
Chris@0
|
140 // If a normalizer supports the given data, use it
|
Chris@0
|
141 if ($normalizer = $this->getNormalizer($data, $format)) {
|
Chris@0
|
142 return $normalizer->normalize($data, $format, $context);
|
Chris@0
|
143 }
|
Chris@0
|
144
|
Chris@0
|
145 if (null === $data || is_scalar($data)) {
|
Chris@0
|
146 return $data;
|
Chris@0
|
147 }
|
Chris@0
|
148
|
Chris@0
|
149 if (is_array($data) || $data instanceof \Traversable) {
|
Chris@0
|
150 $normalized = array();
|
Chris@0
|
151 foreach ($data as $key => $val) {
|
Chris@0
|
152 $normalized[$key] = $this->normalize($val, $format, $context);
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 return $normalized;
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 if (is_object($data)) {
|
Chris@0
|
159 if (!$this->normalizers) {
|
Chris@0
|
160 throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
|
Chris@0
|
161 }
|
Chris@0
|
162
|
Chris@0
|
163 throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($data)));
|
Chris@0
|
164 }
|
Chris@0
|
165
|
Chris@0
|
166 throw new UnexpectedValueException(sprintf('An unexpected value could not be normalized: %s', var_export($data, true)));
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 /**
|
Chris@0
|
170 * {@inheritdoc}
|
Chris@0
|
171 */
|
Chris@0
|
172 public function denormalize($data, $type, $format = null, array $context = array())
|
Chris@0
|
173 {
|
Chris@0
|
174 return $this->denormalizeObject($data, $type, $format, $context);
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 /**
|
Chris@0
|
178 * {@inheritdoc}
|
Chris@0
|
179 */
|
Chris@0
|
180 public function supportsNormalization($data, $format = null)
|
Chris@0
|
181 {
|
Chris@0
|
182 return null !== $this->getNormalizer($data, $format);
|
Chris@0
|
183 }
|
Chris@0
|
184
|
Chris@0
|
185 /**
|
Chris@0
|
186 * {@inheritdoc}
|
Chris@0
|
187 */
|
Chris@0
|
188 public function supportsDenormalization($data, $type, $format = null)
|
Chris@0
|
189 {
|
Chris@0
|
190 return null !== $this->getDenormalizer($data, $type, $format);
|
Chris@0
|
191 }
|
Chris@0
|
192
|
Chris@0
|
193 /**
|
Chris@0
|
194 * Returns a matching normalizer.
|
Chris@0
|
195 *
|
Chris@0
|
196 * @param mixed $data Data to get the serializer for
|
Chris@0
|
197 * @param string $format format name, present to give the option to normalizers to act differently based on formats
|
Chris@0
|
198 *
|
Chris@0
|
199 * @return NormalizerInterface|null
|
Chris@0
|
200 */
|
Chris@0
|
201 private function getNormalizer($data, $format)
|
Chris@0
|
202 {
|
Chris@0
|
203 foreach ($this->normalizers as $normalizer) {
|
Chris@0
|
204 if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format)) {
|
Chris@0
|
205 return $normalizer;
|
Chris@0
|
206 }
|
Chris@0
|
207 }
|
Chris@0
|
208 }
|
Chris@0
|
209
|
Chris@0
|
210 /**
|
Chris@0
|
211 * Returns a matching denormalizer.
|
Chris@0
|
212 *
|
Chris@0
|
213 * @param mixed $data data to restore
|
Chris@0
|
214 * @param string $class the expected class to instantiate
|
Chris@0
|
215 * @param string $format format name, present to give the option to normalizers to act differently based on formats
|
Chris@0
|
216 *
|
Chris@0
|
217 * @return DenormalizerInterface|null
|
Chris@0
|
218 */
|
Chris@0
|
219 private function getDenormalizer($data, $class, $format)
|
Chris@0
|
220 {
|
Chris@0
|
221 foreach ($this->normalizers as $normalizer) {
|
Chris@0
|
222 if ($normalizer instanceof DenormalizerInterface && $normalizer->supportsDenormalization($data, $class, $format)) {
|
Chris@0
|
223 return $normalizer;
|
Chris@0
|
224 }
|
Chris@0
|
225 }
|
Chris@0
|
226 }
|
Chris@0
|
227
|
Chris@0
|
228 /**
|
Chris@0
|
229 * {@inheritdoc}
|
Chris@0
|
230 */
|
Chris@0
|
231 final public function encode($data, $format, array $context = array())
|
Chris@0
|
232 {
|
Chris@0
|
233 return $this->encoder->encode($data, $format, $context);
|
Chris@0
|
234 }
|
Chris@0
|
235
|
Chris@0
|
236 /**
|
Chris@0
|
237 * {@inheritdoc}
|
Chris@0
|
238 */
|
Chris@0
|
239 final public function decode($data, $format, array $context = array())
|
Chris@0
|
240 {
|
Chris@0
|
241 return $this->decoder->decode($data, $format, $context);
|
Chris@0
|
242 }
|
Chris@0
|
243
|
Chris@0
|
244 /**
|
Chris@0
|
245 * Denormalizes data back into an object of the given class.
|
Chris@0
|
246 *
|
Chris@0
|
247 * @param mixed $data data to restore
|
Chris@0
|
248 * @param string $class the expected class to instantiate
|
Chris@0
|
249 * @param string $format format name, present to give the option to normalizers to act differently based on formats
|
Chris@0
|
250 * @param array $context The context data for this particular denormalization
|
Chris@0
|
251 *
|
Chris@0
|
252 * @return object
|
Chris@0
|
253 *
|
Chris@0
|
254 * @throws LogicException
|
Chris@0
|
255 * @throws UnexpectedValueException
|
Chris@0
|
256 */
|
Chris@0
|
257 private function denormalizeObject($data, $class, $format, array $context = array())
|
Chris@0
|
258 {
|
Chris@0
|
259 if (!$this->normalizers) {
|
Chris@0
|
260 throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
|
Chris@0
|
261 }
|
Chris@0
|
262
|
Chris@0
|
263 if ($normalizer = $this->getDenormalizer($data, $class, $format)) {
|
Chris@0
|
264 return $normalizer->denormalize($data, $class, $format, $context);
|
Chris@0
|
265 }
|
Chris@0
|
266
|
Chris@0
|
267 throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class));
|
Chris@0
|
268 }
|
Chris@0
|
269
|
Chris@0
|
270 /**
|
Chris@0
|
271 * {@inheritdoc}
|
Chris@0
|
272 */
|
Chris@0
|
273 public function supportsEncoding($format)
|
Chris@0
|
274 {
|
Chris@0
|
275 return $this->encoder->supportsEncoding($format);
|
Chris@0
|
276 }
|
Chris@0
|
277
|
Chris@0
|
278 /**
|
Chris@0
|
279 * {@inheritdoc}
|
Chris@0
|
280 */
|
Chris@0
|
281 public function supportsDecoding($format)
|
Chris@0
|
282 {
|
Chris@0
|
283 return $this->decoder->supportsDecoding($format);
|
Chris@0
|
284 }
|
Chris@0
|
285 }
|