diff --git a/lib/Cake/Test/Case/Utility/Set2Test.php b/lib/Cake/Test/Case/Utility/Set2Test.php index 505cf588b..264815cc2 100644 --- a/lib/Cake/Test/Case/Utility/Set2Test.php +++ b/lib/Cake/Test/Case/Utility/Set2Test.php @@ -783,4 +783,17 @@ class Set2Test extends CakeTestCase { $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); + } + } diff --git a/lib/Cake/Utility/Set2.php b/lib/Cake/Utility/Set2.php index 57230f3e4..85f0b02ae 100644 --- a/lib/Cake/Utility/Set2.php +++ b/lib/Cake/Utility/Set2.php @@ -163,7 +163,6 @@ class Set2 { } } } - } // Filter for attributes. @@ -211,17 +210,22 @@ class Set2 { // 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) || @@ -229,11 +233,9 @@ class Set2 { ($op === '>=' && $prop < $val) || ($op === '<=' && $prop > $val) ) { - $ok = false; - } - if (!$ok) { return false; } + } return true; }