Added support for dot char (selecting the current context node)

Fixed some more issues and added more tests

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6660 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
the_undefined 2008-04-14 11:08:45 +00:00
parent 9f040bd395
commit 4679dc9a12
2 changed files with 15 additions and 3 deletions

View file

@ -423,7 +423,7 @@ class Set extends Object {
$matches[] = $context; $matches[] = $context;
continue; continue;
} }
$match = false; $match = false;
if (array_key_exists($token, $context['item'])) { if (array_key_exists($token, $context['item'])) {
$items = $context['item'][$token]; $items = $context['item'][$token];
@ -441,7 +441,7 @@ class Set extends Object {
'item' => $item, 'item' => $item,
); );
} }
} else if ($key === $token && (!$conditions || Set::matches($conditions, $context['item'], $i+1))) { } elseif (($key === $token || (ctype_digit($token) && $key == $token) || $token === '.') && (!$conditions || Set::matches($conditions, $context['item'], $i))) {
$matches[] = array( $matches[] = array(
'trace' => am($context['trace'], $key), 'trace' => am($context['trace'], $key),
'key' => $key, 'key' => $key,
@ -457,7 +457,7 @@ class Set extends Object {
$r = array(); $r = array();
foreach ($matches as $match) { foreach ($matches as $match) {
if (!$options['flatten'] || is_array($match['item'])) { if ((!$options['flatten'] || is_array($match['item'])) && !is_int($match['key'])) {
$r[] = array($match['key'] => $match['item']); $r[] = array($match['key'] => $match['item']);
} else { } else {
$r[] = $match['item']; $r[] = $match['item'];

View file

@ -494,6 +494,18 @@ class SetTest extends UnitTestCase {
$expected = array(1, 2, 4, 5); $expected = array(1, 2, 4, 5);
$r = Set::extract($common, '/Comment[id!=3]/id'); $r = Set::extract($common, '/Comment[id!=3]/id');
$this->assertEqual($r, $expected); $this->assertEqual($r, $expected);
$expected = array($common[0]['Comment'][2]);
$r = Set::extract($common, '/Comment/2');
$this->assertEqual($r, $expected);
$expected = array($common[0]['Comment'][1]);
$r = Set::extract($common, '/Comment[1]/.[id=2]');
$this->assertEqual($r, $expected);
$expected = array($common[0]['Comment'][1]);
$r = Set::extract($common, '/Comment[1]/.[2]');
$this->assertEqual($r, $expected);
} }
/** /**
* undocumented function * undocumented function