1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Paymaxi\Component\Query\Handler; |
5
|
|
|
|
6
|
|
|
use Paymaxi\Component\Query\Filter\FilterInterface; |
7
|
|
|
use Paymaxi\Component\Query\Sort\SortInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AbstractHandler |
11
|
|
|
* |
12
|
|
|
* @package Paymaxi\Component\Query\Handler |
13
|
|
|
*/ |
14
|
|
|
abstract class AbstractHandler implements SortHandlerInterface, FilterHandlerInterface |
15
|
|
|
{ |
16
|
|
|
/** @var SortInterface[] */ |
17
|
|
|
protected $sortingFields = []; |
18
|
|
|
|
19
|
|
|
/** @var FilterInterface[] */ |
20
|
|
|
protected $filteringFields = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param SortInterface $sort |
24
|
|
|
*/ |
25
|
|
|
public function addSorting(SortInterface $sort):void |
26
|
|
|
{ |
27
|
|
|
$this->sortingFields[] = $sort; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param FilterInterface $filter |
32
|
|
|
*/ |
33
|
4 |
|
public function addFilter(FilterInterface $filter): void |
34
|
|
|
{ |
35
|
4 |
|
$this->filteringFields[] = $filter; |
36
|
4 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param SortInterface $sort |
40
|
|
|
* @param string $order |
41
|
|
|
*/ |
42
|
|
|
abstract protected function handleSorting(SortInterface $sort, string $order): void; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param FilterInterface $filter |
46
|
|
|
* @param mixed $value |
47
|
|
|
*/ |
48
|
|
|
abstract protected function handleFiltering(FilterInterface $filter, $value): void; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $field |
52
|
|
|
* @param string $order |
53
|
|
|
*/ |
54
|
|
|
public function sort(string $field, string $order): void |
55
|
|
|
{ |
56
|
|
|
/** @var SortInterface $sort */ |
57
|
|
|
foreach ($this->sortingFields as $sort) { |
58
|
|
|
if ($sort->supports($field)) { |
59
|
|
|
$this->handleSorting($sort, $order); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $field |
66
|
|
|
* @param mixed $value |
67
|
|
|
*/ |
68
|
3 |
|
public function filter(string $field, $value): void |
69
|
|
|
{ |
70
|
|
|
/** @var FilterInterface $filteringField */ |
71
|
3 |
|
foreach ($this->filteringFields as $filteringField) { |
72
|
3 |
|
if ($filteringField->supports($field)) { |
73
|
3 |
|
$this->handleFiltering($filteringField, $value); |
74
|
|
|
} |
75
|
|
|
} |
76
|
3 |
|
} |
77
|
|
|
} |
78
|
|
|
|