Browse files

Add formatoutput option to Xml::fromArray

  • Loading branch information...
1 parent 026df62 commit fc34d9fe7fe4c2453a8b909cae3ddf7427b31a2d @ceeram ceeram committed Mar 13, 2013
Showing with 87 additions and 1 deletion.
  1. +81 −0 lib/Cake/Test/Case/Utility/XmlTest.php
  2. +6 −1 lib/Cake/Utility/Xml.php
View
81 lib/Cake/Test/Case/Utility/XmlTest.php
@@ -403,6 +403,87 @@ public function testFromArrayNonSequentialKeys() {
}
/**
+ * testFromArrayFormatOutput method
+ *
+ * @return void
+ */
+ public function testFromArrayFormatOutput() {
+ $xml = array(
+ 'tags' => array(
+ 'tag' => array(
+ array(
+ 'id' => '1',
+ 'name' => 'defect'
+ ),
+ array(
+ 'id' => '2',
+ 'name' => 'enhancement'
+ )
+ )
+ )
+ );
+
+ $expected = <<<XML
+<?xml version="1.0" encoding="UTF-8"?>
+<tags><tag><id>1</id><name>defect</name></tag><tag><id>2</id><name>enhancement</name></tag></tags>
+
+XML;
+ $xmlResponse = Xml::fromArray($xml, array('formatOutput' => false));
+ $this->assertEquals($expected, $xmlResponse->asXML());
+
+ $expected = <<<XML
+<?xml version="1.0" encoding="UTF-8"?>
+<tags>
+ <tag>
+ <id>1</id>
+ <name>defect</name>
+ </tag>
+ <tag>
+ <id>2</id>
+ <name>enhancement</name>
+ </tag>
+</tags>
+
+XML;
+ $xmlResponse = Xml::fromArray($xml, array('formatOutput' => true));
+ $this->assertEquals($expected, $xmlResponse->asXML());
+
+ $xml = array(
+ 'tags' => array(
+ 'tag' => array(
+ array(
+ 'id' => '1',
+ 'name' => 'defect'
+ ),
+ array(
+ 'id' => '2',
+ 'name' => 'enhancement'
+ )
+ )
+ )
+ );
+
+ $expected = <<<XML
+<?xml version="1.0" encoding="UTF-8"?>
+<tags><tag id="1" name="defect"/><tag id="2" name="enhancement"/></tags>
+
+XML;
+ $xmlResponse = Xml::fromArray($xml, array('formatOutput' => false, 'format' => 'attributes'));
+ $this->assertEquals($expected, $xmlResponse->asXML());
+
+ $expected = <<<XML
+<?xml version="1.0" encoding="UTF-8"?>
+<tags>
+ <tag id="1" name="defect"/>
+ <tag id="2" name="enhancement"/>
+</tags>
+
+XML;
+ $xmlResponse = Xml::fromArray($xml, array('formatOutput' => true, 'format' => 'attributes'));
+ $this->assertEquals($expected, $xmlResponse->asXML());
+ }
+
+/**
* data provider for fromArray() failures
*
* @return array
View
7 lib/Cake/Utility/Xml.php
@@ -150,6 +150,7 @@ protected static function _loadXml($input, $options) {
* ### Options
*
* - `format` If create childs ('tags') or attributes ('attribute').
+ * - `formatOutput` Returns formatted Xml
* - `version` Version of XML document. Default is 1.0.
* - `encoding` Encoding of XML document. If null remove from XML header. Default is the some of application.
* - `return` If return object of SimpleXMLElement ('simplexml') or DOMDocument ('domdocument'). Default is SimpleXMLElement.
@@ -197,11 +198,15 @@ public static function fromArray($input, $options = array()) {
'format' => 'tags',
'version' => '1.0',
'encoding' => Configure::read('App.encoding'),
- 'return' => 'simplexml'
+ 'return' => 'simplexml',
+ 'formatOutput' => false
);
$options = array_merge($defaults, $options);
$dom = new DOMDocument($options['version'], $options['encoding']);
+ if ($options['formatOutput'] === true) {
+ $dom->formatOutput = true;
+ }
self::_fromArray($dom, $dom, $input, $options['format']);
$options['return'] = strtolower($options['return']);

0 comments on commit fc34d9f

Please sign in to comment.