Skip to content

Commit

Permalink
Merge pull request #223 from lyke/fix-numeric
Browse files Browse the repository at this point in the history
Fix QueryBuilder with numerically indexed arrays
  • Loading branch information
markstory authored May 11, 2020
2 parents 4dbc859 + 86de512 commit f8cfc54
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,6 @@ public function parse($conditions)
$result = [];
foreach ($conditions as $k => $c) {
$numericKey = is_numeric($k);
$operator = strtolower($k);

if ($numericKey) {
$c = $this->parse($c);
Expand All @@ -717,6 +716,8 @@ public function parse($conditions)
continue;
}

$operator = strtolower($k);

if ($operator === 'and') {
$result[] = $this->and(...$this->parse($c));
continue;
Expand Down
31 changes: 31 additions & 0 deletions tests/TestCase/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -731,4 +731,35 @@ public function testParseNot()
];
$this->assertEquals($expected, $filter);
}

/**
* Tests the parse() method with numerically indexed arrays
*
* @return void
*/
public function testParseNumericArray()
{
$builder = new QueryBuilder();
$filter = $builder->parse([
$builder->simpleQueryString('name', 'mark'),
['age >' => 29],
'not' => [
['name' => 'jose'],
['age >' => 35],
],
]);
$expected = [
$builder->simpleQueryString('name', 'mark'),
$builder->and(
$builder->gt('age', 29)
),
$builder->not(
$builder->and(
$builder->and($builder->term('name', 'jose')),
$builder->and($builder->gt('age', 35))
)
),
];
$this->assertEquals($expected, $filter);
}
}

0 comments on commit f8cfc54

Please sign in to comment.