125a2cc30df4223fa4c92940f4b4ffa8629b3df2
[yaffs-website] / web / core / modules / serialization / src / Encoder / XmlEncoder.php
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  * @internal
17  *   This encoder should not be used directly. Rather, use the `serializer`
18  *   service.
19  */
20 class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface {
21
22   /**
23    * The formats that this Encoder supports.
24    *
25    * @var array
26    */
27   static protected $format = ['xml'];
28
29   /**
30    * An instance of the Symfony XmlEncoder to perform the actual encoding.
31    *
32    * @var \Symfony\Component\Serializer\Encoder\XmlEncoder
33    */
34   protected $baseEncoder;
35
36   /**
37    * Gets the base encoder instance.
38    *
39    * @return \Symfony\Component\Serializer\Encoder\XmlEncoder
40    *   The base encoder.
41    */
42   public function getBaseEncoder() {
43     if (!isset($this->baseEncoder)) {
44       $this->baseEncoder = new BaseXmlEncoder();
45       $this->baseEncoder->setSerializer($this->serializer);
46     }
47
48     return $this->baseEncoder;
49   }
50
51   /**
52    * Sets the base encoder instance.
53    *
54    * @param \Symfony\Component\Serializer\Encoder\XmlEncoder $encoder
55    */
56   public function setBaseEncoder($encoder) {
57     $this->baseEncoder = $encoder;
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function encode($data, $format, array $context = []) {
64     return $this->getBaseEncoder()->encode($data, $format, $context);
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function supportsEncoding($format) {
71     return in_array($format, static::$format);
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function decode($data, $format, array $context = []) {
78     return $this->getBaseEncoder()->decode($data, $format, $context);
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function supportsDecoding($format) {
85     return in_array($format, static::$format);
86   }
87
88 }