Skip to content
Permalink
Browse files

Xml::build now supports options and can return DOMDocument.

  • Loading branch information...
jrbasso committed Sep 8, 2010
1 parent 071ff04 commit c8c20eafdcdfe0e5666240ca54644430216121e2
Showing with 41 additions and 8 deletions.
  1. +26 −8 cake/libs/xml.php
  2. +15 −0 cake/tests/cases/libs/xml.test.php
@@ -65,13 +65,31 @@ class Xml {
* @return object SimpleXMLElement
* @throws Exception
*/
public static function build($input) {
public static function build($input, $options = array()) {
if (!is_array($options)) {
$options = array('return' => (string)$options);
}
$defaults = array(
'return' => 'simplexml'
);
$options = array_merge($defaults, $options);
if (is_array($input) || is_object($input)) {
return self::fromArray((array)$input);
} elseif (strstr($input, "<")) {
return new SimpleXMLElement($input);
} elseif (file_exists($input) || strpos($input, 'http://') === 0 || strpos($input, 'https://') === 0 ) {
return new SimpleXMLElement($input, null, true);
return self::fromArray((array)$input, $options);
} elseif (strpos($input, '<') !== false) {
if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') {
return new SimpleXMLElement($input);
}
$dom = new DOMDocument();
$dom->loadXML($input);
return $dom;
} elseif (file_exists($input) || strpos($input, 'http://') === 0 || strpos($input, 'https://') === 0) {
if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') {
return new SimpleXMLElement($input, null, true);
}
$dom = new DOMDocument();
$dom->load($input);
return $dom;
} elseif (!is_string($input)) {
throw new Exception(__('Invalid input.'));
}
@@ -123,8 +141,8 @@ public static function fromArray($input, $options = array()) {
throw new Exception(__('The key of input must be alphanumeric'));
}
if (is_string($options)) {
$options = array('format' => $options);
if (!is_array($options)) {
$options = array('format' => (string)$options);
}
$defaults = array(
'format' => 'tags',
@@ -65,17 +65,32 @@ public function testBuild() {
$xml = '<?xml version="1.0" encoding="UTF-8"?><tag>value</tag>';
$this->assertEqual($obj, Xml::build($xml));
$obj = Xml::build($xml, array('return' => 'domdocument'));
$this->assertTrue($obj instanceof DOMDocument);
$this->assertEqual($obj->firstChild->nodeName, 'tag');
$this->assertEqual($obj->firstChild->nodeValue, 'value');
$xml = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'fixtures' . DS . 'sample.xml';
$obj = Xml::build($xml);
$this->assertEqual($obj->getName(), 'tags');
$this->assertEqual(count($obj), 2);
$this->assertEqual(Xml::build($xml), Xml::build(file_get_contents($xml)));
$obj = Xml::build($xml, array('return' => 'domdocument'));
$this->assertEqual($obj->firstChild->nodeName, 'tags');
$this->assertEqual(Xml::build($xml, array('return' => 'domdocument')), Xml::build(file_get_contents($xml), array('return' => 'domdocument')));
$this->assertEqual(Xml::build($xml, array('return' => 'simplexml')), Xml::build($xml, 'simplexml'));
$xml = array('tag' => 'value');
$obj = Xml::build($xml);
$this->assertEqual($obj->getName(), 'tag');
$this->assertEqual((string)$obj, 'value');
$obj = Xml::build($xml, array('return' => 'domdocument'));
$this->assertEqual($obj->firstChild->nodeName, 'tag');
$this->assertEqual($obj->firstChild->nodeValue, 'value');
}
/**

0 comments on commit c8c20ea

Please sign in to comment.
You can’t perform that action at this time.