Commit 06fdb797 by Alexander Makarov

Added support for arbitrary number of parameters for NOT, AND, OR in filter methods of Query

parent 94fd4e7e
...@@ -822,11 +822,24 @@ class Query extends Component implements QueryInterface ...@@ -822,11 +822,24 @@ class Query extends Component implements QueryInterface
case 'NOT': case 'NOT':
case 'AND': case 'AND':
case 'OR': case 'OR':
$subCondition = $this->filterCondition($condition[1]); for ($i = 1, $operandsCount = count($condition); $i < $operandsCount; $i++) {
if ($this->parameterNotEmpty($subCondition)) { $subCondition = $this->filterCondition($condition[$i]);
$condition[1] = $subCondition; if ($this->parameterNotEmpty($subCondition)) {
} else { $condition[$i] = $subCondition;
} else {
unset($condition[$i]);
}
}
$operandsCount = count($condition) - 1;
if ($operator === 'NOT' && $operandsCount === 0) {
$condition = []; $condition = [];
} else {
// reindex array
array_splice($condition, 0, 0);
if ($operandsCount === 1) {
$condition = $condition[1];
}
} }
break; break;
case 'IN': case 'IN':
......
...@@ -909,11 +909,24 @@ class Query extends Component implements QueryInterface ...@@ -909,11 +909,24 @@ class Query extends Component implements QueryInterface
case 'NOT': case 'NOT':
case 'AND': case 'AND':
case 'OR': case 'OR':
$subCondition = $this->filterCondition($condition[1]); for ($i = 1, $operandsCount = count($condition); $i < $operandsCount; $i++) {
if ($this->parameterNotEmpty($subCondition)) { $subCondition = $this->filterCondition($condition[$i]);
$condition[1] = $subCondition; if ($this->parameterNotEmpty($subCondition)) {
} else { $condition[$i] = $subCondition;
} else {
unset($condition[$i]);
}
}
$operandsCount = count($condition) - 1;
if ($operator === 'NOT' && $operandsCount === 0) {
$condition = []; $condition = [];
} else {
// reindex array
array_splice($condition, 0, 0);
if ($operandsCount === 1) {
$condition = $condition[1];
}
} }
break; break;
case 'IN': case 'IN':
......
...@@ -120,11 +120,7 @@ class QueryTest extends SphinxTestCase ...@@ -120,11 +120,7 @@ class QueryTest extends SphinxTestCase
public function testFilterRecursively() public function testFilterRecursively()
{ {
$query = new Query(); $query = new Query();
$query->filter(['not', ['like', 'name', '']]); $query->filter(['and', ['like', 'name', ''], ['like', 'title', ''], ['id' => 1], ['not', ['like', 'name', '']]]);
$this->assertEquals(null, $query->where);
$query->where(['id' => 1]);
$query->filter(['and', ['like', 'name', '']]);
$this->assertEquals(['id' => 1], $query->where); $this->assertEquals(['id' => 1], $query->where);
} }
......
...@@ -109,11 +109,7 @@ class QueryTest extends DatabaseTestCase ...@@ -109,11 +109,7 @@ class QueryTest extends DatabaseTestCase
public function testFilterRecursively() public function testFilterRecursively()
{ {
$query = new Query(); $query = new Query();
$query->filter(['not', ['like', 'name', '']]); $query->filter(['and', ['like', 'name', ''], ['like', 'title', ''], ['id' => 1], ['not', ['like', 'name', '']]]);
$this->assertEquals(null, $query->where);
$query->where(['id' => 1]);
$query->filter(['and', ['like', 'name', '']]);
$this->assertEquals(['id' => 1], $query->where); $this->assertEquals(['id' => 1], $query->where);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment