|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Serializers\AdditionalAttributeCsvSerializer |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2018 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Serializers; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Configuration\CsvConfigurationInterface; |
|
24
|
|
|
use TechDivision\Import\Utils\FrontendInputTypes; |
|
25
|
|
|
use TechDivision\Import\Utils\MemberNames; |
|
26
|
|
|
use TechDivision\Import\ConfigurationInterface; |
|
27
|
|
|
use TechDivision\Import\Services\ImportProcessorInterface; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Serializer implementation that un-/serializes the additional product attribues found in the CSV file |
|
31
|
|
|
* in the row 'additional_attributes'. |
|
32
|
|
|
* |
|
33
|
|
|
* @author Tim Wagner <[email protected]> |
|
34
|
|
|
* @copyright 2018 TechDivision GmbH <[email protected]> |
|
35
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
36
|
|
|
* @link https://github.com/techdivision/import |
|
37
|
|
|
* @link http://www.techdivision.com |
|
38
|
|
|
*/ |
|
39
|
|
|
class AdditionalAttributeCsvSerializer extends AbstractCsvSerializer |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The factory instance for the CSV value serializer. |
|
44
|
|
|
* |
|
45
|
|
|
* @var \TechDivision\Import\Serializers\ConfigurationAwareSerializerFactoryInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
private $valueCsvSerializerFactory; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The CSV value serializer instance. |
|
51
|
|
|
* |
|
52
|
|
|
* @var \TechDivision\Import\Serializers\SerializerInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
private $valueCsvSerializer; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* The entity type from the configuration. |
|
58
|
|
|
* |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
private $entityType; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* The configuration instance. |
|
65
|
|
|
* |
|
66
|
|
|
* @var \TechDivision\Import\ConfigurationInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
private $configuration; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* The convert processor instance. |
|
72
|
|
|
* |
|
73
|
|
|
* @var \TechDivision\Import\Services\ImportProcessorInterface |
|
74
|
|
|
*/ |
|
75
|
|
|
private $importProcessor; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Initialize the serializer with the passed CSV value serializer factory. |
|
79
|
|
|
* |
|
80
|
|
|
* @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance |
|
81
|
|
|
* @param \TechDivision\Import\Services\ImportProcessorInterface $converterProcessor The processor instance |
|
|
|
|
|
|
82
|
|
|
* @param \TechDivision\Import\Serializers\ConfigurationAwareSerializerFactoryInterface $valueCsvSerializerFactory The CSV value serializer factory |
|
83
|
|
|
*/ |
|
84
|
8 |
|
public function __construct( |
|
85
|
|
|
ConfigurationInterface $configuration, |
|
86
|
|
|
ImportProcessorInterface $importProcessor, |
|
87
|
|
|
ConfigurationAwareSerializerFactoryInterface $valueCsvSerializerFactory |
|
88
|
|
|
) { |
|
89
|
|
|
|
|
90
|
|
|
// set the passed instances |
|
91
|
8 |
|
$this->configuration = $configuration; |
|
92
|
8 |
|
$this->importProcessor = $importProcessor; |
|
93
|
8 |
|
$this->valueCsvSerializerFactory = $valueCsvSerializerFactory; |
|
94
|
|
|
|
|
95
|
|
|
// load the entity type for the entity type defined in the configuration |
|
96
|
8 |
|
$this->entityType = $importProcessor->getEavEntityTypeByEntityTypeCode($configuration->getEntityTypeCode()); |
|
97
|
8 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns the configuration instance. |
|
101
|
|
|
* |
|
102
|
|
|
* @return \TechDivision\Import\ConfigurationInterface The configuration instance |
|
103
|
|
|
*/ |
|
104
|
8 |
|
protected function getConfiguration() |
|
105
|
|
|
{ |
|
106
|
8 |
|
return $this->configuration; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns the factory instance for the CSV value serializer. |
|
111
|
|
|
* |
|
112
|
|
|
* @return \TechDivision\Import\Serializers\ConfigurationAwareSerializerFactoryInterface The CSV value serializer factory instance |
|
113
|
|
|
*/ |
|
114
|
8 |
|
protected function getValueCsvSerializerFactory() |
|
115
|
|
|
{ |
|
116
|
8 |
|
return $this->valueCsvSerializerFactory; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Returns the CSV value serializer instance. |
|
121
|
|
|
* |
|
122
|
|
|
* @param \TechDivision\Import\Serializers\SerializerInterface $valueCsvSerializer The CSV value serializer instance |
|
123
|
|
|
* |
|
124
|
|
|
* @return void |
|
125
|
|
|
*/ |
|
126
|
8 |
|
protected function setValueCsvSerializer(SerializerInterface $valueCsvSerializer) |
|
127
|
|
|
{ |
|
128
|
8 |
|
$this->valueCsvSerializer = $valueCsvSerializer; |
|
129
|
8 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Returns the CSV value serializer instance. |
|
133
|
|
|
* |
|
134
|
|
|
* @return \TechDivision\Import\Serializers\SerializerInterface The CSV value serializer instance |
|
135
|
|
|
*/ |
|
136
|
8 |
|
protected function getValueCsvSerializer() |
|
137
|
|
|
{ |
|
138
|
8 |
|
return $this->valueCsvSerializer; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Returns the import processor instance. |
|
143
|
|
|
* |
|
144
|
|
|
* @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
|
145
|
|
|
*/ |
|
146
|
6 |
|
protected function getImportProcessor() |
|
147
|
|
|
{ |
|
148
|
6 |
|
return $this->importProcessor; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Returns entity type ID mapped from the configuration. |
|
153
|
|
|
* |
|
154
|
|
|
* @return integer The mapped entity type ID |
|
155
|
|
|
*/ |
|
156
|
6 |
|
protected function getEntityTypeId() |
|
157
|
|
|
{ |
|
158
|
6 |
|
return $this->entityType[MemberNames::ENTITY_TYPE_ID]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Returns the multiple value delimiter from the configuration. |
|
163
|
|
|
* |
|
164
|
|
|
* @return string The multiple value delimiter |
|
165
|
|
|
*/ |
|
166
|
2 |
|
protected function getMultipleValueDelimiter() |
|
167
|
|
|
{ |
|
168
|
2 |
|
return $this->getConfiguration()->getMultipleValueDelimiter(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Returns the multiple field delimiter from the configuration. |
|
173
|
|
|
* |
|
174
|
|
|
* @return string The multiple field delimiter |
|
175
|
|
|
*/ |
|
176
|
8 |
|
protected function getMultipleFieldDelimiter() |
|
177
|
|
|
{ |
|
178
|
8 |
|
return $this->getConfiguration()->getMultipleFieldDelimiter(); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Loads and returns the attribute with the passed code from the database. |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $attributeCode The code of the attribute to return |
|
185
|
|
|
* |
|
186
|
|
|
* @return array The attribute |
|
187
|
|
|
*/ |
|
188
|
6 |
|
protected function loadAttributeByAttributeCode($attributeCode) |
|
189
|
|
|
{ |
|
190
|
6 |
|
return $this->getImportProcessor()->getEavAttributeByEntityTypeIdAndAttributeCode($this->getEntityTypeId(), $attributeCode); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Packs the passed value according to the frontend input type of the attribute with the passed code. |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $attributeCode The code of the attribute to pack the passed value for |
|
197
|
|
|
* @param mixed $value The value to pack |
|
198
|
|
|
* |
|
199
|
|
|
* @return string The packed value |
|
200
|
|
|
*/ |
|
201
|
3 |
View Code Duplication |
protected function pack($attributeCode, $value) |
|
|
|
|
|
|
202
|
|
|
{ |
|
203
|
|
|
|
|
204
|
|
|
// load the attibute with the passed code |
|
205
|
3 |
|
$attribute = $this->loadAttributeByAttributeCode($attributeCode); |
|
206
|
|
|
|
|
207
|
|
|
// pack the value according to the attribute's frontend input type |
|
208
|
3 |
|
switch ($attribute[MemberNames::FRONTEND_INPUT]) { |
|
209
|
3 |
|
case FrontendInputTypes::MULTISELECT: |
|
210
|
1 |
|
return implode($this->getMultipleValueDelimiter(), $value); |
|
211
|
|
|
break; |
|
|
|
|
|
|
212
|
|
|
|
|
213
|
3 |
|
case FrontendInputTypes::BOOLEAN: |
|
214
|
1 |
|
return $value === true ? 'true' : 'false'; |
|
215
|
|
|
break; |
|
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
default: |
|
218
|
3 |
|
return $value; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Unpacks the passed value according to the frontend input type of the attribute with the passed code. |
|
224
|
|
|
* |
|
225
|
|
|
* @param string $attributeCode The code of the attribute to pack the passed value for |
|
226
|
|
|
* @param string $value The value to unpack |
|
227
|
|
|
* |
|
228
|
|
|
* @return mixed The unpacked value |
|
229
|
|
|
*/ |
|
230
|
3 |
View Code Duplication |
protected function unpack($attributeCode, $value) |
|
|
|
|
|
|
231
|
|
|
{ |
|
232
|
|
|
|
|
233
|
|
|
// load the attibute with the passed code |
|
234
|
3 |
|
$attribute = $this->loadAttributeByAttributeCode($attributeCode); |
|
235
|
|
|
|
|
236
|
|
|
// unpack the value according to the attribute's frontend input type |
|
237
|
3 |
|
switch ($attribute[MemberNames::FRONTEND_INPUT]) { |
|
238
|
|
|
|
|
239
|
3 |
|
case FrontendInputTypes::MULTISELECT: |
|
240
|
1 |
|
return explode($this->getMultipleValueDelimiter(), $value); |
|
241
|
|
|
break; |
|
|
|
|
|
|
242
|
|
|
|
|
243
|
3 |
|
case FrontendInputTypes::BOOLEAN: |
|
244
|
1 |
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
245
|
|
|
break; |
|
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
default: |
|
248
|
3 |
|
return $value; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Passes the configuration and initializes the serializer. |
|
254
|
|
|
* |
|
255
|
|
|
* @param \TechDivision\Import\Configuration\CsvConfigurationInterface $configuration The CSV configuration |
|
256
|
|
|
* |
|
257
|
|
|
* @return void |
|
258
|
|
|
*/ |
|
259
|
8 |
|
public function init(CsvConfigurationInterface $configuration) |
|
260
|
|
|
{ |
|
261
|
|
|
|
|
262
|
|
|
// pass the configuration to the parent instance |
|
263
|
8 |
|
parent::init($configuration); |
|
264
|
|
|
|
|
265
|
|
|
// create the CSV value serializer instance |
|
266
|
8 |
|
$this->setValueCsvSerializer($this->getValueCsvSerializerFactory()->createSerializer($configuration)); |
|
267
|
8 |
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Unserializes the elements of the passed string. |
|
271
|
|
|
* |
|
272
|
|
|
* @param string|null $serialized The value to unserialize |
|
273
|
|
|
* @param string|null $delimiter The delimiter used to unserialize the elements |
|
274
|
|
|
* |
|
275
|
|
|
* @return array The unserialized values |
|
276
|
|
|
* @see \TechDivision\Import\Serializers\SerializerInterface::unserialize() |
|
277
|
|
|
*/ |
|
278
|
4 |
|
public function unserialize($serialized = null, $delimiter = null) |
|
279
|
|
|
{ |
|
280
|
|
|
|
|
281
|
|
|
// initialize the array for the attributes |
|
282
|
4 |
|
$attrs = array(); |
|
283
|
|
|
|
|
284
|
|
|
// explode the additional attributes |
|
285
|
4 |
|
$additionalAttributes = $this->getValueCsvSerializer()->unserialize($serialized, $delimiter ? $delimiter : $this->getMultipleFieldDelimiter()); |
|
286
|
|
|
|
|
287
|
|
|
// iterate over the attributes and append them to the row |
|
288
|
4 |
|
if (is_array($additionalAttributes)) { |
|
289
|
3 |
|
foreach ($additionalAttributes as $additionalAttribute) { |
|
290
|
|
|
// explode attribute code/option value from the attribute |
|
291
|
3 |
|
list ($attributeCode, $optionValue) = $this->explode($additionalAttribute, '='); |
|
292
|
3 |
|
$attrs[$attributeCode] = $this->unpack($attributeCode, $optionValue); |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
// return the extracted array with the additional attributes |
|
297
|
4 |
|
return $attrs; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Serializes the elements of the passed array. |
|
302
|
|
|
* |
|
303
|
|
|
* @param array|null $unserialized The serialized data |
|
304
|
|
|
* @param string|null $delimiter The delimiter used to serialize the values |
|
305
|
|
|
* |
|
306
|
|
|
* @return string The serialized array |
|
307
|
|
|
* @see \TechDivision\Import\Serializers\SerializerInterface::serialize() |
|
308
|
|
|
*/ |
|
309
|
4 |
|
public function serialize(array $unserialized = null, $delimiter = null) |
|
310
|
|
|
{ |
|
311
|
|
|
|
|
312
|
|
|
// initialize the array for the attributes |
|
313
|
4 |
|
$attrs = array(); |
|
314
|
|
|
|
|
315
|
|
|
// iterate over the attributes and append them to the row |
|
316
|
4 |
|
if (is_array($unserialized)) { |
|
317
|
4 |
|
foreach ($unserialized as $attributeCode => $optionValue) { |
|
318
|
3 |
|
$attrs[] = sprintf('%s=%s', $attributeCode, $this->pack($attributeCode, $optionValue)); |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
// implode the array with the packed additional attributes and return it |
|
323
|
4 |
|
return $this->getValueCsvSerializer()->serialize($attrs, $delimiter ? $delimiter : $this->getMultipleFieldDelimiter()); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* Extracts the elements of the passed value by exploding them |
|
328
|
|
|
* with the also passed delimiter. |
|
329
|
|
|
* |
|
330
|
|
|
* @param string|null $value The value to extract |
|
331
|
|
|
* @param string|null $delimiter The delimiter used to extrace the elements |
|
332
|
|
|
* |
|
333
|
|
|
* @return array|null The exploded values |
|
334
|
|
|
* @see \TechDivision\Import\Serializers\SerializerInterface::unserialize() |
|
335
|
|
|
*/ |
|
336
|
3 |
|
public function explode($value = null, $delimiter = null) |
|
337
|
|
|
{ |
|
338
|
3 |
|
return $this->getValueCsvSerializer()->explode($value, $delimiter ? $delimiter : $this->getMultipleFieldDelimiter()); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* Compacts the elements of the passed value by imploding them |
|
343
|
|
|
* with the also passed delimiter. |
|
344
|
|
|
* |
|
345
|
|
|
* @param array|null $value The values to compact |
|
346
|
|
|
* @param string|null $delimiter The delimiter use to implode the values |
|
347
|
|
|
* |
|
348
|
|
|
* @return string|null The compatected value |
|
349
|
|
|
* @see \TechDivision\Import\Serializers\SerializerInterface::serialize() |
|
350
|
|
|
*/ |
|
351
|
|
|
public function implode(array $value = null, $delimiter = null) |
|
352
|
|
|
{ |
|
353
|
|
|
return $this->getValueCsvSerializer()->implode($value, $delimiter ? $delimiter : $this->getMultipleFieldDelimiter()); |
|
354
|
|
|
} |
|
355
|
|
|
} |
|
356
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.