Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.00% |
19 / 20 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
ResourceMetadataPopulateService | |
95.00% |
19 / 20 |
|
66.67% |
2 / 3 |
9 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addMetadata | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
populate | |
94.12% |
16 / 17 |
|
0.00% |
0 / 1 |
7.01 |
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) 2024 (original work) Open Assessment Technologies SA; |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\tao\model\Translation\Service; |
24 | |
25 | use core_kernel_classes_Literal; |
26 | use core_kernel_classes_Resource; |
27 | use oat\generis\model\data\Ontology; |
28 | use oat\generis\model\OntologyRdf; |
29 | use oat\tao\model\Translation\Entity\AbstractResource; |
30 | |
31 | class ResourceMetadataPopulateService |
32 | { |
33 | private Ontology $ontology; |
34 | private array $metadata; |
35 | |
36 | public function __construct(Ontology $ontology) |
37 | { |
38 | $this->ontology = $ontology; |
39 | } |
40 | |
41 | public function addMetadata(string $resourceType, string $metadataUri): void |
42 | { |
43 | $this->metadata[$resourceType] ??= []; |
44 | $this->metadata[$resourceType] = array_unique(array_merge($this->metadata[$resourceType], [$metadataUri])); |
45 | } |
46 | |
47 | public function populate(AbstractResource $resource, core_kernel_classes_Resource $originResource): void |
48 | { |
49 | $valueProperty = $this->ontology->getProperty(OntologyRdf::RDF_VALUE); |
50 | |
51 | $parentClasses = $originResource->getParentClassesIds(); |
52 | $resourceType = array_pop($parentClasses); |
53 | |
54 | foreach ($this->metadata[$resourceType] ?? [] as $metadataUri) { |
55 | $resource->addMetadataUri($metadataUri); |
56 | } |
57 | |
58 | foreach ($resource->getMetadataUris() as $metadataUri) { |
59 | $values = $originResource->getPropertyValues($this->ontology->getProperty($metadataUri)); |
60 | |
61 | if (empty($values)) { |
62 | continue; |
63 | } |
64 | |
65 | $literal = null; |
66 | $value = empty($values) ? null : current($values); |
67 | |
68 | if ($value) { |
69 | $literalProperty = $this->ontology->getProperty($value); |
70 | $oneValue = $literalProperty->getOnePropertyValue($valueProperty); |
71 | |
72 | if ($oneValue instanceof core_kernel_classes_Literal) { |
73 | $literal = $oneValue->literal; |
74 | } |
75 | } |
76 | |
77 | $resource->addMetadata($metadataUri, $value, $literal); |
78 | } |
79 | } |
80 | } |