Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ResourceMetadataRetriever
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getProperties
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 getPropertyValues
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
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
21declare(strict_types=1);
22
23namespace oat\taoQtiItem\model\metadata;
24
25use core_kernel_classes_Resource as Resource;
26use core_kernel_classes_Triple as Triple;
27use oat\generis\model\data\Ontology;
28use oat\generis\model\OntologyRdf;
29use oat\generis\model\OntologyRdfs;
30use oat\taoItems\model\TaoItemOntology;
31
32class ResourceMetadataRetriever
33{
34    private const IGNORED_PROPERTIES = [
35        TaoItemOntology::PROPERTY_ITEM_CONTENT,
36        TaoItemOntology::PROPERTY_ITEM_MODEL,
37        OntologyRdf::RDF_TYPE,
38        OntologyRdfs::RDFS_LABEL
39    ];
40    private Ontology $ontology;
41
42    public function __construct(Ontology $ontology)
43    {
44        $this->ontology = $ontology;
45    }
46
47    /**
48     * Will retrieve properties and their values for a given resource
49     */
50    public function getProperties(Resource $resource): array
51    {
52        $properties = [];
53
54        /** @var Triple $triple */
55        foreach ($resource->getRdfTriples() as $triple) {
56            if (in_array($triple->predicate, self::IGNORED_PROPERTIES)) {
57                continue;
58            }
59
60            $property = $this->ontology->getProperty($triple->predicate);
61
62            if ($property->isProperty() !== true) {
63                continue;
64            }
65
66            $properties[$property->getLabel()] = $this->getPropertyValues($resource->getPropertyValues($property));
67        }
68
69        return $properties;
70    }
71
72    private function getPropertyValues(array $propertyValues): array
73    {
74        $values = [];
75        foreach ($propertyValues as $propertyValue) {
76            $values[] = $this->ontology->getResource($propertyValue)->exists()
77                ? $this->ontology->getResource($propertyValue)->getLabel()
78                : $propertyValue;
79        }
80        return $values;
81    }
82}