From 73b0345ff4c2fc55339a19b204131daf27069b2e Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 3 May 2012 20:35:01 -0400 Subject: [PATCH] Fix issue with non-sequential array keys. Xml::fromArray() should not cause errors with non-sequential numeric array keys. Fixes #2580 --- lib/Cake/Test/Case/Utility/XmlTest.php | 37 ++++++++++++++++++++++++++ lib/Cake/Utility/Xml.php | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Utility/XmlTest.php b/lib/Cake/Test/Case/Utility/XmlTest.php index d1bf8d1b22a..7eb68f82360 100644 --- a/lib/Cake/Test/Case/Utility/XmlTest.php +++ b/lib/Cake/Test/Case/Utility/XmlTest.php @@ -322,6 +322,43 @@ public function testFromArray() { $this->assertEquals(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText); } +/** + * 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 = << + + 235 + + 9646 + + + 9647 + + +XML; + $this->assertXmlStringEqualsXmlString($expected, $obj->asXML()); + } + /** * data provider for fromArray() failures * diff --git a/lib/Cake/Utility/Xml.php b/lib/Cake/Utility/Xml.php index 70044471eaf..6b96f7f6151 100644 --- a/lib/Cake/Utility/Xml.php +++ b/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 foreach ($value as $item) { $itemData = compact('dom', 'node', 'key', 'format'); $itemData['value'] = $item;