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.
17 * This encoder should not be used directly. Rather, use the `serializer`
20 class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface {
23 * The formats that this Encoder supports.
27 static protected $format = ['xml'];
30 * An instance of the Symfony XmlEncoder to perform the actual encoding.
32 * @var \Symfony\Component\Serializer\Encoder\XmlEncoder
34 protected $baseEncoder;
37 * Gets the base encoder instance.
39 * @return \Symfony\Component\Serializer\Encoder\XmlEncoder
42 public function getBaseEncoder() {
43 if (!isset($this->baseEncoder)) {
44 $this->baseEncoder = new BaseXmlEncoder();
45 $this->baseEncoder->setSerializer($this->serializer);
48 return $this->baseEncoder;
52 * Sets the base encoder instance.
54 * @param \Symfony\Component\Serializer\Encoder\XmlEncoder $encoder
56 public function setBaseEncoder($encoder) {
57 $this->baseEncoder = $encoder;
63 public function encode($data, $format, array $context = []) {
64 return $this->getBaseEncoder()->encode($data, $format, $context);
70 public function supportsEncoding($format) {
71 return in_array($format, static::$format);
77 public function decode($data, $format, array $context = []) {
78 return $this->getBaseEncoder()->decode($data, $format, $context);
84 public function supportsDecoding($format) {
85 return in_array($format, static::$format);