-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProductCategoryCsvSerializer.php
More file actions
244 lines (216 loc) · 8.09 KB
/
ProductCategoryCsvSerializer.php
File metadata and controls
244 lines (216 loc) · 8.09 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
<?php
/**
* TechDivision\Import\Serializer\Csv\ProductCategoryCsvSerializer
*
* 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\ProductCategorySerializerInterface;
use TechDivision\Import\Serializer\Configuration\SerializerConfigurationInterface;
/**
* Serializer implementation that un-/serializes the categories found in product CSV file
* in the row 'categories'.
*
* @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 ProductCategoryCsvSerializer extends AbstractCsvSerializer implements ProductCategorySerializerInterface
{
/**
* 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 configuration instance.
*
* @var \TechDivision\Import\Serializer\Configuration\SerializerConfigurationInterface
*/
private $configuration;
/**
* Initialize the serializer with the passed CSV value serializer factory.
*
* @param \TechDivision\Import\Serializer\Configuration\SerializerConfigurationInterface $configuration The configuration instance
* @param \TechDivision\Import\Serializer\SerializerFactoryInterface $valueCsvSerializerFactory The CSV value serializer factory
*/
public function __construct(
SerializerConfigurationInterface $configuration,
SerializerFactoryInterface $valueCsvSerializerFactory
) {
// set the passed instances
$this->configuration = $configuration;
$this->valueCsvSerializerFactory = $valueCsvSerializerFactory;
// 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;
}
/**
* Return's the delimiter character for categories, default value is comma (/).
*
* @return string The delimiter character for categories
*/
protected function getCategoryDelimiter()
{
return $this->getConfiguration()->getCategoryDelimiter();
}
/**
* 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));
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* Denormalizes the passed path.
*
* @param string $path The path that has to be normalized
*
* @return string The denormalized path
* @throws \Exception Is thrown, because the method has not yet been implemented
*/
public function denormalize(string $path) : string
{
throw new \Exception(sprintf('Method "%s" has not been implemented yet', __METHOD__));
}
/**
* 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 string $path The category path that has to be normalized
*
* @return string The normalized category path
*/
public function normalize(string $path) : string
{
// initialize the array for the normalized categories
$normalized = array();
// unserialize the categories by using a comma (,)
$categories = $this->unserialize($path);
// unserializie//serialize the category
// elements by using a slash (/)
foreach ($categories as $category) {
$normalized[] = $this->serialize($this->unserialize(trim($category), $this->getCategoryDelimiter()), $this->getCategoryDelimiter());
}
// return the normalized categories
return $this->serialize($normalized);
}
}