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