diff --git a/.scrutinizer.yml b/.scrutinizer.yml index d253de30..c7d3c9e5 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -18,7 +18,7 @@ tools: enabled: true timeout: 900 php_sim: - min_mass: 30 + min_mass: 16 php_pdepend: true php_analyzer: true sensiolabs_security_checker: true diff --git a/CHANGELOG.md b/CHANGELOG.md index 8878e3db..45b35a21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r - QA: Add `.scrutinizer.yml` and include `composer.lock` for preparation to Scrutinizer - @ivanlanin GH-186 - Writer: Refactor writer parts using composite pattern - @ivanlanin - Docs: Show code quality and test code coverage badge on README +- Style: Change behaviour of `set...` function of boolean properties; when none is defined, assumed true - @ivanlanin ## 0.10.0 - 4 May 2014 diff --git a/phpmd.xml.dist b/phpmd.xml.dist index 18d5b2a9..34759560 100644 --- a/phpmd.xml.dist +++ b/phpmd.xml.dist @@ -8,11 +8,12 @@ - - + + + - + diff --git a/samples/Sample_09_Tables.php b/samples/Sample_09_Tables.php index 6732e336..882653bc 100644 --- a/samples/Sample_09_Tables.php +++ b/samples/Sample_09_Tables.php @@ -51,7 +51,7 @@ for($i = 1; $i <= 8; $i++) { // 3. colspan (gridSpan) and rowspan (vMerge) -$section->addTextBreak(1); +$section->addPageBreak(); $section->addText("Table with colspan and rowspan", $header); $styleTable = array('borderSize' => 6, 'borderColor' => '999999'); diff --git a/samples/Sample_25_TextBox.php b/samples/Sample_25_TextBox.php index 4beb9553..0a659ddb 100644 --- a/samples/Sample_25_TextBox.php +++ b/samples/Sample_25_TextBox.php @@ -8,7 +8,7 @@ $phpWord = new \PhpOffice\PhpWord\PhpWord(); $section = $phpWord->addSection(); // In section -$textbox = $section->addTextBox(array('align' => 'left', 'width' => 400, 'height' => 150, 'borderSize' => 1, 'borderColor' => '#FF0000')); +$textbox = $section->addTextBox(array('align' => 'center', 'width' => 400, 'height' => 150, 'borderSize' => 1, 'borderColor' => '#FF0000')); $textbox->addText('Text box content in section.'); $textbox->addText('Another line.'); $cell = $textbox->addTable()->addRow()->addCell(); @@ -22,7 +22,7 @@ $textbox->addText('Textbox inside table'); // Inside header with textrun $header = $section->addHeader(); -$textbox = $header->addTextBox(array('align' => 'center', 'width' => 600, 'borderSize' => 1, 'borderColor' => '#00FF00')); +$textbox = $header->addTextBox(array('width' => 600, 'borderSize' => 1, 'borderColor' => '#00FF00')); $textrun = $textbox->addTextRun(); $textrun->addText('TextBox in header. TextBox can contain a TextRun '); $textrun->addText('with bold text', array('bold' => true)); diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 59795af7..78f1d99d 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -219,7 +219,13 @@ class Html $text = $cNode->nodeValue; } } - $object->addListItem($text, $data['listdepth'], $styles['fontStyle'], $styles['listStyle'], $styles['paragraphStyle']); + $object->addListItem( + $text, + $data['listdepth'], + $styles['fontStyle'], + $styles['listStyle'], + $styles['paragraphStyle'] + ); } } diff --git a/src/PhpWord/Style/AbstractStyle.php b/src/PhpWord/Style/AbstractStyle.php index d909a20a..00fe6a09 100644 --- a/src/PhpWord/Style/AbstractStyle.php +++ b/src/PhpWord/Style/AbstractStyle.php @@ -152,13 +152,13 @@ abstract class AbstractStyle } /** - * Set boolean value + * Set bool value * - * @param mixed $value - * @param boolean|null $default - * @return boolean|null + * @param bool $value + * @param bool $default + * @return bool */ - protected function setBoolVal($value, $default = null) + protected function setBoolVal($value, $default) { if (!is_bool($value)) { $value = $default; @@ -184,7 +184,7 @@ abstract class AbstractStyle } /** - * Set float value: Convert string that contains only numeric into integer + * Set integer value: Convert string that contains only numeric into integer * * @param mixed $value * @param int|null $default diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index 85ad52b9..4fe70068 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -333,7 +333,7 @@ class Font extends AbstractStyle * @param bool $value * @return self */ - public function setBold($value = false) + public function setBold($value = true) { $this->bold = $this->setBoolVal($value, $this->bold); @@ -356,7 +356,7 @@ class Font extends AbstractStyle * @param bool $value * @return self */ - public function setItalic($value = false) + public function setItalic($value = true) { $this->italic = $this->setBoolVal($value, $this->italic); @@ -402,7 +402,7 @@ class Font extends AbstractStyle * @param bool $value * @return self */ - public function setSuperScript($value = false) + public function setSuperScript($value = true) { $this->superScript = $this->setBoolVal($value, $this->superScript); $this->toggleFalse($this->subScript, $this->superScript); @@ -426,13 +426,10 @@ class Font extends AbstractStyle * @param bool $value * @return self */ - public function setSubScript($value = false) + public function setSubScript($value = true) { $this->subScript = $this->setBoolVal($value, $this->subScript); $this->toggleFalse($this->subScript, $this->superScript); - if ($this->subScript) { - $this->superScript = false; - } return $this; } @@ -453,7 +450,7 @@ class Font extends AbstractStyle * @param bool $value * @return self */ - public function setStrikethrough($value = false) + public function setStrikethrough($value = true) { $this->strikethrough = $this->setBoolVal($value, $this->strikethrough); $this->toggleFalse($this->doubleStrikethrough, $this->strikethrough); @@ -477,7 +474,7 @@ class Font extends AbstractStyle * @param bool $value * @return self */ - public function setDoubleStrikethrough($value = false) + public function setDoubleStrikethrough($value = true) { $this->doubleStrikethrough = $this->setBoolVal($value, $this->doubleStrikethrough); $this->toggleFalse($this->strikethrough, $this->doubleStrikethrough); @@ -501,7 +498,7 @@ class Font extends AbstractStyle * @param bool $value * @return self */ - public function setSmallCaps($value = false) + public function setSmallCaps($value = true) { $this->smallCaps = $this->setBoolVal($value, $this->smallCaps); $this->toggleFalse($this->allCaps, $this->smallCaps); @@ -525,7 +522,7 @@ class Font extends AbstractStyle * @param bool $value * @return self */ - public function setAllCaps($value = false) + public function setAllCaps($value = true) { $this->allCaps = $this->setBoolVal($value, $this->allCaps); $this->toggleFalse($this->smallCaps, $this->allCaps); diff --git a/src/PhpWord/Style/Image.php b/src/PhpWord/Style/Image.php index 5dc82d0b..fc9f9630 100644 --- a/src/PhpWord/Style/Image.php +++ b/src/PhpWord/Style/Image.php @@ -94,9 +94,9 @@ class Image extends AbstractStyle /** * Alignment * - * @var string + * @var \PhpOffice\PhpWord\Style\Alignment */ - private $align; + private $alignment; /** * Margin Top @@ -154,8 +154,18 @@ class Image extends AbstractStyle */ private $posVerticalRel = self::POSITION_RELATIVE_TO_LINE; + /** + * Create new instance + */ + public function __construct() + { + $this->alignment = new Alignment(); + } + /** * Get width + * + * @return int */ public function getWidth() { @@ -166,14 +176,19 @@ class Image extends AbstractStyle * Set width * * @param int $value + * @return self */ public function setWidth($value = null) { $this->width = $value; + + return $this; } /** * Get height + * + * @return int */ public function getHeight() { @@ -184,32 +199,40 @@ class Image extends AbstractStyle * Set height * * @param int $value + * @return self */ public function setHeight($value = null) { $this->height = $value; + + return $this; } /** * Get alignment + * + * @return string */ public function getAlign() { - return $this->align; + return $this->alignment->getValue(); } /** * Set alignment * * @param string $value + * @return self */ public function setAlign($value = null) { - $this->align = $value; + $this->alignment->setValue($value); + + return $this; } /** - * Get Margin Top + * Get margin top * * @return int */ @@ -219,7 +242,7 @@ class Image extends AbstractStyle } /** - * Set Margin Top + * Set margin top * * @param int $value * @return self @@ -227,11 +250,12 @@ class Image extends AbstractStyle public function setMarginTop($value = null) { $this->marginTop = $value; + return $this; } /** - * Get Margin Left + * Get margin left * * @return int */ @@ -241,7 +265,7 @@ class Image extends AbstractStyle } /** - * Set Margin Left + * Set margin left * * @param int $value * @return self @@ -249,6 +273,7 @@ class Image extends AbstractStyle public function setMarginLeft($value = null) { $this->marginLeft = $value; + return $this; } diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index 56609119..9c8e0c5b 100644 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -420,7 +420,7 @@ class Paragraph extends AbstractStyle * @param bool $value * @return self */ - public function setKeepNext($value = false) + public function setKeepNext($value = true) { $this->keepNext = $this->setBoolVal($value, $this->keepNext); @@ -443,7 +443,7 @@ class Paragraph extends AbstractStyle * @param bool $value * @return self */ - public function setKeepLines($value = false) + public function setKeepLines($value = true) { $this->keepLines = $this->setBoolVal($value, $this->keepLines); @@ -466,7 +466,7 @@ class Paragraph extends AbstractStyle * @param bool $value * @return self */ - public function setPageBreakBefore($value = false) + public function setPageBreakBefore($value = true) { $this->pageBreakBefore = $this->setBoolVal($value, $this->pageBreakBefore); diff --git a/src/PhpWord/Style/Row.php b/src/PhpWord/Style/Row.php index 310564d8..45897aed 100644 --- a/src/PhpWord/Style/Row.php +++ b/src/PhpWord/Style/Row.php @@ -68,7 +68,7 @@ class Row extends AbstractStyle * @param bool $value * @return self */ - public function setTblHeader($value = false) + public function setTblHeader($value = true) { $this->tblHeader = $this->setBoolVal($value, $this->tblHeader); @@ -91,7 +91,7 @@ class Row extends AbstractStyle * @param bool $value * @return self */ - public function setCantSplit($value = false) + public function setCantSplit($value = true) { $this->cantSplit = $this->setBoolVal($value, $this->cantSplit); @@ -114,7 +114,7 @@ class Row extends AbstractStyle * @param bool $value * @return self */ - public function setExactHeight($value = false) + public function setExactHeight($value = true) { $this->exactHeight = $this->setBoolVal($value, $this->exactHeight); diff --git a/src/PhpWord/Writer/HTML/Element/Container.php b/src/PhpWord/Writer/HTML/Element/Container.php index c4bbc2f8..079e16c2 100644 --- a/src/PhpWord/Writer/HTML/Element/Container.php +++ b/src/PhpWord/Writer/HTML/Element/Container.php @@ -24,6 +24,13 @@ namespace PhpOffice\PhpWord\Writer\HTML\Element; */ class Container extends AbstractElement { + /** + * Namespace; Can't use __NAMESPACE__ in inherited class (RTF) + * + * @var string + */ + protected $namespace = 'PhpOffice\\PhpWord\\Writer\\HTML\\Element'; + /** * Write container * @@ -40,8 +47,10 @@ class Container extends AbstractElement $content = ''; $elements = $container->getElements(); + $elementClass = ''; foreach ($elements as $element) { - $writerClass = str_replace('\\Element', '\\Writer\\HTML\\Element', get_class($element)); + $elementClass = get_class($element); + $writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, $elementClass); if (class_exists($writerClass)) { $writer = new $writerClass($this->parentWriter, $element, $withoutP); $content .= $writer->write(); diff --git a/src/PhpWord/Writer/RTF/Element/Container.php b/src/PhpWord/Writer/RTF/Element/Container.php index f4b5f2ac..38c77894 100644 --- a/src/PhpWord/Writer/RTF/Element/Container.php +++ b/src/PhpWord/Writer/RTF/Element/Container.php @@ -25,29 +25,9 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; class Container extends \PhpOffice\PhpWord\Writer\HTML\Element\Container { /** - * Write container + * Namespace; Can't use __NAMESPACE__ in inherited class (RTF) * - * @return string + * @var string */ - public function write() - { - $container = $this->element; - if (!$container instanceof \PhpOffice\PhpWord\Element\AbstractContainer) { - return; - } - $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1); - $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false; - $content = ''; - - $elements = $container->getElements(); - foreach ($elements as $element) { - $writerClass = str_replace('\\Element', '\\Writer\\RTF\\Element', get_class($element)); - if (class_exists($writerClass)) { - $writer = new $writerClass($this->parentWriter, $element, $withoutP); - $content .= $writer->write(); - } - } - - return $content; - } + protected $namespace = 'PhpOffice\\PhpWord\\Writer\\RTF\\Element'; } diff --git a/src/PhpWord/Writer/RTF/Style/Paragraph.php b/src/PhpWord/Writer/RTF/Style/Paragraph.php index 8811cacf..26c62f02 100644 --- a/src/PhpWord/Writer/RTF/Style/Paragraph.php +++ b/src/PhpWord/Writer/RTF/Style/Paragraph.php @@ -18,7 +18,6 @@ namespace PhpOffice\PhpWord\Writer\RTF\Style; use PhpOffice\PhpWord\Style\Alignment; -use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle; /** * RTF paragraph style writer diff --git a/src/PhpWord/Writer/Word2007/Element/Container.php b/src/PhpWord/Writer/Word2007/Element/Container.php index 57a27208..20e0018a 100644 --- a/src/PhpWord/Writer/Word2007/Element/Container.php +++ b/src/PhpWord/Writer/Word2007/Element/Container.php @@ -38,25 +38,23 @@ class Container extends AbstractElement */ public function write() { - $xmlWriter = $this->getXmlWriter(); $container = $this->getElement(); if (!$container instanceof \PhpOffice\PhpWord\Element\AbstractContainer) { return; } $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1); $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote', 'ListItemRun')) ? true : false; + $xmlWriter = $this->getXmlWriter(); // Loop through elements $elements = $container->getElements(); $elementClass = ''; - if (count($elements) > 0) { - foreach ($elements as $element) { - $elementClass = get_class($element); - $writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, $elementClass); - if (class_exists($writerClass)) { - $writer = new $writerClass($xmlWriter, $element, $withoutP); - $writer->write(); - } + foreach ($elements as $element) { + $elementClass = get_class($element); + $writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, $elementClass); + if (class_exists($writerClass)) { + $writer = new $writerClass($xmlWriter, $element, $withoutP); + $writer->write(); } } diff --git a/src/PhpWord/Writer/Word2007/Element/Image.php b/src/PhpWord/Writer/Word2007/Element/Image.php index c41199f8..e044606e 100644 --- a/src/PhpWord/Writer/Word2007/Element/Image.php +++ b/src/PhpWord/Writer/Word2007/Element/Image.php @@ -57,15 +57,8 @@ class Image extends AbstractElement if (!$this->withoutP) { $xmlWriter->startElement('w:p'); - if (!is_null($style->getAlign())) { - $xmlWriter->startElement('w:pPr'); - $xmlWriter->startElement('w:jc'); - $xmlWriter->writeAttribute('w:val', $style->getAlign()); - $xmlWriter->endElement(); // w:jc - $xmlWriter->endElement(); // w:pPr - } + $styleWriter->writeAlignment(); } - $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:pict'); $xmlWriter->startElement('v:shape'); diff --git a/src/PhpWord/Writer/Word2007/Element/Object.php b/src/PhpWord/Writer/Word2007/Element/Object.php index 07580a42..ae03f04f 100644 --- a/src/PhpWord/Writer/Word2007/Element/Object.php +++ b/src/PhpWord/Writer/Word2007/Element/Object.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; +use PhpOffice\PhpWord\Writer\Word2007\Style\Image as ImageStyleWriter; + /** * Object element writer * @@ -40,17 +42,11 @@ class Object extends AbstractElement $shapeId = md5($rIdObject . '_' . $rIdImage); $objectId = $element->getRelationId() + 1325353440; $style = $element->getStyle(); - $align = $style->getAlign(); + $styleWriter = new ImageStyleWriter($xmlWriter, $style); if (!$this->withoutP) { $xmlWriter->startElement('w:p'); - } - if (!is_null($align)) { - $xmlWriter->startElement('w:pPr'); - $xmlWriter->startElement('w:jc'); - $xmlWriter->writeAttribute('w:val', $align); - $xmlWriter->endElement(); - $xmlWriter->endElement(); + $styleWriter->writeAlignment(); } $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:object'); diff --git a/src/PhpWord/Writer/Word2007/Element/Table.php b/src/PhpWord/Writer/Word2007/Element/Table.php index 4ce97d10..f62dfbb4 100644 --- a/src/PhpWord/Writer/Word2007/Element/Table.php +++ b/src/PhpWord/Writer/Word2007/Element/Table.php @@ -21,8 +21,10 @@ use PhpOffice\PhpWord\Element\Cell as CellElement; use PhpOffice\PhpWord\Element\Row as RowElement; use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Style\Cell as CellStyle; +use PhpOffice\PhpWord\Style\Row as RowStyle; use PhpOffice\PhpWord\Style\Table as TableStyle; use PhpOffice\PhpWord\Writer\Word2007\Style\Cell as CellStyleWriter; +use PhpOffice\PhpWord\Writer\Word2007\Style\Row as RowStyleWriter; use PhpOffice\PhpWord\Writer\Word2007\Style\Table as TableStyleWriter; /** @@ -109,33 +111,21 @@ class Table extends AbstractElement */ private function writeRow(XMLWriter $xmlWriter, RowElement $row) { - $height = $row->getHeight(); - $rowStyle = $row->getStyle(); - $xmlWriter->startElement('w:tr'); - if (!is_null($height) || $rowStyle->isTblHeader() || $rowStyle->isCantSplit()) { - $xmlWriter->startElement('w:trPr'); - if (!is_null($height)) { - $xmlWriter->startElement('w:trHeight'); - $xmlWriter->writeAttribute('w:val', $height); - $xmlWriter->writeAttribute('w:hRule', ($rowStyle->isExactHeight() ? 'exact' : 'atLeast')); - $xmlWriter->endElement(); - } - if ($rowStyle->isTblHeader()) { - $xmlWriter->startElement('w:tblHeader'); - $xmlWriter->writeAttribute('w:val', '1'); - $xmlWriter->endElement(); - } - if ($rowStyle->isCantSplit()) { - $xmlWriter->startElement('w:cantSplit'); - $xmlWriter->writeAttribute('w:val', '1'); - $xmlWriter->endElement(); - } - $xmlWriter->endElement(); + + // Write style + $rowStyle = $row->getStyle(); + if ($rowStyle instanceof RowStyle) { + $styleWriter = new RowStyleWriter($xmlWriter, $rowStyle); + $styleWriter->setHeight($row->getHeight()); + $styleWriter->write(); } + + // Write cells foreach ($row->getCells() as $cell) { $this->writeCell($xmlWriter, $cell); } + $xmlWriter->endElement(); // w:tr } @@ -144,20 +134,18 @@ class Table extends AbstractElement */ private function writeCell(XMLWriter $xmlWriter, CellElement $cell) { - $cellStyle = $cell->getStyle(); $xmlWriter->startElement('w:tc'); - $xmlWriter->startElement('w:tcPr'); - $xmlWriter->startElement('w:tcW'); - $xmlWriter->writeAttribute('w:w', $cell->getWidth()); - $xmlWriter->writeAttribute('w:type', 'dxa'); - $xmlWriter->endElement(); // w:tcW + + // Write style + $cellStyle = $cell->getStyle(); if ($cellStyle instanceof CellStyle) { $styleWriter = new CellStyleWriter($xmlWriter, $cellStyle); + $styleWriter->setWidth($cell->getWidth()); $styleWriter->write(); } - $xmlWriter->endElement(); // w:tcPr + // Write content $containerWriter = new Container($xmlWriter, $cell); $containerWriter->write(); diff --git a/src/PhpWord/Writer/Word2007/Element/TextBox.php b/src/PhpWord/Writer/Word2007/Element/TextBox.php index bb1629bc..4ee5e68e 100644 --- a/src/PhpWord/Writer/Word2007/Element/TextBox.php +++ b/src/PhpWord/Writer/Word2007/Element/TextBox.php @@ -41,13 +41,7 @@ class TextBox extends AbstractElement if (!$this->withoutP) { $xmlWriter->startElement('w:p'); - if (!is_null($style->getAlign())) { - $xmlWriter->startElement('w:pPr'); - $xmlWriter->startElement('w:jc'); - $xmlWriter->writeAttribute('w:val', $style->getAlign()); - $xmlWriter->endElement(); // w:jc - $xmlWriter->endElement(); // w:pPr - } + $styleWriter->writeAlignment(); } $xmlWriter->startElement('w:r'); diff --git a/src/PhpWord/Writer/Word2007/Style/Cell.php b/src/PhpWord/Writer/Word2007/Style/Cell.php index c6e21625..ce7efb3b 100644 --- a/src/PhpWord/Writer/Word2007/Style/Cell.php +++ b/src/PhpWord/Writer/Word2007/Style/Cell.php @@ -26,6 +26,11 @@ use PhpOffice\PhpWord\Style\Cell as CellStyle; */ class Cell extends AbstractStyle { + /** + * @var int Cell width + */ + private $width; + /** * Write style */ @@ -37,6 +42,14 @@ class Cell extends AbstractStyle } $xmlWriter = $this->getXmlWriter(); + $xmlWriter->startElement('w:tcPr'); + + // Width + $xmlWriter->startElement('w:tcW'); + $xmlWriter->writeAttribute('w:w', $this->width); + $xmlWriter->writeAttribute('w:type', 'dxa'); + $xmlWriter->endElement(); // w:tcW + // Text direction $textDir = $style->getTextDirection(); $xmlWriter->writeElementIf(!is_null($textDir), 'w:textDirection', 'w:val', $textDir); @@ -70,5 +83,17 @@ class Cell extends AbstractStyle $vMerge = $style->getVMerge(); $xmlWriter->writeElementIf(!is_null($gridSpan), 'w:gridSpan', 'w:val', $gridSpan); $xmlWriter->writeElementIf(!is_null($vMerge), 'w:vMerge', 'w:val', $vMerge); + + $xmlWriter->endElement(); // w:tcPr + } + + /** + * Set width + * + * @param int $value + */ + public function setWidth($value = null) + { + $this->width = $value; } } diff --git a/src/PhpWord/Writer/Word2007/Style/Image.php b/src/PhpWord/Writer/Word2007/Style/Image.php index 0b25f4b9..ecc3fb7b 100644 --- a/src/PhpWord/Writer/Word2007/Style/Image.php +++ b/src/PhpWord/Writer/Word2007/Style/Image.php @@ -17,6 +17,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Style; +use PhpOffice\PhpWord\Style\Alignment as AlignmentStyle; use PhpOffice\PhpWord\Style\Image as ImageStyle; /** @@ -45,23 +46,6 @@ class Image extends AbstractStyle $this->writeStyle($style); } - /** - * Write w10 wrapping - * - * @return array - */ - public function writeW10Wrap() - { - if (is_null($this->w10wrap)) { - return; - } - - $xmlWriter = $this->getXmlWriter(); - $xmlWriter->startElement('w10:wrap'); - $xmlWriter->writeAttribute('type', $this->w10wrap); - $xmlWriter->endElement(); // w10:wrap - } - /** * Write style attribute */ @@ -117,6 +101,38 @@ class Image extends AbstractStyle $xmlWriter->writeAttribute('style', $imageStyle); } + /** + * Write alignment + */ + public function writeAlignment() + { + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Image) { + return; + } + + $xmlWriter = $this->getXmlWriter(); + $xmlWriter->startElement('w:pPr'); + $styleWriter = new Alignment($xmlWriter, new AlignmentStyle(array('value' => $style->getAlign()))); + $styleWriter->write(); + $xmlWriter->endElement(); // w:pPr + } + + /** + * Write w10 wrapping + */ + public function writeW10Wrap() + { + if (is_null($this->w10wrap)) { + return; + } + + $xmlWriter = $this->getXmlWriter(); + $xmlWriter->startElement('w10:wrap'); + $xmlWriter->writeAttribute('type', $this->w10wrap); + $xmlWriter->endElement(); // w10:wrap + } + /** * Get element style * diff --git a/src/PhpWord/Writer/Word2007/Style/Row.php b/src/PhpWord/Writer/Word2007/Style/Row.php new file mode 100644 index 00000000..175aa16b --- /dev/null +++ b/src/PhpWord/Writer/Word2007/Style/Row.php @@ -0,0 +1,68 @@ +getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Row) { + return; + } + + $xmlWriter = $this->getXmlWriter(); + $xmlWriter->startElement('w:trPr'); + + if ($this->height !== null) { + $xmlWriter->startElement('w:trHeight'); + $xmlWriter->writeAttribute('w:val', $this->height); + $xmlWriter->writeAttribute('w:hRule', ($style->isExactHeight() ? 'exact' : 'atLeast')); + $xmlWriter->endElement(); + } + $xmlWriter->writeElementIf($style->isTblHeader(), 'w:tblHeader', 'w:val', '1'); + $xmlWriter->writeElementIf($style->isCantSplit(), 'w:cantSplit', 'w:val', '1'); + + $xmlWriter->endElement(); // w:trPr + } + + /** + * Set height + * + * @param int $value + */ + public function setHeight($value = null) + { + $this->height = $value; + } +} diff --git a/tests/PhpWord/Tests/Style/FontTest.php b/tests/PhpWord/Tests/Style/FontTest.php index 74aa5e2e..68342efd 100644 --- a/tests/PhpWord/Tests/Style/FontTest.php +++ b/tests/PhpWord/Tests/Style/FontTest.php @@ -59,17 +59,21 @@ class FontTest extends \PHPUnit_Framework_TestCase 'size' => PhpWord::DEFAULT_FONT_SIZE, 'bold' => false, 'italic' => false, + 'underline' => Font::UNDERLINE_NONE, 'superScript' => false, 'subScript' => false, - 'underline' => Font::UNDERLINE_NONE, 'strikethrough' => false, + 'doubleStrikethrough' => false, + 'smallCaps' => false, + 'allCaps' => false, + 'doubleStrikethrough' => false, 'color' => PhpWord::DEFAULT_FONT_COLOR, 'fgColor' => null, 'bgColor' => null, 'hint' => PhpWord::DEFAULT_FONT_CONTENT_TYPE, ); foreach ($attributes as $key => $default) { - $get = "get{$key}"; + $get = is_bool($default) ? "is{$key}" : "get{$key}"; $object->setStyleValue("$key", null); $this->assertEquals($default, $object->$get()); $object->setStyleValue("$key", ''); @@ -89,10 +93,13 @@ class FontTest extends \PHPUnit_Framework_TestCase 'size' => 9, 'bold' => true, 'italic' => true, + 'underline' => Font::UNDERLINE_HEAVY, 'superScript' => true, 'subScript' => false, - 'underline' => Font::UNDERLINE_HEAVY, 'strikethrough' => true, + 'doubleStrikethrough' => false, + 'smallCaps' => true, + 'allCaps' => false, 'color' => '999999', 'fgColor' => Font::FGCOLOR_YELLOW, 'bgColor' => 'FFFF00', @@ -101,7 +108,7 @@ class FontTest extends \PHPUnit_Framework_TestCase ); $object->setStyleByArray($attributes); foreach ($attributes as $key => $value) { - $get = "get{$key}"; + $get = is_bool($value) ? "is{$key}" : "get{$key}"; $this->assertEquals($value, $object->$get()); } } diff --git a/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php b/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php index b036c21b..7798f30e 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php @@ -179,7 +179,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase $textrun->addTextBreak(); $textrun = $section->addTextRun($aStyle); $textrun->addLink('http://test.com'); - $textrun->addImage($imageSrc, array('align' => 'top')); + $textrun->addImage($imageSrc, array('align' => 'center')); $textrun->addFootnote(); $doc = TestHelperDOCX::getDocument($phpWord); diff --git a/tests/PhpWord/Tests/Writer/Word2007/StyleTest.php b/tests/PhpWord/Tests/Writer/Word2007/StyleTest.php index dfcf3fee..cc722efb 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/StyleTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/StyleTest.php @@ -30,7 +30,7 @@ class StyleTest extends \PHPUnit_Framework_TestCase { $styles = array( 'Alignment', 'Cell', 'Font', 'Image', 'Indentation', 'LineNumbering', - 'Paragraph', 'Section', 'Shading', 'Spacing', 'Tab', 'Table', 'TextBox' + 'Paragraph', 'Row', 'Section', 'Shading', 'Spacing', 'Tab', 'Table', 'TextBox' ); foreach ($styles as $style) { $objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Style\\' . $style;