Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.37% covered (warning)
85.37%
35 / 41
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Title
85.37% covered (warning)
85.37%
35 / 41
50.00% covered (danger)
50.00%
1 / 2
13.53
0.00% covered (danger)
0.00%
0 / 1
 getStyles
60.00% covered (warning)
60.00%
9 / 15
0.00% covered (danger)
0.00%
0 / 1
8.30
 write
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
7
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\RTF\Element;
20
21/**
22 * Title element RTF writer; extends from text.
23 *
24 * @since 0.11.0
25 */
26class Title extends Text
27{
28    protected function getStyles(): void
29    {
30        /** @var \PhpOffice\PhpWord\Element\Title $element Type hint */
31        $element = $this->element;
32        $style = $element->getStyle();
33        $style = str_replace('Heading', 'Heading_', $style ?? '');
34        $style = \PhpOffice\PhpWord\Style::getStyle($style);
35        if ($style instanceof \PhpOffice\PhpWord\Style\Font) {
36            $this->fontStyle = $style;
37            $pstyle = $style->getParagraph();
38            if ($pstyle instanceof \PhpOffice\PhpWord\Style\Paragraph && $pstyle->hasPageBreakBefore()) {
39                $sect = $element->getParent();
40                if ($sect instanceof \PhpOffice\PhpWord\Element\Section) {
41                    $elems = $sect->getElements();
42                    if ($elems[0] === $element) {
43                        $pstyle = clone $pstyle;
44                        $pstyle->setPageBreakBefore(false);
45                    }
46                }
47            }
48            $this->paragraphStyle = $pstyle;
49        }
50    }
51
52    /**
53     * Write element.
54     *
55     * @return string
56     */
57    public function write()
58    {
59        /** @var \PhpOffice\PhpWord\Element\Title $element Type hint */
60        $element = $this->element;
61        $elementClass = str_replace('\\Writer\\RTF', '', static::class);
62        if (!$element instanceof $elementClass) {
63            return '';
64        }
65
66        $textToWrite = $element->getText();
67        if ($textToWrite instanceof \PhpOffice\PhpWord\Element\TextRun) {
68            $textToWrite = $textToWrite->getText(); // gets text from TextRun
69        }
70
71        $this->getStyles();
72
73        $content = '';
74
75        $content .= $this->writeOpening();
76        $endout = '';
77        $style = $element->getStyle();
78        if (is_string($style)) {
79            $style = str_replace('Heading', '', $style);
80            if ("$style" !== '') {
81                $style = (int) $style - 1;
82                if ($style >= 0 && $style <= 8) {
83                    $content .= '{\\outlinelevel' . $style;
84                    $endout = '}';
85                }
86            }
87        }
88
89        $content .= '{';
90        $content .= $this->writeFontStyle();
91        $content .= $this->writeText($textToWrite);
92        $content .= '}';
93        $content .= $this->writeClosing();
94        $content .= $endout;
95
96        return $content;
97    }
98}