1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Atlance\HttpDbalFilter; |
6
|
|
|
|
7
|
|
|
use Atlance\HttpDbalFilter\Query\Expression\Contract\ConditionFactoryInterface; |
8
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
9
|
|
|
|
10
|
|
|
final class Filter |
11
|
|
|
{ |
12
|
75 |
|
public function __construct(private readonly ConditionFactoryInterface $factory) |
13
|
|
|
{ |
14
|
75 |
|
} |
15
|
|
|
|
16
|
73 |
|
public function apply(QueryBuilder $qb, Query\Configuration $configuration): void |
17
|
|
|
{ |
18
|
73 |
|
$this->select($qb, $configuration->filter)->order($qb, $configuration->order); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param array<string,array<string,array<int,string|int|float|\Stringable>>> $conditions |
23
|
|
|
*/ |
24
|
73 |
|
private function select(QueryBuilder $qb, array $conditions): self |
25
|
|
|
{ |
26
|
73 |
|
foreach ($conditions as $expr => $aliases) { |
27
|
72 |
|
foreach ($aliases as $alias => $values) { |
28
|
72 |
|
$this->andWhere($qb, $this->factory->create($qb, $alias, $expr), $values); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
64 |
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param array<string,string> $conditions |
37
|
|
|
*/ |
38
|
64 |
|
private function order(QueryBuilder $qb, array $conditions): void |
39
|
|
|
{ |
40
|
64 |
|
foreach ($conditions as $alias => $value) { |
41
|
1 |
|
$this->andWhere($qb, $this->factory->create($qb, $alias, 'order_by'), [$value]); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array<int,string|int|float|\Stringable> $values |
47
|
|
|
*/ |
48
|
71 |
|
private function andWhere(QueryBuilder $qb, Query\Expression\Condition $condition, array $values): void |
49
|
|
|
{ |
50
|
71 |
|
(new Query\Builder($qb))->andWhere($condition->setValues($values)); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|