You are here

class WSDecoderXML in Web Service Data 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/WSDecoder/WSDecoderXML.php \Drupal\wsdata\Plugin\WSDecoder\WSDecoderXML

XML Decoder.

Plugin annotation


@WSDecoder(
  id = "WSDecoderXML",
  label = @Translation("XML Decoder", context = "WSDecoder"),
)

Hierarchy

Expanded class hierarchy of WSDecoderXML

File

src/Plugin/WSDecoder/WSDecoderXML.php, line 15

Namespace

Drupal\wsdata\Plugin\WSDecoder
View source
class WSDecoderXML extends WSDecoderBase {

  /**
   * {@inheritdoc}
   *
   * Decode the web service response string.
   */
  public function decode($data) {
    if (!isset($data) || empty($data)) {
      return;
    }
    $data = trim($data);
    libxml_use_internal_errors(TRUE);
    try {
      $data = new \SimpleXMLElement($data);
      if ($data
        ->count() == 0) {
        return [
          $data
            ->getName() => $data
            ->__toString(),
        ];
      }
      $data = get_object_vars($data);
      foreach ($data as $key => $value) {
        $data[$key] = $this
          ->decodeXml($value);
      }
    } catch (exception $e) {
      return FALSE;
    }
    libxml_use_internal_errors(FALSE);
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  public function accepts() {
    return [
      'text/xml',
    ];
  }

  /**
   * XML Parsing helper function, converts nested XML objects into arrays.
   */
  private function decodeXml($value) {
    if (is_object($value) and get_class($value)) {
      $value = get_object_vars($value);
      foreach ($value as $k => $v) {
        $value[$k] = $this
          ->decodeXml($v);
      }
    }
    elseif (is_array($value)) {
      foreach ($value as $key => $xml) {
        $value[$key] = $this
          ->decodeXml($xml);
      }
    }
    return $value;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
WSDecoderBase::$data public property Storage for decoded data.
WSDecoderBase::$error protected property Storage for error information.
WSDecoderBase::$languages protected property Languages which we have data for.
WSDecoderBase::addData public function Add data to an empty object or replace all existing data. Overrides WSDecoderInterface::addData
WSDecoderBase::getData public function Retrieve the value for the given data key. Overrides WSDecoderInterface::getData
WSDecoderBase::getError public function Retrieve error message, if any. Overrides WSDecoderInterface::getError
WSDecoderBase::isCacheable public function Returns whether or not the result on the decoder are cacheable.
WSDecoderBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
WSDecoderXML::accepts public function Returns an array of the content type of the data this processor accepts. Overrides WSDecoderBase::accepts
WSDecoderXML::decode public function Decode the web service response string. Overrides WSDecoderBase::decode
WSDecoderXML::decodeXml private function XML Parsing helper function, converts nested XML objects into arrays.