mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-02-12 06:56:24 +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();
|
$data = self::articleData();
|
||||||
|
|
||||||
$result = Set2::extract($data, '{n}.Article[published]');
|
$result = Set2::extract($data, '{n}.Article[published]');
|
||||||
$expected = array($data['1']['Article']);
|
$expected = array($data[1]['Article']);
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
|
|
||||||
$result = Set2::extract($data, '{n}.Article[id][published]');
|
$result = Set2::extract($data, '{n}.Article[id][published]');
|
||||||
$expected = array($data['1']['Article']);
|
$expected = array($data[1]['Article']);
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -766,4 +766,21 @@ class Set2Test extends CakeTestCase {
|
||||||
$this->assertEquals(2, $expected[0]['user_id']);
|
$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,44 +193,50 @@ class Set2 {
|
||||||
*/
|
*/
|
||||||
protected static function _matches(array $data, $selector) {
|
protected static function _matches(array $data, $selector) {
|
||||||
preg_match_all(
|
preg_match_all(
|
||||||
'/(\[ (?<attr>.+?) (?: \s* (?<op>(?:[><!]?[=]|[><])) \s* (?<val>.*) )? \])+/x',
|
'/(\[ (?<attr>[^=><!]+?) (\s* (?<op>[><!]?[=]|[><]) \s* (?<val>[^\]]+) )? \])/x',
|
||||||
$selector,
|
$selector,
|
||||||
$conditions,
|
$conditions,
|
||||||
PREG_SET_ORDER
|
PREG_SET_ORDER
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach ($conditions as $cond) {
|
$ok = true;
|
||||||
|
while ($ok) {
|
||||||
|
if (empty($conditions)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$cond = array_shift($conditions);
|
||||||
$attr = $cond['attr'];
|
$attr = $cond['attr'];
|
||||||
$op = isset($cond['op']) ? $cond['op'] : null;
|
$op = isset($cond['op']) ? $cond['op'] : null;
|
||||||
$val = isset($cond['val']) ? $cond['val'] : null;
|
$val = isset($cond['val']) ? $cond['val'] : null;
|
||||||
|
|
||||||
// Presence test.
|
// Presence test.
|
||||||
if (empty($op) && empty($val)) {
|
if (empty($op) && empty($val) && !isset($data[$attr])) {
|
||||||
return isset($data[$attr]);
|
$ok = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Empty attribute = fail.
|
// Empty attribute = fail.
|
||||||
if (!isset($data[$attr])) {
|
if (!isset($data[$attr])) {
|
||||||
return false;
|
$ok = false;
|
||||||
}
|
}
|
||||||
$prop = $data[$attr];
|
|
||||||
|
|
||||||
if ($op === '=') {
|
$prop = isset($data[$attr]) ? $data[$attr] : null;
|
||||||
return $prop == $val;
|
|
||||||
} elseif ($op === '!=') {
|
if (
|
||||||
return $prop != $val;
|
($op === '=' && $prop != $val) ||
|
||||||
} elseif ($op === '>') {
|
($op === '!=' && $prop == $val) ||
|
||||||
return $prop > $val;
|
($op === '>' && $prop <= $val) ||
|
||||||
} elseif ($op === '<') {
|
($op === '<' && $prop >= $val) ||
|
||||||
return $prop < $val;
|
($op === '>=' && $prop < $val) ||
|
||||||
} elseif ($op === '>=') {
|
($op === '<=' && $prop > $val)
|
||||||
return $prop >= $val;
|
) {
|
||||||
} elseif ($op === '<=') {
|
$ok = false;
|
||||||
return $prop <= $val;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if (!$ok) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public static function insert(array $data, $path, $values = null) {
|
public static function insert(array $data, $path, $values = null) {
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue