Skip to content

Commit

Permalink
Xml::fromArray now receives a list of options unless simple format.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Sep 8, 2010
1 parent a8b6182 commit 071ff04
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
36 changes: 30 additions & 6 deletions cake/libs/xml.php
Expand Up @@ -81,6 +81,13 @@ public static function build($input) {
/**
* Transform an array into a SimpleXMLElement
*
* ### Options
*
* - `format` If create childs ('tags') or attributes ('attribute').
* - `version` Version of XML document. Default is 1.0.
* - `encoding` Encoding of XML document. Default is the some of application.
* - `return` If return object of SimpleXMLElement ('simplexml') or DOMDocument ('domdocument'). Default is SimpleXMLElement.
*
* Using the following data:
*
* {{{
Expand All @@ -104,20 +111,37 @@ public static function build($input) {
* `<root><tag id="1" value="defect">description</tag></root>`
*
* @param array $input Array with data
* @param string $format If create childs ('tags') or attributes ('attribute').
* @return object SimpleXMLElement
* @param array $options The options to use
* @return object SimpleXMLElement or DOMDocument
*/
public static function fromArray($input, $format = 'tags') {
public static function fromArray($input, $options = array()) {
if (!is_array($input) || count($input) !== 1) {
throw new Exception(__('Invalid input.'));
}
$key = key($input);
if (is_integer($key)) {
throw new Exception(__('The key of input must be alphanumeric'));
}
$dom = new DOMDocument('1.0');
self::_fromArray($dom, $dom, $input, $format);
return new SimpleXMLElement($dom->saveXML());

if (is_string($options)) {
$options = array('format' => $options);
}
$defaults = array(
'format' => 'tags',
'version' => '1.0',
'encoding' => Configure::read('App.encoding'),
'return' => 'simplexml'
);
$options = array_merge($defaults, $options);

$dom = new DOMDocument($options['version'], $options['encoding']);
self::_fromArray($dom, $dom, $input, $options['format']);

$options['return'] = strtolower($options['return']);
if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') {
return new SimpleXMLElement($dom->saveXML());
}
return $dom;
}

/**
Expand Down
37 changes: 30 additions & 7 deletions cake/tests/cases/libs/xml.test.php
Expand Up @@ -27,6 +27,29 @@
*/
class XmlTest extends CakeTestCase {

/**
* setup method
*
* @access public
* @return void
*/
function setUp() {
parent::setup();
$this->_appEncoding = Configure::read('App.encoding');
Configure::write('App.encoding', 'UTF-8');
}

/**
* tearDown method
*
* @access public
* @return void
*/
function tearDown() {
parent::tearDown();
Configure::write('App.encoding', $this->_appEncoding);
}

/**
* testBuild method
*
Expand All @@ -39,7 +62,7 @@ public function testBuild() {
$this->assertEqual((string)$obj->getName(), 'tag');
$this->assertEqual((string)$obj, 'value');

$xml = '<?xml version="1.0"?><tag>value</tag>';
$xml = '<?xml version="1.0" encoding="UTF-8"?><tag>value</tag>';
$this->assertEqual($obj, Xml::build($xml));

$xml = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'fixtures' . DS . 'sample.xml';
Expand Down Expand Up @@ -119,14 +142,14 @@ public function testFromArray() {
$this->assertTrue($obj instanceof SimpleXMLElement);
$this->assertEqual($obj->getName(), 'tags');
$this->assertEqual(count($obj), 2);
$xmlText = '<' . '?xml version="1.0"?><tags><tag id="1" name="defect"/><tag id="2" name="enhancement"/></tags>';
$xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags><tag id="1" name="defect"/><tag id="2" name="enhancement"/></tags>';
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);

$obj = Xml::fromArray($xml);
$this->assertTrue($obj instanceof SimpleXMLElement);
$this->assertEqual($obj->getName(), 'tags');
$this->assertEqual(count($obj), 2);
$xmlText = '<' . '?xml version="1.0"?><tags><tag><id>1</id><name>defect</name></tag><tag><id>2</id><name>enhancement</name></tag></tags>';
$xmlText = '<' . '?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>';
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);

$xml = array(
Expand Down Expand Up @@ -171,7 +194,7 @@ public function testFromArray() {
)
);
$obj = Xml::fromArray($xml, 'tags');
$xmlText = '<' . '?xml version="1.0"?><tags><tag id="1"><name>defect</name></tag><tag id="2"><name>enhancement</name></tag></tags>';
$xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags><tag id="1"><name>defect</name></tag><tag id="2"><name>enhancement</name></tag></tags>';
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);

$xml = array(
Expand All @@ -191,7 +214,7 @@ public function testFromArray() {
)
);
$obj = Xml::fromArray($xml, 'tags');
$xmlText = '<' . '?xml version="1.0"?><tags>All tags<tag id="1">Tag 1<name>defect</name></tag><tag id="2"><name>enhancement</name></tag></tags>';
$xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags>All tags<tag id="1">Tag 1<name>defect</name></tag><tag id="2"><name>enhancement</name></tag></tags>';
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);

$xml = array(
Expand All @@ -203,7 +226,7 @@ public function testFromArray() {
)
);
$obj = Xml::fromArray($xml, 'attributes');
$xmlText = '<' . '?xml version="1.0"?><tags><tag id="1">defect</tag></tags>';
$xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags><tag id="1">defect</tag></tags>';
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
}

Expand Down Expand Up @@ -467,7 +490,7 @@ public function testXmlRpc() {
);
$this->assertIdentical(Xml::toArray($xml), $expected);

$xmlText = '<?xml version="1.0"?><methodResponse><params><param><value><array><data><value><int>1</int></value><value><string>testing</string></value></data></array></value></param></params></methodResponse>';
$xmlText = '<?xml version="1.0" encoding="UTF-8"?><methodResponse><params><param><value><array><data><value><int>1</int></value><value><string>testing</string></value></data></array></value></param></params></methodResponse>';
$xml = Xml::build($xmlText);
$expected = array(
'methodResponse' => array(
Expand Down

0 comments on commit 071ff04

Please sign in to comment.