You are here

class XmlEncoder in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/serialization/src/Encoder/XmlEncoder.php \Drupal\serialization\Encoder\XmlEncoder
  2. 10 core/modules/serialization/src/Encoder/XmlEncoder.php \Drupal\serialization\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

2 files declare their use of XmlEncoder
ResourceResponseSubscriberTest.php in core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php
XmlEncoderTest.php in core/modules/serialization/tests/src/Unit/Encoder/XmlEncoderTest.php
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
   */
  public function setBaseEncoder($encoder) {
    $this->baseEncoder = $encoder;
  }

  /**
   * {@inheritdoc}
   */
  public function encode($data, $format, array $context = []) {
    return $this
      ->getBaseEncoder()
      ->encode($data, $format, $context);
  }

  /**
   * {@inheritdoc}
   */
  public function supportsEncoding($format) {
    return in_array($format, static::$format);
  }

  /**
   * {@inheritdoc}
   */
  public function decode($data, $format, array $context = []) {
    return $this
      ->getBaseEncoder()
      ->decode($data, $format, $context);
  }

  /**
   * {@inheritdoc}
   */
  public function supportsDecoding($format) {
    return in_array($format, static::$format);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
XmlEncoder::encode public function Encodes data into the given format.
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.
XmlEncoder::supportsEncoding public function Checks whether the serializer can encode to given format.