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