-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdditionalAttributeCsvSerializer.php
More file actions
400 lines (353 loc) · 14.3 KB
/
AdditionalAttributeCsvSerializer.php
File metadata and controls
400 lines (353 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
/**
* TechDivision\Import\Serializer\Csv\AdditionalAttributeCsvSerializer
*
* PHP version 7
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2021 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import-serializer-csv
* @link http://www.techdivision.com
*/
namespace TechDivision\Import\Serializer\Csv;
use TechDivision\Import\Serializer\SerializerInterface;
use TechDivision\Import\Serializer\SerializerFactoryInterface;
use TechDivision\Import\Serializer\Configuration\ConfigurationInterface;
use TechDivision\Import\Serializer\AdditionalAttributeSerializerInterface;
use TechDivision\Import\Serializer\Configuration\SerializerConfigurationInterface;
use TechDivision\Import\Serializer\Csv\Utils\MemberNames;
use TechDivision\Import\Serializer\Csv\Utils\FrontendInputTypes;
use TechDivision\Import\Serializer\Csv\Services\EavAttributeAwareProcessorInterface;
/**
* Serializer implementation that un-/serializes the additional product attribues found in the CSV file
* in the row 'additional_attributes'.
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2021 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import-serializer-csv
* @link http://www.techdivision.com
*/
class AdditionalAttributeCsvSerializer extends AbstractCsvSerializer implements AdditionalAttributeSerializerInterface
{
/**
* The factory instance for the CSV value serializer.
*
* @var \TechDivision\Import\Serializer\SerializerFactoryInterface
*/
private $valueCsvSerializerFactory;
/**
* The CSV value serializer instance.
*
* @var \TechDivision\Import\Serializer\SerializerInterface
*/
private $valueCsvSerializer;
/**
* The entity type from the configuration.
*
* @var array
*/
private $entityType = array();
/**
* The configuration instance.
*
* @var \TechDivision\Import\Serializer\Configuration\ConfigurationInterface
*/
private $configuration;
/**
* The attribute loader instance.
*
* @var \TechDivision\Import\Serializer\Csv\Services\EavAttributeAwareProcessorInterface
*/
private $eavAttributeAwareProcessor;
/**
* Initialize the serializer with the passed CSV value serializer factory.
*
* @param \TechDivision\Import\Serializer\Configuration\ConfigurationInterface $configuration The configuration instance
* @param \TechDivision\Import\Serializer\Csv\Services\EavAttributeAwareProcessorInterface $attributeLoader The attribute loader instance
* @param \TechDivision\Import\Serializer\SerializerFactoryInterface $valueCsvSerializerFactory The CSV value serializer factory
*/
public function __construct(
ConfigurationInterface $configuration,
EavAttributeAwareProcessorInterface $attributeLoader,
SerializerFactoryInterface $valueCsvSerializerFactory
) {
// set the passed instances
$this->configuration = $configuration;
$this->eavAttributeAwareProcessor = $attributeLoader;
$this->valueCsvSerializerFactory = $valueCsvSerializerFactory;
// load the entity type for the entity type defined in the configuration
$this->entityType = $attributeLoader->getEavEntityTypeByEntityTypeCode($configuration->getEntityTypeCode());
// pre-initialize the serialize with the values
// found in the main configuration
$this->init($configuration);
}
/**
* Returns the configuration instance.
*
* @return \TechDivision\Import\Serializer\Configuration\SerializerConfigurationInterface The configuration instance
*/
protected function getConfiguration()
{
return $this->configuration;
}
/**
* Returns the factory instance for the CSV value serializer.
*
* @return \TechDivision\Import\Serializer\SerializerFactoryInterface The CSV value serializer factory instance
*/
protected function getValueCsvSerializerFactory()
{
return $this->valueCsvSerializerFactory;
}
/**
* Returns the CSV value serializer instance.
*
* @param \TechDivision\Import\Serializer\SerializerInterface $valueCsvSerializer The CSV value serializer instance
*
* @return void
*/
protected function setValueCsvSerializer(SerializerInterface $valueCsvSerializer)
{
$this->valueCsvSerializer = $valueCsvSerializer;
}
/**
* Returns the CSV value serializer instance.
*
* @return \TechDivision\Import\Serializer\SerializerInterface The CSV value serializer instance
*/
protected function getValueCsvSerializer()
{
return $this->valueCsvSerializer;
}
/**
* Returns the EAV attribute aware processor instance.
*
* @return \TechDivision\Import\Serializer\Csv\Services\EavAttributeAwareProcessorInterface The EAV attribute aware processor instance
*/
protected function getEavAttributeAwareProcessor() : EavAttributeAwareProcessorInterface
{
return $this->eavAttributeAwareProcessor;
}
/**
* Returns entity type ID mapped from the configuration.
*
* @return integer The mapped entity type ID
*/
protected function getEntityTypeId()
{
return $this->entityType[MemberNames::ENTITY_TYPE_ID];
}
/**
* Returns the multiple value delimiter from the configuration.
*
* @return string The multiple value delimiter
*/
protected function getMultipleValueDelimiter()
{
return $this->getConfiguration()->getMultipleValueDelimiter();
}
/**
* Returns the multiple field delimiter from the configuration.
*
* @return string The multiple field delimiter
*/
protected function getMultipleFieldDelimiter()
{
return $this->getConfiguration()->getMultipleFieldDelimiter();
}
/**
* Loads and returns the attribute with the passed code from the database.
*
* @param string $attributeCode The code of the attribute to return
*
* @return array The attribute
*/
protected function loadAttributeByAttributeCode($attributeCode)
{
return $this->getEavAttributeAwareProcessor()->getEavAttributeByEntityTypeIdAndAttributeCode($this->getEntityTypeId(), $attributeCode);
}
/**
* Packs the passed value according to the frontend input type of the attribute with the passed code.
*
* @param string $attributeCode The code of the attribute to pack the passed value for
* @param mixed $value The value to pack
*
* @return string The packed value
*/
protected function pack($attributeCode, $value)
{
// load the attibute with the passed code
$attribute = $this->loadAttributeByAttributeCode($attributeCode);
// pack the value according to the attribute's frontend input type
switch ($attribute[MemberNames::FRONTEND_INPUT]) {
case FrontendInputTypes::MULTISELECT:
return $this->implode($value, $this->getMultipleValueDelimiter());
break;
case FrontendInputTypes::BOOLEAN:
return $value === true ? 'true' : 'false';
break;
default:
return $value;
}
}
/**
* Unpacks the passed value according to the frontend input type of the attribute with the passed code.
*
* @param string $attributeCode The code of the attribute to pack the passed value for
* @param string $value The value to unpack
*
* @return mixed The unpacked value
*/
protected function unpack($attributeCode, $value)
{
// load the attibute with the passed code
$attribute = $this->loadAttributeByAttributeCode($attributeCode);
// unpack the value according to the attribute's frontend input type
switch ($attribute[MemberNames::FRONTEND_INPUT]) {
case FrontendInputTypes::MULTISELECT:
return $this->explode($value, $this->getMultipleValueDelimiter());
break;
case FrontendInputTypes::BOOLEAN:
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
break;
default:
return $value;
}
}
/**
* Passes the configuration and initializes the serializer.
*
* @param \TechDivision\Import\Serializer\Configuration\SerializerConfigurationInterface $configuration The CSV configuration
*
* @return void
*/
public function init(SerializerConfigurationInterface $configuration)
{
// pass the configuration to the parent instance
parent::init($configuration);
// create the CSV value serializer instance
$this->setValueCsvSerializer($this->getValueCsvSerializerFactory()->createSerializer($configuration));
}
/**
* Unserializes the elements of the passed string.
*
* @param string|null $serialized The value to unserialize
* @param string|null $delimiter The delimiter used to unserialize the elements
*
* @return array The unserialized values
* @see \TechDivision\Import\Serializer\SerializerInterface::unserialize()
*/
public function unserialize($serialized = null, $delimiter = null)
{
return $this->getValueCsvSerializer()->explode($serialized, $delimiter ? $delimiter : $this->getMultipleFieldDelimiter());
}
/**
* Serializes the elements of the passed array.
*
* @param array|null $unserialized The serialized data
* @param string|null $delimiter The delimiter used to serialize the values
*
* @return string The serialized array
* @see \TechDivision\Import\Serializer\SerializerInterface::serialize()
*/
public function serialize(?array $unserialized = null, $delimiter = null)
{
return $this->getValueCsvSerializer()->implode($unserialized, $delimiter ? $delimiter : $this->getMultipleFieldDelimiter());
}
/**
* Extracts the elements of the passed value by exploding them
* with the also passed delimiter.
*
* @param string|null $value The value to extract
* @param string|null $delimiter The delimiter used to extrace the elements
*
* @return array|null The exploded values
* @see \TechDivision\Import\Serializer\SerializerInterface::unserialize()
*/
public function explode($value = null, $delimiter = null)
{
return $this->unserialize($value, $delimiter);
}
/**
* Compacts the elements of the passed value by imploding them
* with the also passed delimiter.
*
* @param array|null $value The values to compact
* @param string|null $delimiter The delimiter use to implode the values
*
* @return string|null The compatected value
* @see \TechDivision\Import\Serializer\SerializerInterface::serialize()
*/
public function implode(?array $value = null, $delimiter = null)
{
return $this->serialize($value, $delimiter);
}
/**
* Create a CSV compatible string from the passed category path.
*
* @param string|null $value The normalized category path (usually from the DB)
* @param bool $unpack TRUE if the option values has to be unpacked, in case of a multiselect attribute
*
* @return array The array with the denormalized attribute values
*/
public function denormalize(?string $value = null, bool $unpack = true) : array
{
// initialize the array for the attributes
$attrs = array();
// explode the additional attributes and iterate
// over the attributes and append them to the row
if (is_array($additionalAttributes = $this->unserialize($value))) {
foreach ($additionalAttributes as $additionalAttribute) {
// initialize the option value
$optionValue = '';
// explode attribute code/option value from the attribute
$exploded = $this->explode($additionalAttribute, '=');
// initialize attribute code and option value, depending on what we've exploded
if (!is_array($exploded) || sizeof($exploded) < 1) {
continue;
} elseif (sizeof($exploded) === 1) {
list ($attributeCode) = $exploded;
} else {
list ($attributeCode, $optionValue) = $exploded;
}
// query whether or not we've to pack the option
// values in case we've a multiselect input field
$optionValue = $unpack ? $this->unpack($attributeCode, $optionValue) : $optionValue;
// unpack the attribute values and add them to the array
$attrs[$attributeCode] = $optionValue;
}
}
// return the extracted array with the additional attributes
return $attrs;
}
/**
* Normalizes the category path in a standard representation.
*
* For example this means, that a category path `Default Category/Gear`
* will be normalized into `"Default Category"/Gear`.
*
* @param array $values The category path that has to be normalized
* @param bool $pack TRUE if the option values has to be packed, in case of a multiselect attribute
*
* @return string The normalized category path
*/
public function normalize(array $values, bool $pack = true) : string
{
// initialize the array for the attributes
$attrs = array();
// iterate over the attributes and append them to the row
if (is_array($values)) {
foreach ($values as $attributeCode => $optionValue) {
// query whether or not we've to unpack the option
// values in case we've a multiselect input field
$optionValue = $pack ? $this->pack($attributeCode, $optionValue) : $optionValue;
// append th eption value to the array
$attrs[] = sprintf('%s=%s', $attributeCode, $optionValue);
}
}
// implode the array with the packed additional attributes and return it
return $this->serialize($attrs);
}
}