Skip to content

Commit 3e0fd78

Browse files
committed
- DBExprFilter is now able to deal with array-values
1 parent 9b078b3 commit 3e0fd78

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/Builder/Expr/DBExprFilter.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public function __construct($expression, array $data, $keyPath, $validator = nul
3030
$this->value = $this->recursiveGet($data, $this->keyPath, null);
3131
if($validator === null) {
3232
$validator = function ($data) {
33+
if(is_array($data)) {
34+
return $this->isValidArray($data);
35+
}
3336
return (string) $data !== '';
3437
};
3538
}
@@ -81,6 +84,20 @@ private function buildKey($keyPath) {
8184
return $keyPath;
8285
}
8386

87+
/**
88+
* @param array $array
89+
* @return bool
90+
*/
91+
private function isValidArray(array $array) {
92+
$data = array_filter($array, function ($value) {
93+
if(is_array($value)) {
94+
return $this->isValidArray($value);
95+
}
96+
return (string) $value !== '';
97+
});
98+
return count($data) > 0;
99+
}
100+
84101
/**
85102
* @param array $array
86103
* @param array $path

tests/Builder/SelectTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public function testDistinct() {
244244
}
245245

246246
public function testOptionalExpressions() {
247-
$filter = ['filter' => ['name' => 'aaa']];
247+
$filter = ['filter' => ['name' => 'aaa', 'ids' => [1, 2, 3], 'empty' => [null, '', ['']]]];
248248
$opt = new OptionalDBFilterMap($filter);
249249

250250
$query = TestSelect::create()
@@ -273,6 +273,24 @@ public function testOptionalExpressions() {
273273
->asString();
274274

275275
$this->assertEquals("SELECT DISTINCT\n\tt.field\nFROM\n\ttest t\n", $query);
276+
277+
$query = TestSelect::create()
278+
->distinct()
279+
->field('t.field')
280+
->from('t', 'test')
281+
->where($opt('t.field IN (?)', ['filter', 'ids']))
282+
->asString();
283+
284+
$this->assertEquals("SELECT DISTINCT\n\tt.field\nFROM\n\ttest t\nWHERE\n\t(t.field IN ('1', '2', '3'))\n", $query);
285+
286+
$query = TestSelect::create()
287+
->distinct()
288+
->field('t.field')
289+
->from('t', 'test')
290+
->where($opt('t.field=?', ['filter', 'empty']))
291+
->asString();
292+
293+
$this->assertEquals("SELECT DISTINCT\n\tt.field\nFROM\n\ttest t\n", $query);
276294
}
277295

278296
public function testRequired() {

0 commit comments

Comments
 (0)