1 | <?php |
||
36 | class GenericValidatorObserver extends AbstractObserver |
||
37 | { |
||
38 | |||
39 | /** |
||
40 | * The registry processor instance. |
||
41 | * |
||
42 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
43 | */ |
||
44 | protected $registryProcessor; |
||
45 | |||
46 | /** |
||
47 | * Initializes the callback with the loader instance. |
||
48 | * |
||
49 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
50 | */ |
||
51 | public function __construct(RegistryProcessorInterface $registryProcessor) |
||
55 | |||
56 | /** |
||
57 | * Will be invoked by the action on the events the listener has been registered for. |
||
58 | * |
||
59 | * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance |
||
60 | * |
||
61 | * @return array The modified row |
||
62 | * @see \TechDivision\Import\Observers\ObserverInterface::handle() |
||
63 | */ |
||
64 | public function handle(SubjectInterface $subject) |
||
77 | |||
78 | /** |
||
79 | * Process the observer's business logic. |
||
80 | * |
||
81 | * @return void |
||
82 | */ |
||
83 | protected function process() |
||
84 | { |
||
85 | |||
86 | // load the available header names |
||
87 | $headerNames = array_keys($this->getHeaders()); |
||
88 | $emptyValueDefinition = $this->getEmptyAttributeValueConstant(); |
||
89 | // iterate over the custom validations |
||
90 | foreach ($headerNames as $headerName) { |
||
91 | // map the header name to the attribute code, if an mapping is available |
||
92 | $attributeCode = $this->mapAttributeCodeByHeaderMapping($headerName); |
||
93 | // load the attribute value from the row |
||
94 | $attributeValue = $this->getValue($attributeCode); |
||
95 | // load the callbacks for the actual attribute code |
||
96 | $callbacks = $this->getCallbacksByType($attributeCode); |
||
97 | // invoke the registered callbacks |
||
98 | foreach ($callbacks as $callback) { |
||
99 | try { |
||
100 | // query whether or not to cleanup complete attribute |
||
101 | if ($attributeValue === $emptyValueDefinition) { |
||
102 | continue; |
||
103 | } |
||
104 | $callback->handle($attributeCode, $attributeValue); |
||
105 | } catch (\InvalidArgumentException $iea) { |
||
106 | // add the the validation result to the status |
||
107 | $this->mergeStatus( |
||
108 | array( |
||
109 | RegistryKeys::VALIDATIONS => array( |
||
110 | basename($this->getFilename()) => array( |
||
111 | $this->getSubject()->getLineNumber() => array( |
||
112 | $attributeCode => $iea->getMessage() |
||
113 | ) |
||
114 | ) |
||
115 | ) |
||
116 | ) |
||
117 | ); |
||
118 | } |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | public function getEmptyAttributeValueConstant() |
||
127 | { |
||
128 | return $this->getSubject()->getConfiguration()->getConfiguration()->getEmptyAttributeValueConstant(); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Map the passed attribute code, if a header mapping exists and return the |
||
133 | * mapped mapping. |
||
134 | * |
||
135 | * @param string $attributeCode The attribute code to map |
||
136 | * |
||
137 | * @return string The mapped attribute code, or the original one |
||
138 | */ |
||
139 | protected function mapAttributeCodeByHeaderMapping($attributeCode) |
||
143 | |||
144 | /** |
||
145 | * Return's the array with callbacks for the passed type. |
||
146 | * |
||
147 | * @param string $type The type of the callbacks to return |
||
148 | * |
||
149 | * @return array The callbacks |
||
150 | */ |
||
151 | protected function getCallbacksByType($type) |
||
155 | } |
||
156 |