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 * 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\Element;
19
20use InvalidArgumentException;
21use PhpOffice\PhpWord\Shared\Text as SharedText;
22use PhpOffice\PhpWord\Style;
23
24/**
25 * Title element.
26 */
27class Title extends AbstractElement
28{
29    /**
30     * Title Text content.
31     *
32     * @var string|TextRun
33     */
34    private $text;
35
36    /**
37     * Title depth.
38     *
39     * @var int
40     */
41    private $depth = 1;
42
43    /**
44     * Name of the heading style, e.g. 'Heading1'.
45     *
46     * @var ?string
47     */
48    private $style;
49
50    /**
51     * Is part of collection.
52     *
53     * @var bool
54     */
55    protected $collectionRelation = true;
56
57    /**
58     * Page number.
59     *
60     * @var int
61     */
62    private $pageNumber;
63
64    /**
65     * Create a new Title Element.
66     *
67     * @param string|TextRun $text
68     * @param int $depth
69     */
70    public function __construct($text, $depth = 1, ?int $pageNumber = null)
71    {
72        if (is_string($text)) {
73            $this->text = SharedText::toUTF8($text);
74        } elseif ($text instanceof TextRun) {
75            $this->text = $text;
76        } else {
77            throw new InvalidArgumentException('Invalid text, should be a string or a TextRun');
78        }
79
80        $this->depth = $depth;
81        $styleName = $depth === 0 ? 'Title' : "Heading_{$this->depth}";
82        if (array_key_exists($styleName, Style::getStyles())) {
83            $this->style = str_replace('_', '', $styleName);
84        }
85
86        if ($pageNumber !== null) {
87            $this->pageNumber = $pageNumber;
88        }
89    }
90
91    /**
92     * Get Title Text content.
93     *
94     * @return string|TextRun
95     */
96    public function getText()
97    {
98        return $this->text;
99    }
100
101    /**
102     * Get depth.
103     *
104     * @return int
105     */
106    public function getDepth()
107    {
108        return $this->depth;
109    }
110
111    /**
112     * Get Title style.
113     *
114     * @return ?string
115     */
116    public function getStyle()
117    {
118        return $this->style;
119    }
120
121    /**
122     * Get page number.
123     */
124    public function getPageNumber(): ?int
125    {
126        return $this->pageNumber;
127    }
128}