Skip to content

Commit

Permalink
Update XmlResponse.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinochkin committed Jan 15, 2014
1 parent d9d602e commit 2e8783b
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions XmlResponse.php
@@ -1,7 +1,7 @@
<?php

/**
* This class is based on the JsonResponse class by Weavora Team and the code from various posters on http://snipplr.com/view/3491/
* This class is based on the JsonResponse class by Weavora Team and code from various posters on http://snipplr.com/view/3491/
*
* @author Fredrik Wollsén <fredrik@neam.se>
* @link http://neam.se
Expand All @@ -17,7 +17,7 @@ public function getContentType()

public function setParams($params = array())
{
// Associative single-valued array is treated as the root element
// An associative single-valued array is treated as a root element
if (is_array($params) && count($params) == 1 && ($keys = array_keys($params)) && !is_numeric($keys[0])) //count($params) == 1 && ($keys = array_keys($params)) && !is_int($keys[0])
{
$xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><{$keys[0]} />");
Expand All @@ -33,7 +33,7 @@ public function setParams($params = array())

/**
* The main function for converting to an XML document.
* Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
* Pass in a multi dimensional array and this recursively loops through and builds up an XML document.
*
* @param array $data
* @param string $rootNodeName - what you want the root node to be - defaultsto data.
Expand All @@ -45,7 +45,7 @@ public static function toXML($data, $defaultKey = null, &$xml = null)

if (is_null($defaultKey)) $defaultKey = 'element';

// turn off compatibility mode as simple xml throws a wobbly if you don't.
// turn off the compatibility mode as simple xml throws a wobbly if you don't.
if (ini_get('zend.ze1_compatibility_mode') == 1)
ini_set('zend.ze1_compatibility_mode', 0);

Expand All @@ -60,27 +60,27 @@ public static function toXML($data, $defaultKey = null, &$xml = null)
$key = $defaultKey;
}

// delete any char not allowed in XML element names
// delete any characters not allowed in XML element names
$key = preg_replace('/[^a-z0-9\-\_\.\:]/i', '', $key);

if (is_object($value))
{
$value = get_object_vars($value);
}

// if there is another array found recrusively call this function
// if there is another array found recursively call this function
if (is_array($value))
{
$node = self::is_assoc($value) || $numeric ? $xml->addChild($key) : $xml;

// recrusive call.
// recursive call.
if ($numeric)
$key = 'anon';
self::toXml($value, $key, $node);
} else
{

// add single node.
// add a single node.
$value = htmlspecialchars($value);
$xml->addChild($key, $value);
}
Expand All @@ -89,7 +89,7 @@ public static function toXML($data, $defaultKey = null, &$xml = null)
// pass back as XML
//return $xml->asXML();
//
// if you want the XML to be formatted, use the below instead to return the XML
// if you want the XML to be formatted, use the below part instead to return the XML
$doc = new DOMDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->loadXML($xml->asXML());
Expand All @@ -99,7 +99,7 @@ public static function toXML($data, $defaultKey = null, &$xml = null)

/**
* Convert an XML document to a multi dimensional array
* Pass in an XML document (or SimpleXMLElement object) and this recrusively loops through and builds a representative array
* Pass in an XML document (or SimpleXMLElement object) and this recursively loops through and builds a representative array
*
* @param string $xml - XML document - can optionally be a SimpleXMLElement object
* @return array ARRAY
Expand Down

0 comments on commit 2e8783b

Please sign in to comment.