Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Title
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
6
 getText
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDepth
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStyle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPageNumber
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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\Element;
20
21use InvalidArgumentException;
22use PhpOffice\PhpWord\Shared\Text as SharedText;
23use PhpOffice\PhpWord\Style;
24
25/**
26 * Title element.
27 */
28class 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     */
71    public function __construct($text, $depth = 1, ?int $pageNumber = null)
72    {
73        if (is_string($text)) {
74            $this->text = SharedText::toUTF8($text);
75        } elseif ($text instanceof TextRun) {
76            $this->text = $text;
77        } else {
78            throw new InvalidArgumentException('Invalid text, should be a string or a TextRun');
79        }
80
81        $this->depth = $depth;
82        $styleName = $depth === 0 ? 'Title' : "Heading_{$this->depth}";
83        if (array_key_exists($styleName, Style::getStyles())) {
84            $this->style = str_replace('_', '', $styleName);
85        }
86
87        if ($pageNumber !== null) {
88            $this->pageNumber = $pageNumber;
89        }
90    }
91
92    /**
93     * Get Title Text content.
94     *
95     * @return string|TextRun
96     */
97    public function getText()
98    {
99        return $this->text;
100    }
101
102    /**
103     * Get depth.
104     *
105     * @return int
106     */
107    public function getDepth()
108    {
109        return $this->depth;
110    }
111
112    /**
113     * Get Title style.
114     *
115     * @return ?string
116     */
117    public function getStyle()
118    {
119        return $this->style;
120    }
121
122    /**
123     * Get page number.
124     */
125    public function getPageNumber(): ?int
126    {
127        return $this->pageNumber;
128    }
129}