mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Fix muliple attribute conditions.
This commit is contained in:
parent
8ad4e66eba
commit
cbfa938303
2 changed files with 45 additions and 22 deletions
|
@ -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']);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Add table
Reference in a new issue