From c6464fb9a51b87df8ec52336eaf2b78524a9a80a Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Sat, 2 May 2015 14:20:26 +0200 Subject: [PATCH] XmlView should also have options just as JsonView has. Also fixed documentation. --- src/Utility/Xml.php | 6 +-- src/View/JsonView.php | 4 ++ src/View/XmlView.php | 7 +++ tests/TestCase/View/XmlViewTest.php | 72 +++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 3 deletions(-) diff --git a/src/Utility/Xml.php b/src/Utility/Xml.php index 8ca4c41c96f..0c66054a3c2 100644 --- a/src/Utility/Xml.php +++ b/src/Utility/Xml.php @@ -160,7 +160,7 @@ protected static function _loadXml($input, $options) * * ### Options * - * - `format` If create childs ('tags') or attributes ('attribute'). + * - `format` If create childs ('tags') or attributes ('attributes'). * - `pretty` Returns formatted Xml when set to `true`. Defaults to `false` * - `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. @@ -184,7 +184,7 @@ protected static function _loadXml($input, $options) * * `1defectdescription` * - * And calling `Xml::fromArray($value, 'attribute');` Will generate: + * And calling `Xml::fromArray($value, 'attributes');` Will generate: * * `description` * @@ -237,7 +237,7 @@ public static function fromArray($input, $options = []) * @param \DOMDocument $dom Handler to DOMDocument * @param \DOMElement $node Handler to DOMElement (child) * @param array $data Array of data to append to the $node. - * @param string $format Either 'attribute' or 'tags'. This determines where nested keys go. + * @param string $format Either 'attributes' or 'tags'. This determines where nested keys go. * @return void * @throws \Cake\Utility\Exception\XmlException */ diff --git a/src/View/JsonView.php b/src/View/JsonView.php index be1ad2f6baa..ef9b11caaef 100644 --- a/src/View/JsonView.php +++ b/src/View/JsonView.php @@ -147,6 +147,10 @@ public function render($view = null, $layout = null) /** * Serialize view vars * + * ### Special parameters + * `_jsonOptions` You can set custom options for json_encode() this way, + * e.g. `JSON_HEX_TAG | JSON_HEX_APOS`. + * * @param array|string $serialize The name(s) of the view variable(s) that need(s) to be serialized * @return string The serialized data */ diff --git a/src/View/XmlView.php b/src/View/XmlView.php index 172c473efcb..706baa3730b 100644 --- a/src/View/XmlView.php +++ b/src/View/XmlView.php @@ -131,6 +131,10 @@ public function render($view = null, $layout = null) /** * Serialize view vars. * + * ### Special parameters + * `_xmlOptions` You can set an array of custom options for Xml::fromArray() this way, e.g. + * 'format' as 'attributes' instead of 'tags'. + * * @param array|string $serialize The name(s) of the view variable(s) that need(s) to be serialized * @return string The serialized data */ @@ -154,6 +158,9 @@ protected function _serialize($serialize) } $options = []; + if (isset($this->viewVars['_xmlOptions'])) { + $options = $this->viewVars['_xmlOptions']; + } if (Configure::read('debug')) { $options['pretty'] = true; } diff --git a/tests/TestCase/View/XmlViewTest.php b/tests/TestCase/View/XmlViewTest.php index 46cfe163977..1f3f45f1b2b 100644 --- a/tests/TestCase/View/XmlViewTest.php +++ b/tests/TestCase/View/XmlViewTest.php @@ -107,6 +107,78 @@ public function testRenderSerializeNoHelpers() $this->assertFalse(isset($View->Html), 'No helper loaded.'); } + /** + * Test that rendering with _serialize respects XML options. + * + * @return void + */ + public function testRenderSerializeWithOptions() + { + $Request = new Request(); + $Response = new Response(); + $Controller = new Controller($Request, $Response); + $data = [ + '_serialize' => ['tags'], + '_xmlOptions' => ['format' => 'attributes'], + 'tags' => [ + 'tag' => [ + [ + 'id' => '1', + 'name' => 'defect' + ], + [ + 'id' => '2', + 'name' => 'enhancement' + ] + ] + ] + ]; + $Controller->set($data); + $Controller->viewClass = 'Xml'; + $View = $Controller->createView(); + $result = $View->render(); + + $expected = Xml::build(['response' => ['tags' => $data['tags']]], $data['_xmlOptions'])->asXML(); + $this->assertSame($expected, $result); + } + + /** + * Test that rendering with _serialize can work with string setting. + * + * @return void + */ + public function testRenderSerializeWithString() + { + $Request = new Request(); + $Response = new Response(); + $Controller = new Controller($Request, $Response); + $data = [ + '_serialize' => 'tags', + '_xmlOptions' => ['format' => 'attributes'], + 'tags' => [ + 'tags' => [ + 'tag' => [ + [ + 'id' => '1', + 'name' => 'defect' + ], + [ + 'id' => '2', + 'name' => 'enhancement' + ] + ] + ] + ] + ]; + $Controller->set($data); + $Controller->viewClass = 'Xml'; + $View = $Controller->createView(); + $result = $View->render(); + + $expected = Xml::build($data['tags'], $data['_xmlOptions'])->asXML(); + $this->assertSame($expected, $result); + } + /** * Test render with an array in _serialize *