Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
72 / 72
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Document
100.00% covered (success)
100.00%
72 / 72
100.00% covered (success)
100.00%
3 / 3
14
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
1 / 1
4
 writeSection
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 writeSectionSettings
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
1 / 1
9
1<?php
2/**
3 * This file is part of PHPWord - A pure PHP library for reading and writing
4 * word processing documents.
5 *
6 * PHPWord is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12 *
13 * @see         https://github.com/PHPOffice/PHPWord
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18namespace PhpOffice\PhpWord\Writer\Word2007\Part;
19
20use PhpOffice\PhpWord\Element\Section;
21use PhpOffice\PhpWord\Shared\XMLWriter;
22use PhpOffice\PhpWord\Writer\Word2007\Element\Container;
23use PhpOffice\PhpWord\Writer\Word2007\Style\Section as SectionStyleWriter;
24
25/**
26 * Word2007 document part writer: word/document.xml.
27 */
28class Document extends AbstractPart
29{
30    /**
31     * Write part.
32     *
33     * @return string
34     */
35    public function write()
36    {
37        $phpWord = $this->getParentWriter()->getPhpWord();
38        $xmlWriter = $this->getXmlWriter();
39
40        $sections = $phpWord->getSections();
41        $sectionCount = count($sections);
42        $currentSection = 0;
43        $drawingSchema = 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing';
44
45        $xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
46        $xmlWriter->startElement('w:document');
47        $xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
48        $xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
49        $xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
50        $xmlWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math');
51        $xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
52        $xmlWriter->writeAttribute('xmlns:wp', $drawingSchema);
53        $xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word');
54        $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
55        $xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml');
56
57        $xmlWriter->startElement('w:body');
58
59        if ($sectionCount > 0) {
60            foreach ($sections as $section) {
61                ++$currentSection;
62
63                $containerWriter = new Container($xmlWriter, $section);
64                $containerWriter->write();
65
66                if ($currentSection == $sectionCount) {
67                    $this->writeSectionSettings($xmlWriter, $section);
68                } else {
69                    $this->writeSection($xmlWriter, $section);
70                }
71            }
72        }
73
74        $xmlWriter->endElement(); // w:body
75        $xmlWriter->endElement(); // w:document
76
77        return $xmlWriter->getData();
78    }
79
80    /**
81     * Write begin section.
82     */
83    private function writeSection(XMLWriter $xmlWriter, Section $section): void
84    {
85        $xmlWriter->startElement('w:p');
86        $xmlWriter->startElement('w:pPr');
87        $this->writeSectionSettings($xmlWriter, $section);
88        $xmlWriter->endElement();
89        $xmlWriter->endElement();
90    }
91
92    /**
93     * Write end section.
94     */
95    private function writeSectionSettings(XMLWriter $xmlWriter, Section $section): void
96    {
97        $xmlWriter->startElement('w:sectPr');
98
99        // Header reference
100        foreach ($section->getHeaders() as $header) {
101            $rId = $header->getRelationId();
102            $xmlWriter->startElement('w:headerReference');
103            $xmlWriter->writeAttribute('w:type', $header->getType());
104            $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
105            $xmlWriter->endElement();
106        }
107
108        // Footer reference
109        foreach ($section->getFooters() as $footer) {
110            $rId = $footer->getRelationId();
111            $xmlWriter->startElement('w:footerReference');
112            $xmlWriter->writeAttribute('w:type', $footer->getType());
113            $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
114            $xmlWriter->endElement();
115        }
116
117        // Different first page
118        if ($section->hasDifferentFirstPage()) {
119            $xmlWriter->startElement('w:titlePg');
120            $xmlWriter->endElement();
121        }
122
123        // Footnote properties
124        if ($section->getFootnoteProperties() !== null) {
125            $xmlWriter->startElement('w:footnotePr');
126            if ($section->getFootnoteProperties()->getPos() != null) {
127                $xmlWriter->startElement('w:pos');
128                $xmlWriter->writeAttribute('w:val', $section->getFootnoteProperties()->getPos());
129                $xmlWriter->endElement();
130            }
131            if ($section->getFootnoteProperties()->getNumFmt() != null) {
132                $xmlWriter->startElement('w:numFmt');
133                $xmlWriter->writeAttribute('w:val', $section->getFootnoteProperties()->getNumFmt());
134                $xmlWriter->endElement();
135            }
136            if ($section->getFootnoteProperties()->getNumStart() != null) {
137                $xmlWriter->startElement('w:numStart');
138                $xmlWriter->writeAttribute('w:val', $section->getFootnoteProperties()->getNumStart());
139                $xmlWriter->endElement();
140            }
141            if ($section->getFootnoteProperties()->getNumRestart() != null) {
142                $xmlWriter->startElement('w:numRestart');
143                $xmlWriter->writeAttribute('w:val', $section->getFootnoteProperties()->getNumRestart());
144                $xmlWriter->endElement();
145            }
146            $xmlWriter->endElement();
147        }
148
149        // Section settings
150        $styleWriter = new SectionStyleWriter($xmlWriter, $section->getStyle());
151        $styleWriter->write();
152
153        $xmlWriter->endElement(); // w:sectPr
154    }
155}