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