Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
27 / 27 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
MetaMetadataExtractor | |
100.00% |
27 / 27 |
|
100.00% |
1 / 1 |
5 | |
100.00% |
1 / 1 |
extract | |
100.00% |
27 / 27 |
|
100.00% |
1 / 1 |
5 |
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\taoQtiItem\model\qti\metadata\imsManifest; |
24 | |
25 | use DOMDocument; |
26 | use DOMXPath; |
27 | use InvalidArgumentException; |
28 | |
29 | class MetaMetadataExtractor |
30 | { |
31 | private const NAMESPACE_LOM = 'http://ltsc.ieee.org/xsd/LOM'; |
32 | private const NAMESPACE_DEFAULT = 'http://www.imsglobal.org/xsd/imscp_v1p1'; |
33 | // phpcs:disable Generic.Files.LineLength |
34 | private const META_METADATA_PROPERTIES_QUERY_STRING = '//imsmd:metaMetadata/default:extension/default:customProperties/default:property'; |
35 | // phpcs:enable Generic.Files.LineLength |
36 | |
37 | public function extract($manifest): array |
38 | { |
39 | if ($manifest instanceof DOMDocument === false) { |
40 | throw new InvalidArgumentException( |
41 | __('Metadata import requires an instance of DomManifest to extract metadata') |
42 | ); |
43 | } |
44 | |
45 | $xpath = new DOMXPath($manifest); |
46 | $xpath->registerNamespace('imsmd', self::NAMESPACE_LOM); |
47 | $xpath->registerNamespace('default', self::NAMESPACE_DEFAULT); |
48 | |
49 | $metaMetadata = []; |
50 | |
51 | $properties = $xpath->query(self::META_METADATA_PROPERTIES_QUERY_STRING); |
52 | foreach ($properties as $property) { |
53 | $uri = $xpath->evaluate('string(default:uri)', $property); |
54 | $alias = $xpath->evaluate('string(default:alias)', $property); |
55 | $label = $xpath->evaluate('string(default:label)', $property); |
56 | $multiple = $xpath->evaluate('string(default:multiple)', $property); |
57 | $checksum = $xpath->evaluate('string(default:checksum)', $property); |
58 | $widget = $xpath->evaluate('string(default:widget)', $property); |
59 | |
60 | if (strlen($uri) === 0 || strlen($label) === 0) { |
61 | continue; |
62 | } |
63 | |
64 | $metaMetadata[] = [ |
65 | 'uri' => trim($uri), |
66 | 'alias' => trim($alias), |
67 | 'label' => trim($label), |
68 | 'multiple' => trim($multiple), |
69 | 'checksum' => trim($checksum), |
70 | 'widget' => trim($widget), |
71 | ]; |
72 | } |
73 | |
74 | return $metaMetadata; |
75 | } |
76 | } |