1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Queries; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Mado\QueryBundle\Dictionary; |
7
|
|
|
|
8
|
|
|
class Filters implements FiltersInterface |
9
|
|
|
{ |
10
|
|
|
private $discriminatorCounter = 1; |
11
|
|
|
|
12
|
|
|
private $route = ''; |
13
|
|
|
private $routeParams = ''; |
14
|
|
|
private $id = ''; |
15
|
|
|
|
16
|
|
|
private $andFilters = []; |
17
|
|
|
private $orFilters = []; |
18
|
|
|
|
19
|
|
|
private $sorting = []; |
20
|
|
|
|
21
|
|
|
private $printing = []; |
22
|
|
|
private $rel = ''; |
23
|
|
|
private $page = ''; |
24
|
|
|
private $select = ''; |
25
|
|
|
private $limit = ''; |
26
|
|
|
|
27
|
|
|
protected function __construct() |
28
|
|
|
{ |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function addAndFilter(string $field, string $operator, string $value, bool $subscribeIfExist = false) |
32
|
|
|
{ |
33
|
|
|
Dictionary::isValidOperator($operator); |
34
|
|
|
|
35
|
|
|
$key = $field . '|' . $operator; |
36
|
|
|
while ($subscribeIfExist === false && isset($this->andFilters[$key])) { |
37
|
|
|
$key = $field . '|' . $operator . '|' . $this->getDiscriminator(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->andFilters[$key] = $value; |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function addOrFilter(string $field, string $operator, string $value, bool $subscribeIfExist = false) |
46
|
|
|
{ |
47
|
|
|
Dictionary::isValidOperator($operator); |
48
|
|
|
|
49
|
|
|
$key = $field . '|' . $operator; |
50
|
|
|
while ($subscribeIfExist === false && isset($this->orFilters[$key])) { |
51
|
|
|
$key = $field . '|' . $operator . '|' . $this->getDiscriminator(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->orFilters[$key] = $value; |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function getDiscriminator() |
60
|
|
|
{ |
61
|
|
|
return $this->discriminatorCounter++; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function setId($id) |
65
|
|
|
{ |
66
|
|
|
$this->id = $id; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function setRoute(string $route) |
70
|
|
|
{ |
71
|
|
|
$this->route = $route; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function setRouteParams(array $routeParams) |
75
|
|
|
{ |
76
|
|
|
$this->routeParams = $routeParams; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function setAndFilters(array $andFilters) |
80
|
|
|
{ |
81
|
|
|
$this->andFilters = $andFilters; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function setOrFilters(array $orFilters) |
85
|
|
|
{ |
86
|
|
|
$this->orFilters = $orFilters; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function setSorting(array $sorting) |
90
|
|
|
{ |
91
|
|
|
$this->sorting = $sorting; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function setPrinting(array $printing) |
95
|
|
|
{ |
96
|
|
|
$this->printing = $printing; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function setRel(string $rel) |
100
|
|
|
{ |
101
|
|
|
$this->rel = $rel; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function setPage(string $page) |
105
|
|
|
{ |
106
|
|
|
$this->page = $page; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function setSelect(string $select) |
110
|
|
|
{ |
111
|
|
|
$this->select = $select; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function setLimit(string $limit) |
115
|
|
|
{ |
116
|
|
|
$this->limit = $limit; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getQueryBuilderOptions(): QueryBuilderOptions |
120
|
|
|
{ |
121
|
|
|
return QueryBuilderOptions::fromArray([ |
122
|
|
|
'_route' => $this->route, |
123
|
|
|
'_route_params' => $this->routeParams, |
124
|
|
|
'id' => $this->id, |
125
|
|
|
'page' => $this->page, |
126
|
|
|
'limit' => $this->limit, |
127
|
|
|
'filters' => $this->andFilters, |
128
|
|
|
'orFilters' => $this->orFilters, |
129
|
|
|
'sorting' => $this->sorting, |
130
|
|
|
'rel' => $this->rel, |
131
|
|
|
'select' => $this->select, |
132
|
|
|
'printing' => $this->printing, |
133
|
|
|
]); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|