Add attribute pattern matching.

This commit is contained in:
mark_story 2012-01-24 22:27:01 -05:00
parent cbfa938303
commit 31181f58d6
2 changed files with 22 additions and 7 deletions

View file

@ -783,4 +783,17 @@ class Set2Test extends CakeTestCase {
$this->assertEquals(4, $expected[0]['user_id']); $this->assertEquals(4, $expected[0]['user_id']);
} }
/**
* Test attribute pattern matching.
*
* @return void
*/
public function testExtractAttributePattern() {
$data = self::articleData();
$result = Set2::extract($data, '{n}.Article[title=/^First/]');
$expected = array($data[0]['Article']);
$this->assertEquals($expected, $result);
}
} }

View file

@ -163,7 +163,6 @@ class Set2 {
} }
} }
} }
} }
// Filter for attributes. // Filter for attributes.
@ -211,17 +210,22 @@ class Set2 {
// Presence test. // Presence test.
if (empty($op) && empty($val) && !isset($data[$attr])) { if (empty($op) && empty($val) && !isset($data[$attr])) {
$ok = false; return false;
} }
// Empty attribute = fail. // Empty attribute = fail.
if (!isset($data[$attr])) { if (!isset($data[$attr])) {
$ok = false; return false;
} }
$prop = isset($data[$attr]) ? $data[$attr] : null; $prop = isset($data[$attr]) ? $data[$attr] : null;
if ( // Pattern matches and other operators.
if ($op === '=' && $val && $val[0] === '/') {
if (!preg_match($val, $prop)) {
return false;
}
} elseif (
($op === '=' && $prop != $val) || ($op === '=' && $prop != $val) ||
($op === '!=' && $prop == $val) || ($op === '!=' && $prop == $val) ||
($op === '>' && $prop <= $val) || ($op === '>' && $prop <= $val) ||
@ -229,11 +233,9 @@ class Set2 {
($op === '>=' && $prop < $val) || ($op === '>=' && $prop < $val) ||
($op === '<=' && $prop > $val) ($op === '<=' && $prop > $val)
) { ) {
$ok = false;
}
if (!$ok) {
return false; return false;
} }
} }
return true; return true;
} }