2012-05-06 18:25:40 +02:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-03-26 16:33:20 +07:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
|
2014-03-22 10:06:08 +04:00
|
|
|
namespace PhpOffice\PhpWord;
|
2013-12-11 14:45:44 -05:00
|
|
|
|
2014-03-22 10:06:08 +04:00
|
|
|
use PhpOffice\PhpWord\Style\Font;
|
|
|
|
|
use PhpOffice\PhpWord\Style\Paragraph;
|
2014-03-24 11:33:20 +07:00
|
|
|
use PhpOffice\PhpWord\Style\Table;
|
2014-04-11 16:16:22 +07:00
|
|
|
use PhpOffice\PhpWord\Style\Numbering;
|
2014-03-22 10:06:08 +04:00
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
2014-04-08 16:09:31 +07:00
|
|
|
* Style collection
|
2014-03-24 00:26:10 +07:00
|
|
|
*/
|
2014-03-22 10:06:08 +04:00
|
|
|
class Style
|
|
|
|
|
{
|
2013-12-11 14:45:44 -05:00
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Style register
|
|
|
|
|
*
|
2013-12-11 14:45:44 -05:00
|
|
|
* @var array
|
|
|
|
|
*/
|
2014-04-05 00:57:50 +07:00
|
|
|
private static $styles = array();
|
2013-12-11 14:45:44 -05:00
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Add paragraph style
|
|
|
|
|
*
|
2013-12-11 14:45:44 -05:00
|
|
|
* @param string $styleName
|
|
|
|
|
* @param array $styles
|
|
|
|
|
*/
|
|
|
|
|
public static function addParagraphStyle($styleName, $styles)
|
|
|
|
|
{
|
2014-04-05 00:57:50 +07:00
|
|
|
self::setStyleValues($styleName, $styles, new Paragraph());
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Add font style
|
|
|
|
|
*
|
2013-12-11 14:45:44 -05:00
|
|
|
* @param string $styleName
|
|
|
|
|
* @param array $styleFont
|
|
|
|
|
* @param array $styleParagraph
|
|
|
|
|
*/
|
|
|
|
|
public static function addFontStyle($styleName, $styleFont, $styleParagraph = null)
|
|
|
|
|
{
|
2014-04-05 00:57:50 +07:00
|
|
|
self::setStyleValues($styleName, $styleFont, new Font('text', $styleParagraph));
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Add link style
|
|
|
|
|
*
|
2013-12-11 14:45:44 -05:00
|
|
|
* @param string $styleName
|
|
|
|
|
* @param array $styles
|
|
|
|
|
*/
|
|
|
|
|
public static function addLinkStyle($styleName, $styles)
|
|
|
|
|
{
|
2014-04-05 00:57:50 +07:00
|
|
|
self::setStyleValues($styleName, $styles, new Font('link'));
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Add table style
|
|
|
|
|
*
|
2013-12-11 14:45:44 -05:00
|
|
|
* @param string $styleName
|
2014-03-24 00:26:10 +07:00
|
|
|
* @param array $styleTable
|
|
|
|
|
* @param array $styleFirstRow
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
2014-03-22 10:06:08 +04:00
|
|
|
public static function addTableStyle($styleName, $styleTable, $styleFirstRow = null)
|
2013-12-11 14:45:44 -05:00
|
|
|
{
|
2014-04-11 16:16:22 +07:00
|
|
|
self::setStyleValues($styleName, null, new Table($styleTable, $styleFirstRow));
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Add title style
|
|
|
|
|
*
|
|
|
|
|
* @param int $titleCount
|
2013-12-11 14:45:44 -05:00
|
|
|
* @param array $styleFont
|
2014-03-22 10:06:08 +04:00
|
|
|
* @param array $styleParagraph
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
|
|
|
|
public static function addTitleStyle($titleCount, $styleFont, $styleParagraph = null)
|
|
|
|
|
{
|
2014-04-05 00:57:50 +07:00
|
|
|
self::setStyleValues("Heading_{$titleCount}", $styleFont, new Font('title', $styleParagraph));
|
|
|
|
|
}
|
2013-12-11 14:45:44 -05:00
|
|
|
|
2014-04-11 16:16:22 +07:00
|
|
|
/**
|
|
|
|
|
* Add numbering style
|
|
|
|
|
*
|
|
|
|
|
* @param string $styleName
|
|
|
|
|
* @param array $styleValues
|
|
|
|
|
* @return Numbering
|
|
|
|
|
* @since 0.9.2
|
|
|
|
|
*/
|
|
|
|
|
public static function addNumberingStyle($styleName, $styleValues)
|
|
|
|
|
{
|
|
|
|
|
self::setStyleValues($styleName, $styleValues, new Numbering());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Count styles
|
|
|
|
|
*
|
|
|
|
|
* @return integer
|
|
|
|
|
* @since 0.9.2
|
|
|
|
|
*/
|
|
|
|
|
public static function countStyles()
|
|
|
|
|
{
|
|
|
|
|
return count(self::$styles);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-05 00:57:50 +07:00
|
|
|
/**
|
|
|
|
|
* Reset styles
|
2014-04-11 16:16:22 +07:00
|
|
|
* @since 0.9.2
|
2014-04-05 00:57:50 +07:00
|
|
|
*/
|
2014-04-08 16:09:31 +07:00
|
|
|
public static function resetStyles()
|
2014-04-05 00:57:50 +07:00
|
|
|
{
|
|
|
|
|
self::$styles = array();
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
2014-03-07 18:02:47 +07:00
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Set default paragraph style
|
|
|
|
|
*
|
2014-03-17 07:26:25 +07:00
|
|
|
* @param array $styles Paragraph style definition
|
2014-03-07 18:02:47 +07:00
|
|
|
*/
|
2014-03-07 19:31:13 +07:00
|
|
|
public static function setDefaultParagraphStyle($styles)
|
2014-03-07 18:02:47 +07:00
|
|
|
{
|
|
|
|
|
self::addParagraphStyle('Normal', $styles);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-11 14:45:44 -05:00
|
|
|
/**
|
|
|
|
|
* Get all styles
|
|
|
|
|
*
|
2014-04-09 21:35:55 +07:00
|
|
|
* @return array
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
|
|
|
|
public static function getStyles()
|
|
|
|
|
{
|
2014-04-05 00:57:50 +07:00
|
|
|
return self::$styles;
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Get style by name
|
|
|
|
|
*
|
|
|
|
|
* @param string $styleName
|
2014-04-11 16:16:22 +07:00
|
|
|
* @return Paragraph|Font|Table|Numbering|null
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
|
|
|
|
public static function getStyle($styleName)
|
|
|
|
|
{
|
2014-04-05 00:57:50 +07:00
|
|
|
if (array_key_exists($styleName, self::$styles)) {
|
|
|
|
|
return self::$styles[$styleName];
|
2013-12-11 14:45:44 -05:00
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-05 00:57:50 +07:00
|
|
|
|
|
|
|
|
/**
|
2014-04-11 16:16:22 +07:00
|
|
|
* Set style values and put it to static style collection
|
2014-04-05 00:57:50 +07:00
|
|
|
*
|
|
|
|
|
* @param string $styleName
|
|
|
|
|
* @param array $styleValues
|
2014-04-11 16:16:22 +07:00
|
|
|
* @param Paragraph|Font|Table|Numbering $styleObject
|
2014-04-05 00:57:50 +07:00
|
|
|
*/
|
|
|
|
|
private static function setStyleValues($styleName, $styleValues, $styleObject)
|
|
|
|
|
{
|
|
|
|
|
if (!array_key_exists($styleName, self::$styles)) {
|
2014-04-07 18:06:42 +07:00
|
|
|
if (is_array($styleValues)) {
|
|
|
|
|
foreach ($styleValues as $key => $value) {
|
|
|
|
|
$styleObject->setStyleValue($key, $value);
|
2014-04-05 00:57:50 +07:00
|
|
|
}
|
|
|
|
|
}
|
2014-04-11 16:16:22 +07:00
|
|
|
$styleObject->setIndex(self::countStyles() + 1); // One based index
|
2014-04-05 00:57:50 +07:00
|
|
|
self::$styles[$styleName] = $styleObject;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-05-06 18:25:40 +02:00
|
|
|
}
|