Skip to content

Commit 47d9181

Browse files
committed
Adding test case from 'mradosta'.
Fixing issue with non-zero indexed arrays and Set::extract() Fixes #6
1 parent 98ad480 commit 47d9181

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

cake/libs/set.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,10 @@ function extract($path, $data = null, $options = array()) {
386386
$contexts = $data;
387387
$options = array_merge(array('flatten' => true), $options);
388388
if (!isset($contexts[0])) {
389-
$contexts = array($data);
389+
$current = current($data);
390+
if ((is_array($current) && count($data) <= 1) || !is_array($current)) {
391+
$contexts = array($data);
392+
}
390393
}
391394
$tokens = array_slice(preg_split('/(?<!=)\/(?![a-z-]*\])/', $path), 1);
392395

cake/tests/cases/libs/set.test.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,30 @@ function testExtract() {
535535
$r = Set::extract('/User/@*', $tricky);
536536
$this->assertEqual($r, $expected);
537537

538+
$nonZero = array(
539+
1 => array(
540+
'User' => array(
541+
'id' => 1,
542+
'name' => 'John',
543+
)
544+
),
545+
2 => array(
546+
'User' => array(
547+
'id' => 2,
548+
'name' => 'Bob',
549+
)
550+
),
551+
3 => array(
552+
'User' => array(
553+
'id' => 3,
554+
'name' => 'Tony',
555+
)
556+
)
557+
);
558+
$expected = array(1, 2, 3);
559+
$r = Set::extract('/User/id', $nonZero);
560+
$this->assertEqual($r, $expected);
561+
538562
$common = array(
539563
array(
540564
'Article' => array(
@@ -969,7 +993,6 @@ function testExtract() {
969993
$expected = array(array('name' => 'zipfile.zip','type' => 'application/zip','tmp_name' => '/tmp/php178.tmp','error' => 0,'size' => '564647'));
970994
$r = Set::extract('/file/.[type=application/zip]', $f);
971995
$this->assertEqual($r, $expected);
972-
973996
}
974997
/**
975998
* testMatches method
@@ -2396,7 +2419,7 @@ function testXmlSetReverse() {
23962419
array(
23972420
'Item' => array(
23982421
'title' => 'An example of a correctly reversed XMLNode',
2399-
'Desc' => array(),
2422+
'desc' => array(),
24002423
)
24012424
)
24022425
);

0 commit comments

Comments
 (0)