/** * Add and get title */ public function testAddAndGetTitle() { $titleCount = 3; $anchor = '_Toc' . (252634154 + $titleCount); $bookmark = $titleCount - 1; $titles = array('Heading 1' => 1, 'Heading 2' => 2, 'Heading 3' => 3); $toc = new TOC(); foreach ($titles as $text => $depth) { $response = $toc->addTitle($text, $depth); } $this->assertEquals($anchor, $response[0]); $this->assertEquals($bookmark, $response[1]); $i = 0; $savedTitles = $toc->getTitles(); foreach ($titles as $text => $depth) { $this->assertEquals($text, $savedTitles[$i]['text']); $this->assertEquals($depth, $savedTitles[$i]['depth']); $i++; } TOC::resetTitles(); $this->assertEquals(0, count($toc->getTitles())); }
/** * Get all titles * * @return array */ public function getTitles() { $titles = Titles::getTitles(); foreach ($titles as $i => $title) { if ($this->minDepth > $title['depth']) { unset($titles[$i]); } if ($this->maxDepth != 0 && $this->maxDepth < $title['depth']) { unset($titles[$i]); } } $titles = array_merge(array(), $titles); return $titles; }
/** * Set/get minDepth and maxDepth */ public function testSetGetMinMaxDepth() { $toc = new TOC(); $titles = array('Heading 1' => 1, 'Heading 2' => 2, 'Heading 3' => 3, 'Heading 4' => 4); foreach ($titles as $text => $depth) { \PhpOffice\PhpWord\TOC::addTitle($text, $depth); } $this->assertEquals(1, $toc->getMinDepth()); $this->assertEquals(9, $toc->getMaxDepth()); $toc->setMinDepth(2); $toc->setMaxDepth(3); $toc->getTitles(); $this->assertEquals(2, $toc->getMinDepth()); $this->assertEquals(3, $toc->getMaxDepth()); }
/** * Add a Title Element * * @param string $text * @param int $depth * @return Title * @todo Enable title element in other containers */ public function addTitle($text, $depth = 1) { $this->checkValidity('title'); $styles = Style::getStyles(); if (array_key_exists('Heading_' . $depth, $styles)) { $style = 'Heading' . $depth; } else { $style = null; } $text = String::toUTF8($text); $title = new Title($text, $depth, $style); $title->setDocPart($this->getDocPart(), $this->getDocPartId()); $data = Titles::addTitle($text, $depth); $anchor = $data[0]; $bookmarkId = $data[1]; $title->setAnchor($anchor); $title->setBookmarkId($bookmarkId); $this->addElement($title); return $title; }