PHPWord/src/PhpWord/Style/Table.php

608 lines
13 KiB
PHP
Raw Normal View History

<?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.
*
2014-03-27 23:55:06 +07:00
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
2014-05-04 21:03:28 +04:00
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Style;
/**
* Table style
*/
class Table extends Border
{
/**
* @const string Table width units http://www.schemacentral.com/sc/ooxml/t-w_ST_TblWidth.html
*/
const WIDTH_AUTO = 'auto'; // Automatically determined width
const WIDTH_PERCENT = 'pct'; // Width in fiftieths (1/50) of a percent (1% = 50 unit)
const WIDTH_TWIP = 'dxa'; // Width in twentieths (1/20) of a point (twip)
2014-05-15 14:41:08 +07:00
/**
* Is this a first row style?
*
* @var bool
*/
private $isFirstRow = false;
2014-03-24 11:33:20 +07:00
/**
* Style for first row
*
* @var \PhpOffice\PhpWord\Style\Table
*/
2014-05-15 14:41:08 +07:00
private $firstRowStyle;
2014-03-24 11:33:20 +07:00
/**
* Cell margin top
*
* @var int
*/
private $cellMarginTop;
/**
* Cell margin left
*
* @var int
*/
private $cellMarginLeft;
/**
* Cell margin right
*
* @var int
*/
private $cellMarginRight;
/**
* Cell margin bottom
*
* @var int
*/
private $cellMarginBottom;
2014-03-24 11:33:20 +07:00
/**
* Border size inside horizontal
*
* @var int
*/
private $borderInsideHSize;
2014-03-24 11:33:20 +07:00
/**
* Border color inside horizontal
*
* @var string
*/
private $borderInsideHColor;
2014-03-24 11:33:20 +07:00
/**
* Border size inside vertical
*
* @var int
*/
private $borderInsideVSize;
2014-03-24 11:33:20 +07:00
/**
* Border color inside vertical
*
* @var string
*/
private $borderInsideVColor;
2014-03-24 11:33:20 +07:00
/**
* Shading
*
* @var \PhpOffice\PhpWord\Style\Shading
*/
private $shading;
/**
* @var \PhpOffice\PhpWord\Style\Alignment Alignment
*/
private $alignment;
/**
* @var int|float Width value
*/
private $width = 0;
/**
* @var string Width unit
*/
private $unit = self::WIDTH_AUTO;
/**
* Create new table style
2014-03-24 11:33:20 +07:00
*
* @param mixed $tableStyle
* @param mixed $firstRowStyle
*/
public function __construct($tableStyle = null, $firstRowStyle = null)
{
$this->alignment = new Alignment();
// Clone first row from table style, but with certain properties disabled
if ($firstRowStyle !== null && is_array($firstRowStyle)) {
2014-05-15 14:41:08 +07:00
$this->firstRowStyle = clone $this;
$this->firstRowStyle->isFirstRow = true;
unset($this->firstRowStyle->firstRowStyle);
unset($this->firstRowStyle->borderInsideHSize);
unset($this->firstRowStyle->borderInsideHColor);
unset($this->firstRowStyle->borderInsideVSize);
unset($this->firstRowStyle->borderInsideVColor);
unset($this->firstRowStyle->cellMarginTop);
unset($this->firstRowStyle->cellMarginLeft);
unset($this->firstRowStyle->cellMarginRight);
unset($this->firstRowStyle->cellMarginBottom);
$this->firstRowStyle->setStyleByArray($firstRowStyle);
2014-03-24 11:33:20 +07:00
}
if ($tableStyle !== null && is_array($tableStyle)) {
$this->setStyleByArray($tableStyle);
}
}
2014-03-24 11:33:20 +07:00
/**
2014-05-15 14:41:08 +07:00
* Set first row
2014-03-24 11:33:20 +07:00
*
* @return \PhpOffice\PhpWord\Style\Table
*/
public function getFirstRow()
{
2014-05-15 14:41:08 +07:00
return $this->firstRowStyle;
2014-03-24 11:33:20 +07:00
}
/**
* Get background
*
2014-04-03 10:13:13 +07:00
* @return string
2014-03-24 11:33:20 +07:00
*/
public function getBgColor()
{
if (!is_null($this->shading)) {
return $this->shading->getFill();
}
return null;
2014-03-24 11:33:20 +07:00
}
/**
* Set background
*
* @param string $value
* @return self
2014-03-24 11:33:20 +07:00
*/
public function setBgColor($value = null)
2014-03-24 11:33:20 +07:00
{
$this->setShading(array('fill' => $value));
return $this;
}
/**
* Get TLRBHV Border Size
*
* @return int[]
*/
public function getBorderSize()
{
return array(
$this->getBorderTopSize(),
$this->getBorderLeftSize(),
$this->getBorderRightSize(),
$this->getBorderBottomSize(),
$this->getBorderInsideHSize(),
$this->getBorderInsideVSize(),
);
2014-03-24 11:33:20 +07:00
}
/**
* Set TLRBHV Border Size
2014-03-24 11:33:20 +07:00
*
* @param int $value Border size in eighths of a point (1/8 point)
* @return self
2014-03-24 11:33:20 +07:00
*/
public function setBorderSize($value = null)
2014-03-24 11:33:20 +07:00
{
$this->setBorderTopSize($value);
$this->setBorderLeftSize($value);
$this->setBorderRightSize($value);
$this->setBorderBottomSize($value);
$this->setBorderInsideHSize($value);
$this->setBorderInsideVSize($value);
return $this;
2014-03-24 11:33:20 +07:00
}
/**
* Get TLRBHV Border Color
2014-03-24 11:33:20 +07:00
*
* @return string[]
2014-03-24 11:33:20 +07:00
*/
public function getBorderColor()
2014-03-24 11:33:20 +07:00
{
return array(
$this->getBorderTopColor(),
$this->getBorderLeftColor(),
$this->getBorderRightColor(),
$this->getBorderBottomColor(),
$this->getBorderInsideHColor(),
$this->getBorderInsideVColor(),
);
2014-03-24 11:33:20 +07:00
}
/**
* Set TLRBHV Border Color
*
* @param string $value
* @return self
2014-03-24 11:33:20 +07:00
*/
public function setBorderColor($value = null)
2014-03-24 11:33:20 +07:00
{
$this->setBorderTopColor($value);
$this->setBorderLeftColor($value);
$this->setBorderRightColor($value);
$this->setBorderBottomColor($value);
$this->setBorderInsideHColor($value);
$this->setBorderInsideVColor($value);
return $this;
2014-03-24 11:33:20 +07:00
}
/**
* Get border size inside horizontal
2014-03-24 11:33:20 +07:00
*
* @return int
2014-03-24 11:33:20 +07:00
*/
public function getBorderInsideHSize()
2014-03-24 11:33:20 +07:00
{
return $this->getTableOnlyProperty('borderInsideHSize');
2014-03-24 11:33:20 +07:00
}
/**
* Set border size inside horizontal
2014-03-24 11:33:20 +07:00
*
2014-05-04 15:13:31 +07:00
* @param int $value
* @return self
2014-03-24 11:33:20 +07:00
*/
public function setBorderInsideHSize($value = null)
2014-03-24 11:33:20 +07:00
{
return $this->setTableOnlyProperty('borderInsideHSize', $value);
2014-03-24 11:33:20 +07:00
}
/**
* Get border color inside horizontal
2014-03-24 11:33:20 +07:00
*
* @return string
2014-03-24 11:33:20 +07:00
*/
public function getBorderInsideHColor()
2014-03-24 11:33:20 +07:00
{
return $this->getTableOnlyProperty('borderInsideHColor');
2014-03-24 11:33:20 +07:00
}
/**
* Set border color inside horizontal
2014-03-24 11:33:20 +07:00
*
* @param string $value
* @return self
2014-03-24 11:33:20 +07:00
*/
public function setBorderInsideHColor($value = null)
2014-03-24 11:33:20 +07:00
{
return $this->setTableOnlyProperty('borderInsideHColor', $value, false);
2014-03-24 11:33:20 +07:00
}
/**
* Get border size inside vertical
2014-03-24 11:33:20 +07:00
*
2014-05-04 15:13:31 +07:00
* @return int
2014-03-24 11:33:20 +07:00
*/
public function getBorderInsideVSize()
2014-03-24 11:33:20 +07:00
{
return $this->getTableOnlyProperty('borderInsideVSize');
2014-03-24 11:33:20 +07:00
}
/**
* Set border size inside vertical
2014-03-24 11:33:20 +07:00
*
* @param int $value
* @return self
2014-03-24 11:33:20 +07:00
*/
public function setBorderInsideVSize($value = null)
2014-03-24 11:33:20 +07:00
{
return $this->setTableOnlyProperty('borderInsideVSize', $value);
2014-03-24 11:33:20 +07:00
}
/**
* Get border color inside vertical
2014-03-24 11:33:20 +07:00
*
2014-05-04 15:13:31 +07:00
* @return string
2014-03-24 11:33:20 +07:00
*/
public function getBorderInsideVColor()
2014-03-24 11:33:20 +07:00
{
return $this->getTableOnlyProperty('borderInsideVColor');
2014-03-24 11:33:20 +07:00
}
/**
* Set border color inside vertical
2014-03-24 11:33:20 +07:00
*
2014-05-04 15:13:31 +07:00
* @param string $value
* @return self
2014-03-24 11:33:20 +07:00
*/
public function setBorderInsideVColor($value = null)
2014-03-24 11:33:20 +07:00
{
return $this->setTableOnlyProperty('borderInsideVColor', $value, false);
2014-03-24 11:33:20 +07:00
}
/**
* Get cell margin top
2014-03-24 11:33:20 +07:00
*
* @return int
2014-03-24 11:33:20 +07:00
*/
public function getCellMarginTop()
2014-03-24 11:33:20 +07:00
{
return $this->getTableOnlyProperty('cellMarginTop');
}
/**
* Set cell margin top
*
* @param int $value
* @return self
*/
public function setCellMarginTop($value = null)
{
return $this->setTableOnlyProperty('cellMarginTop', $value);
}
/**
* Get cell margin left
*
* @return int
*/
public function getCellMarginLeft()
{
return $this->getTableOnlyProperty('cellMarginLeft');
}
/**
* Set cell margin left
*
* @param int $value
* @return self
*/
public function setCellMarginLeft($value = null)
{
return $this->setTableOnlyProperty('cellMarginLeft', $value);
}
/**
* Get cell margin right
*
* @return int
*/
public function getCellMarginRight()
{
return $this->getTableOnlyProperty('cellMarginRight');
}
/**
* Set cell margin right
*
* @param int $value
* @return self
*/
public function setCellMarginRight($value = null)
{
return $this->setTableOnlyProperty('cellMarginRight', $value);
}
/**
* Get cell margin bottom
*
* @return int
*/
public function getCellMarginBottom()
{
return $this->getTableOnlyProperty('cellMarginBottom');
}
/**
* Set cell margin bottom
*
* @param int $value
* @return self
*/
public function setCellMarginBottom($value = null)
{
return $this->setTableOnlyProperty('cellMarginBottom', $value);
}
/**
* Get cell margin
*
2014-05-23 18:32:56 +07:00
* @return int[]
*/
public function getCellMargin()
{
return array(
$this->cellMarginTop,
$this->cellMarginLeft,
$this->cellMarginRight,
$this->cellMarginBottom
);
}
/**
* Set TLRB cell margin
*
* @param int $value Margin in twips
* @return self
*/
public function setCellMargin($value = null)
{
$this->setCellMarginTop($value);
$this->setCellMarginLeft($value);
$this->setCellMarginRight($value);
$this->setCellMarginBottom($value);
return $this;
}
/**
* Check if any of the margin is not null
*
* @return bool
*/
public function hasMargin()
{
$margins = $this->getCellMargin();
return $margins !== array_filter($margins, 'is_null');
}
/**
* Get shading
*
* @return \PhpOffice\PhpWord\Style\Shading
*/
public function getShading()
{
return $this->shading;
}
/**
* Set shading
*
2014-05-07 09:47:02 +07:00
* @param mixed $value
* @return self
*/
public function setShading($value = null)
{
2014-05-07 09:47:02 +07:00
$this->setObjectVal($value, 'Shading', $this->shading);
return $this;
}
/**
* Get alignment
*
* @return string
*/
public function getAlign()
{
return $this->alignment->getValue();
}
/**
* Set alignment
*
* @param string $value
* @return self
*/
public function setAlign($value = null)
{
$this->alignment->setValue($value);
return $this;
}
/**
* Get width
*
* @return int|float
*/
public function getWidth()
{
return $this->width;
}
/**
* Set width
*
* @param int|float $value
* @return self
*/
public function setWidth($value = null)
{
$this->width = $this->setNumericVal($value, $this->width);
return $this;
}
/**
* Get width unit
*
* @return string
*/
public function getUnit()
{
return $this->unit;
}
/**
* Set width unit
*
* @param string $value
* @return self
*/
public function setUnit($value = null)
{
$enum = array(self::WIDTH_AUTO, self::WIDTH_PERCENT, self::WIDTH_TWIP);
$this->unit = $this->setEnumVal($value, $enum, $this->unit);
return $this;
}
/**
2014-05-15 14:41:08 +07:00
* Get table style only property by checking if it's a firstRow
*
* This is necessary since firstRow style is cloned from table style but
* without certain properties activated, e.g. margins
*
* @param string $property
* @return int|string|null
*/
private function getTableOnlyProperty($property)
{
2014-05-15 14:41:08 +07:00
if ($this->isFirstRow === false) {
return $this->$property;
}
return null;
}
/**
2014-05-15 14:41:08 +07:00
* Set table style only property by checking if it's a firstRow
*
* This is necessary since firstRow style is cloned from table style but
* without certain properties activated, e.g. margins
*
* @param string $property
* @param int|string $value
* @param bool $isNumeric
* @return self
*/
private function setTableOnlyProperty($property, $value, $isNumeric = true)
{
2014-05-15 14:41:08 +07:00
if ($this->isFirstRow === false) {
if ($isNumeric === true) {
$this->$property = $this->setNumericVal($value, $this->$property);
} else {
$this->$property = $value;
}
}
return $this;
}
}