Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.14% covered (success)
97.14%
34 / 35
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Body
97.14% covered (success)
97.14%
34 / 35
50.00% covered (danger)
50.00%
1 / 2
9
0.00% covered (danger)
0.00%
0 / 1
 write
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
4.00
 writeNotes
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
5
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\HTML\Part;
19
20use PhpOffice\PhpWord\Writer\HTML\Element\Container;
21use PhpOffice\PhpWord\Writer\HTML\Element\TextRun as TextRunWriter;
22use PhpOffice\PhpWord\Writer\PDF\TCPDF;
23
24/**
25 * RTF body part writer.
26 *
27 * @since 0.11.0
28 */
29class Body extends AbstractPart
30{
31    /**
32     * Write part.
33     *
34     * @return string
35     */
36    public function write()
37    {
38        $phpWord = $this->getParentWriter()->getPhpWord();
39
40        $content = '';
41
42        $content .= '<body>' . PHP_EOL;
43        $sections = $phpWord->getSections();
44        $secno = 0;
45        $isTCPDFWriter = $this->getParentWriter() instanceof TCPDF;
46        foreach ($sections as $section) {
47            ++$secno;
48            if ($isTCPDFWriter && $secno > 1) {
49                $content .= "<div style=\"page: page$secno; page-break-before:always;\">" . PHP_EOL;
50            } else {
51                $content .= "<div style='page: page$secno'>" . PHP_EOL;
52            }
53            $writer = new Container($this->getParentWriter(), $section);
54            $content .= $writer->write();
55            $content .= '</div>' . PHP_EOL;
56        }
57
58        $content .= $this->writeNotes();
59        $content .= '</body>' . PHP_EOL;
60
61        return $content;
62    }
63
64    /**
65     * Write footnote/endnote contents as textruns.
66     *
67     * @return string
68     */
69    private function writeNotes()
70    {
71        /** @var \PhpOffice\PhpWord\Writer\HTML $parentWriter Type hint */
72        $parentWriter = $this->getParentWriter();
73        $phpWord = $parentWriter->getPhpWord();
74        $notes = $parentWriter->getNotes();
75
76        $content = '';
77
78        if (!empty($notes)) {
79            $content .= '<hr />' . PHP_EOL;
80            foreach ($notes as $noteId => $noteMark) {
81                [$noteType, $noteTypeId] = explode('-', $noteMark);
82                $method = 'get' . ($noteType == 'endnote' ? 'Endnotes' : 'Footnotes');
83                $collection = $phpWord->$method()->getItems();
84
85                if (isset($collection[$noteTypeId])) {
86                    $element = $collection[$noteTypeId];
87                    $noteAnchor = "<a name=\"note-{$noteId}\" />";
88                    $noteAnchor .= "<a href=\"#{$noteMark}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>";
89
90                    $writer = new TextRunWriter($this->getParentWriter(), $element);
91                    $writer->setOpeningText($noteAnchor);
92                    $content .= $writer->write();
93                }
94            }
95        }
96
97        return $content;
98    }
99}