109 lines
2.3 KiB
PHP
Raw Normal View History

2012-05-06 18:25:40 +02:00
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
2012-05-06 18:25:40 +02:00
*
* @see https://github.com/PHPOffice/PHPWord
2018-03-08 23:52:25 +01:00
* @copyright 2010-2018 PHPWord contributors
2014-05-04 21:03:28 +04:00
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
2012-05-06 18:25:40 +02:00
*/
namespace PhpOffice\PhpWord\Element;
2016-01-23 19:16:34 +04:00
use PhpOffice\Common\Text as CommonText;
use PhpOffice\PhpWord\Style;
/**
* Title element
*/
class Title extends AbstractElement
{
/**
* Title Text content
*
* @var string|TextRun
*/
2014-04-03 08:44:41 +07:00
private $text;
/**
* Title depth
*
* @var int
*/
2014-05-04 15:13:31 +07:00
private $depth = 1;
/**
* Name of the heading style, e.g. 'Heading1'
*
* @var string
*/
2014-04-03 08:44:41 +07:00
private $style;
/**
2014-06-10 07:11:32 +07:00
* Is part of collection
*
* @var bool
*/
protected $collectionRelation = true;
/**
* Create a new Title Element
*
* @param string|TextRun $text
2014-03-17 07:26:25 +07:00
* @param int $depth
*/
public function __construct($text, $depth = 1)
{
2018-07-14 02:54:17 +02:00
if (is_string($text)) {
$this->text = CommonText::toUTF8($text);
} elseif ($text instanceof TextRun) {
$this->text = $text;
} else {
throw new \InvalidArgumentException('Invalid text, should be a string or a TextRun');
}
2014-04-03 08:44:41 +07:00
$this->depth = $depth;
$styleName = $depth === 0 ? 'Title' : "Heading_{$this->depth}";
if (array_key_exists($styleName, Style::getStyles())) {
$this->style = str_replace('_', '', $styleName);
}
}
/**
* Get Title Text content
*
* @return string
*/
public function getText()
{
2014-04-03 08:44:41 +07:00
return $this->text;
}
2014-04-12 10:12:24 +07:00
/**
* Get depth
*
* @return int
2014-04-12 10:12:24 +07:00
*/
public function getDepth()
{
return $this->depth;
}
/**
* Get Title style
*
* @return string
*/
public function getStyle()
{
2014-04-03 08:44:41 +07:00
return $this->style;
}
2012-05-06 18:25:40 +02:00
}