Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.65% covered (warning)
80.65%
25 / 31
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
MetadataLomService
80.65% covered (warning)
80.65%
25 / 31
50.00% covered (danger)
50.00%
1 / 2
8.46
0.00% covered (danger)
0.00%
0 / 1
 addPropertiesToMetadataBlock
76.92% covered (warning)
76.92%
20 / 26
0.00% covered (danger)
0.00%
0 / 1
7.60
 getCustomProperties
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
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\taoQtiTest\models\classes\metadata;
24
25use DOMDocument;
26use DOMNodeList;
27use DOMXPath;
28
29class MetadataLomService
30{
31    public function addPropertiesToMetadataBlock(array $properties, DOMDocument $manifest): void
32    {
33        /** @var DOMNodeList $metadataBlock */
34        $metadataBlock = $manifest->getElementsByTagName('metadata');
35
36        if ($metadataBlock === null) {
37            $metadataBlock = $manifest->createElement('metadata');
38            $manifest->documentElement->appendChild($metadataBlock);
39        }
40
41        $customProperties = $this->getCustomProperties($manifest);
42
43        // If there is no custom properties means that it is initial loop and we need to create customProperties node
44        if (empty($customProperties)) {
45            $metadataBlock->item(0)
46                ->appendChild($manifest->createElement('imsmd:lom'))
47                ->appendChild($manifest->createElement('imsmd:metaMetadata'))
48                ->appendChild($manifest->createElement('extension'))
49                ->appendChild($manifest->createElement('customProperties'));
50        }
51
52        foreach ($properties as $property) {
53            $propertyExists = false;
54            if (!empty($customProperties)) {
55                //Check if custom property DomElement array contain object where nodeValue is equal to $property['uri']
56                $propertyExists = !empty(array_filter($customProperties, function ($customProperty) use ($property) {
57                    return $customProperty->nodeValue === $property['uri'];
58                }));
59            }
60
61            if ($propertyExists) {
62                continue;
63            }
64
65            $propertyNode = $manifest->createElement('property');
66            foreach ($property as $key => $value) {
67                $propertyNode->appendChild($manifest->createElement($key, $value));
68            }
69            $metadataBlock->item(0)
70                ->getElementsByTagName('customProperties')
71                ->item(0)
72                ->appendChild($propertyNode);
73        }
74    }
75
76    private function getCustomProperties(DOMDocument $manifest): array
77    {
78        $xpath = new DOMXPath($manifest);
79        $xpath->registerNamespace('manifest', 'http://www.imsglobal.org/xsd/imscp_v1p1');
80        $xpath->registerNamespace('imsmd', 'http://ltsc.ieee.org/xsd/LOM');
81
82        $customProperties = $xpath->query('//customProperties/property/uri');
83        return iterator_to_array($customProperties);
84    }
85}