Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Backporting regression fix in Set::extract() for 1.3 to 1.2. Closes #…
  • Loading branch information
ADmad committed Oct 20, 2011
1 parent b0f9894 commit f0af34b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
18 changes: 10 additions & 8 deletions cake/libs/set.php
Expand Up @@ -432,14 +432,9 @@ function extract($path, $data = null, $options = array()) {
'key' => $key,
'item' => array_keys($context['item']),
);
} elseif (($key === $token || (ctype_digit($token) && $key == $token) || $token === '.')) {
$context['trace'][] = $key;
$matches[] = array(
'trace' => $context['trace'],
'key' => $key,
'item' => $context['item'],
);
} elseif (is_array($context['item']) && array_key_exists($token, $context['item'])) {
} elseif (is_array($context['item'])
&& array_key_exists($token, $context['item'])
&& !(strval($key) === strval($token) && count($tokens) == 1 && $tokens[0] === '.')) {
$items = $context['item'][$token];
if (!is_array($items)) {
$items = array($items);
Expand Down Expand Up @@ -478,6 +473,13 @@ function extract($path, $data = null, $options = array()) {
'item' => $item,
);
}
} elseif ($key === $token || (ctype_digit($token) && $key == $token) || $token === '.') {
$context['trace'][] = $key;
$matches[] = array(
'trace' => $context['trace'],
'key' => $key,
'item' => $context['item'],
);
}
}
if ($conditions) {
Expand Down
30 changes: 28 additions & 2 deletions cake/tests/cases/libs/set.test.php
Expand Up @@ -902,7 +902,7 @@ function testExtract() {
$result = Set::extract('/ParentNode/name', $hasMany);
$expected = array('Second');
$this->assertEqual($result, $expected);

$data = array(
array(
'Category' => array(
Expand Down Expand Up @@ -936,6 +936,32 @@ function testExtract() {
);
$result = Set::extract('/Category[id=1]/..', $data);
$this->assertEqual($result, $expected);

$data = array(
array(
'ChildNode' => array('id' => 1),
array('name' => 'Item 1')
),
array(
'ChildNode' => array('id' => 2),
array('name' => 'Item 2')
),
);

$expected = array(
'Item 1',
'Item 2'
);
$result = Set::extract('/0/name', $data);
$this->assertEqual($result, $expected);

$data = array(
array('A1', 'B1'),
array('A2', 'B2')
);
$expected = array('A1', 'A2');
$result = Set::extract('/0', $data);
$this->assertEqual($result, $expected);
}
/**
* test parent selectors with extract
Expand Down Expand Up @@ -1141,7 +1167,7 @@ function testExtractWithNonZeroArrays() {
$expected = array(1, 2, 3);
$r = Set::extract('/User/id', $nonZero);
$this->assertEqual($r, $expected);

$expected = array(
array('User' => array('id' => 1, 'name' => 'John')),
array('User' => array('id' => 2, 'name' => 'Bob')),
Expand Down

0 comments on commit f0af34b

Please sign in to comment.