174 lines
4.7 KiB
PHP
Raw Normal View History

2014-04-26 11:14:27 +07: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.
2014-04-26 11:14:27 +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
2014-04-26 11:14:27 +07:00
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style\Alignment as AlignmentStyle;
use PhpOffice\PhpWord\Style\Table as TableStyle;
2014-04-26 11:14:27 +07:00
/**
* Table style writer
*
* @since 0.10.0
*/
class Table extends AbstractStyle
{
/**
* @var int Table width
2014-04-26 11:14:27 +07:00
*/
private $width;
2014-04-26 11:14:27 +07:00
/**
* Write style
*/
public function write()
{
2014-05-11 11:23:46 +07:00
$style = $this->getStyle();
2014-05-07 09:47:02 +07:00
$xmlWriter = $this->getXmlWriter();
2014-05-08 19:25:29 +07:00
2014-05-15 14:41:08 +07:00
if ($style instanceof TableStyle) {
$this->writeStyle($xmlWriter, $style);
} elseif (is_string($style)) {
$xmlWriter->startElement('w:tblPr');
$xmlWriter->startElement('w:tblStyle');
$xmlWriter->writeAttribute('w:val', $style);
$xmlWriter->endElement();
if ($this->width !== null) {
$this->writeWidth($xmlWriter, $this->width, 'pct');
}
$xmlWriter->endElement();
}
}
/**
* Write full style
*/
private function writeStyle(XMLWriter $xmlWriter, TableStyle $style)
{
// w:tblPr
$xmlWriter->startElement('w:tblPr');
// Alignment
$styleWriter = new Alignment($xmlWriter, new AlignmentStyle(array('value' => $style->getAlign())));
$styleWriter->write();
$this->writeWidth($xmlWriter, $style->getWidth(), $style->getUnit());
$this->writeMargin($xmlWriter, $style);
$this->writeBorder($xmlWriter, $style);
$xmlWriter->endElement(); // w:tblPr
2014-05-08 19:25:29 +07:00
$this->writeShading($xmlWriter, $style);
// First row style
$firstRow = $style->getFirstRow();
2014-05-15 14:41:08 +07:00
if ($firstRow instanceof TableStyle) {
$this->writeFirstRow($xmlWriter, $firstRow);
}
}
/**
* Write width
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param int $width
* @param string $unit
*/
private function writeWidth(XMLWriter $xmlWriter, $width, $unit)
{
$xmlWriter->startElement('w:tblW');
$xmlWriter->writeAttribute('w:w', $width);
$xmlWriter->writeAttribute('w:type', $unit);
$xmlWriter->endElement(); // w:tblW
}
/**
* Write margin
*/
private function writeMargin(XMLWriter $xmlWriter, TableStyle $style)
{
if ($style->hasMargin()) {
$xmlWriter->startElement('w:tblCellMar');
$styleWriter = new MarginBorder($xmlWriter);
$styleWriter->setSizes($style->getCellMargin());
$styleWriter->write();
$xmlWriter->endElement(); // w:tblCellMar
2014-04-26 11:14:27 +07:00
}
}
/**
* Write border
2014-04-26 11:14:27 +07:00
*/
private function writeBorder(XMLWriter $xmlWriter, TableStyle $style)
2014-04-26 11:14:27 +07:00
{
if ($style->hasBorder()) {
$xmlWriter->startElement('w:tblBorders');
$styleWriter = new MarginBorder($xmlWriter);
$styleWriter->setSizes($style->getBorderSize());
$styleWriter->setColors($style->getBorderColor());
$styleWriter->write();
$xmlWriter->endElement(); // w:tblBorders
}
2014-04-26 11:14:27 +07:00
}
/**
* Write row style
*/
private function writeFirstRow(XMLWriter $xmlWriter, TableStyle $style)
2014-04-26 11:14:27 +07:00
{
2014-05-07 09:47:02 +07:00
$xmlWriter->startElement('w:tblStylePr');
$xmlWriter->writeAttribute('w:type', 'firstRow');
$xmlWriter->startElement('w:tcPr');
$this->writeBorder($xmlWriter, $style);
$this->writeShading($xmlWriter, $style);
$xmlWriter->endElement(); // w:tcPr
$xmlWriter->endElement(); // w:tblStylePr
}
/**
* Write shading
*/
private function writeShading(XMLWriter $xmlWriter, TableStyle $style)
{
if ($style->getShading() !== null) {
$xmlWriter->startElement('w:tcPr');
2014-05-07 09:47:02 +07:00
$styleWriter = new Shading($xmlWriter, $style->getShading());
$styleWriter->write();
2014-04-26 11:14:27 +07:00
$xmlWriter->endElement();
2014-04-26 11:14:27 +07:00
}
}
2014-04-26 11:14:27 +07:00
/**
* Set width
*
* @param int $value
*/
public function setWidth($value = null)
{
$this->width = $value;
2014-04-26 11:14:27 +07:00
}
}