Fixed issues with Set::extract and multiple numerically indexed nestings

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6656 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
the_undefined 2008-04-13 13:05:52 +00:00
parent c38c6a21ba
commit 593c4d7245
2 changed files with 66 additions and 10 deletions

View file

@ -418,22 +418,25 @@ class Set extends Object {
$match = false; $match = false;
if (array_key_exists($token, $context['item']) && (!$conditions || Set::matches($conditions, $context['item'][$token], $i))) { if (array_key_exists($token, $context['item']) && (!$conditions || Set::matches($conditions, $context['item'][$token], $i))) {
$match = array( $items = $context['item'][$token];
'trace' => am($context['trace'], $context['key']), if (!is_array($items) || !isset($items[0])) {
'key' => $token, $items = array($items);
'item' => $context['item'][$token], }
);
foreach ($items as $item) {
$matches[] = array(
'trace' => am($context['trace'], $context['key']),
'key' => $token,
'item' => $item,
);
}
} else if ($key === $token && (!$conditions || Set::matches($conditions, $context['item'], $i+1))) { } else if ($key === $token && (!$conditions || Set::matches($conditions, $context['item'], $i+1))) {
$match = array( $matches[] = array(
'trace' => am($context['trace'], $key), 'trace' => am($context['trace'], $key),
'key' => $key, 'key' => $key,
'item' => $context['item'], 'item' => $context['item'],
); );
} }
if ($match) {
$matches[] = $match;
}
} }
if (empty($tokens)) { if (empty($tokens)) {
break; break;

View file

@ -423,6 +423,59 @@ class SetTest extends UnitTestCase {
$expected = array(4); $expected = array(4);
$r = Set::extract('/User/id', $single); $r = Set::extract('/User/id', $single);
$this->assertEqual($r, $expected); $this->assertEqual($r, $expected);
$common = array(
array(
'Article' => array(
'id' => 1,
'name' => 'Article 1',
),
'Comment' => array(
array(
'id' => 1,
'user_id' => 5,
'article_id' => 1,
'text' => 'Comment 1',
),
array(
'id' => 2,
'user_id' => 23,
'article_id' => 1,
'text' => 'Comment 2',
),
array(
'id' => 3,
'user_id' => 17,
'article_id' => 1,
'text' => 'Comment 3',
),
),
),
array(
'Article' => array(
'id' => 2,
'name' => 'Article 2',
),
'Comment' => array(
array(
'id' => 4,
'user_id' => 2,
'article_id' => 2,
'text' => 'Comment 4',
),
array(
'id' => 5,
'user_id' => 3,
'article_id' => 2,
'text' => 'Comment 5',
),
),
)
);
$r = Set::extract('/Comment/id', $common);
$expected = array(1, 2, 3, 4, 5);
$this->assertEqual($r, $expected);
} }
/** /**
* undocumented function * undocumented function