Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
42 / 42 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
OfficeMathML | |
100.00% |
42 / 42 |
|
100.00% |
3 / 3 |
12 | |
100.00% |
1 / 1 |
write | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
writeElementItem | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
7 | |||
getElementTagName | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 |
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 OfficeMathML 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->startElement('m:oMathPara'); |
25 | $this->output->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math'); |
26 | $this->output->startElement('m:oMath'); |
27 | |
28 | foreach ($math->getElements() as $element) { |
29 | $this->writeElementItem($element); |
30 | } |
31 | |
32 | $this->output->endElement(); |
33 | $this->output->endElement(); |
34 | |
35 | return $this->output->outputMemory(); |
36 | } |
37 | |
38 | protected function writeElementItem(Element\AbstractElement $element): void |
39 | { |
40 | // Element\Row |
41 | if ($element instanceof Element\Row) { |
42 | foreach ($element->getElements() as $childElement) { |
43 | $this->writeElementItem($childElement); |
44 | } |
45 | |
46 | return; |
47 | } |
48 | |
49 | // Element\Fraction |
50 | if ($element instanceof Element\Fraction) { |
51 | $this->output->startElement($this->getElementTagName($element)); |
52 | $this->output->startElement('m:num'); |
53 | $this->writeElementItem($element->getNumerator()); |
54 | $this->output->endElement(); |
55 | $this->output->startElement('m:den'); |
56 | $this->writeElementItem($element->getDenominator()); |
57 | $this->output->endElement(); |
58 | $this->output->endElement(); |
59 | |
60 | return; |
61 | } |
62 | |
63 | if ($element instanceof Element\Identifier |
64 | || $element instanceof Element\Numeric |
65 | || $element instanceof Element\Operator) { |
66 | $this->output->startElement('m:r'); |
67 | $this->output->startElement('m:t'); |
68 | $this->output->text((string) $element->getValue()); |
69 | $this->output->endElement(); |
70 | $this->output->endElement(); |
71 | |
72 | return; |
73 | } |
74 | |
75 | // Check if managed |
76 | $this->getElementTagName($element); |
77 | } |
78 | |
79 | protected function getElementTagName(Element\AbstractElement $element): string |
80 | { |
81 | // Group |
82 | if ($element instanceof Element\AbstractGroupElement) { |
83 | /* |
84 | throw new NotImplementedException(sprintf( |
85 | '%s : The element of the class `%s` has no tag name', |
86 | __METHOD__, |
87 | get_class($element) |
88 | )); |
89 | */ |
90 | } |
91 | |
92 | if ($element instanceof Element\Fraction) { |
93 | return 'm:f'; |
94 | } |
95 | |
96 | throw new NotImplementedException(sprintf( |
97 | '%s : The element of the class `%s` has no tag name', |
98 | __METHOD__, |
99 | get_class($element) |
100 | )); |
101 | } |
102 | } |