diff --git a/cake/libs/xml.php b/cake/libs/xml.php index 73a5aa62a47..16abceed509 100644 --- a/cake/libs/xml.php +++ b/cake/libs/xml.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', diff --git a/cake/tests/cases/libs/xml.test.php b/cake/tests/cases/libs/xml.test.php index b682b943b60..64876a9f115 100644 --- a/cake/tests/cases/libs/xml.test.php +++ b/cake/tests/cases/libs/xml.test.php @@ -65,6 +65,11 @@ public function testBuild() { $xml = 'value'; $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'); @@ -72,10 +77,20 @@ public function testBuild() { $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'); } /**