|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Atlance\HttpDbalFilter\Query; |
|
6
|
|
|
|
|
7
|
|
|
use Atlance\HttpDbalFilter\Dto\AbstractCommand; |
|
8
|
|
|
use Atlance\HttpDbalFilter\Utils\JsonNormalizer; |
|
9
|
|
|
use Webmozart\Assert\Assert; |
|
10
|
|
|
|
|
11
|
|
|
final class Configuration extends AbstractCommand |
|
12
|
|
|
{ |
|
13
|
|
|
public int $page; |
|
14
|
|
|
|
|
15
|
|
|
public int $limit; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array<string,array<string,array<int,string|int|float|\Stringable>>> |
|
19
|
|
|
*/ |
|
20
|
|
|
public array $filter = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array<string,string> |
|
24
|
|
|
*/ |
|
25
|
|
|
public array $order = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @psalm-suppress MixedArrayAssignment |
|
29
|
|
|
* @psalm-suppress MixedPropertyTypeCoercion |
|
30
|
|
|
* |
|
31
|
|
|
* @param non-empty-array<string,array<string,string|int|float>> $conditions |
|
|
|
|
|
|
32
|
|
|
* |
|
33
|
|
|
* @return Configuration |
|
34
|
|
|
* |
|
35
|
|
|
* @throws \JsonException |
|
36
|
|
|
*/ |
|
37
|
73 |
|
public function setFilter(array $conditions): self |
|
38
|
|
|
{ |
|
39
|
73 |
|
foreach ($conditions as $expr => $aliases) { |
|
40
|
73 |
|
Assert::oneOf($expr, Builder::SUPPORTED_EXPRESSIONS); |
|
41
|
|
|
|
|
42
|
72 |
|
foreach ($aliases as $alias => $values) { |
|
43
|
72 |
|
if (!\array_key_exists($expr, $this->filter)) { |
|
44
|
72 |
|
$this->filter[$expr] = []; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
72 |
|
if (\is_string($values) && (bool) preg_match('#\|#', $values)) { |
|
48
|
29 |
|
$values = JsonNormalizer::normalize(explode('|', $values)); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
72 |
|
$this->filter[$expr][$alias] = \is_array($values) ? $values : [$values]; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
72 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
public function setOrder(array $conditions): self |
|
59
|
|
|
{ |
|
60
|
|
|
/** |
|
61
|
|
|
* @var string $alias |
|
62
|
|
|
* @var string $direction |
|
63
|
|
|
*/ |
|
64
|
2 |
|
foreach ($conditions as $alias => $direction) { |
|
65
|
2 |
|
Assert::oneOf($direction, ['asc', 'desc']); |
|
66
|
1 |
|
$this->order[$alias] = $direction; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|