comparison core/modules/serialization/src/Encoder/XmlEncoder.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\serialization\Encoder;
4
5 use Symfony\Component\Serializer\Encoder\EncoderInterface;
6 use Symfony\Component\Serializer\Encoder\DecoderInterface;
7 use Symfony\Component\Serializer\Encoder\SerializerAwareEncoder;
8 use Symfony\Component\Serializer\Encoder\XmlEncoder as BaseXmlEncoder;
9
10 /**
11 * Adds XML support for serializer.
12 *
13 * This acts as a wrapper class for Symfony's XmlEncoder so that it is not
14 * implementing NormalizationAwareInterface, and can be normalized externally.
15 */
16 class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface {
17
18 /**
19 * The formats that this Encoder supports.
20 *
21 * @var array
22 */
23 static protected $format = ['xml'];
24
25 /**
26 * An instance of the Symfony XmlEncoder to perform the actual encoding.
27 *
28 * @var \Symfony\Component\Serializer\Encoder\XmlEncoder
29 */
30 protected $baseEncoder;
31
32 /**
33 * Gets the base encoder instance.
34 *
35 * @return \Symfony\Component\Serializer\Encoder\XmlEncoder
36 * The base encoder.
37 */
38 public function getBaseEncoder() {
39 if (!isset($this->baseEncoder)) {
40 $this->baseEncoder = new BaseXmlEncoder();
41 $this->baseEncoder->setSerializer($this->serializer);
42 }
43
44 return $this->baseEncoder;
45 }
46
47 /**
48 * Sets the base encoder instance.
49 *
50 * @param \Symfony\Component\Serializer\Encoder\XmlEncoder $encoder
51 */
52 public function setBaseEncoder($encoder) {
53 $this->baseEncoder = $encoder;
54 }
55
56 /**
57 * {@inheritdoc}
58 */
59 public function encode($data, $format, array $context = []){
60 return $this->getBaseEncoder()->encode($data, $format, $context);
61 }
62
63 /**
64 * {@inheritdoc}
65 */
66 public function supportsEncoding($format) {
67 return in_array($format, static::$format);
68 }
69
70 /**
71 * {@inheritdoc}
72 */
73 public function decode($data, $format, array $context = []){
74 return $this->getBaseEncoder()->decode($data, $format, $context);
75 }
76
77 /**
78 * {@inheritdoc}
79 */
80 public function supportsDecoding($format) {
81 return in_array($format, static::$format);
82 }
83
84 }