Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
89.86% |
62 / 69 |
|
58.33% |
7 / 12 |
CRAP | |
0.00% |
0 / 1 |
tao_models_classes_dataBinding_GenerisInstanceDataBinder | |
89.86% |
62 / 69 |
|
58.33% |
7 / 12 |
35.21 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
withServiceManager | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
withEventManager | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
forceModification | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTargetInstance | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
bind | |
97.87% |
46 / 47 |
|
0.00% |
0 / 1 |
17 | |||
isBlockedForModification | |
62.50% |
5 / 8 |
|
0.00% |
0 / 1 |
6.32 | |||
isEmptyValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getEventManager | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
getFeatureFlagChecker | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getOntology | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getServiceManager | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
19 | * (under the project TAO & TAO2); |
20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
21 | * (under the project TAO-TRANSFER); |
22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
24 | * 2022 (update and modification) Open Assessment Technologies SA; |
25 | */ |
26 | |
27 | use oat\generis\model\data\Ontology; |
28 | use oat\generis\model\OntologyRdf; |
29 | use oat\tao\model\dataBinding\AbstractDataBinder; |
30 | use oat\tao\model\dataBinding\GenerisInstanceDataBindingException; |
31 | use oat\tao\model\event\MetadataModified; |
32 | use oat\oatbox\event\EventManager; |
33 | use oat\oatbox\service\ServiceManager; |
34 | use oat\tao\model\featureFlag\FeatureFlagChecker; |
35 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
36 | |
37 | /** |
38 | * A data binder focusing on binding a source of data to a Generis instance |
39 | * |
40 | * @access public |
41 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
42 | * @package tao |
43 | |
44 | */ |
45 | class tao_models_classes_dataBinding_GenerisInstanceDataBinder extends tao_models_classes_dataBinding_AbstractDataBinder |
46 | { |
47 | /** @var core_kernel_classes_Resource */ |
48 | private $targetInstance; |
49 | |
50 | /** @var EventManager */ |
51 | private $eventManager; |
52 | |
53 | /** @var ServiceManager */ |
54 | private $serviceManager; |
55 | |
56 | /** @var bool */ |
57 | private $forceModification = false; |
58 | |
59 | /** |
60 | * Creates a new instance of binder. |
61 | * |
62 | * @access public |
63 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
64 | * @param Resource targetInstance The |
65 | * @return mixed |
66 | */ |
67 | public function __construct(core_kernel_classes_Resource $targetInstance) |
68 | { |
69 | $this->targetInstance = $targetInstance; |
70 | } |
71 | |
72 | public function withServiceManager(ServiceManager $serviceManager): void |
73 | { |
74 | $this->serviceManager = $serviceManager; |
75 | } |
76 | |
77 | public function withEventManager(EventManager $eventManager): void |
78 | { |
79 | $this->eventManager = $eventManager; |
80 | } |
81 | |
82 | public function forceModification(): void |
83 | { |
84 | $this->forceModification = true; |
85 | } |
86 | |
87 | /** |
88 | * Returns the target instance. |
89 | * |
90 | * @access protected |
91 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
92 | * @return core_kernel_classes_Resource |
93 | */ |
94 | protected function getTargetInstance() |
95 | { |
96 | return $this->targetInstance; |
97 | } |
98 | |
99 | /** |
100 | * Simply bind data from the source to a specific generis class instance. |
101 | * |
102 | * The array of the data to be bound must contain keys that are property |
103 | * The respective values can be either scalar or vector (array) values or |
104 | * values. |
105 | * |
106 | * - If the element of the $data array is scalar, it is simply bound using |
107 | * - If the element of the $data array is a vector, the property values are |
108 | * with the values of the vector. |
109 | * |
110 | * @access public |
111 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
112 | * @param array data An array of values where keys are Property URIs and values are either scalar or vector values. |
113 | * @throws tao_models_classes_dataBinding_GenerisInstanceDataBindingException |
114 | * @return mixed |
115 | */ |
116 | public function bind($data) |
117 | { |
118 | try { |
119 | $instance = $this->getTargetInstance(); |
120 | $eventManager = $this->getEventManager(); |
121 | foreach ($data as $propertyUri => $propertyValue) { |
122 | if ($propertyUri == OntologyRdf::RDF_TYPE) { |
123 | foreach ($instance->getTypes() as $type) { |
124 | $instance->removeType($type); |
125 | } |
126 | |
127 | $types = is_array($propertyValue) ? $propertyValue : [$propertyValue]; |
128 | |
129 | foreach ($types as $type) { |
130 | $instance->setType($this->getOntology()->getClass($type)); |
131 | } |
132 | continue; |
133 | } |
134 | |
135 | $prop = $this->getOntology()->getProperty($propertyUri); |
136 | |
137 | if ($this->isBlockedForModification($prop)) { |
138 | continue; |
139 | } |
140 | |
141 | $values = $instance->getPropertyValuesCollection($prop); |
142 | if ($values->count() > 0) { |
143 | if (is_array($propertyValue)) { |
144 | $instance->removePropertyValues($prop); |
145 | foreach ($propertyValue as $aPropertyValue) { |
146 | $instance->setPropertyValue( |
147 | $prop, |
148 | $aPropertyValue |
149 | ); |
150 | } |
151 | } elseif (is_string($propertyValue)) { |
152 | if ($this->isEmptyValue($propertyValue)) { |
153 | $instance->removePropertyValues($prop); |
154 | } else { |
155 | $instance->editPropertyValues( |
156 | $prop, |
157 | $propertyValue |
158 | ); |
159 | } |
160 | } |
161 | } else { |
162 | if (is_array($propertyValue)) { |
163 | foreach ($propertyValue as $aPropertyValue) { |
164 | $instance->setPropertyValue( |
165 | $prop, |
166 | $aPropertyValue |
167 | ); |
168 | } |
169 | } elseif (is_string($propertyValue) && !$this->isEmptyValue($propertyValue)) { |
170 | $instance->setPropertyValue( |
171 | $prop, |
172 | $propertyValue |
173 | ); |
174 | } |
175 | } |
176 | |
177 | $eventManager->trigger( |
178 | new MetadataModified($instance, $propertyUri, $propertyValue) |
179 | ); |
180 | } |
181 | |
182 | return $instance; |
183 | } catch (common_Exception $e) { |
184 | $msg = "An error occured while binding property values to instance '': " . $e->getMessage(); |
185 | throw new tao_models_classes_dataBinding_GenerisInstanceDataBindingException($msg); |
186 | } |
187 | } |
188 | |
189 | private function isBlockedForModification(core_kernel_classes_Property $property): bool |
190 | { |
191 | if ($this->forceModification) { |
192 | return false; |
193 | } |
194 | |
195 | if ( |
196 | $property->getWidget() |
197 | && $property->getWidget()->getUri() === tao_helpers_form_elements_Readonly::WIDGET_ID |
198 | ) { |
199 | return true; |
200 | } |
201 | |
202 | if ($this->getFeatureFlagChecker()->isEnabled('FEATURE_FLAG_STATISTIC_METADATA_IMPORT')) { |
203 | return $property->isStatistical(); |
204 | } |
205 | |
206 | return false; |
207 | } |
208 | |
209 | private function isEmptyValue(string $value): bool |
210 | { |
211 | return strlen(trim($value)) === 0; |
212 | } |
213 | |
214 | private function getEventManager(): EventManager |
215 | { |
216 | if (!isset($this->eventManager)) { |
217 | $this->eventManager = $this->getServiceManager()->get(EventManager::SERVICE_ID); |
218 | } |
219 | |
220 | return $this->eventManager; |
221 | } |
222 | |
223 | private function getFeatureFlagChecker(): FeatureFlagCheckerInterface |
224 | { |
225 | return $this->getServiceManager()->get(FeatureFlagChecker::class); |
226 | } |
227 | |
228 | private function getOntology(): Ontology |
229 | { |
230 | return $this->getServiceManager()->get(Ontology::SERVICE_ID); |
231 | } |
232 | |
233 | private function getServiceManager(): ServiceManager |
234 | { |
235 | if (!isset($this->serviceManager)) { |
236 | $this->serviceManager = ServiceManager::getServiceManager(); |
237 | } |
238 | |
239 | return $this->serviceManager; |
240 | } |
241 | } |