Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
25 / 25 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| TOC | |
100.00% |
25 / 25 |
|
100.00% |
8 / 8 |
16 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| getTitles | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
6 | |||
| getStyleTOC | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getStyleFont | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setMaxDepth | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMaxDepth | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setMinDepth | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMinDepth | |
100.00% |
1 / 1 |
|
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 | |
| 19 | namespace PhpOffice\PhpWord\Element; |
| 20 | |
| 21 | use PhpOffice\PhpWord\PhpWord; |
| 22 | use PhpOffice\PhpWord\Style\Font; |
| 23 | use PhpOffice\PhpWord\Style\TOC as TOCStyle; |
| 24 | |
| 25 | /** |
| 26 | * Table of contents. |
| 27 | */ |
| 28 | class 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 int $minDepth |
| 63 | * @param int $maxDepth |
| 64 | */ |
| 65 | public function __construct($fontStyle = null, ?array $tocStyle = null, $minDepth = 1, $maxDepth = 9) |
| 66 | { |
| 67 | $this->tocStyle = new TOCStyle(); |
| 68 | |
| 69 | if (null !== $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 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 TOCStyle |
| 114 | */ |
| 115 | public function getStyleTOC() |
| 116 | { |
| 117 | return $this->tocStyle; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get Font Style. |
| 122 | * |
| 123 | * @return 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 | } |