|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Micayael\NativeQueryFromFileBuilderBundle\Event; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Contracts\EventDispatcher\Event; |
|
6
|
|
|
|
|
7
|
|
|
class ProcessQueryParamsEvent extends Event |
|
8
|
|
|
{ |
|
9
|
|
|
public const NAME = 'native_query_from_file_builder.events.process_query_params'; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $snippetKey; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
private $originalParams; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $paramKey; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $filterType; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $originalFilter; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
private $processedParams = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
private $processedFilter; |
|
45
|
|
|
|
|
46
|
|
|
public function __construct(string $snippetKey, ?string $filterType, string $paramKey, array $originalParams, string $originalFilter) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->snippetKey = $snippetKey; |
|
49
|
|
|
$this->filterType = $filterType; |
|
50
|
|
|
|
|
51
|
|
|
$this->paramKey = $paramKey; |
|
52
|
|
|
|
|
53
|
|
|
$this->originalParams = $originalParams; |
|
54
|
|
|
$this->originalFilter = $originalFilter; |
|
55
|
|
|
|
|
56
|
|
|
$this->processedParams = $originalParams; |
|
57
|
|
|
$this->processedFilter = $originalFilter; |
|
58
|
|
|
|
|
59
|
|
|
switch ($filterType) { |
|
60
|
|
|
case 'like': |
|
61
|
|
|
$newValue = '%'.$this->getOriginalParamValue().'%'; |
|
62
|
|
|
$this->replaceParamValue($newValue); |
|
63
|
|
|
|
|
64
|
|
|
break; |
|
65
|
|
|
case 'string_any': |
|
66
|
|
|
$newValue = '{'.implode(', ', array_map('strval', $this->getOriginalParamValue())).'}'; |
|
67
|
|
|
$this->replaceParamValue($newValue); |
|
68
|
|
|
|
|
69
|
|
|
break; |
|
70
|
|
|
case 'numeric_any': |
|
71
|
|
|
$newValue = '{'.implode(', ', array_map('intval', $this->getOriginalParamValue())).'}::integer'; |
|
72
|
|
|
$this->replaceParamValue($newValue); |
|
73
|
|
|
|
|
74
|
|
|
break; |
|
75
|
|
|
case 'in': |
|
76
|
|
|
$newFilter = ''; |
|
77
|
|
|
$newParams = []; |
|
78
|
|
|
|
|
79
|
|
|
$i = 0; |
|
80
|
|
|
|
|
81
|
|
|
foreach ($this->getOriginalParamValue() as $value) { |
|
82
|
|
|
$newFilter .= ':'.$paramKey.'_'.$i.','; |
|
83
|
|
|
$newParams[$paramKey.'_'.$i] = $value; |
|
84
|
|
|
|
|
85
|
|
|
++$i; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (strlen($newFilter) > 0) { |
|
89
|
|
|
$newFilter = substr($newFilter, 0, strlen($newFilter) - 1); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$this->removeParams([$this->getParamKey()]); |
|
93
|
|
|
|
|
94
|
|
|
$processedParams = array_merge($this->processedParams, $newParams); |
|
95
|
|
|
$processedFilter = str_replace(':'.$paramKey, $newFilter, $originalFilter); |
|
96
|
|
|
|
|
97
|
|
|
$this->setProcessedParams($processedParams); |
|
98
|
|
|
$this->setProcessedFilter($processedFilter); |
|
99
|
|
|
|
|
100
|
|
|
break; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Return the value of the current param to be processed. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getOriginalParamValue() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->originalParams[$this->paramKey]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Returns params after they have been processed. |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getProcessedParams(): array |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->processedParams; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Set processed params. |
|
122
|
|
|
*/ |
|
123
|
|
|
public function setProcessedParams(array $params): void |
|
124
|
|
|
{ |
|
125
|
|
|
$this->processedParams = $params; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Return the filter of the current param after it has been processed. |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getProcessedFilter(): string |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->processedFilter; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Replaces the value of the current filter. |
|
138
|
|
|
*/ |
|
139
|
|
|
public function setProcessedFilter(string $filter): void |
|
140
|
|
|
{ |
|
141
|
|
|
$this->processedFilter = $filter; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Remove a param for the list of processed params. |
|
146
|
|
|
*/ |
|
147
|
|
|
public function removeParams(array $paramKeys): void |
|
148
|
|
|
{ |
|
149
|
|
|
foreach ($paramKeys as $paramKey) { |
|
150
|
|
|
unset($this->processedParams[$paramKey]); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Replaces the value of the current param. |
|
156
|
|
|
* |
|
157
|
|
|
* @param $newValue |
|
158
|
|
|
*/ |
|
159
|
|
|
public function replaceParamValue($newValue): void |
|
160
|
|
|
{ |
|
161
|
|
|
$this->addOrReplaceParamValue($this->paramKey, $newValue); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Adds or replaces the value of a para defined by $paramKey. |
|
166
|
|
|
* |
|
167
|
|
|
* @param $newValue |
|
168
|
|
|
*/ |
|
169
|
|
|
public function addOrReplaceParamValue(string $paramKey, $newValue): void |
|
170
|
|
|
{ |
|
171
|
|
|
$this->processedParams[$paramKey] = $newValue; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Return the type of the param to be processed. |
|
176
|
|
|
*/ |
|
177
|
|
|
public function getFilterType(): ?string |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->filterType; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Return the key of the param to be processed. |
|
184
|
|
|
*/ |
|
185
|
|
|
public function getParamKey(): string |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->paramKey; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Return the key of the snnipet query key: products.base. |
|
192
|
|
|
*/ |
|
193
|
|
|
public function getSnippetKey() |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->snippetKey; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Return the original params and values passed to the bundle. |
|
200
|
|
|
*/ |
|
201
|
|
|
public function getOriginalParams(): array |
|
202
|
|
|
{ |
|
203
|
|
|
return $this->originalParams; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
public function getOriginalFilter(): string |
|
207
|
|
|
{ |
|
208
|
|
|
return $this->originalFilter; |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|