You are here

public function RestWSFormatXML::xmlToArray in RESTful Web Services 7.2

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

Turns the xml structure into an array of values.

1 call to RestWSFormatXML::xmlToArray()
RestWSFormatXML::unserialize in ./restws.formats.inc

File

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

Class

RestWSFormatXML
A formatter for XML.

Code

public function xmlToArray($properties, SimpleXMLElement $xml, $listItemType = NULL) {
  foreach ($xml
    ->children() as $name => $element) {

    // Check if we are processing an entity, an item from a list or a list.
    if (isset($properties[$name]['type']) && (entity_property_list_extract_type($properties[$name]['type']) || entity_get_info($properties[$name]['type'])) || isset($listItemType)) {

      // If we are processing a list, then set the type of the list and save
      // the results into a a numeric array.
      if (isset($listItemType)) {
        $type = $listItemType;
        $result_pointer =& $result[];
      }
      else {
        $type = $properties[$name]['type'];
        $result_pointer =& $result[$name];
      }

      // Check if the type is a list.
      if (entity_property_list_extract_type($type)) {
        $result_pointer = $this
          ->xmlToArray($properties, $element, entity_property_list_extract_type($type));
      }
      else {
        $attributes = $element
          ->attributes();
        $values['id'] = (string) $attributes['id'];
        $values['resource'] = (string) $attributes['resource'];
        $values['uri'] = $this
          ->xmlToArray($properties, $element);
        $id = $this
          ->getResourceReferenceValue($type, $values);

        // If an id could be extracted, then a resource array was send.
        if ($id !== FALSE) {
          $result_pointer = $id;
        }
        else {

          // If no ID could be extracted, then save the inner text content of
          // the node, which is saved in the $values['uri'].
          $result_pointer = $values['uri'];
        }
      }
    }
    else {
      $result[$name] = $this
        ->xmlToArray($properties, $element);
    }
    foreach ($xml
      ->attributes() as $attribute_name => $attribute_value) {
      $result[$attribute_name] = $attribute_value;
    }
  }
  if (!isset($result)) {
    $result = ($string = (string) $xml) ? $string : NULL;
  }
  return $result;
}