Permalink
Browse files

Merge pull request #935 from bar/master-hash-flatten

Set::flatten() fails to generate keys when 'tip' value is an empty array.
  • Loading branch information...
2 parents ce3aa69 + 8b2b077 commit 4c6c3b05542bef349470bf87423674d8657a9f52 @markstory markstory committed Nov 2, 2012
Showing with 32 additions and 2 deletions.
  1. +15 −1 lib/Cake/Test/Case/Utility/HashTest.php
  2. +16 −0 lib/Cake/Test/Case/Utility/SetTest.php
  3. +1 −1 lib/Cake/Utility/Hash.php
@@ -298,7 +298,6 @@ public function testFlatten() {
'Author' => array('id' => '3', 'user' => 'larry', 'password' => null),
)
);
-
$result = Hash::flatten($data);
$expected = array(
'0.Post.id' => '1',
@@ -318,6 +317,21 @@ public function testFlatten() {
$this->assertEquals($expected, $result);
$data = array(
+ array(
+ 'Post' => array('id' => '1', 'author_id' => null, 'title' => 'First Post'),
+ 'Author' => array(),
+ )
+ );
+ $result = Hash::flatten($data);
+ $expected = array(
+ '0.Post.id' => '1',
+ '0.Post.author_id' => null,
+ '0.Post.title' => 'First Post',
+ '0.Author' => array()
+ );
+ $this->assertEquals($expected, $result);
+
+ $data = array(
array('Post' => array('id' => 1)),
array('Post' => array('id' => 2)),
);
@@ -3054,6 +3054,7 @@ public function testStrictKeyCheck() {
/**
* Tests Set::flatten
*
+ * @see Hash test cases, as Set::flatten() is just a proxy.
* @return void
*/
public function testFlatten() {
@@ -3064,6 +3065,21 @@ public function testFlatten() {
$data[9] = 'Shemp';
$result = Set::flatten($data);
$this->assertEquals($data, $result);
+
+ $data = array(
+ array(
+ 'Post' => array('id' => '1', 'author_id' => null, 'title' => 'First Post'),
+ 'Author' => array(),
+ )
+ );
+ $result = Set::flatten($data);
+ $expected = array(
+ '0.Post.id' => '1',
+ '0.Post.author_id' => null,
+ '0.Post.title' => 'First Post',
+ '0.Author' => array()
+ );
+ $this->assertEquals($expected, $result);
}
/**
@@ -527,7 +527,7 @@ public static function flatten(array $data, $separator = '.') {
$element = $data[$key];
unset($data[$key]);
- if (is_array($element)) {
+ if (is_array($element) && !empty($element)) {
if (!empty($data)) {
$stack[] = array($data, $path);
}

0 comments on commit 4c6c3b0

Please sign in to comment.