Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
48 / 48 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Content | |
100.00% |
48 / 48 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getXmlWriter | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| write | |
100.00% |
43 / 43 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of PHPWord - A pure PHP library for reading and writing |
| 5 | * word processing documents. |
| 6 | * |
| 7 | * PHPWord is free software distributed under the terms of the GNU Lesser |
| 8 | * General Public License version 3 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * For the full copyright and license information, please read the LICENSE |
| 11 | * file that was distributed with this source code. For the full list of |
| 12 | * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
| 13 | * |
| 14 | * @see https://github.com/PHPOffice/PHPWord |
| 15 | * |
| 16 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
| 17 | */ |
| 18 | |
| 19 | namespace PhpOffice\PhpWord\Writer\EPub3\Part; |
| 20 | |
| 21 | use PhpOffice\PhpWord\Exception\Exception; |
| 22 | use PhpOffice\PhpWord\PhpWord; |
| 23 | use XMLWriter; |
| 24 | |
| 25 | /** |
| 26 | * Class for EPub3 content part. |
| 27 | */ |
| 28 | class Content extends AbstractPart |
| 29 | { |
| 30 | /** |
| 31 | * PHPWord object. |
| 32 | * |
| 33 | * @var ?PhpWord |
| 34 | */ |
| 35 | private $phpWord; |
| 36 | |
| 37 | /** |
| 38 | * Constructor. |
| 39 | */ |
| 40 | public function __construct(?PhpWord $phpWord = null) |
| 41 | { |
| 42 | $this->phpWord = $phpWord; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get XML Writer. |
| 47 | * |
| 48 | * @return XMLWriter |
| 49 | */ |
| 50 | protected function getXmlWriter() |
| 51 | { |
| 52 | $xmlWriter = new XMLWriter(); |
| 53 | $xmlWriter->openMemory(); |
| 54 | $xmlWriter->startDocument('1.0', 'UTF-8'); |
| 55 | |
| 56 | return $xmlWriter; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Write part content. |
| 61 | */ |
| 62 | public function write(): string |
| 63 | { |
| 64 | if ($this->phpWord === null) { |
| 65 | throw new Exception('No PhpWord assigned.'); |
| 66 | } |
| 67 | |
| 68 | $xmlWriter = $this->getXmlWriter(); |
| 69 | $docInfo = $this->phpWord->getDocInfo(); |
| 70 | |
| 71 | // Write package |
| 72 | $xmlWriter->startElement('package'); |
| 73 | $xmlWriter->writeAttribute('xmlns', 'http://www.idpf.org/2007/opf'); |
| 74 | $xmlWriter->writeAttribute('version', '3.0'); |
| 75 | $xmlWriter->writeAttribute('unique-identifier', 'book-id'); |
| 76 | $xmlWriter->writeAttribute('xml:lang', 'en'); |
| 77 | |
| 78 | // Write metadata |
| 79 | $xmlWriter->startElement('metadata'); |
| 80 | $xmlWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/'); |
| 81 | $xmlWriter->writeAttribute('xmlns:opf', 'http://www.idpf.org/2007/opf'); |
| 82 | |
| 83 | // Required elements |
| 84 | $xmlWriter->startElement('dc:identifier'); |
| 85 | $xmlWriter->writeAttribute('id', 'book-id'); |
| 86 | $xmlWriter->text('book-id-' . uniqid()); |
| 87 | $xmlWriter->endElement(); |
| 88 | $xmlWriter->writeElement('dc:title', $docInfo->getTitle() ?: 'Untitled'); |
| 89 | $xmlWriter->writeElement('dc:language', 'en'); |
| 90 | |
| 91 | // Required modified timestamp |
| 92 | $xmlWriter->startElement('meta'); |
| 93 | $xmlWriter->writeAttribute('property', 'dcterms:modified'); |
| 94 | $xmlWriter->text(date('Y-m-d\TH:i:s\Z')); |
| 95 | $xmlWriter->endElement(); |
| 96 | |
| 97 | $xmlWriter->endElement(); // metadata |
| 98 | |
| 99 | // Write manifest |
| 100 | $xmlWriter->startElement('manifest'); |
| 101 | |
| 102 | // Add nav document (required) |
| 103 | $xmlWriter->startElement('item'); |
| 104 | $xmlWriter->writeAttribute('id', 'nav'); |
| 105 | $xmlWriter->writeAttribute('href', 'nav.xhtml'); |
| 106 | $xmlWriter->writeAttribute('media-type', 'application/xhtml+xml'); |
| 107 | $xmlWriter->writeAttribute('properties', 'nav'); |
| 108 | $xmlWriter->endElement(); |
| 109 | |
| 110 | // Add content document |
| 111 | $xmlWriter->startElement('item'); |
| 112 | $xmlWriter->writeAttribute('id', 'content'); |
| 113 | $xmlWriter->writeAttribute('href', 'content.xhtml'); |
| 114 | $xmlWriter->writeAttribute('media-type', 'application/xhtml+xml'); |
| 115 | $xmlWriter->endElement(); |
| 116 | |
| 117 | $xmlWriter->endElement(); // manifest |
| 118 | |
| 119 | // Write spine |
| 120 | $xmlWriter->startElement('spine'); |
| 121 | $xmlWriter->startElement('itemref'); |
| 122 | $xmlWriter->writeAttribute('idref', 'content'); |
| 123 | $xmlWriter->endElement(); |
| 124 | $xmlWriter->endElement(); // spine |
| 125 | |
| 126 | $xmlWriter->endElement(); // package |
| 127 | |
| 128 | return $xmlWriter->outputMemory(true); |
| 129 | } |
| 130 | } |