|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Product\Observers\AbstractProductImportObserver |
|
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 2016 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-product |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Observers; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\EntityStatus; |
|
24
|
|
|
use TechDivision\Import\Product\Utils\ColumnKeys; |
|
25
|
|
|
use TechDivision\Import\Observers\AbstractObserver; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* A SLSB that handles the process to import product bunches. |
|
29
|
|
|
* |
|
30
|
|
|
* @author Tim Wagner <[email protected]> |
|
31
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
32
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
33
|
|
|
* @link https://github.com/techdivision/import-product |
|
34
|
|
|
* @link http://www.techdivision.com |
|
35
|
|
|
*/ |
|
36
|
|
|
abstract class AbstractProductImportObserver extends AbstractObserver implements ProductImportObserverInterface |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The actual row, that has to be processed. |
|
41
|
|
|
* |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $row = array(); |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Set's the array containing header row. |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $headers The array with the header row |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setHeaders(array $headers) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->getSubject()->setHeaders($headers); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Return's the array containing header row. |
|
60
|
|
|
* |
|
61
|
|
|
* @return array The array with the header row |
|
62
|
|
|
*/ |
|
63
|
7 |
|
public function getHeaders() |
|
64
|
|
|
{ |
|
65
|
7 |
|
return $this->getSubject()->getHeaders(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Return's TRUE if the passed SKU is the actual one. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $sku The SKU to check |
|
72
|
|
|
* |
|
73
|
|
|
* @return boolean TRUE if the passed SKU is the actual one |
|
74
|
|
|
*/ |
|
75
|
7 |
|
public function isLastSku($sku) |
|
76
|
|
|
{ |
|
77
|
7 |
|
return $this->getSubject()->getLastSku() === $sku; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Return's the ID of the product that has been created recently. |
|
82
|
|
|
* |
|
83
|
|
|
* @return string The entity Id |
|
84
|
|
|
*/ |
|
85
|
3 |
|
public function getLastEntityId() |
|
86
|
|
|
{ |
|
87
|
3 |
|
return $this->getSubject()->getLastEntityId(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Return's the source date format to use. |
|
92
|
|
|
* |
|
93
|
|
|
* @return string The source date format |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function getSourceDateFormat() |
|
96
|
|
|
{ |
|
97
|
1 |
|
return $this->getSubject()->getSourceDateFormat(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Cast's the passed value based on the backend type information. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $backendType The backend type to cast to |
|
104
|
|
|
* @param mixed $value The value to be casted |
|
105
|
|
|
* |
|
106
|
|
|
* @return mixed The casted value |
|
107
|
|
|
*/ |
|
108
|
|
|
public function castValueByBackendType($backendType, $value) |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->getSubject()->castValueByBackendType($backendType, $value); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Set's the store view code the create the product/attributes for. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $storeViewCode The store view code |
|
117
|
|
|
* |
|
118
|
|
|
* @return void |
|
119
|
|
|
*/ |
|
120
|
3 |
|
public function setStoreViewCode($storeViewCode) |
|
121
|
|
|
{ |
|
122
|
3 |
|
$this->getSubject()->setStoreViewCode($storeViewCode); |
|
123
|
3 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Return's the store view code the create the product/attributes for. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string|null $default The default value to return, if the store view code has not been set |
|
129
|
|
|
* |
|
130
|
|
|
* @return string The store view code |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getStoreViewCode($default = null) |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->getSubject()->getStoreViewCode($default); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Prepare's the store view code in the subject. |
|
139
|
|
|
* |
|
140
|
|
|
* @return void |
|
141
|
|
|
*/ |
|
142
|
3 |
|
public function prepareStoreViewCode() |
|
143
|
|
|
{ |
|
144
|
|
|
|
|
145
|
|
|
// load the headers |
|
146
|
3 |
|
$row = $this->getRow(); |
|
147
|
3 |
|
$headers = $this->getHeaders(); |
|
148
|
|
|
|
|
149
|
|
|
// initialize the store view code |
|
150
|
3 |
|
$this->setStoreViewCode(null); |
|
151
|
|
|
|
|
152
|
|
|
// initialize the store view code |
|
153
|
3 |
|
if (isset($row[$headers[ColumnKeys::STORE_VIEW_CODE]])) { |
|
154
|
|
|
$storeViewCode = $row[$headers[ColumnKeys::STORE_VIEW_CODE]]; |
|
155
|
|
|
if (!empty($storeViewCode)) { |
|
156
|
|
|
$this->setStoreViewCode($storeViewCode); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
3 |
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Set's the actual row, that has to be processed. |
|
163
|
|
|
* |
|
164
|
|
|
* @param array $row The row |
|
165
|
|
|
* |
|
166
|
|
|
* @return void |
|
167
|
|
|
*/ |
|
168
|
6 |
|
public function setRow(array $row) |
|
169
|
|
|
{ |
|
170
|
6 |
|
$this->row = $row; |
|
171
|
6 |
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Return's the actual row, that has to be processed. |
|
175
|
|
|
* |
|
176
|
|
|
* @return array The row |
|
177
|
|
|
*/ |
|
178
|
6 |
|
public function getRow() |
|
179
|
|
|
{ |
|
180
|
6 |
|
return $this->row; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Tries to format the passed value to a valid date with format 'Y-m-d H:i:s'. |
|
185
|
|
|
* If the passed value is NOT a valid date, NULL will be returned. |
|
186
|
|
|
* |
|
187
|
|
|
* @param string|null $value The value to format |
|
188
|
|
|
* |
|
189
|
|
|
* @return string The formatted date |
|
190
|
|
|
*/ |
|
191
|
1 |
|
public function formatDate($value) |
|
192
|
|
|
{ |
|
193
|
|
|
|
|
194
|
|
|
// create a DateTime instance from the passed value |
|
195
|
1 |
|
if ($dateTime = \DateTime::createFromFormat($this->getSourceDateFormat(), $value)) { |
|
196
|
1 |
|
return $dateTime->format('Y-m-d H:i:s'); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
// return NULL, if the passed value is NOT a valid date |
|
200
|
|
|
return null; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Extracts the elements of the passed value by exploding them |
|
205
|
|
|
* with the also passed separator. |
|
206
|
|
|
* |
|
207
|
|
|
* @param string $value The value to extract |
|
208
|
|
|
* @param string $separator The separator used to extrace the elements |
|
209
|
|
|
* |
|
210
|
|
|
* @return array The exploded values |
|
211
|
|
|
*/ |
|
212
|
|
|
public function explode($value, $separator = ',') |
|
213
|
|
|
{ |
|
214
|
|
|
return explode($separator, $value); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Query whether or not the value with the passed key exists. |
|
219
|
|
|
* |
|
220
|
|
|
* @param string $key The key of the value to query |
|
221
|
|
|
* |
|
222
|
|
|
* @return boolean TRUE if the value is set, else FALSE |
|
223
|
|
|
*/ |
|
224
|
2 |
|
public function hasValue($key) |
|
225
|
|
|
{ |
|
226
|
|
|
|
|
227
|
|
|
// load row and headers |
|
228
|
2 |
|
$row = $this->getRow(); |
|
229
|
2 |
|
$headers = $this->getHeaders(); |
|
230
|
|
|
|
|
231
|
|
|
// query whether or not the value exists |
|
232
|
2 |
|
return isset($row[$headers[$key]]); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Resolve's the value with the passed key from the actual row. If a callback will |
|
237
|
|
|
* be passed, the callback will be invoked with the found value as parameter. If |
|
238
|
|
|
* the value is NULL or empty, the default value will be returned. |
|
239
|
|
|
* |
|
240
|
|
|
* @param string $key The key of the value to return |
|
241
|
|
|
* @param mixed|null $default The default value, that has to be returned, if the row's value is empty |
|
242
|
|
|
* @param callable|null $callback The callback that has to be invoked on the value, e. g. to format it |
|
243
|
|
|
* |
|
244
|
|
|
* @return mixed|null The, almost formatted, value |
|
245
|
|
|
*/ |
|
246
|
6 |
|
public function getValue($key, $default = null, callable $callback = null) |
|
247
|
|
|
{ |
|
248
|
|
|
|
|
249
|
|
|
// load row and headers |
|
250
|
6 |
|
$row = $this->getRow(); |
|
251
|
6 |
|
$headers = $this->getHeaders(); |
|
252
|
|
|
|
|
253
|
|
|
// initialize the value |
|
254
|
6 |
|
$value = null; |
|
255
|
|
|
|
|
256
|
|
|
// query wheter or not, the value with the requested key is available |
|
257
|
6 |
|
if (isset($headers[$key]) && isset($row[$headers[$key]])) { |
|
258
|
6 |
|
$value = $row[$headers[$key]]; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
// query whether or not, a callback has been passed |
|
262
|
6 |
|
if ($value != null && is_callable($callback)) { |
|
263
|
1 |
|
$value = call_user_func($callback, $value); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
// query whether or not |
|
267
|
6 |
|
if ($value == null && $default != null) { |
|
268
|
|
|
$value = $default; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
// return the value |
|
272
|
6 |
|
return $value; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Initialize's and return's a new entity with the status 'create'. |
|
277
|
|
|
* |
|
278
|
|
|
* @param array $attr The attributes to merge into the new entity |
|
279
|
|
|
* |
|
280
|
|
|
* @return array The initialized entity |
|
281
|
|
|
*/ |
|
282
|
4 |
|
public function initializeEntity(array $attr = array()) |
|
283
|
|
|
{ |
|
284
|
4 |
|
return array_merge(array(EntityStatus::MEMBER_NAME => EntityStatus::STATUS_CREATE), $attr); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Merge's and return's the entity with the passed attributes and set's the |
|
289
|
|
|
* status to 'update'. |
|
290
|
|
|
* |
|
291
|
|
|
* @param array $entity The entity to merge the attributes into |
|
292
|
|
|
* @param array $attr The attributes to be merged |
|
293
|
|
|
* |
|
294
|
|
|
* @return array The merged entity |
|
295
|
|
|
*/ |
|
296
|
2 |
|
public function mergeEntity(array $entity, array $attr) |
|
297
|
|
|
{ |
|
298
|
2 |
|
return array_merge($entity, $attr, array(EntityStatus::MEMBER_NAME => EntityStatus::STATUS_UPDATE)); |
|
299
|
|
|
} |
|
300
|
|
|
} |
|
301
|
|
|
|