diff --git a/phpmd.xml.dist b/phpmd.xml.dist
index 34759560..f0b62b2d 100644
--- a/phpmd.xml.dist
+++ b/phpmd.xml.dist
@@ -9,9 +9,14 @@
-
+
+
+
+
+
+
-
+
diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php
index b8dfe8cd..ebd79cd9 100644
--- a/src/PhpWord/Settings.php
+++ b/src/PhpWord/Settings.php
@@ -95,7 +95,7 @@ class Settings
/**
* Measurement unit
*
- * @var string
+ * @var int|float
*/
private static $measurementUnit = self::UNIT_TWIP;
diff --git a/src/PhpWord/Style/AbstractStyle.php b/src/PhpWord/Style/AbstractStyle.php
index 00fe6a09..5be042bd 100644
--- a/src/PhpWord/Style/AbstractStyle.php
+++ b/src/PhpWord/Style/AbstractStyle.php
@@ -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
*/
diff --git a/src/PhpWord/Writer/HTML/Element/Container.php b/src/PhpWord/Writer/HTML/Element/Container.php
index 079e16c2..54ec7ee6 100644
--- a/src/PhpWord/Writer/HTML/Element/Container.php
+++ b/src/PhpWord/Writer/HTML/Element/Container.php
@@ -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);
diff --git a/src/PhpWord/Writer/RTF/Element/Text.php b/src/PhpWord/Writer/RTF/Element/Text.php
index ae97fd3a..31966c7d 100644
--- a/src/PhpWord/Writer/RTF/Element/Text.php
+++ b/src/PhpWord/Writer/RTF/Element/Text.php
@@ -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)
diff --git a/src/PhpWord/Writer/Word2007/Element/Table.php b/src/PhpWord/Writer/Word2007/Element/Table.php
index f62dfbb4..6349172a 100644
--- a/src/PhpWord/Writer/Word2007/Element/Table.php
+++ b/src/PhpWord/Writer/Word2007/Element/Table.php
@@ -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
}
/**
diff --git a/src/PhpWord/Writer/Word2007/Style/MarginBorder.php b/src/PhpWord/Writer/Word2007/Style/MarginBorder.php
index 28cf1224..b5c05ca9 100644
--- a/src/PhpWord/Writer/Word2007/Style/MarginBorder.php
+++ b/src/PhpWord/Writer/Word2007/Style/MarginBorder.php
@@ -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
*
diff --git a/src/PhpWord/Writer/Word2007/Style/Row.php b/src/PhpWord/Writer/Word2007/Style/Row.php
index 175aa16b..5517cc2b 100644
--- a/src/PhpWord/Writer/Word2007/Style/Row.php
+++ b/src/PhpWord/Writer/Word2007/Style/Row.php
@@ -17,8 +17,6 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
-use PhpOffice\PhpWord\Style\Row as RowStyle;
-
/**
* Row style writer
*
diff --git a/src/PhpWord/Writer/Word2007/Style/Table.php b/src/PhpWord/Writer/Word2007/Style/Table.php
index 3fbf8497..d996df00 100644
--- a/src/PhpWord/Writer/Word2007/Style/Table.php
+++ b/src/PhpWord/Writer/Word2007/Style/Table.php
@@ -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;
+ }
}