From 86de512c39f7f2e68f7e1c7cf3367ab5b5ae0930 Mon Sep 17 00:00:00 2001 From: Constantin Schomburg Date: Sat, 2 May 2020 11:54:48 +0200 Subject: [PATCH] Fix QueryBuilder with numerically indexed arrays --- src/QueryBuilder.php | 3 ++- tests/TestCase/QueryBuilderTest.php | 31 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index a4c9ec56..8057fb93 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -681,7 +681,6 @@ public function parse($conditions) $result = []; foreach ($conditions as $k => $c) { $numericKey = is_numeric($k); - $operator = strtolower($k); if ($numericKey) { $c = $this->parse($c); @@ -692,6 +691,8 @@ public function parse($conditions) continue; } + $operator = strtolower($k); + if ($operator === 'and') { $result[] = $this->__call('and', $this->parse($c)); continue; diff --git a/tests/TestCase/QueryBuilderTest.php b/tests/TestCase/QueryBuilderTest.php index 1e12d71a..ff8b83b5 100644 --- a/tests/TestCase/QueryBuilderTest.php +++ b/tests/TestCase/QueryBuilderTest.php @@ -732,4 +732,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); + } }