Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
18 / 21
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CustomPropertiesManifestScanner
85.71% covered (warning)
85.71%
18 / 21
66.67% covered (warning)
66.67%
2 / 3
8.19
0.00% covered (danger)
0.00%
0 / 1
 getCustomPropertyByUri
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 getCustomProperties
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 registerAllNamespaces
72.73% covered (warning)
72.73%
8 / 11
0.00% covered (danger)
0.00%
0 / 1
6.73
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) 2025 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoQtiItem\model\qti\metadata\exporter;
24
25use DOMDocument;
26use DOMNodeList;
27use DOMXPath;
28
29class CustomPropertiesManifestScanner
30{
31    public function getCustomPropertyByUri(DOMDocument $manifest, string $scaleUri): DOMNodeList
32    {
33        $xpath = new DOMXPath($manifest);
34        $this->registerAllNamespaces($xpath, $manifest);
35        $query = sprintf(
36            '//*[local-name()="customProperties"]/*[local-name()="property"][*[local-name()="uri"]="%s"]',
37            $scaleUri
38        );
39
40        return $xpath->query($query);
41    }
42
43    public function getCustomProperties(DOMDocument $manifest): DOMNodeList
44    {
45        $xpath = new DOMXPath($manifest);
46        $this->registerAllNamespaces($xpath, $manifest);
47        return $xpath->query('//*//*[local-name()="customProperties"]');
48    }
49
50    private function registerAllNamespaces(DOMXPath $xpath, DOMDocument $manifest)
51    {
52        $rootElement = $manifest->documentElement;
53        if (!$rootElement) {
54            return;
55        }
56
57        // Register default namespace
58        $defaultNamespace = $rootElement->lookupNamespaceUri(null);
59        if ($defaultNamespace) {
60            $xpath->registerNamespace('default', $defaultNamespace);
61        }
62
63        // Register all namespaces
64        if ($rootElement->hasAttributes()) {
65            foreach ($rootElement->attributes as $attribute) {
66                if (strpos($attribute->nodeName, 'xmlns:') === 0) {
67                    $prefix = substr($attribute->nodeName, 6);
68                    $xpath->registerNamespace($prefix, $attribute->nodeValue);
69                }
70            }
71        }
72    }
73}