Skip to content

Commit

Permalink
Fixing regression in Set::extract(). Closes #1895 #1816 #1692
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Aug 9, 2011
1 parent 6ef4ce4 commit 392102b
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 @@ -431,14 +431,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 @@ -477,6 +472,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 @@ -939,7 +939,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 @@ -973,6 +973,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);
}

/**
Expand Down Expand Up @@ -1180,7 +1206,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

2 comments on commit 392102b

@markstory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done :)

@ADmad
Copy link
Member Author

@ADmad ADmad commented on 392102b Aug 10, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I don't know whether it was divine intervention or just plain luck that i was able to patch this, cause I still have no idea how the whole logic works :)

Please sign in to comment.