You are here

WSDecoderXML.php in Web Service Data 2.0.x

Same filename and directory in other branches
  1. 8 src/Plugin/WSDecoder/WSDecoderXML.php

File

src/Plugin/WSDecoder/WSDecoderXML.php
View source
<?php

namespace Drupal\wsdata\Plugin\WSDecoder;

use Drupal\wsdata\Plugin\WSDecoderBase;

/**
 * XML Decoder.
 *
 * @WSDecoder(
 *   id = "WSDecoderXML",
 *   label = @Translation("XML Decoder", context = "WSDecoder"),
 * )
 */
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;
  }

}

Classes

Namesort descending Description
WSDecoderXML XML Decoder.