Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
55 / 55 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
MathML | |
100.00% |
55 / 55 |
|
100.00% |
3 / 3 |
18 | |
100.00% |
1 / 1 |
write | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
writeElementItem | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
8 | |||
getElementTagName | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
8 |
1 | <?php |
2 | |
3 | namespace PhpOffice\Math\Writer; |
4 | |
5 | use PhpOffice\Math\Element; |
6 | use PhpOffice\Math\Exception\NotImplementedException; |
7 | use PhpOffice\Math\Math; |
8 | use XMLWriter; |
9 | |
10 | class MathML implements WriterInterface |
11 | { |
12 | /** @var XMLWriter */ |
13 | private $output; |
14 | |
15 | /** |
16 | * @param Math $math |
17 | * |
18 | * @return string |
19 | */ |
20 | public function write(Math $math): string |
21 | { |
22 | $this->output = new XMLWriter(); |
23 | $this->output->openMemory(); |
24 | $this->output->startDocument('1.0', 'UTF-8'); |
25 | $this->output->writeDtd('math', '-//W3C//DTD MathML 2.0//EN', 'http://www.w3.org/Math/DTD/mathml2/mathml2.dtd'); |
26 | $this->output->startElement('math'); |
27 | $this->output->writeAttribute('xmlns', 'http://www.w3.org/1998/Math/MathML'); |
28 | |
29 | foreach ($math->getElements() as $element) { |
30 | $this->writeElementItem($element); |
31 | } |
32 | |
33 | $this->output->endElement(); |
34 | $this->output->endDocument(); |
35 | |
36 | return $this->output->outputMemory(); |
37 | } |
38 | |
39 | protected function writeElementItem(Element\AbstractElement $element): void |
40 | { |
41 | $tagName = $this->getElementTagName($element); |
42 | |
43 | // Element\AbstractGroupElement |
44 | if ($element instanceof Element\AbstractGroupElement) { |
45 | $this->output->startElement($tagName); |
46 | foreach ($element->getElements() as $childElement) { |
47 | $this->writeElementItem($childElement); |
48 | } |
49 | $this->output->endElement(); |
50 | |
51 | return; |
52 | } |
53 | |
54 | // Element\Superscript |
55 | if ($element instanceof Element\Superscript) { |
56 | $this->output->startElement($tagName); |
57 | $this->writeElementItem($element->getBase()); |
58 | $this->writeElementItem($element->getSuperscript()); |
59 | $this->output->endElement(); |
60 | |
61 | return; |
62 | } |
63 | |
64 | // Element\Fraction |
65 | if ($element instanceof Element\Fraction) { |
66 | $this->output->startElement($tagName); |
67 | $this->writeElementItem($element->getNumerator()); |
68 | $this->writeElementItem($element->getDenominator()); |
69 | $this->output->endElement(); |
70 | |
71 | return; |
72 | } |
73 | |
74 | if ($element instanceof Element\Identifier |
75 | || $element instanceof Element\Numeric |
76 | || $element instanceof Element\Operator) { |
77 | $this->output->startElement($tagName); |
78 | $this->output->text((string) $element->getValue()); |
79 | $this->output->endElement(); |
80 | |
81 | return; |
82 | } |
83 | |
84 | /* |
85 | throw new NotImplementedException(sprintf( |
86 | '%s : The class `%s` is not implemented', |
87 | __METHOD__, |
88 | get_class($element) |
89 | )); |
90 | */ |
91 | } |
92 | |
93 | protected function getElementTagName(Element\AbstractElement $element): string |
94 | { |
95 | // Group |
96 | if ($element instanceof Element\Row) { |
97 | return 'mrow'; |
98 | } |
99 | if ($element instanceof Element\AbstractGroupElement) { |
100 | /* |
101 | throw new NotImplementedException(sprintf( |
102 | '%s : The element of the class `%s` has no tag name', |
103 | __METHOD__, |
104 | get_class($element) |
105 | )); |
106 | */ |
107 | } |
108 | |
109 | if ($element instanceof Element\Superscript) { |
110 | return 'msup'; |
111 | } |
112 | if ($element instanceof Element\Fraction) { |
113 | return 'mfrac'; |
114 | } |
115 | if ($element instanceof Element\Identifier) { |
116 | return 'mi'; |
117 | } |
118 | if ($element instanceof Element\Numeric) { |
119 | return 'mn'; |
120 | } |
121 | if ($element instanceof Element\Operator) { |
122 | return 'mo'; |
123 | } |
124 | |
125 | throw new NotImplementedException(sprintf( |
126 | '%s : The element of the class `%s` has no tag name', |
127 | __METHOD__, |
128 | get_class($element) |
129 | )); |
130 | } |
131 | } |