1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SprykerEco\Zed\AkeneoPimMiddlewareConnector\Business\Translator\TranslatorFunction; |
9
|
|
|
|
10
|
|
|
use SprykerMiddleware\Zed\Process\Business\Translator\TranslatorFunction\AbstractTranslatorFunction; |
|
|
|
|
11
|
|
|
use SprykerMiddleware\Zed\Process\Business\Translator\TranslatorFunction\TranslatorFunctionInterface; |
|
|
|
|
12
|
|
|
|
13
|
|
|
class EnrichAttributes extends AbstractTranslatorFunction implements TranslatorFunctionInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected const KEY_OPTIONS = 'options'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected const KEY_DATA = 'data'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected const KEY_LOCALE = 'locale'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected const KEY_TYPE = 'type'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected const KEY_LOCALIZABLE = 'localizable'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected const KEY_KEY = 'key'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected const ATTRIBUTE_PRICE = 'price'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected const ATTRIBUTE_TYPES_WITH_OPTIONS = [ |
54
|
|
|
'pim_catalog_simpleselect', |
55
|
|
|
'pim_catalog_multiselect', |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
protected const ATTRIBUTES_TYPES_FOR_SKIPPING = [ |
62
|
|
|
'pim_assets_collection', |
63
|
|
|
'pim_reference_data_multiselect', |
64
|
|
|
'pim_reference_data_simpleselect', |
65
|
|
|
'pim_catalog_price_collection', |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
protected static $attributeOptionMap; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
protected static $attributeLocalizableMap; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var array |
80
|
|
|
*/ |
81
|
|
|
protected static $attributesForSkippingMap; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var array |
85
|
|
|
*/ |
86
|
|
|
protected $requiredOptions = [ |
87
|
|
|
'map', |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param mixed $value |
92
|
|
|
* @param array $payload |
93
|
|
|
* |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
public function translate($value, array $payload) |
97
|
|
|
{ |
98
|
|
|
$this->initAttributeOptionMap(); |
99
|
|
|
|
100
|
|
|
foreach ($value as $attributeKey => $attributeValues) { |
101
|
|
|
if ($this->isKeySkipped($attributeKey)) { |
102
|
|
|
unset($value[$attributeKey]); |
103
|
|
|
|
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (!$this->hasKey($attributeKey) || $this->isKeyExcluded($attributeKey)) { |
108
|
|
|
continue; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$isAttributeLocalizable = $this->isAttributeLocalizable($attributeKey); |
112
|
|
|
|
113
|
|
|
foreach ($attributeValues as $index => $attributeValue) { |
114
|
|
|
$attributeData = $attributeValue[static::KEY_DATA]; |
115
|
|
|
$locale = $attributeValue[static::KEY_LOCALE]; |
116
|
|
|
|
117
|
|
|
if ($attributeData === null) { |
118
|
|
|
unset($value[$attributeKey]); |
119
|
|
|
|
120
|
|
|
continue 2; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($isAttributeLocalizable) { |
124
|
|
|
$options = is_array($attributeData) ? $this->getArrayOptions($attributeKey, $attributeData) : $this->getOptions($attributeKey, $attributeData); |
125
|
|
|
if (!array_key_exists($locale, $options)) { |
126
|
|
|
unset($value[$attributeKey]); |
127
|
|
|
|
128
|
|
|
continue; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$value[$attributeKey][$index][static::KEY_DATA] = $options[$locale]; |
132
|
|
|
|
133
|
|
|
continue; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$value[$attributeKey] = $this->getAttributeValue($attributeKey, $attributeData); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $value; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
protected function getMap(): array |
147
|
|
|
{ |
148
|
|
|
return $this->options['map']; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
protected function initAttributeOptionMap(): void |
155
|
|
|
{ |
156
|
|
|
if (static::$attributeOptionMap !== null) { |
|
|
|
|
157
|
|
|
return; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
static::$attributeOptionMap = array_map( |
161
|
|
|
function ($element) { |
162
|
|
|
return $element[static::KEY_OPTIONS]; |
163
|
|
|
}, |
164
|
|
|
array_filter($this->getMap(), function ($element) { |
165
|
|
|
return count($element[static::KEY_OPTIONS] ?? []) > 0 && |
166
|
|
|
in_array($element[static::KEY_TYPE], static::ATTRIBUTE_TYPES_WITH_OPTIONS); |
167
|
|
|
}), |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
static::$attributeLocalizableMap = array_map( |
171
|
|
|
function ($element) { |
172
|
|
|
return $element[static::KEY_LOCALIZABLE]; |
173
|
|
|
}, |
174
|
|
|
array_filter($this->getMap(), function ($element) { |
175
|
|
|
return in_array($element[static::KEY_TYPE], static::ATTRIBUTE_TYPES_WITH_OPTIONS); |
176
|
|
|
}), |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
static::$attributesForSkippingMap = array_map( |
180
|
|
|
function ($element) { |
181
|
|
|
return $element[static::KEY_KEY]; |
182
|
|
|
}, |
183
|
|
|
array_filter($this->getMap(), function ($element) { |
184
|
|
|
return in_array($element[static::KEY_TYPE], static::ATTRIBUTES_TYPES_FOR_SKIPPING) && $element[static::KEY_KEY] != static::ATTRIBUTE_PRICE; |
185
|
|
|
}), |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string $key |
191
|
|
|
* |
192
|
|
|
* @return bool |
193
|
|
|
*/ |
194
|
|
|
protected function hasKey(string $key): bool |
195
|
|
|
{ |
196
|
|
|
return array_key_exists($key, static::$attributeOptionMap); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string $key |
201
|
|
|
* |
202
|
|
|
* @return bool |
203
|
|
|
*/ |
204
|
|
|
protected function isKeyExcluded(string $key): bool |
205
|
|
|
{ |
206
|
|
|
return in_array($key, $this->options['excludeKeys']); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param string $key |
211
|
|
|
* |
212
|
|
|
* @return bool |
213
|
|
|
*/ |
214
|
|
|
protected function isKeySkipped(string $key): bool |
215
|
|
|
{ |
216
|
|
|
return array_key_exists($key, static::$attributesForSkippingMap); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param string $attributeKey |
221
|
|
|
* @param string $optionKey |
222
|
|
|
* |
223
|
|
|
* @return array |
224
|
|
|
*/ |
225
|
|
|
protected function getOptions(string $attributeKey, string $optionKey): array |
226
|
|
|
{ |
227
|
|
|
if (!array_key_exists($optionKey, static::$attributeOptionMap[$attributeKey])) { |
228
|
|
|
return [$optionKey]; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return static::$attributeOptionMap[$attributeKey][$optionKey]; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param string $attributeKey |
236
|
|
|
* @param array $optionKeys |
237
|
|
|
* |
238
|
|
|
* @return array |
239
|
|
|
*/ |
240
|
|
|
protected function getArrayOptions(string $attributeKey, array $optionKeys): array |
241
|
|
|
{ |
242
|
|
|
$options = []; |
243
|
|
|
|
244
|
|
|
foreach ($optionKeys as $optionKey) { |
245
|
|
|
$options[] = array_key_exists($optionKey, static::$attributeOptionMap[$attributeKey]) ? |
246
|
|
|
static::$attributeOptionMap[$attributeKey][$optionKey] : |
247
|
|
|
$optionKey; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return $options; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param array $options |
255
|
|
|
* |
256
|
|
|
* @return array |
257
|
|
|
*/ |
258
|
|
|
protected function getMultiSelectAttributeValue(array $options): array |
259
|
|
|
{ |
260
|
|
|
$value = []; |
261
|
|
|
|
262
|
|
|
foreach ($options as $option) { |
263
|
|
|
foreach ($option as $locale => $optionValue) { |
264
|
|
|
$value[$locale][static::KEY_LOCALE] = $locale; |
265
|
|
|
|
266
|
|
|
// Concatenate with comma for multiselect values |
267
|
|
|
$value[$locale][static::KEY_DATA] = isset($value[$locale][static::KEY_DATA]) ? |
268
|
|
|
$value[$locale][static::KEY_DATA] . ', ' . $optionValue : |
269
|
|
|
$optionValue; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return array_values($value); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param array $option |
278
|
|
|
* |
279
|
|
|
* @return array |
280
|
|
|
*/ |
281
|
|
|
protected function getSimpleSelectAttributeValue($option): array |
282
|
|
|
{ |
283
|
|
|
$value = []; |
284
|
|
|
|
285
|
|
|
if (is_array($option)) { |
|
|
|
|
286
|
|
|
foreach ($option as $locale => $optionValue) { |
287
|
|
|
$value[$locale][static::KEY_LOCALE] = $locale; |
288
|
|
|
$value[$locale][static::KEY_DATA] = $optionValue; |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
return array_values($value); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param string $attributeKey |
297
|
|
|
* @param mixed $attributeData |
298
|
|
|
* |
299
|
|
|
* @return array |
300
|
|
|
*/ |
301
|
|
|
protected function getAttributeValue(string $attributeKey, $attributeData): array |
302
|
|
|
{ |
303
|
|
|
if (is_array($attributeData)) { |
304
|
|
|
$options = $this->getArrayOptions($attributeKey, $attributeData); |
305
|
|
|
|
306
|
|
|
return $this->getMultiSelectAttributeValue($options); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
$option = $this->getOptions($attributeKey, $attributeData); |
310
|
|
|
|
311
|
|
|
return $this->getSimpleSelectAttributeValue($option); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param string $attributeKey |
316
|
|
|
* |
317
|
|
|
* @return mixed |
318
|
|
|
*/ |
319
|
|
|
protected function isAttributeLocalizable(string $attributeKey) |
320
|
|
|
{ |
321
|
|
|
return static::$attributeLocalizableMap[$attributeKey]; |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
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