|
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
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Serializer implementation that un-/serializes the additional product attribues found in the CSV file |
|
27
|
|
|
* in the row 'additional_attributes'. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Tim Wagner <[email protected]> |
|
30
|
|
|
* @copyright 2018 TechDivision GmbH <[email protected]> |
|
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
32
|
|
|
* @link https://github.com/techdivision/import |
|
33
|
|
|
* @link http://www.techdivision.com |
|
34
|
|
|
*/ |
|
35
|
|
|
class AdditionalAttributeCsvSerializer extends AbstractCsvSerializer |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The factory instance for the CSV value serializer. |
|
40
|
|
|
* |
|
41
|
|
|
* @var \TechDivision\Import\Serializers\ConfigurationAwareSerializerFactoryInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
private $valueCsvSerializerFactory; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The CSV value serializer instance. |
|
47
|
|
|
* |
|
48
|
|
|
* @var \TechDivision\Import\Serializers\SerializerInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
private $valueCsvSerializer; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Initialize the serializer with the passed CSV value serializer factory. |
|
54
|
|
|
* |
|
55
|
|
|
* @param \TechDivision\Import\Serializers\ConfigurationAwareSerializerFactoryInterface $valueCsvSerializerFactory The CSV value serializer factory |
|
56
|
|
|
*/ |
|
57
|
5 |
|
public function __construct(ConfigurationAwareSerializerFactoryInterface $valueCsvSerializerFactory) |
|
58
|
|
|
{ |
|
59
|
5 |
|
$this->valueCsvSerializerFactory = $valueCsvSerializerFactory; |
|
60
|
5 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns the factory instance for the CSV value serializer. |
|
64
|
|
|
* |
|
65
|
|
|
* @return \TechDivision\Import\Serializers\ConfigurationAwareSerializerFactoryInterface The CSV value serializer factory instance |
|
66
|
|
|
*/ |
|
67
|
5 |
|
protected function getValueCsvSerializerFactory() |
|
68
|
|
|
{ |
|
69
|
5 |
|
return $this->valueCsvSerializerFactory; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Returns the CSV value serializer instance. |
|
74
|
|
|
* |
|
75
|
|
|
* @param \TechDivision\Import\Serializers\SerializerInterface $valueCsvSerializer The CSV value serializer instance |
|
76
|
|
|
* |
|
77
|
|
|
* @return void |
|
78
|
|
|
*/ |
|
79
|
5 |
|
protected function setValueCsvSerializer(SerializerInterface $valueCsvSerializer) |
|
80
|
|
|
{ |
|
81
|
5 |
|
$this->valueCsvSerializer = $valueCsvSerializer; |
|
82
|
5 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Returns the CSV value serializer instance. |
|
86
|
|
|
* |
|
87
|
|
|
* @return \TechDivision\Import\Serializers\SerializerInterface The CSV value serializer instance |
|
88
|
|
|
*/ |
|
89
|
5 |
|
protected function getValueCsvSerializer() |
|
90
|
|
|
{ |
|
91
|
5 |
|
return $this->valueCsvSerializer; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Passes the configuration and initializes the serializer. |
|
96
|
|
|
* |
|
97
|
|
|
* @param \TechDivision\Import\Configuration\CsvConfigurationInterface $configuration The CSV configuration |
|
98
|
|
|
* |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
5 |
|
public function init(CsvConfigurationInterface $configuration) |
|
102
|
|
|
{ |
|
103
|
|
|
|
|
104
|
|
|
// pass the configuration to the parent instance |
|
105
|
5 |
|
parent::init($configuration); |
|
106
|
|
|
|
|
107
|
|
|
// create the CSV value serializer instance |
|
108
|
5 |
|
$this->setValueCsvSerializer($this->getValueCsvSerializerFactory()->createSerializer($configuration)); |
|
109
|
5 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Unserializes the elements of the passed string. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string|null $serialized The value to unserialize |
|
115
|
|
|
* |
|
116
|
|
|
* @return array The unserialized values |
|
117
|
|
|
* @see \TechDivision\Import\Serializers\SerializerInterface::unserialize() |
|
118
|
|
|
*/ |
|
119
|
3 |
|
public function unserialize($serialized = null) |
|
120
|
|
|
{ |
|
121
|
|
|
|
|
122
|
|
|
// initialize the array for the unserialized additional attributes |
|
123
|
3 |
|
$attributes = array(); |
|
124
|
|
|
|
|
125
|
|
|
// explode the additional attributes |
|
126
|
3 |
|
if ($additionalAttributes = $this->explode($serialized)) { |
|
127
|
|
|
// iterate over the attributes and append them to the row |
|
128
|
2 |
|
foreach ($additionalAttributes as $additionalAttribute) { |
|
129
|
|
|
// explode attribute code/option value from the attribute |
|
130
|
2 |
|
list ($attributeCode, $optionValue) = explode('=', $additionalAttribute); |
|
131
|
|
|
|
|
132
|
|
|
// extract the key/value pairs into an array |
|
133
|
2 |
|
$attributes[$attributeCode] = $optionValue; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// return the array with the unserialized additional attributes |
|
138
|
3 |
|
return $attributes; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Serializes the elements of the passed array. |
|
143
|
|
|
* |
|
144
|
|
|
* @param array|null $unserialized The serialized data |
|
145
|
|
|
* |
|
146
|
|
|
* @return string The serialized array |
|
147
|
|
|
* @see \TechDivision\Import\Serializers\SerializerInterface::serialize() |
|
148
|
|
|
*/ |
|
149
|
2 |
|
public function serialize(array $unserialized = null) |
|
150
|
|
|
{ |
|
151
|
|
|
|
|
152
|
|
|
// initialize the array |
|
153
|
2 |
|
$attributes = array(); |
|
154
|
|
|
|
|
155
|
2 |
|
if (is_array($unserialized)) { |
|
156
|
|
|
// serialize the key/value pairs into the array |
|
157
|
2 |
|
foreach ($unserialized as $attributeCode => $attributeValue) { |
|
158
|
1 |
|
$attributes[] = implode('=', array($attributeCode, $attributeValue)); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// serialize the array itself |
|
163
|
2 |
|
return $this->implode($attributes); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Extracts the elements of the passed value by exploding them |
|
168
|
|
|
* with the also passed delimiter. |
|
169
|
|
|
* |
|
170
|
|
|
* @param string|null $value The value to extract |
|
171
|
|
|
* @param string|null $delimiter The delimiter used to extrace the elements |
|
172
|
|
|
* |
|
173
|
|
|
* @return array|null The exploded values |
|
174
|
|
|
* @see \TechDivision\Import\Serializers\SerializerInterface::unserialize() |
|
175
|
|
|
*/ |
|
176
|
3 |
|
public function explode($value = null, $delimiter = null) |
|
177
|
|
|
{ |
|
178
|
3 |
|
return $this->getValueCsvSerializer()->explode($value, $delimiter); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Compacts the elements of the passed value by imploding them |
|
183
|
|
|
* with the also passed delimiter. |
|
184
|
|
|
* |
|
185
|
|
|
* @param array|null $value The values to compact |
|
186
|
|
|
* @param string|null $delimiter The delimiter use to implode the values |
|
187
|
|
|
* |
|
188
|
|
|
* @return string|null The compatected value |
|
189
|
|
|
* @see \TechDivision\Import\Serializers\SerializerInterface::serialize() |
|
190
|
|
|
*/ |
|
191
|
2 |
|
public function implode(array $value = null, $delimiter = null) |
|
192
|
|
|
{ |
|
193
|
2 |
|
return $this->getValueCsvSerializer()->implode($value, $delimiter); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|