Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.00% |
17 / 20 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| QtiXmlLoader | |
85.00% |
17 / 20 |
|
80.00% |
4 / 5 |
8.22 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| load | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| getQtiParserConfig | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| configDomParser | |
66.67% |
6 / 9 |
|
0.00% |
0 / 1 |
2.15 | |||
| parserConfigValid | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 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 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoQtiItem\helpers; |
| 24 | |
| 25 | use common_ext_ExtensionsManager as ExtensionsManager; |
| 26 | use DOMDocument; |
| 27 | use Exception; |
| 28 | use oat\taoQtiItem\model\qti\exception\QtiModelException; |
| 29 | |
| 30 | class QtiXmlLoader |
| 31 | { |
| 32 | private ExtensionsManager $extensionsManager; |
| 33 | |
| 34 | /** |
| 35 | * @param ExtensionsManager $extensionsManager |
| 36 | */ |
| 37 | public function __construct(ExtensionsManager $extensionsManager) |
| 38 | { |
| 39 | $this->extensionsManager = $extensionsManager; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Load QTI xml and return DOMDocument instance. |
| 44 | * This is service implementation of oat\taoQtiItem\helpers\Authoring::loadQtiXml |
| 45 | * @throws QtiModelException |
| 46 | */ |
| 47 | public function load(string $xml): DOMDocument |
| 48 | { |
| 49 | $dom = new DOMDocument('1.0', 'UTF-8'); |
| 50 | $this->configDomParser($dom); |
| 51 | try { |
| 52 | $dom->loadXML($xml); |
| 53 | } catch (Exception $e) { |
| 54 | throw new QtiModelException('Invalid QTI XML', 0, $e); |
| 55 | } |
| 56 | |
| 57 | return $dom; |
| 58 | } |
| 59 | |
| 60 | private function getQtiParserConfig(): array |
| 61 | { |
| 62 | return $this->extensionsManager->getExtensionById('taoQtiItem') |
| 63 | ->getConfig('XMLParser'); |
| 64 | } |
| 65 | |
| 66 | private function configDomParser(DOMDocument $dom): void |
| 67 | { |
| 68 | $parserConfig = $this->getQtiParserConfig(); |
| 69 | if ($this->parserConfigValid($parserConfig)) { |
| 70 | $dom->formatOutput = $parserConfig['formatOutput']; |
| 71 | $dom->preserveWhiteSpace = $parserConfig['preserveWhiteSpace']; |
| 72 | $dom->validateOnParse = $parserConfig['validateOnParse']; |
| 73 | |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | $dom->formatOutput = true; |
| 78 | $dom->preserveWhiteSpace = false; |
| 79 | $dom->validateOnParse = false; |
| 80 | } |
| 81 | |
| 82 | private function parserConfigValid(array $parserConfig): bool |
| 83 | { |
| 84 | return !empty($parserConfig) && |
| 85 | isset($parserConfig['formatOutput'], $parserConfig['preserveWhiteSpace'], $parserConfig['validateOnParse']); |
| 86 | } |
| 87 | } |