• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPOffice / PHPWord / 25414441731

06 May 2026 03:12AM UTC coverage: 94.696% (-2.1%) from 96.757%
25414441731

Pull #2874

github

web-flow
Merge 3fa85d6e3 into 0ab0b4940
Pull Request #2874: Addresses issue #12

135 of 420 new or added lines in 30 files covered. (32.14%)

1 existing line in 1 file now uncovered.

12588 of 13293 relevant lines covered (94.7%)

34.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.17
/src/PhpWord/Element/Title.php
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

19
namespace PhpOffice\PhpWord\Element;
20

21
use InvalidArgumentException;
22
use PhpOffice\PhpWord\Shared\Text as SharedText;
23
use PhpOffice\PhpWord\Style;
24

25
/**
26
 * Title element.
27
 */
28
class Title extends AbstractElement
29
{
30
    /**
31
     * Title Text content.
32
     *
33
     * @var string|TextRun
34
     */
35
    private $text;
36

37
    /**
38
     * Title depth.
39
     *
40
     * @var int
41
     */
42
    private $depth = 1;
43

44
    /**
45
     * Name of the heading style, e.g. 'Heading1'.
46
     *
47
     * @var ?string
48
     */
49
    private $style;
50

51
    /**
52
     * Is part of collection.
53
     *
54
     * @var bool
55
     */
56
    protected $collectionRelation = true;
57

58
    /**
59
     * Page number.
60
     *
61
     * @var int
62
     */
63
    private $pageNumber;
64

65
    /**
66
     * Create a new Title Element.
67
     *
68
     * @param string|TextRun $text
69
     * @param int $depth
70
     * @param string $style
71
     */
72
    public function __construct($text, $depth = 1, ?int $pageNumber = null, $style = '')
73
    {
74
        if (is_string($text)) {
36✔
75
            $this->text = SharedText::toUTF8($text);
26✔
76
        } elseif ($text instanceof TextRun) {
13✔
77
            $this->text = $text;
12✔
78
        } else {
79
            throw new InvalidArgumentException('Invalid text, should be a string or a TextRun');
1✔
80
        }
81

82
        if (empty($style)) {
35✔
83
            $this->setDepth($depth);
35✔
84
            $styleName = $depth === 0 ? 'Title' : "Heading_{$this->depth}";
35✔
85
            if (array_key_exists($styleName, Style::getStyles())) {
35✔
86
                $this->style = str_replace('_', '', $styleName);
35✔
87
            }
88
        } else {
NEW
89
            $this->style = $style;
×
NEW
90
            $titleStyle = Style::getStyle($style);
×
NEW
91
            if (($titleStyle instanceof Style\Font) && (is_int($outlineLvl = $titleStyle->getParagraph()->getOutlineLvl()))) {
×
NEW
92
                $this->setDepth($outlineLvl + 1);
×
93
            } else {
NEW
94
                $this->setDepth($depth);
×
95
            }
96
        }
97

98
        if ($pageNumber !== null) {
35✔
99
            $this->pageNumber = $pageNumber;
2✔
100
        }
101

102
        // Set ElementId for bookmark
103
        $this->setElementId();
35✔
104
    }
105

106
    /**
107
     * Get Title Text content.
108
     *
109
     * @return string|TextRun
110
     */
111
    public function getText()
112
    {
113
        return $this->text;
31✔
114
    }
115

116
    /**
117
     * Get depth.
118
     *
119
     * @return int
120
     */
121
    public function getDepth()
122
    {
123
        return $this->depth;
23✔
124
    }
125

126
    /**
127
     * Set depth.
128
     *
129
     * @param int $value
130
     *
131
     * @return self
132
     */
133
    public function setDepth($value = 0)
134
    {
135
        $this->depth = $value;
35✔
136

137
        return $this;
35✔
138
    }
139

140
    /**
141
     * Get Title style.
142
     *
143
     * @return ?string
144
     */
145
    public function getStyle()
146
    {
147
        return $this->style;
22✔
148
    }
149

150
    /**
151
     * Get page number.
152
     */
153
    public function getPageNumber(): ?int
154
    {
155
        return $this->pageNumber;
7✔
156
    }
157
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc