Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
63.83% |
30 / 47 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
ContentXhtml | |
63.83% |
30 / 47 |
|
40.00% |
2 / 5 |
38.93 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getXmlWriter | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
write | |
83.87% |
26 / 31 |
|
0.00% |
0 / 1 |
9.34 | |||
writeTextElement | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
56 | |||
writeTextRun | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace PhpOffice\PhpWord\Writer\EPub3\Part; |
4 | |
5 | use PhpOffice\PhpWord\Element\Text; |
6 | use PhpOffice\PhpWord\Element\TextRun; |
7 | use PhpOffice\PhpWord\PhpWord; |
8 | use XMLWriter; |
9 | |
10 | /** |
11 | * Class for EPub3 content.xhtml part. |
12 | */ |
13 | class ContentXhtml extends AbstractPart |
14 | { |
15 | /** |
16 | * PHPWord object. |
17 | * |
18 | * @var ?PhpWord |
19 | */ |
20 | private $phpWord; |
21 | |
22 | /** |
23 | * Constructor. |
24 | */ |
25 | public function __construct(?PhpWord $phpWord = null) |
26 | { |
27 | $this->phpWord = $phpWord; |
28 | } |
29 | |
30 | /** |
31 | * Get XML Writer. |
32 | * |
33 | * @return XMLWriter |
34 | */ |
35 | protected function getXmlWriter() |
36 | { |
37 | $xmlWriter = new XMLWriter(); |
38 | $xmlWriter->openMemory(); |
39 | |
40 | return $xmlWriter; |
41 | } |
42 | |
43 | /** |
44 | * Write part content. |
45 | */ |
46 | public function write(): string |
47 | { |
48 | if ($this->phpWord === null) { |
49 | throw new \PhpOffice\PhpWord\Exception\Exception('No PhpWord assigned.'); |
50 | } |
51 | |
52 | $xmlWriter = $this->getXmlWriter(); |
53 | |
54 | $xmlWriter->startDocument('1.0', 'UTF-8'); |
55 | $xmlWriter->startElement('html'); |
56 | $xmlWriter->writeAttribute('xmlns', 'http://www.w3.org/1999/xhtml'); |
57 | $xmlWriter->writeAttribute('xmlns:epub', 'http://www.idpf.org/2007/ops'); |
58 | $xmlWriter->startElement('head'); |
59 | $xmlWriter->writeElement('title', $this->phpWord->getDocInfo()->getTitle() ?: 'Untitled'); |
60 | $xmlWriter->endElement(); // head |
61 | $xmlWriter->startElement('body'); |
62 | |
63 | // Write sections content |
64 | foreach ($this->phpWord->getSections() as $section) { |
65 | $xmlWriter->startElement('div'); |
66 | $xmlWriter->writeAttribute('class', 'section'); |
67 | |
68 | foreach ($section->getElements() as $element) { |
69 | if ($element instanceof TextRun) { |
70 | $xmlWriter->startElement('p'); |
71 | $this->writeTextRun($element, $xmlWriter); |
72 | $xmlWriter->endElement(); // p |
73 | } elseif (method_exists($element, 'getText')) { |
74 | $text = $element->getText(); |
75 | $xmlWriter->startElement('p'); |
76 | if ($text instanceof TextRun) { |
77 | $this->writeTextRun($text, $xmlWriter); |
78 | } elseif ($text !== null) { |
79 | $xmlWriter->text((string) $text); |
80 | } |
81 | $xmlWriter->endElement(); // p |
82 | } |
83 | } |
84 | |
85 | $xmlWriter->endElement(); // div |
86 | } |
87 | |
88 | $xmlWriter->endElement(); // body |
89 | $xmlWriter->endElement(); // html |
90 | |
91 | return $xmlWriter->outputMemory(true); |
92 | } |
93 | |
94 | protected function writeTextElement(\PhpOffice\PhpWord\Element\AbstractElement $textElement, XMLWriter $xmlWriter): void |
95 | { |
96 | if ($textElement instanceof Text) { |
97 | $text = $textElement->getText(); |
98 | if ($text !== null) { |
99 | $xmlWriter->text((string) $text); |
100 | } |
101 | } elseif (is_object($textElement) && method_exists($textElement, 'getText')) { |
102 | $text = $textElement->getText(); |
103 | if ($text instanceof TextRun) { |
104 | $this->writeTextRun($text, $xmlWriter); |
105 | } elseif ($text !== null) { |
106 | $xmlWriter->text((string) $text); |
107 | } |
108 | } |
109 | } |
110 | |
111 | protected function writeTextRun(TextRun $textRun, XMLWriter $xmlWriter): void |
112 | { |
113 | foreach ($textRun->getElements() as $element) { |
114 | $this->writeTextElement($element, $xmlWriter); |
115 | } |
116 | } |
117 | } |