Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.71% |
30 / 35 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| JsonQtiAttributeParser | |
85.71% |
30 / 35 |
|
66.67% |
4 / 6 |
9.24 | |
0.00% |
0 / 1 |
| parse | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| createDomDocument | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
2.02 | |||
| createXInclude | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hydrateXInclude | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| addLanguageAttribute | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
| addClassAttribute | |
33.33% |
2 / 6 |
|
0.00% |
0 / 1 |
3.19 | |||
| 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) 2020 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | declare(strict_types=1); |
| 23 | |
| 24 | namespace oat\taoMediaManager\model\sharedStimulus\parser; |
| 25 | |
| 26 | use DOMDocument; |
| 27 | use DOMElement; |
| 28 | use LogicException; |
| 29 | use oat\oatbox\service\ConfigurableService; |
| 30 | use oat\taoMediaManager\model\sharedStimulus\SharedStimulus; |
| 31 | use oat\taoQtiItem\model\qti\exception\QtiModelException; |
| 32 | use oat\taoQtiItem\model\qti\ParserFactory; |
| 33 | use oat\taoQtiItem\model\qti\XInclude; |
| 34 | |
| 35 | class JsonQtiAttributeParser extends ConfigurableService |
| 36 | { |
| 37 | /** |
| 38 | * @throws QtiModelException |
| 39 | */ |
| 40 | public function parse(SharedStimulus $sharedStimulus): array |
| 41 | { |
| 42 | $document = $this->createDomDocument($sharedStimulus); |
| 43 | $xinclude = $this->createXInclude($document); |
| 44 | $this->addLanguageAttribute($document, $xinclude); |
| 45 | $this->addClassAttribute($document, $xinclude); |
| 46 | |
| 47 | return $xinclude->toArray(); |
| 48 | } |
| 49 | |
| 50 | private function createDomDocument(SharedStimulus $sharedStimulus): DOMDocument |
| 51 | { |
| 52 | $content = $sharedStimulus->getBody(); |
| 53 | if (empty($content)) { |
| 54 | throw new LogicException('SharedStimulus content is empty and cannot be parsed.'); |
| 55 | } |
| 56 | |
| 57 | $document = new DOMDocument(); |
| 58 | $document->loadXML($content, LIBXML_BIGLINES | LIBXML_PARSEHUGE); |
| 59 | |
| 60 | return $document; |
| 61 | } |
| 62 | |
| 63 | private function createXInclude(DOMDocument $document): XInclude |
| 64 | { |
| 65 | return $this->hydrateXInclude(new XInclude(), $document); |
| 66 | } |
| 67 | |
| 68 | private function hydrateXInclude(XInclude $xinclude, DOMDocument $document): XInclude |
| 69 | { |
| 70 | $parser = new ParserFactory($document); |
| 71 | $parser->loadContainerStatic($document->firstChild, $xinclude->getBody()); |
| 72 | |
| 73 | return $xinclude; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @throws QtiModelException |
| 78 | */ |
| 79 | private function addLanguageAttribute(DOMDocument $document, XInclude $xinclude): void |
| 80 | { |
| 81 | $rootNode = $document->firstChild; |
| 82 | $languageAttribute = trim($rootNode->getAttribute('xml:lang')); |
| 83 | |
| 84 | if (strlen($languageAttribute) < 2) { |
| 85 | $this->getLogger()->notice( |
| 86 | 'lang attribute is wrong. Impossible to set the Language Attribute', |
| 87 | [ |
| 88 | 'document' => substr($document->saveXML($rootNode), 0, 200) |
| 89 | ] |
| 90 | ); |
| 91 | |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | $xinclude->setAttribute( |
| 96 | 'xml:lang', |
| 97 | $languageAttribute |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @throws QtiModelException |
| 103 | */ |
| 104 | private function addClassAttribute(DOMDocument $document, XInclude $xinclude): void |
| 105 | { |
| 106 | $classAttr = $document->getElementsByTagName('div')->item(0)->attributes->getNamedItem('class'); |
| 107 | if ($classAttr) { |
| 108 | $xinclude->setAttribute( |
| 109 | 'class', |
| 110 | $classAttr->nodeValue |
| 111 | ); |
| 112 | } |
| 113 | } |
| 114 | } |