comparison vendor/symfony/serializer/Serializer.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
11 11
12 namespace Symfony\Component\Serializer; 12 namespace Symfony\Component\Serializer;
13 13
14 use Symfony\Component\Serializer\Encoder\ChainDecoder; 14 use Symfony\Component\Serializer\Encoder\ChainDecoder;
15 use Symfony\Component\Serializer\Encoder\ChainEncoder; 15 use Symfony\Component\Serializer\Encoder\ChainEncoder;
16 use Symfony\Component\Serializer\Encoder\DecoderInterface;
16 use Symfony\Component\Serializer\Encoder\EncoderInterface; 17 use Symfony\Component\Serializer\Encoder\EncoderInterface;
17 use Symfony\Component\Serializer\Encoder\DecoderInterface; 18 use Symfony\Component\Serializer\Exception\LogicException;
18 use Symfony\Component\Serializer\Exception\NotEncodableValueException; 19 use Symfony\Component\Serializer\Exception\NotEncodableValueException;
19 use Symfony\Component\Serializer\Exception\NotNormalizableValueException; 20 use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
20 use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; 21 use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
22 use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
21 use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; 23 use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
22 use Symfony\Component\Serializer\Normalizer\NormalizerInterface; 24 use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
23 use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
24 use Symfony\Component\Serializer\Exception\LogicException;
25 25
26 /** 26 /**
27 * Serializer serializes and deserializes data. 27 * Serializer serializes and deserializes data.
28 * 28 *
29 * objects are turned into arrays by normalizers. 29 * objects are turned into arrays by normalizers.
30 * arrays are turned into various output formats by encoders. 30 * arrays are turned into various output formats by encoders.
31 * 31 *
32 * $serializer->serialize($obj, 'xml') 32 * $serializer->serialize($obj, 'xml')
33 * $serializer->decode($data, 'xml') 33 * $serializer->decode($data, 'xml')
34 * $serializer->denormalize($data, 'Class', 'xml') 34 * $serializer->denormalize($data, 'Class', 'xml')
35 * 35 *
36 * @author Jordi Boggiano <j.boggiano@seld.be> 36 * @author Jordi Boggiano <j.boggiano@seld.be>
37 * @author Johannes M. Schmitt <schmittjoh@gmail.com> 37 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
38 * @author Lukas Kahwe Smith <smith@pooteeweet.org> 38 * @author Lukas Kahwe Smith <smith@pooteeweet.org>
39 * @author Kévin Dunglas <dunglas@gmail.com> 39 * @author Kévin Dunglas <dunglas@gmail.com>
51 protected $decoder; 51 protected $decoder;
52 52
53 /** 53 /**
54 * @var array 54 * @var array
55 */ 55 */
56 protected $normalizers = array(); 56 protected $normalizers = [];
57 57
58 /** 58 /**
59 * @var array 59 * @var array
60 * 60 *
61 * @deprecated since 3.1 will be removed in 4.0 61 * @deprecated since 3.1 will be removed in 4.0
62 */ 62 */
63 protected $normalizerCache = array(); 63 protected $normalizerCache = [];
64 64
65 /** 65 /**
66 * @var array 66 * @var array
67 * 67 *
68 * @deprecated since 3.1 will be removed in 4.0 68 * @deprecated since 3.1 will be removed in 4.0
69 */ 69 */
70 protected $denormalizerCache = array(); 70 protected $denormalizerCache = [];
71 71
72 public function __construct(array $normalizers = array(), array $encoders = array()) 72 public function __construct(array $normalizers = [], array $encoders = [])
73 { 73 {
74 foreach ($normalizers as $normalizer) { 74 foreach ($normalizers as $normalizer) {
75 if ($normalizer instanceof SerializerAwareInterface) { 75 if ($normalizer instanceof SerializerAwareInterface) {
76 $normalizer->setSerializer($this); 76 $normalizer->setSerializer($this);
77 } 77 }
84 $normalizer->setNormalizer($this); 84 $normalizer->setNormalizer($this);
85 } 85 }
86 } 86 }
87 $this->normalizers = $normalizers; 87 $this->normalizers = $normalizers;
88 88
89 $decoders = array(); 89 $decoders = [];
90 $realEncoders = array(); 90 $realEncoders = [];
91 foreach ($encoders as $encoder) { 91 foreach ($encoders as $encoder) {
92 if ($encoder instanceof SerializerAwareInterface) { 92 if ($encoder instanceof SerializerAwareInterface) {
93 $encoder->setSerializer($this); 93 $encoder->setSerializer($this);
94 } 94 }
95 if ($encoder instanceof DecoderInterface) { 95 if ($encoder instanceof DecoderInterface) {
104 } 104 }
105 105
106 /** 106 /**
107 * {@inheritdoc} 107 * {@inheritdoc}
108 */ 108 */
109 final public function serialize($data, $format, array $context = array()) 109 final public function serialize($data, $format, array $context = [])
110 { 110 {
111 if (!$this->supportsEncoding($format, $context)) { 111 if (!$this->supportsEncoding($format, $context)) {
112 throw new NotEncodableValueException(sprintf('Serialization for the format %s is not supported', $format)); 112 throw new NotEncodableValueException(sprintf('Serialization for the format %s is not supported', $format));
113 } 113 }
114 114
120 } 120 }
121 121
122 /** 122 /**
123 * {@inheritdoc} 123 * {@inheritdoc}
124 */ 124 */
125 final public function deserialize($data, $type, $format, array $context = array()) 125 final public function deserialize($data, $type, $format, array $context = [])
126 { 126 {
127 if (!$this->supportsDecoding($format, $context)) { 127 if (!$this->supportsDecoding($format, $context)) {
128 throw new NotEncodableValueException(sprintf('Deserialization for the format %s is not supported', $format)); 128 throw new NotEncodableValueException(sprintf('Deserialization for the format %s is not supported', $format));
129 } 129 }
130 130
134 } 134 }
135 135
136 /** 136 /**
137 * {@inheritdoc} 137 * {@inheritdoc}
138 */ 138 */
139 public function normalize($data, $format = null, array $context = array()) 139 public function normalize($data, $format = null, array $context = [])
140 { 140 {
141 // If a normalizer supports the given data, use it 141 // If a normalizer supports the given data, use it
142 if ($normalizer = $this->getNormalizer($data, $format, $context)) { 142 if ($normalizer = $this->getNormalizer($data, $format, $context)) {
143 return $normalizer->normalize($data, $format, $context); 143 return $normalizer->normalize($data, $format, $context);
144 } 144 }
146 if (null === $data || is_scalar($data)) { 146 if (null === $data || is_scalar($data)) {
147 return $data; 147 return $data;
148 } 148 }
149 149
150 if (\is_array($data) || $data instanceof \Traversable) { 150 if (\is_array($data) || $data instanceof \Traversable) {
151 $normalized = array(); 151 $normalized = [];
152 foreach ($data as $key => $val) { 152 foreach ($data as $key => $val) {
153 $normalized[$key] = $this->normalize($val, $format, $context); 153 $normalized[$key] = $this->normalize($val, $format, $context);
154 } 154 }
155 155
156 return $normalized; 156 return $normalized;
170 /** 170 /**
171 * {@inheritdoc} 171 * {@inheritdoc}
172 * 172 *
173 * @throws NotNormalizableValueException 173 * @throws NotNormalizableValueException
174 */ 174 */
175 public function denormalize($data, $type, $format = null, array $context = array()) 175 public function denormalize($data, $type, $format = null, array $context = [])
176 { 176 {
177 if (!$this->normalizers) { 177 if (!$this->normalizers) {
178 throw new LogicException('You must register at least one normalizer to be able to denormalize objects.'); 178 throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
179 } 179 }
180 180
186 } 186 }
187 187
188 /** 188 /**
189 * {@inheritdoc} 189 * {@inheritdoc}
190 */ 190 */
191 public function supportsNormalization($data, $format = null/*, array $context = array()*/) 191 public function supportsNormalization($data, $format = null/*, array $context = []*/)
192 { 192 {
193 if (\func_num_args() > 2) { 193 if (\func_num_args() > 2) {
194 $context = \func_get_arg(2); 194 $context = \func_get_arg(2);
195 } else { 195 } else {
196 if (__CLASS__ !== \get_class($this)) { 196 if (__CLASS__ !== \get_class($this)) {
197 $r = new \ReflectionMethod($this, __FUNCTION__); 197 $r = new \ReflectionMethod($this, __FUNCTION__);
198 if (__CLASS__ !== $r->getDeclaringClass()->getName()) { 198 if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
199 @trigger_error(sprintf('Method %s() will have a third `$context = array()` argument in version 4.0. Not defining it is deprecated since Symfony 3.3.', __METHOD__), E_USER_DEPRECATED); 199 @trigger_error(sprintf('The "%s()" method will have a third `$context = []` argument in version 4.0. Not defining it is deprecated since Symfony 3.3.', __METHOD__), E_USER_DEPRECATED);
200 } 200 }
201 } 201 }
202 202
203 $context = array(); 203 $context = [];
204 } 204 }
205 205
206 return null !== $this->getNormalizer($data, $format, $context); 206 return null !== $this->getNormalizer($data, $format, $context);
207 } 207 }
208 208
209 /** 209 /**
210 * {@inheritdoc} 210 * {@inheritdoc}
211 */ 211 */
212 public function supportsDenormalization($data, $type, $format = null/*, array $context = array()*/) 212 public function supportsDenormalization($data, $type, $format = null/*, array $context = []*/)
213 { 213 {
214 if (\func_num_args() > 3) { 214 if (\func_num_args() > 3) {
215 $context = \func_get_arg(3); 215 $context = \func_get_arg(3);
216 } else { 216 } else {
217 if (__CLASS__ !== \get_class($this)) { 217 if (__CLASS__ !== \get_class($this)) {
218 $r = new \ReflectionMethod($this, __FUNCTION__); 218 $r = new \ReflectionMethod($this, __FUNCTION__);
219 if (__CLASS__ !== $r->getDeclaringClass()->getName()) { 219 if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
220 @trigger_error(sprintf('Method %s() will have a fourth `$context = array()` argument in version 4.0. Not defining it is deprecated since Symfony 3.3.', __METHOD__), E_USER_DEPRECATED); 220 @trigger_error(sprintf('The "%s()" method will have a fourth `$context = []` argument in version 4.0. Not defining it is deprecated since Symfony 3.3.', __METHOD__), E_USER_DEPRECATED);
221 } 221 }
222 } 222 }
223 223
224 $context = array(); 224 $context = [];
225 } 225 }
226 226
227 return null !== $this->getDenormalizer($data, $type, $format, $context); 227 return null !== $this->getDenormalizer($data, $type, $format, $context);
228 } 228 }
229 229
265 } 265 }
266 266
267 /** 267 /**
268 * {@inheritdoc} 268 * {@inheritdoc}
269 */ 269 */
270 final public function encode($data, $format, array $context = array()) 270 final public function encode($data, $format, array $context = [])
271 { 271 {
272 return $this->encoder->encode($data, $format, $context); 272 return $this->encoder->encode($data, $format, $context);
273 } 273 }
274 274
275 /** 275 /**
276 * {@inheritdoc} 276 * {@inheritdoc}
277 */ 277 */
278 final public function decode($data, $format, array $context = array()) 278 final public function decode($data, $format, array $context = [])
279 { 279 {
280 return $this->decoder->decode($data, $format, $context); 280 return $this->decoder->decode($data, $format, $context);
281 } 281 }
282 282
283 /** 283 /**
284 * {@inheritdoc} 284 * {@inheritdoc}
285 */ 285 */
286 public function supportsEncoding($format/*, array $context = array()*/) 286 public function supportsEncoding($format/*, array $context = []*/)
287 { 287 {
288 if (\func_num_args() > 1) { 288 if (\func_num_args() > 1) {
289 $context = \func_get_arg(1); 289 $context = \func_get_arg(1);
290 } else { 290 } else {
291 if (__CLASS__ !== \get_class($this)) { 291 if (__CLASS__ !== \get_class($this)) {
292 $r = new \ReflectionMethod($this, __FUNCTION__); 292 $r = new \ReflectionMethod($this, __FUNCTION__);
293 if (__CLASS__ !== $r->getDeclaringClass()->getName()) { 293 if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
294 @trigger_error(sprintf('Method %s() will have a second `$context = array()` argument in version 4.0. Not defining it is deprecated since Symfony 3.3.', __METHOD__), E_USER_DEPRECATED); 294 @trigger_error(sprintf('The "%s()" method will have a second `$context = []` argument in version 4.0. Not defining it is deprecated since Symfony 3.3.', __METHOD__), E_USER_DEPRECATED);
295 } 295 }
296 } 296 }
297 297
298 $context = array(); 298 $context = [];
299 } 299 }
300 300
301 return $this->encoder->supportsEncoding($format, $context); 301 return $this->encoder->supportsEncoding($format, $context);
302 } 302 }
303 303
304 /** 304 /**
305 * {@inheritdoc} 305 * {@inheritdoc}
306 */ 306 */
307 public function supportsDecoding($format/*, array $context = array()*/) 307 public function supportsDecoding($format/*, array $context = []*/)
308 { 308 {
309 if (\func_num_args() > 1) { 309 if (\func_num_args() > 1) {
310 $context = \func_get_arg(1); 310 $context = \func_get_arg(1);
311 } else { 311 } else {
312 if (__CLASS__ !== \get_class($this)) { 312 if (__CLASS__ !== \get_class($this)) {
313 $r = new \ReflectionMethod($this, __FUNCTION__); 313 $r = new \ReflectionMethod($this, __FUNCTION__);
314 if (__CLASS__ !== $r->getDeclaringClass()->getName()) { 314 if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
315 @trigger_error(sprintf('Method %s() will have a second `$context = array()` argument in version 4.0. Not defining it is deprecated since Symfony 3.3.', __METHOD__), E_USER_DEPRECATED); 315 @trigger_error(sprintf('The "%s()" method will have a second `$context = []` argument in version 4.0. Not defining it is deprecated since Symfony 3.3.', __METHOD__), E_USER_DEPRECATED);
316 } 316 }
317 } 317 }
318 318
319 $context = array(); 319 $context = [];
320 } 320 }
321 321
322 return $this->decoder->supportsDecoding($format, $context); 322 return $this->decoder->supportsDecoding($format, $context);
323 } 323 }
324 } 324 }