Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
GenericLomOntologyClassificationInjector
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
4 / 4
17
100.00% covered (success)
100.00%
1 / 1
 inject
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
5
 savePropertyValue
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 groupValuesByLgAndProperty
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
7
 assertIsResource
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
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) 2017-2022 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 */
20
21namespace oat\taoQtiItem\model\qti\metadata\ontology;
22
23use oat\generis\model\OntologyAwareTrait;
24use oat\taoQtiItem\model\qti\metadata\MetadataInjectionException;
25use oat\taoQtiItem\model\qti\metadata\MetadataInjector;
26use oat\taoQtiItem\model\qti\metadata\MetadataValue;
27
28class GenericLomOntologyClassificationInjector implements MetadataInjector
29{
30    use OntologyAwareTrait;
31
32    /**
33     * Inject dynamically a metadata to an item property
34     *
35     * @param mixed $target
36     * @param array $values
37     * @throws MetadataInjectionException
38     */
39    public function inject($target, array $values)
40    {
41        $this->assertIsResource($target);
42
43        /** @var \core_kernel_classes_Class $targetClass */
44        $types = $target->getTypes();
45        $targetClass = reset($types);
46        $classProperties = $targetClass->getProperties(true);
47
48        $properties = [];
49        foreach ($classProperties as $property) {
50            $properties[] = $property->getUri();
51        }
52        $properties = array_diff($properties, GenericLomOntologyClassificationExtractor::$excludedProperties);
53
54        $newValues = $this->groupValuesByLgAndProperty($properties, $values);
55
56        foreach ($newValues as $langCode => $perLangProperties) {
57            foreach ($perLangProperties as $valuePath => $values) {
58                $property = $this->getProperty($valuePath);
59                foreach ($values as $value) {
60                    $this->savePropertyValue($target, $property, $value, $langCode);
61                }
62            }
63        }
64    }
65
66    private function savePropertyValue(
67        \core_kernel_classes_Resource $target,
68        \core_kernel_classes_Property $property,
69        $value,
70        $langCode
71    ): void {
72        $previousValues = $target->getPropertyValuesByLg($property, $langCode);
73
74        if ($previousValues->count() > 0 && $property->isMultiple()) {
75            $target->setPropertyValueByLg($property, $value, $langCode);
76
77            return;
78        }
79
80        $target->editPropertyValueByLg($property, $value, $langCode);
81    }
82
83    private function groupValuesByLgAndProperty(
84        array $propertyURIs,
85        array $values
86    ): array {
87        $newPropertyValues = [];
88
89        foreach ($values as $metadataValues) {
90            /** @var MetadataValue $metadataValue */
91            foreach ($metadataValues as $metadataValue) {
92                $lang = $metadataValue->getLanguage() ?: DEFAULT_LANG;
93                $path = $metadataValue->getPath();
94                $valuePath = trim((string)end($path));
95
96                if (in_array($valuePath, $propertyURIs)) {
97                    if (!isset($newPropertyValues[$lang])) {
98                        $newPropertyValues[$lang] = [];
99                    }
100
101                    if (!isset($newPropertyValues[$lang][$valuePath])) {
102                        $newPropertyValues[$lang][$valuePath] = [];
103                    }
104
105                    $newPropertyValues[$lang][$valuePath][] = $metadataValue->getValue();
106                }
107            }
108        }
109
110        return $newPropertyValues;
111    }
112
113    /**
114     * @throws MetadataInjectionException
115     */
116    private function assertIsResource($target): void
117    {
118        if (!$target instanceof \core_kernel_classes_Resource) {
119            throw new MetadataInjectionException(
120                'The given target is not an instance of core_kernel_classes_Resource.'
121            );
122        }
123    }
124}