Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
TOC
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
8 / 8
17
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
 getTitles
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
6
 getStyleTOC
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStyleFont
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMaxDepth
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMaxDepth
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMinDepth
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMinDepth
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 PhpOffice\PhpWord\PhpWord;
22use PhpOffice\PhpWord\Style\Font;
23use PhpOffice\PhpWord\Style\TOC as TOCStyle;
24
25/**
26 * Table of contents.
27 */
28class TOC extends AbstractElement
29{
30    /**
31     * TOC style.
32     *
33     * @var TOCStyle
34     */
35    private $tocStyle;
36
37    /**
38     * Font style.
39     *
40     * @var Font|string
41     */
42    private $fontStyle;
43
44    /**
45     * Min title depth to show.
46     *
47     * @var int
48     */
49    private $minDepth = 1;
50
51    /**
52     * Max title depth to show.
53     *
54     * @var int
55     */
56    private $maxDepth = 9;
57
58    /**
59     * Create a new Table-of-Contents Element.
60     *
61     * @param mixed $fontStyle
62     * @param array $tocStyle
63     * @param int $minDepth
64     * @param int $maxDepth
65     */
66    public function __construct($fontStyle = null, $tocStyle = null, $minDepth = 1, $maxDepth = 9)
67    {
68        $this->tocStyle = new TOCStyle();
69
70        if (null !== $tocStyle && is_array($tocStyle)) {
71            $this->tocStyle->setStyleByArray($tocStyle);
72        }
73
74        if (null !== $fontStyle && is_array($fontStyle)) {
75            $this->fontStyle = new Font();
76            $this->fontStyle->setStyleByArray($fontStyle);
77        } else {
78            $this->fontStyle = $fontStyle;
79        }
80
81        $this->minDepth = $minDepth;
82        $this->maxDepth = $maxDepth;
83    }
84
85    /**
86     * Get all titles.
87     *
88     * @return array
89     */
90    public function getTitles()
91    {
92        if (!$this->phpWord instanceof PhpWord) {
93            return [];
94        }
95
96        $titles = $this->phpWord->getTitles()->getItems();
97        foreach ($titles as $i => $title) {
98            /** @var Title $title Type hint */
99            $depth = $title->getDepth();
100            if ($this->minDepth > $depth) {
101                unset($titles[$i]);
102            }
103            if (($this->maxDepth != 0) && ($this->maxDepth < $depth)) {
104                unset($titles[$i]);
105            }
106        }
107
108        return $titles;
109    }
110
111    /**
112     * Get TOC Style.
113     *
114     * @return TOCStyle
115     */
116    public function getStyleTOC()
117    {
118        return $this->tocStyle;
119    }
120
121    /**
122     * Get Font Style.
123     *
124     * @return Font|string
125     */
126    public function getStyleFont()
127    {
128        return $this->fontStyle;
129    }
130
131    /**
132     * Set max depth.
133     *
134     * @param int $value
135     */
136    public function setMaxDepth($value): void
137    {
138        $this->maxDepth = $value;
139    }
140
141    /**
142     * Get Max Depth.
143     *
144     * @return int Max depth of titles
145     */
146    public function getMaxDepth()
147    {
148        return $this->maxDepth;
149    }
150
151    /**
152     * Set min depth.
153     *
154     * @param int $value
155     */
156    public function setMinDepth($value): void
157    {
158        $this->minDepth = $value;
159    }
160
161    /**
162     * Get Min Depth.
163     *
164     * @return int Min depth of titles
165     */
166    public function getMinDepth()
167    {
168        return $this->minDepth;
169    }
170}