Permalink
Browse files

Add attribute pattern matching.

  • Loading branch information...
1 parent cbfa938 commit 31181f58d6b9b60225cd3a225d8d40d1265fb70a @markstory markstory committed Jan 25, 2012
Showing with 22 additions and 7 deletions.
  1. +13 −0 lib/Cake/Test/Case/Utility/Set2Test.php
  2. +9 −7 lib/Cake/Utility/Set2.php
@@ -783,4 +783,17 @@ public function testExtractAttributeMultiple() {
$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
@@ -163,7 +163,6 @@ protected static function _traverse(array &$data, $path, $callback) {
}
}
}
-
}
// Filter for attributes.
@@ -211,29 +210,32 @@ protected static function _matches(array $data, $selector) {
// Presence test.
if (empty($op) && empty($val) && !isset($data[$attr])) {
- $ok = false;
+ return false;
}
// Empty attribute = fail.
if (!isset($data[$attr])) {
- $ok = false;
+ return false;
}
$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)
) {
- $ok = false;
- }
- if (!$ok) {
return false;
}
+
}
return true;
}

0 comments on commit 31181f5

Please sign in to comment.