Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
65 / 65
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Footnotes
100.00% covered (success)
100.00%
65 / 65
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
1 / 1
3
 setElements
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 writeNote
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
1
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
19namespace PhpOffice\PhpWord\Writer\Word2007\Part;
20
21use PhpOffice\PhpWord\Element\Footnote;
22use PhpOffice\PhpWord\Shared\XMLWriter;
23use PhpOffice\PhpWord\Writer\Word2007\Element\Container;
24use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
25
26/**
27 * Word2007 footnotes part writer: word/(footnotes|endnotes).xml.
28 */
29class Footnotes extends AbstractPart
30{
31    /**
32     * Name of XML root element.
33     *
34     * @var string
35     */
36    protected $rootNode = 'w:footnotes';
37
38    /**
39     * Name of XML node element.
40     *
41     * @var string
42     */
43    protected $elementNode = 'w:footnote';
44
45    /**
46     * Name of XML reference element.
47     *
48     * @var string
49     */
50    protected $refNode = 'w:footnoteRef';
51
52    /**
53     * Reference style name.
54     *
55     * @var string
56     */
57    protected $refStyle = 'FootnoteReference';
58
59    /**
60     * Footnotes/endnotes collection to be written.
61     *
62     * @var \PhpOffice\PhpWord\Collection\Endnotes|\PhpOffice\PhpWord\Collection\Footnotes
63     */
64    protected $elements;
65
66    /**
67     * Write part.
68     *
69     * @return string
70     */
71    public function write()
72    {
73        $xmlWriter = $this->getXmlWriter();
74        $drawingSchema = 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing';
75
76        $xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
77        $xmlWriter->startElement($this->rootNode);
78        $xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
79        $xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
80        $xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
81        $xmlWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math');
82        $xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
83        $xmlWriter->writeAttribute('xmlns:wp', $drawingSchema);
84        $xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word');
85        $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
86        $xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml');
87
88        // Separator and continuation separator
89        $xmlWriter->startElement($this->elementNode);
90        $xmlWriter->writeAttribute('w:id', -1);
91        $xmlWriter->writeAttribute('w:type', 'separator');
92        $xmlWriter->startElement('w:p');
93        $xmlWriter->startElement('w:r');
94        $xmlWriter->startElement('w:separator');
95        $xmlWriter->endElement(); // w:separator
96        $xmlWriter->endElement(); // w:r
97        $xmlWriter->endElement(); // w:p
98        $xmlWriter->endElement(); // $this->elementNode
99        $xmlWriter->startElement($this->elementNode);
100        $xmlWriter->writeAttribute('w:id', 0);
101        $xmlWriter->writeAttribute('w:type', 'continuationSeparator');
102        $xmlWriter->startElement('w:p');
103        $xmlWriter->startElement('w:r');
104        $xmlWriter->startElement('w:continuationSeparator');
105        $xmlWriter->endElement(); // w:continuationSeparator
106        $xmlWriter->endElement(); // w:r
107        $xmlWriter->endElement(); // w:p
108        $xmlWriter->endElement(); // $this->elementNode
109
110        /** @var array $elements Type hint */
111        $elements = $this->elements;
112        foreach ($elements as $element) {
113            if ($element instanceof Footnote) {
114                $this->writeNote($xmlWriter, $element);
115            }
116        }
117
118        $xmlWriter->endElement(); // $this->rootNode
119
120        return $xmlWriter->getData();
121    }
122
123    /**
124     * Set element.
125     *
126     * @param \PhpOffice\PhpWord\Collection\Endnotes|\PhpOffice\PhpWord\Collection\Footnotes $elements
127     *
128     * @return self
129     */
130    public function setElements($elements)
131    {
132        $this->elements = $elements;
133
134        return $this;
135    }
136
137    /**
138     * Write note item.
139     *
140     * @param Footnote|\PhpOffice\PhpWord\Element\Endnote $element
141     */
142    protected function writeNote(XMLWriter $xmlWriter, $element): void
143    {
144        $xmlWriter->startElement($this->elementNode);
145        $xmlWriter->writeAttribute('w:id', $element->getRelationId() + 1);
146        $xmlWriter->startElement('w:p');
147
148        // Paragraph style
149        $styleWriter = new ParagraphStyleWriter($xmlWriter, $element->getParagraphStyle());
150        $styleWriter->setIsInline(true);
151        $styleWriter->write();
152
153        // Reference symbol
154        $xmlWriter->startElement('w:r');
155        $xmlWriter->startElement('w:rPr');
156        $xmlWriter->startElement('w:rStyle');
157        $xmlWriter->writeAttribute('w:val', $this->refStyle);
158        $xmlWriter->endElement(); // w:rStyle
159        $xmlWriter->endElement(); // w:rPr
160        $xmlWriter->writeElement($this->refNode);
161        $xmlWriter->endElement(); // w:r
162
163        // Empty space after refence symbol
164        $xmlWriter->startElement('w:r');
165        $xmlWriter->startElement('w:t');
166        $xmlWriter->writeAttribute('xml:space', 'preserve');
167        $xmlWriter->text(' ');
168        $xmlWriter->endElement(); // w:t
169        $xmlWriter->endElement(); // w:r
170
171        $containerWriter = new Container($xmlWriter, $element);
172        $containerWriter->write();
173
174        $xmlWriter->endElement(); // w:p
175        $xmlWriter->endElement(); // $this->elementNode
176    }
177}