Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
72 / 72 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Document | |
100.00% |
72 / 72 |
|
100.00% |
3 / 3 |
14 | |
100.00% |
1 / 1 |
| write | |
100.00% |
29 / 29 |
|
100.00% |
1 / 1 |
4 | |||
| writeSection | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| writeSectionSettings | |
100.00% |
38 / 38 |
|
100.00% |
1 / 1 |
9 | |||
| 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\Word2007\Part; |
| 20 | |
| 21 | use PhpOffice\PhpWord\Element\Section; |
| 22 | use PhpOffice\PhpWord\Shared\XMLWriter; |
| 23 | use PhpOffice\PhpWord\Writer\Word2007\Element\Container; |
| 24 | use PhpOffice\PhpWord\Writer\Word2007\Style\Section as SectionStyleWriter; |
| 25 | |
| 26 | /** |
| 27 | * Word2007 document part writer: word/document.xml. |
| 28 | */ |
| 29 | class Document extends AbstractPart |
| 30 | { |
| 31 | /** |
| 32 | * Write part. |
| 33 | * |
| 34 | * @return string |
| 35 | */ |
| 36 | public function write() |
| 37 | { |
| 38 | $phpWord = $this->getParentWriter()->getPhpWord(); |
| 39 | $xmlWriter = $this->getXmlWriter(); |
| 40 | |
| 41 | $sections = $phpWord->getSections(); |
| 42 | $sectionCount = count($sections); |
| 43 | $currentSection = 0; |
| 44 | $drawingSchema = 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing'; |
| 45 | |
| 46 | $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); |
| 47 | $xmlWriter->startElement('w:document'); |
| 48 | $xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006'); |
| 49 | $xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office'); |
| 50 | $xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'); |
| 51 | $xmlWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math'); |
| 52 | $xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml'); |
| 53 | $xmlWriter->writeAttribute('xmlns:wp', $drawingSchema); |
| 54 | $xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word'); |
| 55 | $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); |
| 56 | $xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml'); |
| 57 | |
| 58 | $xmlWriter->startElement('w:body'); |
| 59 | |
| 60 | if ($sectionCount > 0) { |
| 61 | foreach ($sections as $section) { |
| 62 | ++$currentSection; |
| 63 | |
| 64 | $containerWriter = new Container($xmlWriter, $section); |
| 65 | $containerWriter->write(); |
| 66 | |
| 67 | if ($currentSection == $sectionCount) { |
| 68 | $this->writeSectionSettings($xmlWriter, $section); |
| 69 | } else { |
| 70 | $this->writeSection($xmlWriter, $section); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | $xmlWriter->endElement(); // w:body |
| 76 | $xmlWriter->endElement(); // w:document |
| 77 | |
| 78 | return $xmlWriter->getData(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Write begin section. |
| 83 | */ |
| 84 | private function writeSection(XMLWriter $xmlWriter, Section $section): void |
| 85 | { |
| 86 | $xmlWriter->startElement('w:p'); |
| 87 | $xmlWriter->startElement('w:pPr'); |
| 88 | $this->writeSectionSettings($xmlWriter, $section); |
| 89 | $xmlWriter->endElement(); |
| 90 | $xmlWriter->endElement(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Write end section. |
| 95 | */ |
| 96 | private function writeSectionSettings(XMLWriter $xmlWriter, Section $section): void |
| 97 | { |
| 98 | $xmlWriter->startElement('w:sectPr'); |
| 99 | |
| 100 | // Header reference |
| 101 | foreach ($section->getHeaders() as $header) { |
| 102 | $rId = $header->getRelationId(); |
| 103 | $xmlWriter->startElement('w:headerReference'); |
| 104 | $xmlWriter->writeAttribute('w:type', $header->getType()); |
| 105 | $xmlWriter->writeAttribute('r:id', 'rId' . $rId); |
| 106 | $xmlWriter->endElement(); |
| 107 | } |
| 108 | |
| 109 | // Footer reference |
| 110 | foreach ($section->getFooters() as $footer) { |
| 111 | $rId = $footer->getRelationId(); |
| 112 | $xmlWriter->startElement('w:footerReference'); |
| 113 | $xmlWriter->writeAttribute('w:type', $footer->getType()); |
| 114 | $xmlWriter->writeAttribute('r:id', 'rId' . $rId); |
| 115 | $xmlWriter->endElement(); |
| 116 | } |
| 117 | |
| 118 | // Different first page |
| 119 | if ($section->hasDifferentFirstPage()) { |
| 120 | $xmlWriter->startElement('w:titlePg'); |
| 121 | $xmlWriter->endElement(); |
| 122 | } |
| 123 | |
| 124 | // Footnote properties |
| 125 | if ($section->getFootnoteProperties() !== null) { |
| 126 | $xmlWriter->startElement('w:footnotePr'); |
| 127 | if ($section->getFootnoteProperties()->getPos() != null) { |
| 128 | $xmlWriter->startElement('w:pos'); |
| 129 | $xmlWriter->writeAttribute('w:val', $section->getFootnoteProperties()->getPos()); |
| 130 | $xmlWriter->endElement(); |
| 131 | } |
| 132 | if ($section->getFootnoteProperties()->getNumFmt() != null) { |
| 133 | $xmlWriter->startElement('w:numFmt'); |
| 134 | $xmlWriter->writeAttribute('w:val', $section->getFootnoteProperties()->getNumFmt()); |
| 135 | $xmlWriter->endElement(); |
| 136 | } |
| 137 | if ($section->getFootnoteProperties()->getNumStart() != null) { |
| 138 | $xmlWriter->startElement('w:numStart'); |
| 139 | $xmlWriter->writeAttribute('w:val', $section->getFootnoteProperties()->getNumStart()); |
| 140 | $xmlWriter->endElement(); |
| 141 | } |
| 142 | if ($section->getFootnoteProperties()->getNumRestart() != null) { |
| 143 | $xmlWriter->startElement('w:numRestart'); |
| 144 | $xmlWriter->writeAttribute('w:val', $section->getFootnoteProperties()->getNumRestart()); |
| 145 | $xmlWriter->endElement(); |
| 146 | } |
| 147 | $xmlWriter->endElement(); |
| 148 | } |
| 149 | |
| 150 | // Section settings |
| 151 | $styleWriter = new SectionStyleWriter($xmlWriter, $section->getStyle()); |
| 152 | $styleWriter->write(); |
| 153 | |
| 154 | $xmlWriter->endElement(); // w:sectPr |
| 155 | } |
| 156 | } |