@@ -22,69 +22,87 @@ class TOC
2222 *
2323 * @var array
2424 */
25- private static $ _titles = array ();
25+ private static $ titles = array ();
2626
2727 /**
2828 * TOC style
2929 *
3030 * @var TOCStyle
3131 */
32- private static $ _styleTOC ;
32+ private static $ tocStyle ;
3333
3434 /**
3535 * Font style
3636 *
3737 * @var Font|array|string
3838 */
39- private static $ _styleFont ;
39+ private static $ fontStyle ;
4040
4141 /**
4242 * Title anchor
4343 *
4444 * @var int
4545 */
46- private static $ _anchor = 252634154 ;
46+ private static $ anchor = 252634154 ;
4747
4848 /**
4949 * Title bookmark
5050 *
5151 * @var int
5252 */
53- private static $ _bookmarkId = 0 ;
53+ private static $ bookmarkId = 0 ;
5454
55+ /**
56+ * Min title depth to show
57+ *
58+ * @var int
59+ */
60+ private $ minDepth = 1 ;
61+
62+ /**
63+ * Max title depth to show
64+ *
65+ * @var int
66+ */
67+ private $ maxDepth = 9 ;
5568
5669 /**
5770 * Create a new Table-of-Contents Element
5871 *
5972 * @param mixed $styleFont
6073 * @param array $styleTOC
74+ * @param int $minDepth
75+ * @param int $maxDepth
6176 */
62- public function __construct ($ styleFont = null , $ styleTOC = null )
77+ public function __construct ($ styleFont = null , $ styleTOC = null , $ minDepth = 1 , $ maxDepth = 9 )
6378 {
64- self ::$ _styleTOC = new TOCStyle ();
79+ self ::$ tocStyle = new TOCStyle ();
6580
6681 if (!is_null ($ styleTOC ) && is_array ($ styleTOC )) {
6782 foreach ($ styleTOC as $ key => $ value ) {
6883 if (substr ($ key , 0 , 1 ) != '_ ' ) {
6984 $ key = '_ ' . $ key ;
7085 }
71- self ::$ _styleTOC ->setStyleValue ($ key , $ value );
86+ self ::$ tocStyle ->setStyleValue ($ key , $ value );
7287 }
7388 }
7489
7590 if (!is_null ($ styleFont )) {
7691 if (is_array ($ styleFont )) {
77- self ::$ _styleFont = new Font ();
92+ self ::$ fontStyle = new Font ();
7893 foreach ($ styleFont as $ key => $ value ) {
7994 if (substr ($ key , 0 , 1 ) != '_ ' ) {
8095 $ key = '_ ' . $ key ;
8196 }
82- self ::$ _styleFont ->setStyleValue ($ key , $ value );
97+ self ::$ fontStyle ->setStyleValue ($ key , $ value );
8398 }
8499 } else {
85- self ::$ _styleFont = $ styleFont ;
100+ self ::$ fontStyle = $ styleFont ;
86101 }
87102 }
103+
104+ $ this ->minDepth = $ minDepth ;
105+ $ this ->maxDepth = $ maxDepth ;
88106 }
89107
90108 /**
@@ -96,16 +114,16 @@ public function __construct($styleFont = null, $styleTOC = null)
96114 */
97115 public static function addTitle ($ text , $ depth = 0 )
98116 {
99- $ anchor = '_Toc ' . ++self ::$ _anchor ;
100- $ bookmarkId = self ::$ _bookmarkId ++;
117+ $ anchor = '_Toc ' . ++self ::$ anchor ;
118+ $ bookmarkId = self ::$ bookmarkId ++;
101119
102120 $ title = array ();
103121 $ title ['text ' ] = $ text ;
104122 $ title ['depth ' ] = $ depth ;
105123 $ title ['anchor ' ] = $ anchor ;
106124 $ title ['bookmarkId ' ] = $ bookmarkId ;
107125
108- self ::$ _titles [] = $ title ;
126+ self ::$ titles [] = $ title ;
109127
110128 return array ($ anchor , $ bookmarkId );
111129 }
@@ -115,9 +133,20 @@ public static function addTitle($text, $depth = 0)
115133 *
116134 * @return array
117135 */
118- public static function getTitles ()
136+ public function getTitles ()
119137 {
120- return self ::$ _titles ;
138+ $ titles = self ::$ titles ;
139+ foreach ($ titles as $ i => $ title ) {
140+ if ($ this ->minDepth > $ title ['depth ' ]) {
141+ unset($ titles [$ i ]);
142+ }
143+ if (($ this ->maxDepth != 0 ) && ($ this ->maxDepth < $ title ['depth ' ])) {
144+ unset($ titles [$ i ]);
145+ }
146+ }
147+ $ titles = array_merge (array (), $ titles );
148+
149+ return $ titles ;
121150 }
122151
123152 /**
@@ -127,7 +156,7 @@ public static function getTitles()
127156 */
128157 public static function getStyleTOC ()
129158 {
130- return self ::$ _styleTOC ;
159+ return self ::$ tocStyle ;
131160 }
132161
133162 /**
@@ -137,6 +166,46 @@ public static function getStyleTOC()
137166 */
138167 public static function getStyleFont ()
139168 {
140- return self ::$ _styleFont ;
169+ return self ::$ fontStyle ;
170+ }
171+
172+ /**
173+ * Set max depth
174+ *
175+ * @param integer $value
176+ */
177+ public function setMaxDepth ($ value )
178+ {
179+ $ this ->maxDepth = $ value ;
180+ }
181+
182+ /**
183+ * Get Max Depth
184+ *
185+ * @return int Max depth of titles
186+ */
187+ public function getMaxDepth ()
188+ {
189+ return $ this ->maxDepth ;
190+ }
191+
192+ /**
193+ * Set min depth
194+ *
195+ * @param integer $value
196+ */
197+ public function setMinDepth ($ value )
198+ {
199+ $ this ->minDepth = $ value ;
200+ }
201+
202+ /**
203+ * Get Min Depth
204+ *
205+ * @return int Min depth of titles
206+ */
207+ public function getMinDepth ()
208+ {
209+ return $ this ->minDepth ;
141210 }
142211}
0 commit comments