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