You are here

public function HttpClientXMLFormatter::serialize in Http Client 7.2

Same name and namespace in other branches
  1. 6.2 includes/HttpClientXMLFormatter.inc \HttpClientXMLFormatter::serialize()

Serializes arbitrary data to the implemented format. Directly stolen from http_server by Hugo Wetterberg

Parameters

mixed $data: The data that should be serialized.

Return value

string The serialized data as a string.

Overrides HttpClientFormatter::serialize

File

includes/formatter/HttpClientXMLFormatter.inc, line 39

Class

HttpClientXMLFormatter
Class for handling xml-responses. Returns a SimpleXML object

Code

public function serialize($data) {
  $doc = new DOMDocument('1.0', 'utf-8');
  $root_tag = $this->default_root;

  // Normalize any objects into an array.
  if (is_object($data)) {
    $data = get_object_vars($data);
  }

  // Check if we should adapt the name of the root element.
  if ($this->adaptive_root && is_array($data) && count($data) == 1 && !is_numeric(key($data))) {
    $root_tag = $this
      ->sanitizeNodeName(key($data));
    $data = current($data);
  }
  $root = $doc
    ->createElement($root_tag);
  $doc
    ->appendChild($root);
  $this
    ->xml_recurse($doc, $root, $data);
  return $doc
    ->saveXML();
}