1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Subjects\AbstractSubject |
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 |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Subjects; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\MemberNames; |
24
|
|
|
use TechDivision\Import\Utils\RegistryKeys; |
25
|
|
|
use TechDivision\Import\Utils\BackendTypeKeys; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* An abstract EAV subject implementation. |
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 |
34
|
|
|
* @link http://www.techdivision.com |
35
|
|
|
*/ |
36
|
|
|
abstract class AbstractEavSubject extends AbstractSubject implements EavSubjectInterface, NumberConverterSubjectInterface |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The trait that provides number converting functionality. |
41
|
|
|
* |
42
|
|
|
* @var \TechDivision\Import\Subjects\NumberConverterTrait |
43
|
|
|
*/ |
44
|
|
|
use NumberConverterTrait; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The available EAV attributes, grouped by their attribute set and the attribute set name as keys. |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $attributes = array(); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The available user defined EAV attributes, grouped by their entity type. |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $userDefinedAttributes = array(); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The attribute set of the entity that has to be created. |
62
|
|
|
* |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $attributeSet = array(); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The available EAV attribute sets. |
69
|
|
|
* |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
protected $attributeSets = array(); |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The mapping for the supported backend types (for the EAV entity) => persist methods. |
76
|
|
|
* |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
protected $backendTypes = array( |
80
|
|
|
BackendTypeKeys::BACKEND_TYPE_DATETIME => array('persistDatetimeAttribute', 'loadDatetimeAttribute', 'deleteDatetimeAttribute'), |
81
|
|
|
BackendTypeKeys::BACKEND_TYPE_DECIMAL => array('persistDecimalAttribute', 'loadDecimalAttribute', 'deleteDecimalAttribute'), |
82
|
|
|
BackendTypeKeys::BACKEND_TYPE_INT => array('persistIntAttribute', 'loadIntAttribute', 'deleteIntAttribute'), |
83
|
|
|
BackendTypeKeys::BACKEND_TYPE_TEXT => array('persistTextAttribute', 'loadTextAttribute', 'deleteTextAttribute'), |
84
|
|
|
BackendTypeKeys::BACKEND_TYPE_VARCHAR => array('persistVarcharAttribute', 'loadVarcharAttribute', 'deleteVarcharAttribute') |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* The default mappings for the user defined attributes, based on the attributes frontend input type. |
89
|
|
|
* |
90
|
|
|
* @var array |
91
|
|
|
*/ |
92
|
|
|
protected $defaultFrontendInputCallbackMappings = array(); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return's the default callback frontend input mappings for the user defined attributes. |
96
|
|
|
* |
97
|
|
|
* @return array The default frontend input callback mappings |
98
|
|
|
*/ |
99
|
34 |
|
public function getDefaultFrontendInputCallbackMappings() |
100
|
|
|
{ |
101
|
34 |
|
return $this->defaultFrontendInputCallbackMappings; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Intializes the previously loaded global data for exactly one bunch. |
106
|
|
|
* |
107
|
|
|
* @param string $serial The serial of the actual import |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
34 |
|
public function setUp($serial) |
112
|
|
|
{ |
113
|
|
|
|
114
|
|
|
// load the status of the actual import |
115
|
34 |
|
$status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS); |
116
|
|
|
|
117
|
|
|
// load the global data, if prepared initially |
118
|
34 |
|
if (isset($status[RegistryKeys::GLOBAL_DATA])) { |
119
|
34 |
|
$this->attributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::EAV_ATTRIBUTES]; |
120
|
34 |
|
$this->attributeSets = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ATTRIBUTE_SETS]; |
121
|
34 |
|
$this->userDefinedAttributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::EAV_USER_DEFINED_ATTRIBUTES]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// load the default frontend callback mappings from the child instance and merge with the one from the configuration |
125
|
34 |
|
$defaultFrontendInputCallbackMappings = $this->getDefaultFrontendInputCallbackMappings(); |
126
|
|
|
|
127
|
|
|
// load the available frontend input callbacks from the configuration |
128
|
34 |
|
$availableFrontendInputCallbacks = $this->getConfiguration()->getFrontendInputCallbacks(); |
129
|
|
|
|
130
|
|
|
// merge the default mappings with the one's found in the configuration |
131
|
34 |
|
foreach ($availableFrontendInputCallbacks as $frontendInputCallbackMappings) { |
132
|
|
|
foreach ($frontendInputCallbackMappings as $frontendInput => $frontentInputMappings) { |
133
|
|
|
$defaultFrontendInputCallbackMappings[$frontendInput] = $frontentInputMappings; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// load the user defined EAV attributes |
138
|
34 |
|
$eavUserDefinedAttributes = $this->getEavUserDefinedAttributes(); |
139
|
|
|
|
140
|
|
|
// load the user defined attributes and add the callback mappings |
141
|
34 |
|
foreach ($eavUserDefinedAttributes as $eavAttribute) { |
142
|
|
|
// load attribute code and frontend input type |
143
|
1 |
|
$attributeCode = $eavAttribute[MemberNames::ATTRIBUTE_CODE]; |
144
|
1 |
|
$frontendInput = $eavAttribute[MemberNames::FRONTEND_INPUT]; |
145
|
|
|
|
146
|
|
|
// query whether or not the array for the mappings has been initialized |
147
|
1 |
|
if (!isset($this->callbackMappings[$attributeCode])) { |
148
|
1 |
|
$this->callbackMappings[$attributeCode] = array(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// set the appropriate callback mapping for the attributes input type |
152
|
1 |
|
if (isset($defaultFrontendInputCallbackMappings[$frontendInput])) { |
153
|
1 |
|
foreach ($defaultFrontendInputCallbackMappings[$frontendInput] as $defaultFrontendInputCallbackMapping) { |
154
|
1 |
|
$this->callbackMappings[$attributeCode][] = $defaultFrontendInputCallbackMapping; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// prepare the callbacks |
160
|
34 |
|
parent::setUp($serial); |
161
|
34 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Return's mapping for the supported backend types (for the product entity) => persist methods. |
165
|
|
|
* |
166
|
|
|
* @return array The mapping for the supported backend types |
167
|
|
|
*/ |
168
|
1 |
|
public function getBackendTypes() |
169
|
|
|
{ |
170
|
1 |
|
return $this->backendTypes; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Set's the attribute set of the product that has to be created. |
175
|
|
|
* |
176
|
|
|
* @param array $attributeSet The attribute set |
177
|
|
|
* |
178
|
|
|
* @return void |
179
|
|
|
*/ |
180
|
5 |
|
public function setAttributeSet(array $attributeSet) |
181
|
|
|
{ |
182
|
5 |
|
$this->attributeSet = $attributeSet; |
183
|
5 |
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Return's the attribute set of the product that has to be created. |
187
|
|
|
* |
188
|
|
|
* @return array The attribute set |
189
|
|
|
*/ |
190
|
1 |
|
public function getAttributeSet() |
191
|
|
|
{ |
192
|
1 |
|
return $this->attributeSet; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Cast's the passed value based on the backend type information. |
197
|
|
|
* |
198
|
|
|
* @param string $backendType The backend type to cast to |
199
|
|
|
* @param mixed $value The value to be casted |
200
|
|
|
* |
201
|
|
|
* @return mixed The casted value |
202
|
|
|
*/ |
203
|
21 |
|
public function castValueByBackendType($backendType, $value) |
204
|
|
|
{ |
205
|
|
|
|
206
|
|
|
// cast the value to a valid timestamp |
207
|
21 |
|
if ($backendType === BackendTypeKeys::BACKEND_TYPE_DATETIME) { |
208
|
2 |
|
return $this->getDateConverter()->convert($value); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
// cast the value to a string that represents the float/decimal value, because |
212
|
|
|
// PHP will cast float values implicitly to the system locales format when |
213
|
|
|
// rendering as string, e. g. with echo |
214
|
19 |
|
if ($backendType === BackendTypeKeys::BACKEND_TYPE_FLOAT || |
215
|
19 |
|
$backendType === BackendTypeKeys::BACKEND_TYPE_DECIMAL |
216
|
|
|
) { |
217
|
14 |
|
return (string) $this->getNumberConverter()->parse($value); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// cast the value to an integer |
221
|
5 |
|
if ($backendType === BackendTypeKeys::BACKEND_TYPE_INT) { |
222
|
3 |
|
return (integer) $value; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
// we don't need to cast strings |
226
|
2 |
|
return $value; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Return's the attribute set with the passed attribute set name. |
231
|
|
|
* |
232
|
|
|
* @param string $attributeSetName The name of the requested attribute set |
233
|
|
|
* |
234
|
|
|
* @return array The attribute set data |
235
|
|
|
* @throws \Exception Is thrown, if the attribute set or the given entity type with the passed name is not available |
236
|
|
|
*/ |
237
|
3 |
|
public function getAttributeSetByAttributeSetName($attributeSetName) |
238
|
|
|
{ |
239
|
|
|
|
240
|
|
|
// query whether or not attribute sets for the actualy entity type code are available |
241
|
3 |
|
if (isset($this->attributeSets[$entityTypeCode = $this->getEntityTypeCode()])) { |
242
|
|
|
// load the attribute sets for the actualy entity type code |
243
|
2 |
|
$attributSets = $this->attributeSets[$entityTypeCode]; |
244
|
|
|
|
245
|
|
|
// query whether or not, the requested attribute set is available |
246
|
2 |
|
if (isset($attributSets[$attributeSetName])) { |
247
|
1 |
|
return $attributSets[$attributeSetName]; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
// throw an exception, if not |
251
|
1 |
|
throw new \Exception( |
252
|
1 |
|
$this->appendExceptionSuffix( |
253
|
1 |
|
sprintf('Found invalid attribute set name "%s"', $attributeSetName) |
254
|
|
|
) |
255
|
|
|
); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
// throw an exception, if not |
259
|
1 |
|
throw new \Exception( |
260
|
1 |
|
$this->appendExceptionSuffix( |
261
|
1 |
|
sprintf('Found invalid entity type code "%s"', $entityTypeCode) |
262
|
|
|
) |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Return's the attributes for the attribute set of the product that has to be created. |
268
|
|
|
* |
269
|
|
|
* @return array The attributes |
270
|
|
|
* @throws \Exception Is thrown, if the attribute set or the given entity type with the passed name is not available |
271
|
|
|
*/ |
272
|
5 |
|
public function getAttributes() |
273
|
|
|
{ |
274
|
|
|
|
275
|
|
|
// query whether or not, the requested EAV attributes are available |
276
|
5 |
|
if (isset($this->attributes[$entityTypeCode = $this->getEntityTypeCode()])) { |
277
|
|
|
// load the attributes for the entity type code |
278
|
4 |
|
$attributes = $this->attributes[$entityTypeCode]; |
279
|
|
|
|
280
|
|
|
// query whether or not an attribute set has been loaded from the source file |
281
|
4 |
|
if (is_array($this->attributeSet) && isset($this->attributeSet[MemberNames::ATTRIBUTE_SET_NAME])) { |
282
|
|
|
// load the attribute set name |
283
|
4 |
|
$attributeSetName = $this->attributeSet[MemberNames::ATTRIBUTE_SET_NAME]; |
284
|
|
|
|
285
|
|
|
// query whether or not attributes for the actual attribute set name |
286
|
4 |
|
if ($attributeSetName && isset($attributes[$attributeSetName])) { |
287
|
3 |
|
return $attributes[$attributeSetName]; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
// throw an exception, if not |
291
|
1 |
|
throw new \Exception( |
292
|
1 |
|
$this->appendExceptionSuffix( |
293
|
1 |
|
sprintf('Found invalid attribute set name "%s"', $attributeSetName) |
294
|
|
|
) |
295
|
|
|
); |
296
|
|
|
} else { |
297
|
|
|
return call_user_func_array('array_merge', $attributes); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
// throw an exception, if not |
302
|
1 |
|
throw new \Exception( |
303
|
1 |
|
$this->appendExceptionSuffix( |
304
|
1 |
|
sprintf('Found invalid entity type code "%s"', $entityTypeCode) |
305
|
|
|
) |
306
|
|
|
); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Return's an array with the available user defined EAV attributes for the actual entity type. |
311
|
|
|
* |
312
|
|
|
* @return array The array with the user defined EAV attributes |
313
|
|
|
*/ |
314
|
34 |
|
public function getEavUserDefinedAttributes() |
315
|
|
|
{ |
316
|
|
|
|
317
|
|
|
// initialize the array with the user defined EAV attributes |
318
|
34 |
|
$eavUserDefinedAttributes = array(); |
319
|
|
|
|
320
|
|
|
// query whether or not user defined EAV attributes for the actualy entity type are available |
321
|
34 |
|
if (isset($this->userDefinedAttributes[$entityTypeCode = $this->getEntityTypeCode()])) { |
322
|
2 |
|
$eavUserDefinedAttributes = $this->userDefinedAttributes[$entityTypeCode]; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
// return the array with the user defined EAV attributes |
326
|
34 |
|
return $eavUserDefinedAttributes; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Return's the EAV attribute with the passed attribute code. |
331
|
|
|
* |
332
|
|
|
* @param string $attributeCode The attribute code |
333
|
|
|
* |
334
|
|
|
* @return array The array with the EAV attribute |
335
|
|
|
* @throws \Exception Is thrown if the attribute with the passed code is not available |
336
|
|
|
*/ |
337
|
2 |
|
public function getEavAttributeByAttributeCode($attributeCode) |
338
|
|
|
{ |
339
|
|
|
|
340
|
|
|
// load the attributes |
341
|
2 |
|
$attributes = $this->getAttributes(); |
342
|
|
|
|
343
|
|
|
// query whether or not the attribute exists |
344
|
2 |
|
if (isset($attributes[$attributeCode])) { |
345
|
1 |
|
return $attributes[$attributeCode]; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
// throw an exception if the requested attribute is not available |
349
|
1 |
|
throw new \Exception( |
350
|
1 |
|
$this->appendExceptionSuffix( |
351
|
1 |
|
sprintf('Can\'t load attribute with code "%s"', $attributeCode) |
352
|
|
|
) |
353
|
|
|
); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|