Permalink
Browse files

Fix issue with non-sequential array keys.

Xml::fromArray() should not cause errors with non-sequential
numeric array keys.

Fixes #2580
  • Loading branch information...
1 parent 128c719 commit 73b0345ff4c2fc55339a19b204131daf27069b2e @markstory markstory committed May 4, 2012
Showing with 38 additions and 1 deletion.
  1. +37 −0 lib/Cake/Test/Case/Utility/XmlTest.php
  2. +1 −1 lib/Cake/Utility/Xml.php
View
37 lib/Cake/Test/Case/Utility/XmlTest.php
@@ -323,6 +323,43 @@ public function testFromArray() {
}
/**
+ * Test non-sequential keys in list types.
+ *
+ * @return void
+ */
+ public function testFromArrayNonSequentialKeys() {
+ $xmlArray = array(
+ 'Event' => array(
+ array(
+ 'id' => '235',
+ 'Attribute' => array(
+ 0 => array(
+ 'id' => '9646',
+ ),
+ 2 => array(
+ 'id' => '9647',
+ )
+ )
+ )
+ )
+ );
+ $obj = Xml::fromArray($xmlArray);
+ $expected = <<<XML
+<?xml version="1.0" encoding="UTF-8"?>
+<Event>
+ <id>235</id>
+ <Attribute>
+ <id>9646</id>
+ </Attribute>
+ <Attribute>
+ <id>9647</id>
+ </Attribute>
+</Event>
+XML;
+ $this->assertXmlStringEqualsXmlString($expected, $obj->asXML());
+ }
+
+/**
* data provider for fromArray() failures
*
* @return array
View
2 lib/Cake/Utility/Xml.php
@@ -230,7 +230,7 @@ protected static function _fromArray($dom, $node, &$data, $format) {
if ($key[0] === '@') {
throw new XmlException(__d('cake_dev', 'Invalid array'));
}
- if (array_keys($value) === range(0, count($value) - 1)) { // List
+ if (is_numeric(implode(array_keys($value), ''))) { // List
@dereuromark
CakePHP member
dereuromark added a line comment May 4, 2012

isnt the first argument the glue and the second the array for implode()?

@markstory
CakePHP member
markstory added a line comment May 4, 2012

It should but the fun thing about implode() is it doesn't care which order its arguments come in.

@markstory
CakePHP member
markstory added a line comment May 4, 2012

Fixed in [ed1a64c]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
foreach ($value as $item) {
$itemData = compact('dom', 'node', 'key', 'format');
$itemData['value'] = $item;

0 comments on commit 73b0345

Please sign in to comment.