Skip to content

Commit

Permalink
Adding test case from 'mradosta'.
Browse files Browse the repository at this point in the history
Fixing issue with non-zero indexed arrays and Set::extract()
Fixes #6
  • Loading branch information
markstory committed Aug 22, 2009
1 parent 98ad480 commit 47d9181
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
5 changes: 4 additions & 1 deletion cake/libs/set.php
Expand Up @@ -386,7 +386,10 @@ function extract($path, $data = null, $options = array()) {
$contexts = $data;
$options = array_merge(array('flatten' => true), $options);
if (!isset($contexts[0])) {
$contexts = array($data);
$current = current($data);
if ((is_array($current) && count($data) <= 1) || !is_array($current)) {
$contexts = array($data);
}
}
$tokens = array_slice(preg_split('/(?<!=)\/(?![a-z-]*\])/', $path), 1);

Expand Down
27 changes: 25 additions & 2 deletions cake/tests/cases/libs/set.test.php
Expand Up @@ -535,6 +535,30 @@ function testExtract() {
$r = Set::extract('/User/@*', $tricky);
$this->assertEqual($r, $expected);

$nonZero = array(
1 => array(
'User' => array(
'id' => 1,
'name' => 'John',
)
),
2 => array(
'User' => array(
'id' => 2,
'name' => 'Bob',
)
),
3 => array(
'User' => array(
'id' => 3,
'name' => 'Tony',
)
)
);
$expected = array(1, 2, 3);
$r = Set::extract('/User/id', $nonZero);
$this->assertEqual($r, $expected);

$common = array(
array(
'Article' => array(
Expand Down Expand Up @@ -969,7 +993,6 @@ function testExtract() {
$expected = array(array('name' => 'zipfile.zip','type' => 'application/zip','tmp_name' => '/tmp/php178.tmp','error' => 0,'size' => '564647'));
$r = Set::extract('/file/.[type=application/zip]', $f);
$this->assertEqual($r, $expected);

}
/**
* testMatches method
Expand Down Expand Up @@ -2396,7 +2419,7 @@ function testXmlSetReverse() {
array(
'Item' => array(
'title' => 'An example of a correctly reversed XMLNode',
'Desc' => array(),
'desc' => array(),
)
)
);
Expand Down

0 comments on commit 47d9181

Please sign in to comment.