|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Serializers\CategoryCsvSerializer |
|
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 2021 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-serializer-csv |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Serializer\Csv; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Serializer\SerializerInterface; |
|
24
|
|
|
use TechDivision\Import\Serializer\SerializerFactoryInterface; |
|
25
|
|
|
use TechDivision\Import\Serializer\Configuration\ConfigurationInterface; |
|
26
|
|
|
use TechDivision\Import\Serializer\Configuration\SerializerConfigurationInterface; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Serializer implementation that un-/serializes the categories found in the CSV file |
|
30
|
|
|
* in the row 'path'. |
|
31
|
|
|
* |
|
32
|
|
|
* @author Tim Wagner <[email protected]> |
|
33
|
|
|
* @copyright 2021 TechDivision GmbH <[email protected]> |
|
34
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
35
|
|
|
* @link https://github.com/techdivision/import-serializer-csv |
|
36
|
|
|
* @link http://www.techdivision.com |
|
37
|
|
|
*/ |
|
38
|
|
|
class CategoryCsvSerializer extends AbstractCsvSerializer |
|
39
|
|
|
{ |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The factory instance for the CSV value serializer. |
|
43
|
|
|
* |
|
44
|
|
|
* @var \TechDivision\Import\Serializer\SerializerFactoryInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private $valueCsvSerializerFactory; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The CSV value serializer instance. |
|
50
|
|
|
* |
|
51
|
|
|
* @var \TechDivision\Import\Serializer\SerializerInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
private $valueCsvSerializer; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The configuration instance. |
|
57
|
|
|
* |
|
58
|
|
|
* @var \TechDivision\Import\Serializer\Configuration\ConfigurationInterface |
|
59
|
|
|
*/ |
|
60
|
|
|
private $configuration; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Initialize the serializer with the passed CSV value serializer factory. |
|
64
|
|
|
* |
|
65
|
|
|
* @param \TechDivision\Import\Serializer\Configuration\ConfigurationInterface $configuration The configuration instance |
|
66
|
|
|
* @param \TechDivision\Import\Serializer\SerializerFactoryInterface $valueCsvSerializerFactory The CSV value serializer factory |
|
67
|
|
|
*/ |
|
68
|
|
|
public function __construct( |
|
69
|
|
|
ConfigurationInterface $configuration, |
|
70
|
|
|
SerializerFactoryInterface $valueCsvSerializerFactory |
|
71
|
|
|
) { |
|
72
|
|
|
|
|
73
|
|
|
// set the passed instances |
|
74
|
|
|
$this->configuration = $configuration; |
|
75
|
|
|
$this->valueCsvSerializerFactory = $valueCsvSerializerFactory; |
|
76
|
|
|
|
|
77
|
|
|
// pre-initialize the serialize with the values |
|
78
|
|
|
// found in the main configuration |
|
79
|
|
|
$this->init($configuration); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns the configuration instance. |
|
84
|
|
|
* |
|
85
|
|
|
* @return \TechDivision\Import\Serializer\Configuration\ConfigurationInterface The configuration instance |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function getConfiguration() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->configuration; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Returns the factory instance for the CSV value serializer. |
|
94
|
|
|
* |
|
95
|
|
|
* @return \TechDivision\Import\Serializer\SerializerFactoryInterface The CSV value serializer factory instance |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function getValueCsvSerializerFactory() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->valueCsvSerializerFactory; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Returns the CSV value serializer instance. |
|
104
|
|
|
* |
|
105
|
|
|
* @param \TechDivision\Import\Serializer\SerializerInterface $valueCsvSerializer The CSV value serializer instance |
|
106
|
|
|
* |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function setValueCsvSerializer(SerializerInterface $valueCsvSerializer) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->valueCsvSerializer = $valueCsvSerializer; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Returns the CSV value serializer instance. |
|
116
|
|
|
* |
|
117
|
|
|
* @return \TechDivision\Import\Serializer\SerializerInterface The CSV value serializer instance |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function getValueCsvSerializer() |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->valueCsvSerializer; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Return's the delimiter character for categories, default value is comma (/). |
|
126
|
|
|
* |
|
127
|
|
|
* @return string The delimiter character for categories |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function getCategoryDelimiter() |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->getConfiguration()->getCategoryDelimiter(); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Passes the configuration and initializes the serializer. |
|
136
|
|
|
* |
|
137
|
|
|
* @param \TechDivision\Import\Serializer\Configuration\SerializerConfigurationInterface $configuration The CSV configuration |
|
138
|
|
|
* |
|
139
|
|
|
* @return void |
|
140
|
|
|
*/ |
|
141
|
|
|
public function init(SerializerConfigurationInterface $configuration) |
|
142
|
|
|
{ |
|
143
|
|
|
|
|
144
|
|
|
// pass the configuration to the parent instance |
|
145
|
|
|
parent::init($configuration); |
|
146
|
|
|
|
|
147
|
|
|
// create the CSV value serializer instance |
|
148
|
|
|
$this->setValueCsvSerializer($this->getValueCsvSerializerFactory()->createSerializer($configuration)); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Extracts the elements of the passed value by exploding them |
|
153
|
|
|
* with the also passed delimiter. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string|null $value The value to extract |
|
156
|
|
|
* @param string|null $delimiter The delimiter used to extrace the elements |
|
157
|
|
|
* |
|
158
|
|
|
* @return array|null The exploded values |
|
159
|
|
|
* @see \TechDivision\Import\Serializer\SerializerInterface::unserialize() |
|
160
|
|
|
*/ |
|
161
|
|
|
public function explode($value = null, $delimiter = null) |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->unserialize($value, $delimiter); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Compacts the elements of the passed value by imploding them |
|
168
|
|
|
* with the also passed delimiter. |
|
169
|
|
|
* |
|
170
|
|
|
* @param array|null $value The values to compact |
|
171
|
|
|
* @param string|null $delimiter The delimiter use to implode the values |
|
172
|
|
|
* |
|
173
|
|
|
* @return string|null The compatected value |
|
174
|
|
|
* @see \TechDivision\Import\Serializer\SerializerInterface::serialize() |
|
175
|
|
|
*/ |
|
176
|
|
|
public function implode(array $value = null, $delimiter = null) |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->serialize($value, $delimiter); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Unserializes the elements of the passed string. |
|
183
|
|
|
* |
|
184
|
|
|
* @param string|null $serialized The value to unserialize |
|
185
|
|
|
* @param string|null $delimiter The delimiter used to unserialize the elements |
|
186
|
|
|
* |
|
187
|
|
|
* @return array The unserialized values |
|
188
|
|
|
* @see \TechDivision\Import\Serializer\SerializerInterface::unserialize() |
|
189
|
|
|
*/ |
|
190
|
|
|
public function unserialize($serialized = null, $delimiter = null) |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->getValueCsvSerializer()->explode($serialized, $delimiter ? $delimiter : $this->getCategoryDelimiter()); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Serializes the elements of the passed array. |
|
197
|
|
|
* |
|
198
|
|
|
* @param array|null $unserialized The serialized data |
|
199
|
|
|
* @param string|null $delimiter The delimiter used to serialize the values |
|
200
|
|
|
* |
|
201
|
|
|
* @return string The serialized array |
|
202
|
|
|
* @see \TechDivision\Import\Serializer\SerializerInterface::serialize() |
|
203
|
|
|
*/ |
|
204
|
|
|
public function serialize(array $unserialized = null, $delimiter = null) |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->getValueCsvSerializer()->implode($unserialized, $delimiter ? $delimiter : $this->getCategoryDelimiter()); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Create a CSV compatible string from the passed category path. |
|
211
|
|
|
* |
|
212
|
|
|
* @param string The normalized category path (usually from the DB) |
|
|
|
|
|
|
213
|
|
|
* |
|
214
|
|
|
* @return string The denormalized category path for the import file |
|
215
|
|
|
*/ |
|
216
|
|
|
public function denormalize(string $path) : string |
|
|
|
|
|
|
217
|
|
|
{ |
|
218
|
|
|
throw new \Exception(sprintf('Method "%s" has not been implemented yet', __METHOD__)); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Normalizes the category path in a standard representation. |
|
223
|
|
|
* |
|
224
|
|
|
* For example this means, that a category path `Default Category/Gear` |
|
225
|
|
|
* will be normalized into `"Default Category"/Gear`. |
|
226
|
|
|
* |
|
227
|
|
|
* @param string $path The category path that has to be normalized |
|
228
|
|
|
* |
|
229
|
|
|
* @return string The normalized category path |
|
230
|
|
|
*/ |
|
231
|
|
|
public function normalize(string $path) : string |
|
232
|
|
|
{ |
|
233
|
|
|
return $this->implode($this->explode($path)); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.