Refactoring: Word2007 table and marginborder style writer

This commit is contained in:
Ivan Lanin 2014-05-13 14:54:40 +07:00
parent 8745c5ee30
commit 7ae8c3cb81
9 changed files with 205 additions and 157 deletions

View File

@ -9,9 +9,14 @@
<rule ref="rulesets/design.xml/EvalExpression" />
<rule ref="rulesets/design.xml/GotoStatement" />
<rule ref="rulesets/design.xml/DepthOfInheritance" />
<rule ref="rulesets/design.xml/CouplingBetweenObjects" />
<rule ref="rulesets/design.xml/CouplingBetweenObjects">
<!-- AbstractContainer needs more coupling (default: 13) -->
<properties>
<property name="minimum" value="20" />
</properties>
</rule>
<rule ref="rulesets/design.xml/NumberOfChildren">
<!-- AbstractElement and AbstractStyle still needs a lot of children classes -->
<!-- AbstractStyle needs more children (default: 15) -->
<properties>
<property name="minimum" value="20" />
</properties>

View File

@ -95,7 +95,7 @@ class Settings
/**
* Measurement unit
*
* @var string
* @var int|float
*/
private static $measurementUnit = self::UNIT_TWIP;

View File

@ -138,7 +138,7 @@ abstract class AbstractStyle
/**
* Set default for null and empty value
*
* @param mixed $value
* @param string $value
* @param mixed $default
* @return mixed
*/
@ -186,7 +186,7 @@ abstract class AbstractStyle
/**
* Set integer value: Convert string that contains only numeric into integer
*
* @param mixed $value
* @param int|null $value
* @param int|null $default
* @return int|null
*/

View File

@ -47,7 +47,6 @@ class Container extends AbstractElement
$content = '';
$elements = $container->getElements();
$elementClass = '';
foreach ($elements as $element) {
$elementClass = get_class($element);
$writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, $elementClass);

View File

@ -99,7 +99,7 @@ class Text extends AbstractElement
/**
* Write font style beginning
*
* @param mixed $style
* @param \PhpOffice\PhpWord\Style\Font $style
* @return string
*/
private function writeFontStyle($style)

View File

