mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-01 17:12:39 +00:00
Xml::fromArray now receives a list of options unless simple format.
This commit is contained in:
parent
a8b6182d43
commit
071ff04556
2 changed files with 60 additions and 13 deletions
|
@ -81,6 +81,13 @@ class Xml {
|
|||
/**
|
||||
* 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:
|
||||
*
|
||||
* {{{
|
||||
|
@ -104,10 +111,10 @@ class Xml {
|
|||
* `<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.'));
|
||||
}
|
||||
|
@ -115,10 +122,27 @@ class Xml {
|
|||
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);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursive method to create childs from array
|
||||
|
|
|
@ -27,6 +27,29 @@ App::import('Core', 'Xml');
|
|||
*/
|
||||
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
|
||||
*
|
||||
|
@ -39,7 +62,7 @@ class XmlTest extends CakeTestCase {
|
|||
$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';
|
||||
|
@ -119,14 +142,14 @@ class XmlTest extends CakeTestCase {
|
|||
$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(
|
||||
|
@ -171,7 +194,7 @@ class XmlTest extends CakeTestCase {
|
|||
)
|
||||
);
|
||||
$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(
|
||||
|
@ -191,7 +214,7 @@ class XmlTest extends CakeTestCase {
|
|||
)
|
||||
);
|
||||
$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(
|
||||
|
@ -203,7 +226,7 @@ class XmlTest extends CakeTestCase {
|
|||
)
|
||||
);
|
||||
$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);
|
||||
}
|
||||
|
||||
|
@ -467,7 +490,7 @@ class XmlTest extends CakeTestCase {
|
|||
);
|
||||
$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(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue