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