1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bugloos\QueryFilterBundle\Traits; |
4
|
|
|
|
5
|
|
|
use Bugloos\QueryFilterBundle\Enum\ColumnType; |
6
|
|
|
use Bugloos\QueryFilterBundle\Enum\StrategyType; |
7
|
|
|
|
8
|
|
|
trait QueryFilterTrait |
9
|
|
|
{ |
10
|
|
|
public function strategies(array $strategies): self |
11
|
|
|
{ |
12
|
|
|
if (empty($strategies)) { |
13
|
|
|
return $this; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
foreach ($strategies as $parameter => $strategy) { |
17
|
|
|
$this->addStrategy($parameter, $strategy); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
return $this; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function addStrategy(string $parameter, string $strategy): self |
24
|
|
|
{ |
25
|
|
|
if (empty($parameter) || empty($strategy)) { |
26
|
|
|
return $this; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if (!\in_array($strategy, StrategyType::all(), true)) { |
30
|
|
|
StrategyType::createInvalidArgumentException($strategy); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->strategies[$parameter] = $strategy; |
|
|
|
|
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function types(array $types): self |
39
|
|
|
{ |
40
|
|
|
if (empty($types)) { |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach ($types as $parameter => $type) { |
45
|
|
|
$this->addType($parameter, $type); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function addType(string $parameter, string $type): self |
52
|
|
|
{ |
53
|
|
|
if (empty($parameter) || empty($type)) { |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (!\in_array($type, ColumnType::all(), true)) { |
58
|
|
|
StrategyType::createInvalidArgumentException($type); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->types[$parameter] = $type; |
|
|
|
|
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function mappers(array $mappers): self |
67
|
|
|
{ |
68
|
|
|
if (empty($mappers)) { |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
foreach ($mappers as $parameter => $mapper) { |
73
|
|
|
$this->addMapper($parameter, $mapper); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function addMapper(string $parameter, string $mapper): self |
80
|
|
|
{ |
81
|
|
|
if (empty($parameter) || empty($mapper)) { |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->mapper[$parameter] = $mapper; |
|
|
|
|
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function constants(array $constants): self |
91
|
|
|
{ |
92
|
|
|
if (empty($constants)) { |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
foreach ($constants as $parameter => $condition) { |
97
|
|
|
$this->addConstant($parameter, $condition); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function addConstant(string $parameter, string $condition): self |
104
|
|
|
{ |
105
|
|
|
if (empty($parameter) || empty($condition)) { |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->constants[$parameter] = $condition; |
|
|
|
|
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function cacheTime(int $cacheTime): self |
115
|
|
|
{ |
116
|
|
|
$this->cacheTime = $cacheTime; |
|
|
|
|
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function withOr(bool $withOr): self |
122
|
|
|
{ |
123
|
|
|
$this->withOr = $withOr; |
|
|
|
|
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|