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