3 namespace Drupal\serialization\Encoder;
5 use Symfony\Component\Serializer\Encoder\EncoderInterface;
6 use Symfony\Component\Serializer\Encoder\DecoderInterface;
7 use Symfony\Component\Serializer\Encoder\XmlEncoder as BaseXmlEncoder;
8 use Symfony\Component\Serializer\SerializerAwareInterface;
9 use Symfony\Component\Serializer\SerializerAwareTrait;
12 * Adds XML support for serializer.
14 * This acts as a wrapper class for Symfony's XmlEncoder so that it is not
15 * implementing NormalizationAwareInterface, and can be normalized externally.
18 * This encoder should not be used directly. Rather, use the `serializer`
21 class XmlEncoder implements SerializerAwareInterface, EncoderInterface, DecoderInterface {
23 use SerializerAwareTrait;
26 * The formats that this Encoder supports.
30 static protected $format = ['xml'];
33 * An instance of the Symfony XmlEncoder to perform the actual encoding.
35 * @var \Symfony\Component\Serializer\Encoder\XmlEncoder
37 protected $baseEncoder;
40 * Gets the base encoder instance.
42 * @return \Symfony\Component\Serializer\Encoder\XmlEncoder
45 public function getBaseEncoder() {
46 if (!isset($this->baseEncoder)) {
47 $this->baseEncoder = new BaseXmlEncoder();
48 $this->baseEncoder->setSerializer($this->serializer);
51 return $this->baseEncoder;
55 * Sets the base encoder instance.
57 * @param \Symfony\Component\Serializer\Encoder\XmlEncoder $encoder
59 public function setBaseEncoder($encoder) {
60 $this->baseEncoder = $encoder;
66 public function encode($data, $format, array $context = []) {
67 return $this->getBaseEncoder()->encode($data, $format, $context);
73 public function supportsEncoding($format) {
74 return in_array($format, static::$format);
80 public function decode($data, $format, array $context = []) {
81 return $this->getBaseEncoder()->decode($data, $format, $context);
87 public function supportsDecoding($format) {
88 return in_array($format, static::$format);