From fc34d9fe7fe4c2453a8b909cae3ddf7427b31a2d Mon Sep 17 00:00:00 2001 From: Ceeram Date: Wed, 13 Mar 2013 13:00:09 +0100 Subject: [PATCH] Add formatoutput option to Xml::fromArray --- lib/Cake/Test/Case/Utility/XmlTest.php | 81 ++++++++++++++++++++++++++ lib/Cake/Utility/Xml.php | 7 ++- 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Utility/XmlTest.php b/lib/Cake/Test/Case/Utility/XmlTest.php index 9a2b5cc79..0e17fc6ae 100644 --- a/lib/Cake/Test/Case/Utility/XmlTest.php +++ b/lib/Cake/Test/Case/Utility/XmlTest.php @@ -402,6 +402,87 @@ XML; $this->assertXmlStringEqualsXmlString($expected, $obj->asXML()); } +/** + * testFromArrayFormatOutput method + * + * @return void + */ + public function testFromArrayFormatOutput() { + $xml = array( + 'tags' => array( + 'tag' => array( + array( + 'id' => '1', + 'name' => 'defect' + ), + array( + 'id' => '2', + 'name' => 'enhancement' + ) + ) + ) + ); + + $expected = << +1defect2enhancement + +XML; + $xmlResponse = Xml::fromArray($xml, array('formatOutput' => false)); + $this->assertEquals($expected, $xmlResponse->asXML()); + + $expected = << + + + 1 + defect + + + 2 + enhancement + + + +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; + $xmlResponse = Xml::fromArray($xml, array('formatOutput' => false, 'format' => 'attributes')); + $this->assertEquals($expected, $xmlResponse->asXML()); + + $expected = << + + + + + +XML; + $xmlResponse = Xml::fromArray($xml, array('formatOutput' => true, 'format' => 'attributes')); + $this->assertEquals($expected, $xmlResponse->asXML()); + } + /** * data provider for fromArray() failures * diff --git a/lib/Cake/Utility/Xml.php b/lib/Cake/Utility/Xml.php index f822abea8..d43c02dd4 100644 --- a/lib/Cake/Utility/Xml.php +++ b/lib/Cake/Utility/Xml.php @@ -150,6 +150,7 @@ class Xml { * ### 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 @@ class Xml { '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']);