Skip to content

Commit

Permalink
XmlView should also have options just as JsonView has. Also fixed doc…
Browse files Browse the repository at this point in the history
…umentation.
  • Loading branch information
Mark Scherer committed May 2, 2015
1 parent 12caa77 commit c6464fb
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Utility/Xml.php
Expand Up @@ -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.
Expand All @@ -184,7 +184,7 @@ protected static function _loadXml($input, $options)
*
* `<root><tag><id>1</id><value>defect</value>description</tag></root>`
*
* And calling `Xml::fromArray($value, 'attribute');` Will generate:
* And calling `Xml::fromArray($value, 'attributes');` Will generate:
*
* `<root><tag id="1" value="defect">description</tag></root>`
*
Expand Down Expand Up @@ -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
*/
Expand Down
4 changes: 4 additions & 0 deletions src/View/JsonView.php
Expand Up @@ -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
*/
Expand Down
7 changes: 7 additions & 0 deletions src/View/XmlView.php
Expand Up @@ -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
*/
Expand All @@ -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;
}
Expand Down
72 changes: 72 additions & 0 deletions tests/TestCase/View/XmlViewTest.php
Expand Up @@ -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
*
Expand Down

0 comments on commit c6464fb

Please sign in to comment.