TOC.php 3.8 KB

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