Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Title
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
1 / 1
9
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
1 / 1
9
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\Element;
20
21/**
22 * TextRun element writer.
23 *
24 * @since 0.10.0
25 */
26class Title extends AbstractElement
27{
28    /**
29     * Write title element.
30     */
31    public function write(): void
32    {
33        $xmlWriter = $this->getXmlWriter();
34        $element = $this->getElement();
35        if (!$element instanceof \PhpOffice\PhpWord\Element\Title) {
36            return;
37        }
38
39        $style = $element->getStyle();
40        if (empty($style)) {
41            // Part\Styles writes a style for a depth that has none
42            $depth = (int) $element->getDepth();
43            $style = $depth === 0 ? 'Title' : 'Heading' . $depth;
44        }
45
46        $xmlWriter->startElement('w:p');
47
48        if (!empty($style)) {
49            $xmlWriter->startElement('w:pPr');
50            $xmlWriter->startElement('w:pStyle');
51            $xmlWriter->writeAttribute('w:val', $style);
52            $xmlWriter->endElement();
53            $xmlWriter->endElement();
54        }
55
56        $bookmarkRId = null;
57        if ($element->getDepth() !== 0) {
58            $rId = $element->getRelationId();
59            $bookmarkRId = $element->getPhpWord()->addBookmark();
60
61            // Bookmark start for TOC
62            $xmlWriter->startElement('w:bookmarkStart');
63            $xmlWriter->writeAttribute('w:id', $bookmarkRId);
64            $xmlWriter->writeAttribute('w:name', "_Toc{$rId}");
65            $xmlWriter->endElement(); //w:bookmarkStart
66        }
67
68        // Actual text
69        $text = $element->getText();
70        if (is_string($text)) {
71            $xmlWriter->startElement('w:r');
72            $xmlWriter->startElement('w:t');
73            $this->writeText($text);
74            $xmlWriter->endElement(); // w:t
75            $xmlWriter->endElement(); // w:r
76        }
77        if ($text instanceof \PhpOffice\PhpWord\Element\AbstractContainer) {
78            $containerWriter = new Container($xmlWriter, $text);
79            $containerWriter->write();
80        }
81
82        if ($element->getDepth() !== 0) {
83            // Bookmark end
84            $xmlWriter->startElement('w:bookmarkEnd');
85            $xmlWriter->writeAttribute('w:id', $bookmarkRId);
86            $xmlWriter->endElement(); //w:bookmarkEnd
87        }
88        $xmlWriter->endElement(); //w:p
89    }
90}