177 lines
3.8 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;
2014-04-03 08:44:41 +07:00
use PhpOffice\PhpWord\Style\Table as TableStyle;
2014-04-03 09:20:21 +07:00
/**
* Table element
*/
class Table extends AbstractElement
{
/**
* Table style
*
* @var \PhpOffice\PhpWord\Style\Table
*/
2014-04-03 08:44:41 +07:00
private $style;
/**
* Table rows
*
2014-05-19 00:14:14 +07:00
* @var \PhpOffice\PhpWord\Element\Row[]
*/
2014-04-03 08:44:41 +07:00
private $rows = array();
/**
* Table width
*
2014-05-11 22:54:51 +07:00
* @var int
*/
2014-04-03 08:44:41 +07:00
private $width = null;
/**
* Create a new table
*
* @param mixed $style
*/
public function __construct($style = null)
{
$this->style = $this->setNewStyle(new TableStyle(), $style);
}
/**
* Add a row
*
2014-05-11 22:54:51 +07:00
* @param int $height
2014-03-17 07:26:25 +07:00
* @param mixed $style
2014-05-11 22:54:51 +07:00
* @return \PhpOffice\PhpWord\Element\Row
*/
public function addRow($height = null, $style = null)
{
2014-05-11 22:54:51 +07:00
$row = new Row($height, $style);
$row->setParentContainer($this);
2014-04-03 08:44:41 +07:00
$this->rows[] = $row;
2014-05-11 22:54:51 +07:00
return $row;
}
/**
* Add a cell
*
2014-05-11 22:54:51 +07:00
* @param int $width
* @param mixed $style
2014-05-11 22:54:51 +07:00
* @return \PhpOffice\PhpWord\Element\Cell
*/
public function addCell($width = null, $style = null)
{
$index = count($this->rows) - 1;
2014-05-19 00:14:14 +07:00
$row = $this->rows[$index];
$cell = $row->addCell($width, $style);
2014-05-11 22:54:51 +07:00
return $cell;
}
/**
* Get all rows
*
2014-05-19 00:14:14 +07:00
* @return \PhpOffice\PhpWord\Element\Row[]
*/
public function getRows()
{
2014-04-03 08:44:41 +07:00
return $this->rows;
}
/**
* Get table style
*
* @return \PhpOffice\PhpWord\Style\Table
*/
public function getStyle()
{
2014-04-03 08:44:41 +07:00
return $this->style;
}
/**
2014-05-31 09:29:09 +07:00
* Get table width
*
2014-05-31 09:29:09 +07:00
* @return int
*/
2014-05-31 09:29:09 +07:00
public function getWidth()
{
2014-05-31 09:29:09 +07:00
return $this->width;
}
/**
* Set table width.
*
2014-05-31 09:29:09 +07:00
* @param int $width
*/
2014-05-31 09:29:09 +07:00
public function setWidth($width)
{
2014-05-31 09:29:09 +07:00
$this->width = $width;
}
2014-04-11 23:02:05 +07:00
/**
* Get column count
*
2014-05-11 22:54:51 +07:00
* @return int
2014-04-11 23:02:05 +07:00
*/
public function countColumns()
{
$columnCount = 0;
if (is_array($this->rows)) {
$rowCount = count($this->rows);
for ($i = 0; $i < $rowCount; $i++) {
2014-05-19 00:14:14 +07:00
/** @var \PhpOffice\PhpWord\Element\Row $row Type hint */
$row = $this->rows[$i];
$cellCount = count($row->getCells());
2014-04-11 23:02:05 +07:00
if ($columnCount < $cellCount) {
$columnCount = $cellCount;
}
}
}
return $columnCount;
}
2018-04-18 22:34:53 +02:00
/**
* The first declared cell width for each column
*
* @return int[]
*/
public function findFirstDefinedCellWidths()
{
$cellWidths = array();
if (is_array($this->rows)) {
foreach ($this->rows as $row) {
$cells = $row->getCells();
if (count($cells) <= count($cellWidths)) {
continue;
}
$cellWidths = array();
foreach ($cells as $cell) {
$cellWidths[] = $cell->getWidth();
}
}
}
return $cellWidths;
}
2012-05-06 18:25:40 +02:00
}