From 59b832707d8d5c9d016d8944cbe5a4ce8dcea016 Mon Sep 17 00:00:00 2001 From: Phally Date: Wed, 6 Oct 2010 13:19:54 +0200 Subject: [PATCH] Fixed Set::extract() where single numeric keys (everything not 0) were skipped. Fixes #1175 Signed-off-by: mark_story --- cake/libs/set.php | 4 ++-- cake/tests/cases/libs/set.test.php | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/cake/libs/set.php b/cake/libs/set.php index 2849b2897e5..d60231c75ae 100644 --- a/cake/libs/set.php +++ b/cake/libs/set.php @@ -390,7 +390,7 @@ function extract($path, $data = null, $options = array()) { $options = array_merge(array('flatten' => true), $options); if (!isset($contexts[0])) { $current = current($data); - if ((is_array($current) && count($data) <= 1) || !is_array($current) || !Set::numeric(array_keys($data))) { + if ((is_array($current) && count($data) < 1) || !is_array($current) || !Set::numeric(array_keys($data))) { $contexts = array($data); } } @@ -1127,4 +1127,4 @@ function &get() { trigger_error('get() is deprecated. Set class should be called statically', E_USER_WARNING); } } -?> \ No newline at end of file +?> diff --git a/cake/tests/cases/libs/set.test.php b/cake/tests/cases/libs/set.test.php index ece4fd443f6..61cd9c5a04b 100644 --- a/cake/tests/cases/libs/set.test.php +++ b/cake/tests/cases/libs/set.test.php @@ -1127,6 +1127,27 @@ function testExtractWithNonZeroArrays() { $expected = array(0 => array('Article' => array('id' => 1, 'approved' => 1))); $result = Set::extract('/Article[approved=1]', $startingAtOne); $this->assertEqual($result, $expected); + + $items = array( + 240 => array( + 'A' => array( + 'field1' => 'a240', + 'field2' => 'a240', + ), + 'B' => array( + 'field1' => 'b240', + 'field2' => 'b240' + ), + ) + ); + + $expected = array( + 0 => 'b240' + ); + + $result = Set::extract('/B/field1', $items); + $this->assertIdentical($result, $expected); + $this->assertIdentical($result, Set::extract('{n}.B.field1', $items)); } /** * testExtractWithArrays method @@ -2835,4 +2856,4 @@ function testFlatten() { $this->assertEqual($result, $expected); } } -?> \ No newline at end of file +?>