@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Element\Cell as CellElement;
use PhpOffice\PhpWord\Element\Row as RowElement;
use PhpOffice\PhpWord\Element\Table as TableElement;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style\Cell as CellStyle;
use PhpOffice\PhpWord\Style\Row as RowStyle;
@ -51,59 +52,54 @@ class Table extends AbstractElement
if ($rowCount > 0) {
$xmlWriter->startElement('w:tbl');
// Table grid
$cellWidths = array();
for ($i = 0; $i < $rowCount; $i++) {
$row = $rows[$i];
$cells = $row->getCells();
if (count($cells) <= count($cellWidths)) {
continue;
}
$cellWidths = array();
foreach ($cells as $cell) {
$cellWidths[] = $cell->getWidth();
}
}
$xmlWriter->startElement('w:tblGrid');
foreach ($cellWidths as $width) {
$xmlWriter->startElement('w:gridCol');
if (!is_null($width)) {
$xmlWriter->writeAttribute('w:w', $width);
$xmlWriter->writeAttribute('w:type', 'dxa');
}
$xmlWriter->endElement();
}
$xmlWriter->endElement(); // w:tblGrid
// Write columns
$this->writeColumns($xmlWriter, $element);
// Table style
$tblStyle = $element->getStyle();
$tblWidth = $element->getWidth();
if ($tblStyle instanceof TableStyle) {
$styleWriter = new TableStyleWriter($xmlWriter, $tblStyle);
$styleWriter->setIsFullStyle(false);
$styleWriter->write();
} else {
if (!empty($tblStyle)) {
$xmlWriter->startElement('w:tblPr');
$xmlWriter->startElement('w:tblStyle');
$xmlWriter->writeAttribute('w:val', $tblStyle);
$xmlWriter->endElement();
if (!is_null($tblWidth)) {
$xmlWriter->startElement('w:tblW');
$xmlWriter->writeAttribute('w:w', $tblWidth);
$xmlWriter->writeAttribute('w:type', 'pct');
$xmlWriter->endElement();
}
$xmlWriter->endElement();
}
}
// Write style
$styleWriter = new TableStyleWriter($xmlWriter, $element->getStyle());
$styleWriter->setWidth($element->getWidth());
$styleWriter->write();
// Table rows
// Write rows
for ($i = 0; $i < $rowCount; $i++) {
$this->writeRow($xmlWriter, $rows[$i]);
}
$xmlWriter->endElement(); // w:tbl
}
}
/**
* Write column
*/
private function writeColumns(XMLWriter $xmlWriter, TableElement $element)
{
$rows = $element->getRows();
$rowCount = count($rows);
$cellWidths = array();
for ($i = 0; $i < $rowCount; $i++) {
$row = $rows[$i];
$cells = $row->getCells();
if (count($cells) <= count($cellWidths)) {
continue;
}
$cellWidths = array();
foreach ($cells as $cell) {
$cellWidths[] = $cell->getWidth();
}
}
$xmlWriter->startElement('w:tblGrid');
foreach ($cellWidths as $width) {
$xmlWriter->startElement('w:gridCol');
if ($width !== null) {
$xmlWriter->writeAttribute('w:w', $width);
$xmlWriter->writeAttribute('w:type', 'dxa');
}
$xmlWriter->endElement();
}
$xmlWriter->endElement(); // w:tblGrid
}
/**

View File

@ -17,6 +17,8 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\Shared\XMLWriter;
/**
* Margin border style writer
*
@ -56,31 +58,47 @@ class MarginBorder extends AbstractStyle
$sizeCount = count($this->sizes) - 1;
for ($i = 0; $i < $sizeCount; $i++) {
if (!is_null($this->sizes[$i])) {
$xmlWriter->startElement('w:' . $sides[$i]);
if (!empty($this->colors)) {
if (is_null($this->colors[$i]) && !empty($this->attributes)) {
if (array_key_exists('defaultColor', $this->attributes)) {
$this->colors[$i] = $this->attributes['defaultColor'];
}
}
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $this->sizes[$i]);
$xmlWriter->writeAttribute('w:color', $this->colors[$i]);
if (!empty($this->attributes)) {
if (array_key_exists('space', $this->attributes)) {
$xmlWriter->writeAttribute('w:space', $this->attributes['space']);
}
}
} else {
$xmlWriter->writeAttribute('w:w', $this->sizes[$i]);
$xmlWriter->writeAttribute('w:type', 'dxa');
if ($this->sizes[$i] !== null) {
$color = null;
if (isset($this->colors[$i])) {
$color = $this->colors[$i];
}
$xmlWriter->endElement();
$this->writeSide($xmlWriter, $sides[$i], $this->sizes[$i], $color);
}
}
}
/**
* Write side
*
* @param string $side
* @param int $width
* @param string $color
*/
private function writeSide(XMLWriter $xmlWriter, $side, $width, $color = null)
{
$xmlWriter->startElement('w:' . $side);
if (!empty($this->colors)) {
if ($color === null && !empty($this->attributes)) {
if (array_key_exists('defaultColor', $this->attributes)) {
$color = $this->attributes['defaultColor'];
}
}
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $width);
$xmlWriter->writeAttribute('w:color', $color);
if (!empty($this->attributes)) {
if (array_key_exists('space', $this->attributes)) {
$xmlWriter->writeAttribute('w:space', $this->attributes['space']);
}
}
} else {
$xmlWriter->writeAttribute('w:w', $width);
$xmlWriter->writeAttribute('w:type', 'dxa');
}
$xmlWriter->endElement();
}
/**
* Set sizes
*

View File

@ -17,8 +17,6 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\Style\Row as RowStyle;
/**
* Row style writer
*

View File

@ -17,7 +17,9 @@
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;
/**
* Table style writer
@ -27,11 +29,9 @@ use PhpOffice\PhpWord\Style\Alignment as AlignmentStyle;
class Table extends AbstractStyle
{
/**
* Is full style
*
* @var bool
* @var int Table width
*/
private $isFullStyle = true;
private $width;
/**
* Write style
@ -39,105 +39,137 @@ class Table extends AbstractStyle
public function write()
{
$style = $this->getStyle();
if (!$style instanceof \PhpOffice\PhpWord\Style\Table) {
return;
}
$xmlWriter = $this->getXmlWriter();
// w:tblPr
$hasMargins = $style->hasMargins();
$hasBorders = $style->hasBorders();
$align = $style->getAlign();
$xmlWriter->startElement('w:tblPr');
$xmlWriter->startElement('w:tblW');
$xmlWriter->writeAttribute('w:w', $style->getWidth());
$xmlWriter->writeAttribute('w:type', $style->getUnit());
$xmlWriter->endElement(); // w:tblW
// Alignment
$styleWriter = new Alignment($xmlWriter, new AlignmentStyle(array('value' => $align)));
$styleWriter->write();
// Margins
if ($hasMargins) {
$mbWriter = new MarginBorder($xmlWriter);
$mbWriter->setSizes($style->getCellMargin());
$xmlWriter->startElement('w:tblCellMar');
$mbWriter->write();
$xmlWriter->endElement(); // w:tblCellMar
}
// Borders
if ($hasBorders) {
$mbWriter = new MarginBorder($xmlWriter);
$mbWriter->setSizes($style->getBorderSize());
$mbWriter->setColors($style->getBorderColor());
$xmlWriter->startElement('w:tblBorders');
$mbWriter->write();
$xmlWriter->endElement(); // w:tblBorders
}
$xmlWriter->endElement(); // w:tblPr
// Only write background color and first row for full style
if ($this->isFullStyle) {
// Background color
if (!is_null($style->getShading())) {
$xmlWriter->startElement('w:tcPr');
$styleWriter = new Shading($xmlWriter, $style->getShading());
$styleWriter->write();
$xmlWriter->endElement();
}
// First Row
$firstRow = $style->getFirstRow();
if ($firstRow instanceof \PhpOffice\PhpWord\Style\Table) {
$this->writeFirstRow($firstRow);
if ($style instanceof \PhpOffice\PhpWord\Style\Table) {
$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();
}
}
/**
* Set is full style
*
* @param bool $value
* Write full style
*/
public function setIsFullStyle($value)
private function writeStyle(XMLWriter $xmlWriter, TableStyle $style)
{
$this->isFullStyle = $value;
// 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
$this->writeShading($xmlWriter, $style);
// First row style
$firstRow = $style->getFirstRow();
if ($firstRow instanceof \PhpOffice\PhpWord\Style\Table) {
$this->writeFirstRow($xmlWriter, $firstRow);
}
}
/**
* Write width
*
* @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->hasMargins()) {
$xmlWriter->startElement('w:tblCellMar');
$styleWriter = new MarginBorder($xmlWriter);
$styleWriter->setSizes($style->getCellMargin());
$styleWriter->write();
$xmlWriter->endElement(); // w:tblCellMar
}
}
/**
* Write border
*/
private function writeBorder(XMLWriter $xmlWriter, TableStyle $style)
{
if ($style->hasBorders()) {
$xmlWriter->startElement('w:tblBorders');
$styleWriter = new MarginBorder($xmlWriter);
$styleWriter->setSizes($style->getBorderSize());
$styleWriter->setColors($style->getBorderColor());
$styleWriter->write();
$xmlWriter->endElement(); // w:tblBorders
}
}
/**
* Write row style
*/
private function writeFirstRow(\PhpOffice\PhpWord\Style\Table $style)
private function writeFirstRow(XMLWriter $xmlWriter, TableStyle $style)
{
$xmlWriter = $this->getXmlWriter();
$xmlWriter->startElement('w:tblStylePr');
$xmlWriter->writeAttribute('w:type', 'firstRow');
$xmlWriter->startElement('w:tcPr');
if (!is_null($style->getShading())) {
$styleWriter = new Shading($xmlWriter, $style->getShading());
$styleWriter->write();
}
// Borders
if ($style->hasBorders()) {
$mbWriter = new MarginBorder($xmlWriter);
$mbWriter->setSizes($style->getBorderSize());
$mbWriter->setColors($style->getBorderColor());
$xmlWriter->startElement('w:tcBorders');
$mbWriter->write();
$xmlWriter->endElement(); // w:tcBorders
}
$this->writeBorder($xmlWriter, $style);
$this->writeShading($xmlWriter, $style);
$xmlWriter->endElement(); // w:tcPr
$xmlWriter->endElement(); // w:tblStylePr
}
/**
* Write shading
*
* @param int $width
* @param string $unit
*/
private function writeShading(XMLWriter $xmlWriter, TableStyle $style)
{
if ($style->getShading() !== null) {
$xmlWriter->startElement('w:tcPr');
$styleWriter = new Shading($xmlWriter, $style->getShading());
$styleWriter->write();
$xmlWriter->endElement();
}
}
/**
* Set width
*
* @param int $value
*/
public function setWidth($value = null)
{
$this->width = $value;
}
}