Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
76.74% |
33 / 43 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
TransformationService | |
76.74% |
33 / 43 |
|
66.67% |
4 / 6 |
28.09 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
transformChildren | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
8.03 | |||
transformAttributes | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
6 | |||
createQtiElementName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
textInteractionAttributeTransformation | |
40.00% |
6 / 15 |
|
0.00% |
0 / 1 |
10.40 | |||
camelToHyphen | |
100.00% |
4 / 4 |
|
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) 2025 (original work) Open Assessment Technologies SA; |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\taoQtiItem\model\Export\Qti3Package; |
24 | |
25 | use DOMAttr; |
26 | use DOMDocument; |
27 | use DOMElement; |
28 | |
29 | class TransformationService |
30 | { |
31 | private Qti3XsdValidator $validator; |
32 | |
33 | public function __construct(Qti3XsdValidator $validator) |
34 | { |
35 | $this->validator = $validator; |
36 | } |
37 | |
38 | public function transformChildren(DOMElement $oldElement, DOMElement $newParent, DOMDocument $newDom): void |
39 | { |
40 | foreach ($oldElement->childNodes as $child) { |
41 | if ($child instanceof DOMElement) { |
42 | $newName = $this->createQtiElementName($child->nodeName); |
43 | |
44 | if (!$this->validator->isQtiElementName($newName)) { |
45 | $newName = $child->nodeName; |
46 | } |
47 | |
48 | $newElement = $newDom->createElement($newName); |
49 | |
50 | $this->transformAttributes($child, $newElement); |
51 | |
52 | if ($child->childNodes->length === 1 && $child->firstChild->nodeType === XML_TEXT_NODE) { |
53 | $newElement->textContent = $child->textContent; |
54 | } else { |
55 | $this->transformChildren($child, $newElement, $newDom); |
56 | } |
57 | |
58 | $newParent->appendChild($newElement); |
59 | } elseif ($child->nodeType === XML_TEXT_NODE && trim($child->nodeValue) !== '') { |
60 | $newParent->appendChild($newDom->createTextNode($child->nodeValue)); |
61 | } |
62 | } |
63 | } |
64 | |
65 | public function transformAttributes(DOMElement $sourceElement, DOMElement $targetElement): void |
66 | { |
67 | if (!$sourceElement->hasAttributes()) { |
68 | return; |
69 | } |
70 | |
71 | foreach ($sourceElement->attributes as $attribute) { |
72 | if ( |
73 | !str_starts_with($attribute->nodeName, 'xmlns') |
74 | && $attribute->nodeName !== 'xsi:schemaLocation' |
75 | ) { |
76 | $attrName = $this->camelToHyphen($attribute->nodeName); |
77 | if (!empty($attrName)) { |
78 | $targetElement->setAttribute($attrName, $attribute->value); |
79 | } |
80 | } |
81 | |
82 | $this->textInteractionAttributeTransformation($attribute); |
83 | } |
84 | } |
85 | |
86 | public function createQtiElementName(string $nodeName): string |
87 | { |
88 | return sprintf('qti-%s', $this->camelToHyphen($nodeName)); |
89 | } |
90 | |
91 | public function textInteractionAttributeTransformation(DOMAttr $node): void |
92 | { |
93 | $parent = $node->parentNode; |
94 | $classAttribute = $parent->attributes->getNamedItem('class'); |
95 | |
96 | switch ($node->nodeName) { |
97 | case 'expectedLength': |
98 | $classValue = 'qti-input-width-' . $node->nodeValue; |
99 | $parent->removeAttribute('expectedLength'); |
100 | break; |
101 | case 'expectedLines': |
102 | $classValue = 'qti-input-height-' . $node->nodeValue; |
103 | $parent->removeAttribute('expectedLines'); |
104 | break; |
105 | default: |
106 | return; |
107 | } |
108 | |
109 | if ($classAttribute === null) { |
110 | $parent->setAttribute('class', $classValue); |
111 | } else { |
112 | $classAttribute->nodeValue .= ' ' . $classValue; |
113 | } |
114 | } |
115 | |
116 | private function camelToHyphen(string $string): string |
117 | { |
118 | $string = preg_replace('/([a-z])([A-Z])/', '$1-$2', $string); |
119 | $string = str_replace('_', '-', $string); |
120 | $string = strtolower($string); |
121 | return ltrim($string, '-'); |
122 | } |
123 | } |