PHPWord/src/PhpWord/TOC.php

143 lines
2.8 KiB
PHP
Raw Normal View History

2012-05-06 18:25:40 +02:00
<?php
/**
* PHPWord
2012-05-06 18:25:40 +02:00
*
2014-03-27 23:55:06 +07:00
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
2012-05-06 18:25:40 +02:00
*/
namespace PhpOffice\PhpWord;
use PhpOffice\PhpWord\Style\Font;
2014-03-30 18:51:25 +07:00
use PhpOffice\PhpWord\Style\TOC as TOCStyle;
/**
* Table of contents
*/
class TOC
{
/**
2014-03-28 13:50:53 +07:00
* Title elements
*
* @var array
*/
private static $_titles = array();
/**
2014-03-28 13:50:53 +07:00
* TOC style
*
2014-03-30 18:51:25 +07:00
* @var TOCStyle
*/
private static $_styleTOC;
/**
2014-03-28 13:50:53 +07:00
* Font style
*
2014-03-30 18:51:25 +07:00
* @var Font|array|string
*/
private static $_styleFont;
/**
2014-03-28 13:50:53 +07:00
* Title anchor
*
2014-03-28 13:50:53 +07:00
* @var int
*/
private static $_anchor = 252634154;
/**
2014-03-28 13:50:53 +07:00
* Title bookmark
*
2014-03-28 13:50:53 +07:00
* @var int
*/
private static $_bookmarkId = 0;
/**
* Create a new Table-of-Contents Element
*
2014-03-28 13:50:53 +07:00
* @param mixed $styleFont
* @param array $styleTOC
*/
public function __construct($styleFont = null, $styleTOC = null)
{
2014-03-30 18:51:25 +07:00
self::$_styleTOC = new TOCStyle();
if (!is_null($styleTOC) && is_array($styleTOC)) {
foreach ($styleTOC as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
self::$_styleTOC->setStyleValue($key, $value);
}
}
if (!is_null($styleFont)) {
if (is_array($styleFont)) {
self::$_styleFont = new Font();
foreach ($styleFont as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
self::$_styleFont->setStyleValue($key, $value);
}
} else {
self::$_styleFont = $styleFont;
}
}
}
/**
* Add a Title
*
2014-03-17 07:26:25 +07:00
* @param string $text
* @param int $depth
* @return array
*/
public static function addTitle($text, $depth = 0)
{
$anchor = '_Toc' . ++self::$_anchor;
$bookmarkId = self::$_bookmarkId++;
$title = array();
$title['text'] = $text;
$title['depth'] = $depth;
$title['anchor'] = $anchor;
$title['bookmarkId'] = $bookmarkId;
self::$_titles[] = $title;
return array($anchor, $bookmarkId);
}
/**
* Get all titles
*
* @return array
*/
public static function getTitles()
{
return self::$_titles;
}
/**
* Get TOC Style
*
2014-03-30 18:51:25 +07:00
* @return TOCStyle
*/
public static function getStyleTOC()
{
return self::$_styleTOC;
}
/**
* Get Font Style
*
2014-03-30 18:51:25 +07:00
* @return Font
*/
public static function getStyleFont()
{
return self::$_styleFont;
}
}