PHPWord/src/PhpWord/Style/Table.php

545 lines
11 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-03-24 11:33:20 +07:00
/**
* Style for first row
*
* @var \PhpOffice\PhpWord\Style\Table
*/
private $firstRow = null;
2014-03-24 11:33:20 +07:00
/**
* Cell margin top
*
* @var int
*/
private $cellMarginTop = null;
/**
* Cell margin left
*
* @var int
*/
private $cellMarginLeft = null;
/**
* Cell margin right
*
* @var int
*/
private $cellMarginRight = null;
/**
* Cell margin bottom
*
* @var int
*/
private $cellMarginBottom = null;
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();
if (!is_null($firstRowStyle) && is_array($firstRowStyle)) {
$this->firstRow = clone $this;
unset($this->firstRow->firstRow);
unset($this->firstRow->cellMarginBottom);
unset($this->firstRow->cellMarginTop);
unset($this->firstRow->cellMarginLeft);
unset($this->firstRow->cellMarginRight);
unset($this->firstRow->borderInsideVColor);
unset($this->firstRow->borderInsideVSize);
unset($this->firstRow->borderInsideHColor);
unset($this->firstRow->borderInsideHSize);
$this->firstRow->setStyleByArray($firstRowStyle);
2014-03-24 11:33:20 +07:00
}
if (!is_null($tableStyle) && is_array($tableStyle)) {
$this->setStyleByArray($tableStyle);
2014-03-24 11:33:20 +07:00
}
}
2014-03-24 11:33:20 +07:00
/**
* Get First Row Style
*
* @return \PhpOffice\PhpWord\Style\Table
*/
public function getFirstRow()
{
return $this->firstRow;
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();
} else {
return null;
}
2014-03-24 11:33:20 +07:00
}
/**
* Set background
*
* @param string $value
2014-03-24 11:33:20 +07:00
* @return \PhpOffice\PhpWord\Style\Table
*/
public function setBgColor($value = null)
2014-03-24 11:33:20 +07:00
{
$this->setShading(array('fill' => $value));
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 Size
2014-03-24 11:33:20 +07:00
*
2014-05-02 01:15:28 +07:00
* @return int[]
2014-03-24 11:33:20 +07:00
*/
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 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 TLRBHV Border Color
2014-03-24 11:33:20 +07:00
*
2014-05-02 01:15:28 +07:00
* @return string[]
2014-03-24 11:33:20 +07:00
*/
public function getBorderColor()
{
return array(
$this->getBorderTopColor(),
$this->getBorderLeftColor(),
$this->getBorderRightColor(),
$this->getBorderBottomColor(),
$this->getBorderInsideHColor(),
$this->getBorderInsideVColor(),
);
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
2014-03-24 11:33:20 +07:00
*/
public function setBorderInsideHSize($value = null)
2014-03-24 11:33:20 +07:00
{
$this->borderInsideHSize = $value;
2014-03-24 11:33:20 +07:00
}
/**
* Get border size inside horizontal
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 getBorderInsideHSize()
2014-03-24 11:33:20 +07:00
{
return (isset($this->borderInsideHSize)) ? $this->borderInsideHSize : null;
2014-03-24 11:33:20 +07:00
}
/**
* Set border size inside vertical
2014-03-24 11:33:20 +07:00
*
2014-05-04 15:13:31 +07:00
* @param int $value
2014-03-24 11:33:20 +07:00
*/
public function setBorderInsideVSize($value = null)
2014-03-24 11:33:20 +07:00
{
$this->borderInsideVSize = $value;
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 (isset($this->borderInsideVSize)) ? $this->borderInsideVSize : null;
2014-03-24 11:33:20 +07:00
}
/**
* Set border color inside horizontal
2014-03-24 11:33:20 +07:00
*
2014-05-04 15:13:31 +07:00
* @param string $value
2014-03-24 11:33:20 +07:00
*/
public function setBorderInsideHColor($value = null)
2014-03-24 11:33:20 +07:00
{
$this->borderInsideHColor = $value;
2014-03-24 11:33:20 +07:00
}
/**
* Get border color inside horizontal
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 getBorderInsideHColor()
2014-03-24 11:33:20 +07:00
{
return (isset($this->borderInsideHColor)) ? $this->borderInsideHColor : null;
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
2014-03-24 11:33:20 +07:00
*/
public function setBorderInsideVColor($value = null)
2014-03-24 11:33:20 +07:00
{
$this->borderInsideVColor = $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 (isset($this->borderInsideVColor)) ? $this->borderInsideVColor : null;
}
/**
* Set cell margin top
*
* @param int $value
*/
public function setCellMarginTop($value = null)
{
$this->cellMarginTop = $value;
}
/**
* Get cell margin top
*
* @return int
*/
public function getCellMarginTop()
{
return $this->cellMarginTop;
}
/**
* Set cell margin left
*
* @param int $value
*/
public function setCellMarginLeft($value = null)
{
$this->cellMarginLeft = $value;
}
/**
* Get cell margin left
*
* @return int
*/
public function getCellMarginLeft()
{
return $this->cellMarginLeft;
}
/**
* Set cell margin right
*
* @param int $value
*/
public function setCellMarginRight($value = null)
{
$this->cellMarginRight = $value;
}
/**
* Get cell margin right
*
* @return int
*/
public function getCellMarginRight()
{
return $this->cellMarginRight;
}
/**
* Set cell margin bottom
*
* @param int $value
*/
public function setCellMarginBottom($value = null)
{
$this->cellMarginBottom = $value;
}
/**
* Get cell margin bottom
*
* @return int
*/
public function getCellMarginBottom()
{
return $this->cellMarginBottom;
}
/**
* Set TLRB cell margin
*
* @param int $value Margin in twips
*/
public function setCellMargin($value = null)
{
$this->setCellMarginTop($value);
$this->setCellMarginLeft($value);
$this->setCellMarginRight($value);
$this->setCellMarginBottom($value);
}
/**
* Get cell margin
*
2014-05-02 01:15:28 +07:00
* @return int[]
*/
public function getCellMargin()
{
return array($this->cellMarginTop, $this->cellMarginLeft, $this->cellMarginRight, $this->cellMarginBottom);
}
/**
* Has margins?
*
* @return bool
*/
public function hasMargins()
{
$hasMargins = false;
$margins = $this->getCellMargin();
for ($i = 0; $i < count($margins); $i++) {
if (!is_null($margins[$i])) {
$hasMargins = true;
}
}
return $hasMargins;
}
/**
* 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;
}
}