You are here

class RestWSFormatXML in RESTful Web Services 7

Same name and namespace in other branches
  1. 7.2 restws.formats.inc \RestWSFormatXML

A formatter for XML.

Hierarchy

Expanded class hierarchy of RestWSFormatXML

2 string references to 'RestWSFormatXML'
hook_restws_format_info in ./restws.api.php
Define restws compatible formats.
restws_restws_format_info in ./restws.module
Implements hook_restws_format_info().

File

./restws.formats.inc, line 210
RESTful web services module formats.

View source
class RestWSFormatXML extends RestWSBaseFormat {

  /**
   * Gets the representation of a resource.
   */
  public function viewResource($resourceController, $id) {
    $xml = new DOMDocument('1.0', 'utf-8');
    $element = $xml
      ->createElement($resourceController
      ->resource());
    self::addToXML($xml, $element, $resourceController
      ->wrapper($id));
    $xml
      ->appendChild($element);
    return $xml
      ->saveXML();
  }

  /**
   * Creates a new resource.
   */
  public function createResource($resourceController, $data) {
    $values = $this
      ->unserialize($data);
    $id = $resourceController
      ->create($values);
    $xml = new DOMDocument('1.0', 'utf-8');
    $element = $xml
      ->createElement('uri');
    self::setXMLReference($element, $resourceController
      ->resource(), $id);
    $xml
      ->appendChild($element);
    return $xml
      ->saveXML();
  }
  public function serialize($data) {

    // Return an empty XML document.
    $xml = new DOMDocument('1.0', 'utf-8');
    return $xml
      ->saveXML();
  }
  public function unserialize($data) {

    // Disable XML external entity expansion for security reasons.
    libxml_disable_entity_loader(TRUE);
    $xml = simplexml_load_string($data);
    return self::xmlToArray($xml);
  }

  /**
   * Turns the xml structure into an array of values.
   */
  public static function xmlToArray(SimpleXMLElement $xml) {
    $children = $xml
      ->children();
    foreach ($xml
      ->children() as $name => $element) {
      $result[$name] = self::xmlToArray($element);
    }
    if (!isset($result)) {
      $result = ($string = (string) $xml) ? $string : NULL;
    }
    return $result;
  }

  /**
   * Adds the data of the given wrapper to the given XML element.
   */
  public static function addToXML(DOMDocument $doc, DOMNode $parent, $wrapper) {
    $filtered = restws_property_access_filter($wrapper);
    foreach ($filtered as $name => $property) {
      try {
        if ($property instanceof EntityDrupalWrapper) {

          // For referenced entities only return the URI.
          if ($id = $property
            ->getIdentifier()) {
            $element = $doc
              ->createElement(is_numeric($name) ? 'item' : $name);
            $parent
              ->appendChild($element);
            self::setXMLReference($element, $property
              ->type(), $id);
          }
        }
        elseif ($property instanceof EntityValueWrapper) {
          $escaped = $doc
            ->createTextNode($property
            ->value());
          $element = $doc
            ->createElement(is_numeric($name) ? 'item' : $name);
          $element
            ->appendChild($escaped);
          $parent
            ->appendChild($element);
        }
        elseif ($property instanceof EntityListWrapper || $property instanceof EntityStructureWrapper) {
          $element = $doc
            ->createElement(is_numeric($name) ? 'item' : $name);
          $parent
            ->appendChild($element);
          self::addToXML($doc, $element, $property);
        }
      } catch (EntityMetadataWrapperException $e) {

        // A property causes problems - ignore that.
      }
    }
  }
  public static function setXMLReference(DOMElement $node, $resource, $id) {
    $node->nodeValue = restws_resource_uri($resource, $id);
    $node
      ->setAttribute('resource', $resource);
    $node
      ->setAttribute('id', $id);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RestWSBaseFormat::$formatInfo protected property
RestWSBaseFormat::$formatName protected property
RestWSBaseFormat::deleteResource public function Deletes a resource. Overrides RestWSFormatInterface::deleteResource
RestWSBaseFormat::getData public static function Gets a simple PHP array using URI references for some wrapped data.
RestWSBaseFormat::getName public function Returns the short name of this format. Overrides RestWSFormatInterface::getName
RestWSBaseFormat::getResourceReference public static function
RestWSBaseFormat::mimeType public function Returns the mime type of this format, e.g. 'application/json' or 'application/xml'. Overrides RestWSFormatInterface::mimeType
RestWSBaseFormat::updateResource public function Updates a resource. Overrides RestWSFormatInterface::updateResource 1
RestWSBaseFormat::__construct public function 1
RestWSFormatXML::addToXML public static function Adds the data of the given wrapper to the given XML element.
RestWSFormatXML::createResource public function Creates a new resource. Overrides RestWSBaseFormat::createResource
RestWSFormatXML::serialize public function
RestWSFormatXML::setXMLReference public static function
RestWSFormatXML::unserialize public function
RestWSFormatXML::viewResource public function Gets the representation of a resource. Overrides RestWSBaseFormat::viewResource
RestWSFormatXML::xmlToArray public static function Turns the xml structure into an array of values.