You are here

class XmlEncoder in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/serializer/Encoder/XmlEncoder.php \Symfony\Component\Serializer\Encoder\XmlEncoder
  2. 8 core/modules/serialization/src/Encoder/XmlEncoder.php \Drupal\serialization\Encoder\XmlEncoder
Same name and namespace in other branches
  1. 8.0 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.

Hierarchy

Expanded class hierarchy of XmlEncoder

1 file declares its use of XmlEncoder
XmlEncoderTest.php in core/modules/serialization/tests/src/Unit/Encoder/XmlEncoderTest.php
Contains \Drupal\Tests\serialization\Unit\Encoder\XmlEncoderTest.
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 20
Contains \Drupal\serialization\Encoder\XmlEncoder.

Namespace

Drupal\serialization\Encoder
View source
class XmlEncoder implements EncoderInterface, DecoderInterface {

  /**
   * The formats that this Encoder supports.
   *
   * @var array
   */
  protected static $format = array(
    '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();
    }
    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 = array()) {
    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 = array()) {
    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. 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