Skip to content

Commit

Permalink
Xml::build now supports options and can return DOMDocument.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Sep 9, 2010
1 parent 071ff04 commit c8c20ea
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
34 changes: 26 additions & 8 deletions cake/libs/xml.php
Expand Up @@ -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.'));
}
Expand Down Expand Up @@ -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',
Expand Down
15 changes: 15 additions & 0 deletions cake/tests/cases/libs/xml.test.php
Expand Up @@ -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');
}

/**
Expand Down

0 comments on commit c8c20ea

Please sign in to comment.