Fix muliple attribute conditions.

This commit is contained in:
mark_story 2012-01-24 07:27:21 -05:00
parent 8ad4e66eba
commit cbfa938303
2 changed files with 45 additions and 22 deletions

View file

@ -706,11 +706,11 @@ class Set2Test extends CakeTestCase {
$data = self::articleData();
$result = Set2::extract($data, '{n}.Article[published]');
$expected = array($data['1']['Article']);
$expected = array($data[1]['Article']);
$this->assertEquals($expected, $result);
$result = Set2::extract($data, '{n}.Article[id][published]');
$expected = array($data['1']['Article']);
$expected = array($data[1]['Article']);
$this->assertEquals($expected, $result);
}
@ -766,4 +766,21 @@ class Set2Test extends CakeTestCase {
$this->assertEquals(2, $expected[0]['user_id']);
}
/**
* Test multiple attributes with conditions.
*
* @return void
*/
public function testExtractAttributeMultiple() {
$data = self::articleData();
$result = Set2::extract($data, '{n}.Comment.{n}[user_id > 2][id=1]');
$this->assertEmpty($result);
$result = Set2::extract($data, '{n}.Comment.{n}[user_id > 2][id=2]');
$expected = array($data[0]['Comment'][1]);
$this->assertEquals($expected, $result);
$this->assertEquals(4, $expected[0]['user_id']);
}
}

View file

@ -193,43 +193,49 @@ class Set2 {
*/
protected static function _matches(array $data, $selector) {
preg_match_all(
'/(\[ (?<attr>.+?) (?: \s* (?<op>(?:[><!]?[=]|[><])) \s* (?<val>.*) )? \])+/x',
'/(\[ (?<attr>[^=><!]+?) (\s* (?<op>[><!]?[=]|[><]) \s* (?<val>[^\]]+) )? \])/x',
$selector,
$conditions,
PREG_SET_ORDER
);
foreach ($conditions as $cond) {
$ok = true;
while ($ok) {
if (empty($conditions)) {
break;
}
$cond = array_shift($conditions);
$attr = $cond['attr'];
$op = isset($cond['op']) ? $cond['op'] : null;
$val = isset($cond['val']) ? $cond['val'] : null;
// Presence test.
if (empty($op) && empty($val)) {
return isset($data[$attr]);
if (empty($op) && empty($val) && !isset($data[$attr])) {
$ok = false;
}
// Empty attribute = fail.
if (!isset($data[$attr])) {
$ok = false;
}
$prop = isset($data[$attr]) ? $data[$attr] : null;
if (
($op === '=' && $prop != $val) ||
($op === '!=' && $prop == $val) ||
($op === '>' && $prop <= $val) ||
($op === '<' && $prop >= $val) ||
($op === '>=' && $prop < $val) ||
($op === '<=' && $prop > $val)
) {
$ok = false;
}
if (!$ok) {
return false;
}
$prop = $data[$attr];
if ($op === '=') {
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;
return true;
}
public static function insert(array $data, $path, $values = null) {