|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Observers\DynamicAttributeLoader |
|
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\Observers; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Loader implementation that dynamically creates an array with columns from the CSV file that contains values. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Tim Wagner <[email protected]> |
|
27
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
28
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
29
|
|
|
* @link https://github.com/techdivision/import |
|
30
|
|
|
* @link http://www.techdivision.com |
|
31
|
|
|
*/ |
|
32
|
|
|
class DynamicAttributeLoader implements AttributeLoaderInterface |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
|
37
|
|
|
* |
|
38
|
|
|
* Second parameter has to be an associative array with the member name as key |
|
39
|
|
|
* and an array that contains the column name and the data's backend type, e. g. |
|
40
|
|
|
* |
|
41
|
|
|
* <code>array('min_qty => array('out_of_stock_qty', 'float'))</code> |
|
42
|
|
|
* |
|
43
|
|
|
* @param \TechDivision\Import\Observers\DynamicAttributeObserverInterface $observer The observer to load the attributes for |
|
44
|
|
|
* @param array $columns The array with the possible columns to load the data from |
|
45
|
|
|
* |
|
46
|
|
|
* @return array The prepared attributes |
|
47
|
|
|
* @throws \Exception Is thrown, if the size of the option values doesn't equals the size of swatch values, in case |
|
48
|
|
|
*/ |
|
49
|
|
|
public function load(DynamicAttributeObserverInterface $observer, array $columns) |
|
50
|
|
|
{ |
|
51
|
|
|
|
|
52
|
|
|
// initialize the array for the attributes |
|
53
|
|
|
$attr = array(); |
|
54
|
|
|
|
|
55
|
|
|
// iterate over the possible columns and handle the data |
|
56
|
|
|
foreach ($columns as $memberName => $metadata) { |
|
57
|
|
|
// extract column name and backend type from the metadata |
|
58
|
|
|
list ($columnName, $backendType) = $metadata; |
|
59
|
|
|
// query whether or not, the column is available in the CSV file |
|
60
|
|
|
if ($observer->hasHeader($columnName)) { |
|
61
|
|
|
// query whether or not a column contains a value |
|
62
|
|
|
if ($observer->hasValue($columnName)) { |
|
63
|
|
|
$attr[$memberName] = $observer->castValueByBackendType($backendType, $observer->getValue($columnName)); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// return the prepared product |
|
69
|
|
|
return $attr; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|