PHPWord/src/PhpWord/Section/Footer/PreserveText.php
2014-03-30 23:14:14 +07:00

118 lines
2.4 KiB
PHP

<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Section\Footer;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
/**
* Preserve text/field element
*/
class PreserveText
{
/**
* Text content
*
* @var string
*/
private $_text;
/**
* Text style
*
* @var string|Font
*/
private $_styleFont;
/**
* Paragraph style
*
* @var string|Paragraph
*/
private $_styleParagraph;
/**
* Create a new Preserve Text Element
*
* @param string $text
* @param mixed $styleFont
* @param mixed $styleParagraph
* @return $this
*/
public function __construct($text = null, $styleFont = null, $styleParagraph = null)
{
// Set font style
if (is_array($styleFont)) {
$this->_styleFont = new Font('text');
foreach ($styleFont as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$this->_styleFont->setStyleValue($key, $value);
}
} else {
$this->_styleFont = $styleFont;
}
// Set paragraph style
if (is_array($styleParagraph)) {
$this->_styleParagraph = new Paragraph();
foreach ($styleParagraph as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$this->_styleParagraph->setStyleValue($key, $value);
}
} else {
$this->_styleParagraph = $styleParagraph;
}
$matches = preg_split('/({.*?})/', $text, null, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY);
if (isset($matches[0])) {
$this->_text = $matches;
}
return $this;
}
/**
* Get Text style
*
* @return string|Font
*/
public function getFontStyle()
{
return $this->_styleFont;
}
/**
* Get Paragraph style
*
* @return string|Paragraph
*/
public function getParagraphStyle()
{
return $this->_styleParagraph;
}
/**
* Get Text content
*
* @return string
*/
public function getText()
{
return $this->_text;
}
}