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