Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. XmlEncoder.php

class XmlEncoder

Same name in this branch
  1. 11.1.x vendor/symfony/serializer/Encoder/XmlEncoder.php \Symfony\Component\Serializer\Encoder\XmlEncoder

Adds XML support for serializer.

This acts as a wrapper class for Symfony's XmlEncoder so that it is not implementing NormalizationAwareInterface, and can be normalized externally.

@internal This encoder should not be used directly. Rather, use the `serializer` service.

Hierarchy

  • class \Drupal\serialization\Encoder\XmlEncoder implements \Symfony\Component\Serializer\SerializerAwareInterface, \Symfony\Component\Serializer\Encoder\EncoderInterface, \Symfony\Component\Serializer\Encoder\DecoderInterface uses \Symfony\Component\Serializer\SerializerAwareTrait

Expanded class hierarchy of XmlEncoder

1 string reference to 'XmlEncoder'
serialization.services.yml in core/modules/serialization/serialization.services.yml
core/modules/serialization/serialization.services.yml
1 service uses XmlEncoder
serializer.encoder.xml in core/modules/serialization/serialization.services.yml
Drupal\serialization\Encoder\XmlEncoder

File

core/modules/serialization/src/Encoder/XmlEncoder.php, line 21

Namespace

Drupal\serialization\Encoder
View source
class XmlEncoder implements SerializerAwareInterface, EncoderInterface, DecoderInterface {
    use SerializerAwareTrait;
    
    /**
     * The formats that this Encoder supports.
     *
     * @var array
     */
    protected static $format = [
        'xml',
    ];
    
    /**
     * An instance of the Symfony XmlEncoder to perform the actual encoding.
     *
     * @var \Symfony\Component\Serializer\Encoder\XmlEncoder
     */
    protected $baseEncoder;
    
    /**
     * Gets the base encoder instance.
     *
     * @return \Symfony\Component\Serializer\Encoder\XmlEncoder
     *   The base encoder.
     */
    public function getBaseEncoder() {
        if (!isset($this->baseEncoder)) {
            $this->baseEncoder = new BaseXmlEncoder();
            $this->baseEncoder
                ->setSerializer($this->serializer);
        }
        return $this->baseEncoder;
    }
    
    /**
     * Sets the base encoder instance.
     *
     * @param \Symfony\Component\Serializer\Encoder\XmlEncoder $encoder
     *   The XML encoder.
     */
    public function setBaseEncoder($encoder) {
        $this->baseEncoder = $encoder;
    }
    
    /**
     * {@inheritdoc}
     */
    public function encode($data, $format, array $context = []) : string {
        return $this->getBaseEncoder()
            ->encode($data, $format, $context);
    }
    
    /**
     * {@inheritdoc}
     */
    public function supportsEncoding(string $format, array $context = []) : bool {
        return in_array($format, static::$format);
    }
    
    /**
     * {@inheritdoc}
     */
    public function decode($data, $format, array $context = []) : mixed {
        return $this->getBaseEncoder()
            ->decode($data, $format, $context);
    }
    
    /**
     * {@inheritdoc}
     */
    public function supportsDecoding(string $format, array $context = []) : bool {
        return in_array($format, static::$format);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
SerializerAwareTrait::$serializer protected property
SerializerAwareTrait::setSerializer public function
XmlEncoder::$baseEncoder protected property An instance of the Symfony XmlEncoder to perform the actual encoding.
XmlEncoder::$format protected static property The formats that this Encoder supports.
XmlEncoder::decode public function Decodes a string into PHP data. Overrides DecoderInterface::decode
XmlEncoder::encode public function Encodes data into the given format. Overrides EncoderInterface::encode
XmlEncoder::getBaseEncoder public function Gets the base encoder instance.
XmlEncoder::setBaseEncoder public function Sets the base encoder instance.
XmlEncoder::supportsDecoding public function Checks whether the deserializer can decode from given format. Overrides DecoderInterface::supportsDecoding
XmlEncoder::supportsEncoding public function Checks whether the serializer can encode to given format. Overrides EncoderInterface::supportsEncoding
RSS feed
Powered by Drupal