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
case 'NOT':
case 'AND':
case 'OR':
$subCondition = $this->filterCondition($condition[1]);
for ($i = 1, $operandsCount = count($condition); $i < $operandsCount; $i++) {
$subCondition = $this->filterCondition($condition[$i]);
if ($this->parameterNotEmpty($subCondition)) {
$condition[1] = $subCondition;
$condition[$i] = $subCondition;
} else {
unset($condition[$i]);
}
}
$operandsCount = count($condition) - 1;
if ($operator === 'NOT' && $operandsCount === 0) {
$condition = [];
} else {
// reindex array
array_splice($condition, 0, 0);
if ($operandsCount === 1) {
$condition = $condition[1];
}
}
break;
case 'IN':
......
......@@ -909,11 +909,24 @@ class Query extends Component implements QueryInterface
case 'NOT':
case 'AND':
case 'OR':
$subCondition = $this->filterCondition($condition[1]);
for ($i = 1, $operandsCount = count($condition); $i < $operandsCount; $i++) {
$subCondition = $this->filterCondition($condition[$i]);
if ($this->parameterNotEmpty($subCondition)) {
$condition[1] = $subCondition;
$condition[$i] = $subCondition;
} else {
unset($condition[$i]);
}
}
$operandsCount = count($condition) - 1;
if ($operator === 'NOT' && $operandsCount === 0) {
$condition = [];
} else {
// reindex array
array_splice($condition, 0, 0);
if ($operandsCount === 1) {
$condition = $condition[1];
}
}
break;
case 'IN':
......
......@@ -120,11 +120,7 @@ class QueryTest extends SphinxTestCase
public function testFilterRecursively()
{
$query = new Query();
$query->filter(['not', ['like', 'name', '']]);
$this->assertEquals(null, $query->where);
$query->where(['id' => 1]);
$query->filter(['and', ['like', 'name', '']]);
$query->filter(['and', ['like', 'name', ''], ['like', 'title', ''], ['id' => 1], ['not', ['like', 'name', '']]]);
$this->assertEquals(['id' => 1], $query->where);
}
......
......@@ -109,11 +109,7 @@ class QueryTest extends DatabaseTestCase
public function testFilterRecursively()
{
$query = new Query();
$query->filter(['not', ['like', 'name', '']]);
$this->assertEquals(null, $query->where);
$query->where(['id' => 1]);
$query->filter(['and', ['like', 'name', '']]);
$query->filter(['and', ['like', 'name', ''], ['like', 'title', ''], ['id' => 1], ['not', ['like', 'name', '']]]);
$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