|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Observers\GenericValidationObserver |
|
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 |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Observers; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\RegistryKeys; |
|
24
|
|
|
use TechDivision\Import\Subjects\SubjectInterface; |
|
25
|
|
|
use TechDivision\Import\Services\RegistryProcessorInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Observer that invokes the callbacks to validate the actual row. |
|
29
|
|
|
* |
|
30
|
|
|
* @author Tim Wagner <[email protected]> |
|
31
|
|
|
* @copyright 2021 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 GenericValidationObserver extends AbstractObserver implements ObserverFactoryInterface |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The registry processor instance. |
|
41
|
|
|
* |
|
42
|
|
|
* @var \TechDivision\Import\Services\RegistryProcessorInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $registryProcessor; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Array with virtual column name mappings (this is a temporary |
|
48
|
|
|
* solution till techdivision/import#179 as been implemented). |
|
49
|
|
|
* |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $reverseHeaderMappings = array(); |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Initializes the observer with the registry processor instance. |
|
56
|
|
|
* |
|
57
|
|
|
* @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct(RegistryProcessorInterface $registryProcessor) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->registryProcessor = $registryProcessor; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Will be invoked by the observer visitor when a factory has been defined to create the observer instance. |
|
66
|
|
|
* |
|
67
|
|
|
* @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance |
|
68
|
|
|
* |
|
69
|
|
|
* @return \TechDivision\Import\Observers\ObserverInterface The observer instance |
|
70
|
|
|
*/ |
|
71
|
|
|
public function createObserver(SubjectInterface $subject) |
|
72
|
|
|
{ |
|
73
|
|
|
|
|
74
|
|
|
// initialize the array for the reverse header mappings |
|
75
|
|
|
$this->reverseHeaderMappings = array_flip($subject->getHeaderMappings()); |
|
76
|
|
|
|
|
77
|
|
|
// return the intialized instance |
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Will be invoked by the action on the events the listener has been registered for. |
|
83
|
|
|
* |
|
84
|
|
|
* @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance |
|
85
|
|
|
* |
|
86
|
|
|
* @return array The modified row |
|
87
|
|
|
* @see \TechDivision\Import\Observers\ObserverInterface::handle() |
|
88
|
|
|
*/ |
|
89
|
|
|
public function handle(SubjectInterface $subject) |
|
90
|
|
|
{ |
|
91
|
|
|
|
|
92
|
|
|
// initialize the row |
|
93
|
|
|
$this->setSubject($subject); |
|
94
|
|
|
$this->setRow($subject->getRow()); |
|
95
|
|
|
|
|
96
|
|
|
// process the functionality and return the row |
|
97
|
|
|
$this->process(); |
|
98
|
|
|
|
|
99
|
|
|
// return the processed row |
|
100
|
|
|
return $this->getRow(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Process the observer's business logic. |
|
105
|
|
|
* |
|
106
|
|
|
* @return array The processed row |
|
107
|
|
|
*/ |
|
108
|
|
View Code Duplication |
protected function process() |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
|
|
111
|
|
|
// load the available header names |
|
112
|
|
|
$headerNames = array_keys($this->getHeaders()); |
|
113
|
|
|
|
|
114
|
|
|
// iterate over the custom validations |
|
115
|
|
|
foreach ($headerNames as $headerName) { |
|
116
|
|
|
// load the attribute value from the row |
|
117
|
|
|
$attributeValue = $this->getValue($headerName); |
|
118
|
|
|
// reverse map the header name to the original column name |
|
119
|
|
|
$columnName = $this->reverseMapHeaderNameToColumnName($headerName); |
|
120
|
|
|
// load the callbacks for the actual attribute code |
|
121
|
|
|
$callbacks = $this->getCallbacksByType($columnName); |
|
122
|
|
|
// invoke the registered callbacks |
|
123
|
|
|
foreach ($callbacks as $callback) { |
|
124
|
|
|
try { |
|
125
|
|
|
$callback->handle($columnName, $attributeValue); |
|
126
|
|
|
} catch (\InvalidArgumentException $iea) { |
|
127
|
|
|
// add the the validation result to the status |
|
128
|
|
|
$this->mergeStatus( |
|
129
|
|
|
array( |
|
130
|
|
|
RegistryKeys::VALIDATIONS => array( |
|
131
|
|
|
basename($this->getFilename()) => array( |
|
132
|
|
|
$this->getSubject()->getLineNumber() => array( |
|
133
|
|
|
$columnName => $iea->getMessage() |
|
134
|
|
|
) |
|
135
|
|
|
) |
|
136
|
|
|
) |
|
137
|
|
|
) |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Reverse map the passed header name, to the original column name. |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $headerName The header name to reverse map |
|
148
|
|
|
* |
|
149
|
|
|
* @return string The original column name |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function reverseMapHeaderNameToColumnName(string $headerName) : string |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->reverseHeaderMappings[$headerName] ?? $headerName; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Return's the array with callbacks for the passed type. |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $type The type of the callbacks to return |
|
160
|
|
|
* |
|
161
|
|
|
* @return array The callbacks |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function getCallbacksByType($type) |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->getSubject()->getCallbacksByType($type); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.