|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Observers\AdditionalAttributeObserver |
|
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
|
|
|
use TechDivision\Import\Utils\ColumnKeys; |
|
24
|
|
|
use TechDivision\Import\Subjects\SubjectInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Observer that prepares the additional product attribues found in the CSV file |
|
28
|
|
|
* in the row 'additional_attributes'. |
|
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
|
|
|
class AdditionalAttributeObserver extends AbstractObserver |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Will be invoked by the action on the events the listener has been registered for. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance |
|
43
|
|
|
* |
|
44
|
|
|
* @return array The modified row |
|
45
|
|
|
* @see \TechDivision\Import\Observers\ObserverInterface::handle() |
|
46
|
|
|
*/ |
|
47
|
2 |
|
public function handle(SubjectInterface $subject) |
|
48
|
|
|
{ |
|
49
|
|
|
|
|
50
|
|
|
// initialize the row |
|
51
|
2 |
|
$this->setSubject($subject); |
|
52
|
2 |
|
$this->setRow($subject->getRow()); |
|
53
|
|
|
|
|
54
|
|
|
// process the functionality and return the row |
|
55
|
2 |
|
$this->process(); |
|
56
|
|
|
|
|
57
|
|
|
// return the processed row |
|
58
|
2 |
|
return $this->getRow(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Process the observer's business logic. |
|
63
|
|
|
* |
|
64
|
|
|
* @return array The processed row |
|
65
|
|
|
*/ |
|
66
|
2 |
|
protected function process() |
|
67
|
|
|
{ |
|
68
|
|
|
|
|
69
|
|
|
// query whether or not the row has additional attributes |
|
70
|
2 |
|
if ($additionalAttributes = $this->getValue(ColumnKeys::ADDITIONAL_ATTRIBUTES)) { |
|
71
|
|
|
// load the subject instance |
|
72
|
2 |
|
$subject = $this->getSubject(); |
|
73
|
|
|
// explode the additional attributes |
|
74
|
2 |
|
$additionalAttributes = $subject->explode($additionalAttributes, $this->getMultipleFieldDelimiter()); |
|
75
|
|
|
// iterate over the attributes and append them to the row |
|
76
|
2 |
|
foreach ($additionalAttributes as $additionalAttribute) { |
|
77
|
|
|
// initialize the option value |
|
78
|
2 |
|
$optionValue = ''; |
|
79
|
|
|
// explode the attribute code/option value from the attribute |
|
80
|
2 |
|
$exploded = $subject->explode($additionalAttribute, '='); |
|
81
|
|
|
// initialize attribute code and option value, depending on what we've exploded |
|
82
|
2 |
|
if (sizeof($exploded) < 1) { |
|
83
|
|
|
continue; |
|
84
|
2 |
|
} elseif (sizeof($exploded) === 1) { |
|
85
|
|
|
list ($attributeCode) = $exploded; |
|
86
|
|
|
} else { |
|
87
|
2 |
|
list ($attributeCode, $optionValue) = $exploded; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// try to load the appropriate key for the value |
|
91
|
2 |
|
if ($subject->hasHeader($attributeCode) === false) { |
|
92
|
2 |
|
$subject->addHeader($attributeCode); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// append/replace the attribute value |
|
96
|
2 |
|
$this->setValue($attributeCode, $optionValue); |
|
97
|
|
|
|
|
98
|
|
|
// add a log message in debug mod |
|
99
|
2 |
|
if ($subject->isDebugMode()) { |
|
100
|
2 |
|
$subject->getSystemLogger()->debug( |
|
101
|
2 |
|
sprintf( |
|
102
|
2 |
|
'Extract new column "%s" with value "%s" from column "%s" in file %s on line %d', |
|
103
|
|
|
$attributeCode, |
|
104
|
|
|
$optionValue, |
|
105
|
2 |
|
ColumnKeys::ADDITIONAL_ATTRIBUTES, |
|
106
|
2 |
|
$subject->getFilename(), |
|
107
|
2 |
|
$subject->getLineNumber() |
|
108
|
|
|
) |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
2 |
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|