1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MIT License |
5
|
|
|
* For full license information, please view the LICENSE file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SprykerEco\Client\FactFinderNg\Mapper\Request; |
9
|
|
|
|
10
|
|
|
use Generated\Shared\Transfer\FactFinderNgRequestTransfer; |
|
|
|
|
11
|
|
|
use SprykerEco\Client\FactFinderNg\FactFinderNgConfig; |
12
|
|
|
|
13
|
|
|
class FactFinderNgRequestMapper implements FactFinderNgRequestMapperInterface |
14
|
|
|
{ |
15
|
|
|
public const KEY_PARAMS = 'params'; |
16
|
|
|
public const KEY_CHANNEL = 'channel'; |
17
|
|
|
public const KEY_QUERY = 'query'; |
18
|
|
|
public const KEY_PAGE = 'page'; |
19
|
|
|
public const KEY_HITS_PER_PAGE = 'hitsPerPage'; |
20
|
|
|
public const KEY_SORT_ITEMS = 'sortItems'; |
21
|
|
|
public const KEY_NAME = 'name'; |
22
|
|
|
public const KEY_SORT_ORDER = 'order'; |
23
|
|
|
public const KEY_FILTERS = 'filters'; |
24
|
|
|
public const KEY_SUBSTRING = 'substring'; |
25
|
|
|
public const KEY_VALUES = 'values'; |
26
|
|
|
public const KEY_EXCLUDE = 'exclude'; |
27
|
|
|
public const KEY_TYPE = 'type'; |
28
|
|
|
public const KEY_VALUE = 'value'; |
29
|
|
|
|
30
|
|
|
public const DEFAULT_VALUE_POS_PARAM = 1; |
31
|
|
|
public const DEFAULT_VALUE_PAGE_PARAM = 1; |
32
|
|
|
public const DEFAULT_VALUE_QUERY_PARAM = 'query'; |
33
|
|
|
public const DEFAULT_VALUE_PAGE_SIZE_PARAM = 12; |
34
|
|
|
public const DEFAULT_VALUE_QUERY = '*'; |
35
|
|
|
|
36
|
|
|
public const TYPE_OR = 'or'; |
37
|
|
|
public const TYPE_AND = 'and'; |
38
|
|
|
|
39
|
|
|
public const KEY_REQUEST_PARAMETER_Q = 'q'; |
40
|
|
|
public const KEY_REQUEST_PARAMETER_PAGE = 'page'; |
41
|
|
|
public const KEY_REQUEST_PARAMETER_IPP = 'ipp'; |
42
|
|
|
public const KEY_REQUEST_PARAMETER_SORT = 'sort'; |
43
|
|
|
public const KEY_REQUEST_PARAMETER_CATEGORY = 'category'; |
44
|
|
|
|
45
|
|
|
public const DEFAULT_IPP_VALUE = 12; |
46
|
|
|
public const DEFAULT_PAGE_VALUE = 1; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \SprykerEco\Client\FactFinderNg\FactFinderNgConfig |
50
|
|
|
*/ |
51
|
|
|
protected $config; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param \SprykerEco\Client\FactFinderNg\FactFinderNgConfig $config |
55
|
|
|
*/ |
56
|
|
|
public function __construct(FactFinderNgConfig $config) |
57
|
|
|
{ |
58
|
|
|
$this->config = $config; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param array $requestParameters |
63
|
|
|
* |
64
|
|
|
* @return \Generated\Shared\Transfer\FactFinderNgRequestTransfer |
65
|
|
|
*/ |
66
|
|
|
public function mapSearchRequest(array $requestParameters): FactFinderNgRequestTransfer |
67
|
|
|
{ |
68
|
|
|
$params = [ |
69
|
|
|
static::KEY_CHANNEL => $this->config->getFactFinderChannel(), |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
$params = $this->addQueryParam($params, $requestParameters); |
73
|
|
|
$params = $this->addPageParam($params, $requestParameters); |
74
|
|
|
$params = $this->addHitsPerPageParam($params, $requestParameters); |
75
|
|
|
$params = $this->addSortItemsParam($params, $requestParameters); |
76
|
|
|
$params = $this->addFiltersParam($params, $requestParameters); |
77
|
|
|
|
78
|
|
|
$payload = [ |
79
|
|
|
static::KEY_PARAMS => $params, |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
$factFinderNgRequestTransfer = new FactFinderNgRequestTransfer(); |
83
|
|
|
$factFinderNgRequestTransfer->setPayload($payload); |
84
|
|
|
|
85
|
|
|
return $factFinderNgRequestTransfer; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $requestParameters |
90
|
|
|
* |
91
|
|
|
* @return \Generated\Shared\Transfer\FactFinderNgRequestTransfer |
92
|
|
|
*/ |
93
|
|
|
public function mapSuggestionRequest(array $requestParameters): FactFinderNgRequestTransfer |
94
|
|
|
{ |
95
|
|
|
$payload = [ |
96
|
|
|
static::KEY_CHANNEL => $this->config->getFactFinderChannel(), |
97
|
|
|
static::KEY_QUERY => $requestParameters[static::KEY_REQUEST_PARAMETER_Q], |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
$factFinderNgRequestTransfer = new FactFinderNgRequestTransfer(); |
101
|
|
|
$factFinderNgRequestTransfer->setPayload($payload); |
102
|
|
|
|
103
|
|
|
return $factFinderNgRequestTransfer; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param array $requestParameters |
108
|
|
|
* |
109
|
|
|
* @return \Generated\Shared\Transfer\FactFinderNgRequestTransfer |
110
|
|
|
*/ |
111
|
|
|
public function mapNavigationRequest(array $requestParameters): FactFinderNgRequestTransfer |
112
|
|
|
{ |
113
|
|
|
$params = [ |
114
|
|
|
static::KEY_CHANNEL => $this->config->getFactFinderChannel(), |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
$params = $this->addPageParam($params, $requestParameters); |
118
|
|
|
$params = $this->addHitsPerPageParam($params, $requestParameters); |
119
|
|
|
$params = $this->addSortItemsParam($params, $requestParameters); |
120
|
|
|
$params = $this->addFiltersParam($params, $requestParameters); |
121
|
|
|
|
122
|
|
|
$payload = [ |
123
|
|
|
static::KEY_PARAMS => $params, |
124
|
|
|
]; |
125
|
|
|
|
126
|
|
|
$factFinderNgRequestTransfer = new FactFinderNgRequestTransfer(); |
127
|
|
|
$factFinderNgRequestTransfer->setPayload($payload); |
128
|
|
|
|
129
|
|
|
return $factFinderNgRequestTransfer; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return \Generated\Shared\Transfer\FactFinderNgRequestTransfer |
134
|
|
|
*/ |
135
|
|
|
public function mapTriggerSearchImportRequest(): FactFinderNgRequestTransfer |
136
|
|
|
{ |
137
|
|
|
$factFinderNgRequestTransfer = new FactFinderNgRequestTransfer(); |
138
|
|
|
$factFinderNgRequestTransfer->setPayload([]); |
139
|
|
|
|
140
|
|
|
return $factFinderNgRequestTransfer; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param \Generated\Shared\Transfer\CartOrCheckoutEventTransfer[] $cartOrCheckoutEventTransfers |
145
|
|
|
* |
146
|
|
|
* @return \Generated\Shared\Transfer\FactFinderNgRequestTransfer |
147
|
|
|
*/ |
148
|
|
|
public function mapTrackCheckoutEventRequest(array $cartOrCheckoutEventTransfers): FactFinderNgRequestTransfer |
149
|
|
|
{ |
150
|
|
|
$payload = []; |
151
|
|
|
|
152
|
|
|
foreach ($cartOrCheckoutEventTransfers as $cartOrCheckoutEventTransfer) { |
153
|
|
|
$cartOrCheckoutEventTransfer->requireSid(); |
154
|
|
|
$cartOrCheckoutEventTransfer->requireId(); |
155
|
|
|
|
156
|
|
|
$payload[] = $cartOrCheckoutEventTransfer->toArray(true, true); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$factFinderNgRequestTransfer = new FactFinderNgRequestTransfer(); |
160
|
|
|
$factFinderNgRequestTransfer->setPayload($payload); |
161
|
|
|
|
162
|
|
|
return $factFinderNgRequestTransfer; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param \Generated\Shared\Transfer\CartOrCheckoutEventTransfer[] $cartOrCheckoutEventTransfers |
167
|
|
|
* |
168
|
|
|
* @return \Generated\Shared\Transfer\FactFinderNgRequestTransfer |
169
|
|
|
*/ |
170
|
|
|
public function mapTrackCartEventRequest(array $cartOrCheckoutEventTransfers): FactFinderNgRequestTransfer |
171
|
|
|
{ |
172
|
|
|
$payload = []; |
173
|
|
|
|
174
|
|
|
foreach ($cartOrCheckoutEventTransfers as $cartOrCheckoutEventTransfer) { |
175
|
|
|
$cartOrCheckoutEventTransfer->requireSid(); |
176
|
|
|
$cartOrCheckoutEventTransfer->requireId(); |
177
|
|
|
|
178
|
|
|
$payload[] = $cartOrCheckoutEventTransfer->toArray(true, true); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$factFinderNgRequestTransfer = new FactFinderNgRequestTransfer(); |
182
|
|
|
$factFinderNgRequestTransfer->setPayload($payload); |
183
|
|
|
|
184
|
|
|
return $factFinderNgRequestTransfer; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param \Generated\Shared\Transfer\ClickEventTransfer[] $clickEventTransfers |
189
|
|
|
* |
190
|
|
|
* @return \Generated\Shared\Transfer\FactFinderNgRequestTransfer |
191
|
|
|
*/ |
192
|
|
|
public function mapTrackClickEventRequest(array $clickEventTransfers): FactFinderNgRequestTransfer |
193
|
|
|
{ |
194
|
|
|
$payload = []; |
195
|
|
|
|
196
|
|
|
foreach ($clickEventTransfers as $clickEventTransfer) { |
197
|
|
|
$clickEventTransfer->setPos($clickEventTransfer->getPos() ?? static::DEFAULT_VALUE_POS_PARAM); |
198
|
|
|
$clickEventTransfer->setPage($clickEventTransfer->getPage() ?? static::DEFAULT_VALUE_PAGE_PARAM); |
199
|
|
|
$clickEventTransfer->setQuery($clickEventTransfer->getQuery() ?? static::DEFAULT_VALUE_QUERY_PARAM); |
200
|
|
|
$clickEventTransfer->setOrigPos($clickEventTransfer->getOrigPos() ?? static::DEFAULT_VALUE_POS_PARAM); |
201
|
|
|
$clickEventTransfer->setOrigPageSize($clickEventTransfer->getOrigPageSize() ?? static::DEFAULT_VALUE_PAGE_SIZE_PARAM); |
202
|
|
|
|
203
|
|
|
$clickEventTransfer->requireSid(); |
204
|
|
|
$clickEventTransfer->requireId(); |
205
|
|
|
|
206
|
|
|
$payload[] = $clickEventTransfer->toArray(true, true); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$factFinderNgRequestTransfer = new FactFinderNgRequestTransfer(); |
210
|
|
|
$factFinderNgRequestTransfer->setPayload($payload); |
211
|
|
|
|
212
|
|
|
return $factFinderNgRequestTransfer; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param array $params |
217
|
|
|
* @param array $requestParameters |
218
|
|
|
* |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
protected function addSortItemsParam(array $params, array $requestParameters): array |
222
|
|
|
{ |
223
|
|
|
if (isset($requestParameters[static::KEY_REQUEST_PARAMETER_SORT]) && $requestParameters[static::KEY_REQUEST_PARAMETER_SORT]) { |
224
|
|
|
$name = explode('_', $requestParameters[static::KEY_REQUEST_PARAMETER_SORT])[0]; |
225
|
|
|
$order = explode('_', $requestParameters[static::KEY_REQUEST_PARAMETER_SORT])[1]; |
226
|
|
|
|
227
|
|
|
$params[static::KEY_SORT_ITEMS][] = [ |
228
|
|
|
static::KEY_NAME => ucfirst($name), |
229
|
|
|
static::KEY_SORT_ORDER => $order, |
230
|
|
|
]; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $params; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param array $params |
238
|
|
|
* @param array $requestParameters |
239
|
|
|
* |
240
|
|
|
* @return array |
241
|
|
|
*/ |
242
|
|
|
protected function addPageParam(array $params, array $requestParameters): array |
243
|
|
|
{ |
244
|
|
|
$params[static::KEY_PAGE] = $requestParameters[static::KEY_REQUEST_PARAMETER_PAGE] ?? static::DEFAULT_PAGE_VALUE; |
245
|
|
|
|
246
|
|
|
return $params; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param array $params |
251
|
|
|
* @param array $requestParameters |
252
|
|
|
* |
253
|
|
|
* @return array |
254
|
|
|
*/ |
255
|
|
|
protected function addHitsPerPageParam(array $params, array $requestParameters): array |
256
|
|
|
{ |
257
|
|
|
$params[static::KEY_HITS_PER_PAGE] = $requestParameters[static::KEY_REQUEST_PARAMETER_IPP] ?? static::DEFAULT_IPP_VALUE; |
258
|
|
|
|
259
|
|
|
return $params; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param array $params |
264
|
|
|
* @param array $requestParameters |
265
|
|
|
* |
266
|
|
|
* @return array |
267
|
|
|
*/ |
268
|
|
|
protected function addQueryParam(array $params, array $requestParameters): array |
269
|
|
|
{ |
270
|
|
|
$params[static::KEY_QUERY] = static::DEFAULT_VALUE_QUERY; |
271
|
|
|
|
272
|
|
|
if (isset($requestParameters[static::KEY_REQUEST_PARAMETER_Q]) && $requestParameters[static::KEY_REQUEST_PARAMETER_Q]) { |
273
|
|
|
$params[static::KEY_QUERY] = $requestParameters[static::KEY_REQUEST_PARAMETER_Q]; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return $params; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param array $params |
281
|
|
|
* @param array $requestParameters |
282
|
|
|
* |
283
|
|
|
* @return array |
284
|
|
|
*/ |
285
|
|
|
protected function addFiltersParam(array $params, array $requestParameters): array |
286
|
|
|
{ |
287
|
|
|
$baseParameters = [ |
288
|
|
|
static::KEY_REQUEST_PARAMETER_IPP, |
289
|
|
|
static::KEY_REQUEST_PARAMETER_PAGE, |
290
|
|
|
static::KEY_REQUEST_PARAMETER_Q, |
291
|
|
|
static::KEY_REQUEST_PARAMETER_SORT, |
292
|
|
|
static::KEY_REQUEST_PARAMETER_CATEGORY, |
293
|
|
|
]; |
294
|
|
|
|
295
|
|
|
$filters = []; |
296
|
|
|
|
297
|
|
|
foreach ($requestParameters as $key => $value) { |
298
|
|
|
if (in_array($key, $baseParameters) || is_array($value) && (isset($value[0]['min']) || isset($value[0]['max']))) { |
299
|
|
|
continue; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
$filters[] = [ |
303
|
|
|
static::KEY_NAME => $key, |
304
|
|
|
static::KEY_SUBSTRING => false, |
305
|
|
|
static::KEY_VALUES => is_array($value) ? $this->getMultipleFilterValues($value) : $this->getSingleFilterValues($value), |
306
|
|
|
]; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
if ($filters) { |
310
|
|
|
$params[static::KEY_FILTERS] = $filters; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
return $params; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @param array $values |
318
|
|
|
* |
319
|
|
|
* @return array |
320
|
|
|
*/ |
321
|
|
|
protected function getMultipleFilterValues(array $values): array |
322
|
|
|
{ |
323
|
|
|
$filterValues = []; |
324
|
|
|
foreach ($values as $value) { |
325
|
|
|
$filterValues[] = [ |
326
|
|
|
static::KEY_EXCLUDE => false, |
327
|
|
|
static::KEY_TYPE => static::TYPE_OR, |
328
|
|
|
static::KEY_VALUE => urlencode($value), |
329
|
|
|
]; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
return $filterValues; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @param mixed $value |
337
|
|
|
* |
338
|
|
|
* @return array |
339
|
|
|
*/ |
340
|
|
|
protected function getSingleFilterValues($value): array |
341
|
|
|
{ |
342
|
|
|
$filterValues[] = [ |
|
|
|
|
343
|
|
|
static::KEY_EXCLUDE => false, |
344
|
|
|
static::KEY_TYPE => static::TYPE_AND, |
345
|
|
|
static::KEY_VALUE => urlencode($value), |
346
|
|
|
]; |
347
|
|
|
|
348
|
|
|
return $filterValues; |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths