You are here

protected function ServicesParserXML::unmarshalXML in Services 7.3

A recusive function that unmarshals an XML string, into a php array.

1 call to ServicesParserXML::unmarshalXML()
ServicesParserXML::parse in servers/rest_server/includes/ServicesParser.inc

File

servers/rest_server/includes/ServicesParser.inc, line 58

Class

ServicesParserXML

Code

protected function unmarshalXML($node, $array) {

  // For all child XML elements
  foreach ($node
    ->children() as $child) {
    if (count($child
      ->children()) > 0) {

      // if the child has children
      $att = 'is_array';
      if ($child
        ->attributes()->{$att}) {
        $new_array = array();

        // recursive through <item>
        foreach ($child
          ->children() as $item) {

          // Make sure that elements with no children gets a value assigned.
          $item_keys = array_keys((array) $item);
          if (count($item_keys) == 1 && current($item_keys) === 0) {
            $new_array[] = (string) $item[0];
          }
          elseif (is_object($item) && $item
            ->count() == 0) {
            $new_array[] = (string) $item;
          }
          else {
            $new_array[] = self::unmarshalXML($item, $array[$item
              ->getName()]);
          }
        }
      }
      else {

        // else, simply create an array where the key is name of the element
        $array[$child
          ->getName()] = array();
        $new_array = $this
          ->unmarshalXML($child, $array[$child
          ->getName()]);
      }

      // add $new_array to $array
      $array[$child
        ->getName()] = $new_array;
    }
    else {

      // Use the is_raw attribute on value elements for select type fields to
      // pass form validation. Example:
      // <field_terms_select>
      //    <und is_array="true">
      //      <item>
      //        <tid is_raw="true">10513</tid>
      //      </item>
      //      <item>
      //        <tid is_raw="true">10523</tid>
      //      </item>
      //    </und>
      //  </field_terms_select>
      if ($child
        ->attributes()->is_raw) {
        return (string) $child;
      }
      $array[$child
        ->getName()] = (string) $child;
    }
  }

  // return the resulting array
  return $array;
}