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\SerializerAwareEncoder;
8 use Symfony\Component\Serializer\Encoder\XmlEncoder as BaseXmlEncoder;
11 * Adds XML support for serializer.
13 * This acts as a wrapper class for Symfony's XmlEncoder so that it is not
14 * implementing NormalizationAwareInterface, and can be normalized externally.
16 class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface {
19 * The formats that this Encoder supports.
23 static protected $format = ['xml'];
26 * An instance of the Symfony XmlEncoder to perform the actual encoding.
28 * @var \Symfony\Component\Serializer\Encoder\XmlEncoder
30 protected $baseEncoder;
33 * Gets the base encoder instance.
35 * @return \Symfony\Component\Serializer\Encoder\XmlEncoder
38 public function getBaseEncoder() {
39 if (!isset($this->baseEncoder)) {
40 $this->baseEncoder = new BaseXmlEncoder();
41 $this->baseEncoder->setSerializer($this->serializer);
44 return $this->baseEncoder;
48 * Sets the base encoder instance.
50 * @param \Symfony\Component\Serializer\Encoder\XmlEncoder $encoder
52 public function setBaseEncoder($encoder) {
53 $this->baseEncoder = $encoder;
59 public function encode($data, $format, array $context = []){
60 return $this->getBaseEncoder()->encode($data, $format, $context);
66 public function supportsEncoding($format) {
67 return in_array($format, static::$format);
73 public function decode($data, $format, array $context = []){
74 return $this->getBaseEncoder()->decode($data, $format, $context);
80 public function supportsDecoding($format) {
81 return in_array($format, static::$format);