Merge pull request #6466 from cakephp/2.6-xml-options

Backport _xmlOptions
This commit is contained in:
Mark Story 2015-05-02 22:47:11 -04:00
commit f82280f654
3 changed files with 80 additions and 3 deletions

View file

@ -96,6 +96,76 @@ class XmlViewTest extends CakeTestCase {
$this->assertFalse(isset($View->Html), 'No helper loaded.');
}
/**
* Test that rendering with _serialize respects XML options.
*
* @return void
*/
public function testRenderSerializeWithOptions() {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
$data = array(
'_serialize' => array('tags'),
'_xmlOptions' => array('format' => 'attributes'),
'tags' => array(
'tag' => array(
array(
'id' => '1',
'name' => 'defect'
),
array(
'id' => '2',
'name' => 'enhancement'
)
)
)
);
$Controller->set($data);
$Controller->viewClass = 'Xml';
$View = new XmlView($Controller);
$result = $View->render();
$expected = Xml::build(array('response' => array('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 CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
$data = array(
'_serialize' => 'tags',
'_xmlOptions' => array('format' => 'attributes'),
'tags' => array(
'tags' => array(
'tag' => array(
array(
'id' => '1',
'name' => 'defect'
),
array(
'id' => '2',
'name' => 'enhancement'
)
)
)
)
);
$Controller->set($data);
$Controller->viewClass = 'Xml';
$View = new XmlView($Controller);
$result = $View->render();
$expected = Xml::build($data['tags'], $data['_xmlOptions'])->asXML();
$this->assertSame($expected, $result);
}
/**
* Test render with an array in _serialize
*

View file

@ -156,7 +156,7 @@ class Xml {
*
* ### 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.
@ -180,7 +180,7 @@ class Xml {
*
* `<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>`
*
@ -229,7 +229,7 @@ class Xml {
* @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 XmlException
*/

View file

@ -109,6 +109,10 @@ class XmlView extends View {
/**
* 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 $serialize The viewVars that need to be serialized.
* @return string The serialized data
*/
@ -131,6 +135,9 @@ class XmlView extends View {
}
$options = array();
if (isset($this->viewVars['_xmlOptions'])) {
$options = $this->viewVars['_xmlOptions'];
}
if (Configure::read('debug')) {
$options['pretty'] = true;
}