Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
ItemMetadataGuardian | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |
100.00% |
1 / 1 |
guard | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 |
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) 2021 (original work) Open Assessment Technologies SA; |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\taoQtiItem\model\qti\metadata\guardians; |
24 | |
25 | use oat\tao\model\TaoOntology; |
26 | use oat\generis\model\OntologyAwareTrait; |
27 | use oat\oatbox\service\ConfigurableService; |
28 | use oat\taoQtiItem\model\qti\metadata\MetadataValue; |
29 | use oat\taoQtiItem\model\qti\metadata\MetadataGuardian as MetadataGuardianInterface; |
30 | |
31 | class ItemMetadataGuardian extends ConfigurableService implements MetadataGuardianInterface |
32 | { |
33 | use OntologyAwareTrait; |
34 | |
35 | public const SERVICE_ID = 'taoQtiItem/ItemMetadataGuardian'; |
36 | |
37 | /** Path to find metadata value */ |
38 | public const OPTION_EXPECTED_PATH = 'expectedPath'; |
39 | |
40 | /** The property URI whose value need to check */ |
41 | public const OPTION_PROPERTY_URI = 'propertyUri'; |
42 | |
43 | public function guard(array $metadataValues) |
44 | { |
45 | $expectedPath = $this->getOption(self::OPTION_EXPECTED_PATH, []); |
46 | $propertyUri = $this->getOption(self::OPTION_PROPERTY_URI, ''); |
47 | $class = $this->getClass(TaoOntology::CLASS_URI_ITEM); |
48 | |
49 | /** @var MetadataValue $metadataValue */ |
50 | foreach ($metadataValues as $metadataValue) { |
51 | if ($metadataValue->getPath() === $expectedPath) { |
52 | $instances = $class->searchInstances( |
53 | [$propertyUri => $metadataValue->getValue()], |
54 | ['like' => false, 'recursive' => true] |
55 | ); |
56 | |
57 | if (!empty($instances)) { |
58 | return reset($instances); |
59 | } |
60 | } |
61 | } |
62 | |
63 | return false; |
64 | } |
65 | } |