Skip to content

Commit

Permalink
Added some more operators.
Browse files Browse the repository at this point in the history
Attribute + {n} is still not working.
  • Loading branch information
markstory committed Mar 27, 2012
1 parent 6c87be9 commit 9269a6d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/Cake/Test/Case/Utility/Set2Test.php
Expand Up @@ -737,4 +737,16 @@ public function testExtractAttributeEquality() {
$this->assertEquals(5, $result[3]['id']);
}

/**
* Test comparison operators.
*
* @return void
*/
public function testExtractAttributeComparison() {
$data = self::articleData();

$result = Set2::extract($data, '{n}.Comment.{n}.[user_id > 2]');
$this->assertEquals($data[0]['Comment'][1], $result);
}

}
9 changes: 9 additions & 0 deletions lib/Cake/Utility/Set2.php
Expand Up @@ -158,6 +158,7 @@ protected static function _traverse(array &$data, $path, $callback) {
} else {
// attributes
foreach ($item as $k => $v) {
debug(array($k => $v));
if (self::_matches(array($k => $v), $token)) {
$next[] = $v;
}
Expand Down Expand Up @@ -211,6 +212,14 @@ protected static function _matches(array $data, $selector) {
return $prop == $val;
} elseif ($op === '!=') {
return $prop != $val;
} elseif ($op === '>') {
return $prop > $val;
} elseif ($op === '<') {
return $prop < $val;
} elseif ($op === '>=') {
return $prop >= $val;
} elseif ($op === '<=') {
return $prop <= $val;
}
}
return false;
Expand Down

0 comments on commit 9269a6d

Please sign in to comment.