You are here

public static function RestWSFormatXML::addToXML in RESTful Web Services 7.2

Same name and namespace in other branches
  1. 7 restws.formats.inc \RestWSFormatXML::addToXML()

Adds the data of the given wrapper to the given XML element.

2 calls to RestWSFormatXML::addToXML()
RestWSFormatXML::queryResource in ./restws.formats.inc
Overrides RestWSBaseFormat::queryResource().
RestWSFormatXML::viewResource in ./restws.formats.inc
Gets the representation of a resource.

File

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

Class

RestWSFormatXML
A formatter for XML.

Code

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) {

        // Only primitive data types are allowed here. There might be complex
        // arrays/objects in EntityValueWrapper if no property information is
        // provided (example: the "data" property of commerce_price fields.
        if (is_scalar($property
          ->value())) {
          $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.
    }
  }
}