Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.30% covered (success)
96.30%
26 / 27
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
TransformationService
96.30% covered (success)
96.30%
26 / 27
80.00% covered (warning)
80.00%
4 / 5
17
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 transformChildren
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
8.03
 transformAttributes
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
 createQtiElementName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 camelToHyphen
100.00% covered (success)
100.00%
4 / 4
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\taoQtiItem\model\Export\Qti3Package;
24
25use DOMDocument;
26use DOMElement;
27
28class TransformationService
29{
30    private Qti3XsdValidator $validator;
31
32    public function __construct(Qti3XsdValidator $validator)
33    {
34        $this->validator = $validator;
35    }
36
37    public function transformChildren(DOMElement $oldElement, DOMElement $newParent, DOMDocument $newDom): void
38    {
39        foreach ($oldElement->childNodes as $child) {
40            if ($child instanceof DOMElement) {
41                $newName = $this->createQtiElementName($child->nodeName);
42
43                if (!$this->validator->isQtiElementName($newName)) {
44                    $newName = $child->nodeName;
45                }
46
47                $newElement = $newDom->createElement($newName);
48
49                $this->transformAttributes($child, $newElement);
50
51                if ($child->childNodes->length === 1 && $child->firstChild->nodeType === XML_TEXT_NODE) {
52                    $newElement->textContent = $child->textContent;
53                } else {
54                    $this->transformChildren($child, $newElement, $newDom);
55                }
56
57                $newParent->appendChild($newElement);
58            } elseif ($child->nodeType === XML_TEXT_NODE && trim($child->nodeValue) !== '') {
59                $newParent->appendChild($newDom->createTextNode($child->nodeValue));
60            }
61        }
62    }
63
64    public function transformAttributes(DOMElement $sourceElement, DOMElement $targetElement): void
65    {
66        if (!$sourceElement->hasAttributes()) {
67            return;
68        }
69
70        foreach ($sourceElement->attributes as $attribute) {
71            if (
72                !str_starts_with($attribute->nodeName, 'xmlns')
73                && $attribute->nodeName !== 'xsi:schemaLocation'
74            ) {
75                $attrName = $this->camelToHyphen($attribute->nodeName);
76                if (!empty($attrName)) {
77                    $targetElement->setAttribute($attrName, $attribute->value);
78                }
79            }
80        }
81    }
82
83    public function createQtiElementName(string $nodeName): string
84    {
85        return sprintf('qti-%s', $this->camelToHyphen($nodeName));
86    }
87
88    private function camelToHyphen(string $string): string
89    {
90        $string = preg_replace('/([a-z])([A-Z])/', '$1-$2', $string);
91        $string = str_replace('_', '-', $string);
92        $string = strtolower($string);
93        return ltrim($string, '-');
94    }
95}