1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Micayael\NativeQueryFromFileBuilderBundle\Event; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\EventDispatcher\Event; |
6
|
|
|
|
7
|
|
|
class ProcessQueryParamsEvent extends Event |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
private $snippetKey; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $originalParams; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $paramKey; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $filterType; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $originalFilter; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $processedParams = []; |
38
|
|
|
|
39
|
|
|
private $processedFilter = ''; |
40
|
|
|
|
41
|
|
|
public function __construct(string $snippetKey, ?string $filterType = null, string $paramKey, array $originalParams, string $originalFilter) |
42
|
|
|
{ |
43
|
|
|
$this->snippetKey = $snippetKey; |
44
|
|
|
$this->filterType = $filterType; |
45
|
|
|
|
46
|
|
|
$this->paramKey = $paramKey; |
47
|
|
|
|
48
|
|
|
$this->originalParams = $originalParams; |
49
|
|
|
$this->originalFilter = $originalFilter; |
50
|
|
|
|
51
|
|
|
$this->processedParams = $originalParams; |
|
|
|
|
52
|
|
|
$this->processedFilter = $originalFilter; |
53
|
|
|
|
54
|
|
|
switch ($filterType) { |
55
|
|
|
case 'like': |
|
|
|
|
56
|
|
|
$newValue = '%'.$this->getOriginalParamValue().'%'; |
57
|
|
|
$this->replaceParamValue($newValue); |
58
|
|
|
break; |
59
|
|
|
case 'string_any': |
60
|
|
|
$newValue = '{'.implode(', ', array_map('strval', $this->getOriginalParamValue())).'}'; |
61
|
|
|
$this->replaceParamValue($newValue); |
62
|
|
|
break; |
63
|
|
|
case 'numeric_any': |
64
|
|
|
$newValue = '{'.implode(', ', array_map('intval', $this->getOriginalParamValue())).'}'; |
65
|
|
|
$this->replaceParamValue($newValue); |
66
|
|
|
break; |
67
|
|
|
case 'in': |
|
|
|
|
68
|
|
|
|
69
|
|
|
$newFilter = ''; |
70
|
|
|
$newParams = []; |
71
|
|
|
|
72
|
|
|
$i = 0; |
73
|
|
|
|
74
|
|
|
foreach ($this->getOriginalParamValue() as $value) { |
75
|
|
|
$newFilter .= ':'.$paramKey.'_'.$i.','; |
76
|
|
|
$newParams[$paramKey.'_'.$i] = $value; |
77
|
|
|
|
78
|
|
|
++$i; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (strlen($newFilter) > 0) { |
82
|
|
|
$newFilter = substr($newFilter, 0, strlen($newFilter) - 1); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->removeParams([$this->getParamKey()]); |
86
|
|
|
|
87
|
|
|
$processedParams = array_merge($this->processedParams, $newParams); |
88
|
|
|
$processedFilter = str_replace(':'.$paramKey, $newFilter, $originalFilter); |
89
|
|
|
|
90
|
|
|
$this->setProcessedParams($processedParams); |
91
|
|
|
$this->setProcessedFilter($processedFilter); |
92
|
|
|
|
93
|
|
|
break; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return the value of the current param to be processed. |
99
|
|
|
*/ |
100
|
|
|
public function getOriginalParamValue() |
101
|
|
|
{ |
102
|
|
|
return $this->originalParams[$this->paramKey]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns params after they have been processed. |
107
|
|
|
*/ |
108
|
|
|
public function getProcessedParams(): array |
109
|
|
|
{ |
110
|
|
|
return $this->processedParams; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set processed params. |
115
|
|
|
* |
116
|
|
|
* @param array $params |
117
|
|
|
*/ |
118
|
|
|
public function setProcessedParams(array $params): void |
119
|
|
|
{ |
120
|
|
|
$this->processedParams = $params; |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Return the filter of the current param after it has been processed. |
125
|
|
|
*/ |
126
|
|
|
public function getProcessedFilter(): string |
127
|
|
|
{ |
128
|
|
|
return $this->processedFilter; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Replaces the value of the current filter. |
133
|
|
|
* |
134
|
|
|
* @param string $filter |
135
|
|
|
*/ |
136
|
|
|
public function setProcessedFilter(string $filter): void |
137
|
|
|
{ |
138
|
|
|
$this->processedFilter = $filter; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Remove a param for the list of processed params. |
143
|
|
|
* |
144
|
|
|
* @param array $paramKeys |
145
|
|
|
*/ |
146
|
|
|
public function removeParams(array $paramKeys): void |
147
|
|
|
{ |
148
|
|
|
foreach ($paramKeys as $paramKey) { |
149
|
|
|
unset($this->processedParams[$paramKey]); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Replaces the value of the current param. |
155
|
|
|
* |
156
|
|
|
* @param $newValue |
157
|
|
|
*/ |
158
|
|
|
public function replaceParamValue($newValue): void |
159
|
|
|
{ |
160
|
|
|
$this->addOrReplaceParamValue($this->paramKey, $newValue); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Adds or replaces the value of a para defined by $paramKey. |
165
|
|
|
* |
166
|
|
|
* @param string $paramKey |
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
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..