From c243a11e5756c0dfb00c1b4155d8283f60a29e1d Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 11 May 2014 18:57:58 +0700 Subject: [PATCH 01/48] Additional type checks in style writers --- src/PhpWord/Element/ListItemRun.php | 1 - src/PhpWord/Shared/XMLWriter.php | 8 ++++---- src/PhpWord/Style/Font.php | 2 +- src/PhpWord/Writer/HTML/Style/AbstractStyle.php | 4 ++-- src/PhpWord/Writer/Word2007/Style/Image.php | 10 ++++------ src/PhpWord/Writer/Word2007/Style/TextBox.php | 12 +++++++----- 6 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/PhpWord/Element/ListItemRun.php b/src/PhpWord/Element/ListItemRun.php index 1a52bd89..a6dbd932 100644 --- a/src/PhpWord/Element/ListItemRun.php +++ b/src/PhpWord/Element/ListItemRun.php @@ -17,7 +17,6 @@ namespace PhpOffice\PhpWord\Element; -use PhpOffice\PhpWord\Shared\String; use PhpOffice\PhpWord\Style\ListItem as ListItemStyle; use PhpOffice\PhpWord\Style\Paragraph; diff --git a/src/PhpWord/Shared/XMLWriter.php b/src/PhpWord/Shared/XMLWriter.php index a16fe0b4..fd1f1f60 100644 --- a/src/PhpWord/Shared/XMLWriter.php +++ b/src/PhpWord/Shared/XMLWriter.php @@ -153,14 +153,14 @@ class XMLWriter /** * Write element if ... * - * @param bool $condition + * @param bool|null $condition * @param string $element * @param string $attribute * @param string $value */ public function writeElementIf($condition, $element, $attribute = null, $value = null) { - if ($condition) { + if ($condition == true) { if (is_null($attribute)) { $this->xmlWriter->writeElement($element, $value); } else { @@ -174,13 +174,13 @@ class XMLWriter /** * Write attribute if ... * - * @param bool $condition + * @param bool|null $condition * @param string $attribute * @param string $value */ public function writeAttributeIf($condition, $attribute, $value) { - if ($condition) { + if ($condition == true) { $this->xmlWriter->writeAttribute($attribute, $value); } } diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index 32874b2f..85ad52b9 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -651,7 +651,7 @@ class Font extends AbstractStyle /** * Toggle $target property to false when $source true * - * @param bool $target Target property + * @param bool|null $target Target property * @param bool $sourceValue */ private function toggleFalse(&$target, $sourceValue) diff --git a/src/PhpWord/Writer/HTML/Style/AbstractStyle.php b/src/PhpWord/Writer/HTML/Style/AbstractStyle.php index d25e7b6a..3b6d99c1 100644 --- a/src/PhpWord/Writer/HTML/Style/AbstractStyle.php +++ b/src/PhpWord/Writer/HTML/Style/AbstractStyle.php @@ -87,12 +87,12 @@ abstract class AbstractStyle /** * Get value if ... * - * @param bool $condition + * @param bool|null $condition * @param string $value * @return string */ protected function getValueIf($condition, $value) { - return $condition ? $value : ''; + return $condition == true ? $value : ''; } } diff --git a/src/PhpWord/Writer/Word2007/Style/Image.php b/src/PhpWord/Writer/Word2007/Style/Image.php index a891625b..0b25f4b9 100644 --- a/src/PhpWord/Writer/Word2007/Style/Image.php +++ b/src/PhpWord/Writer/Word2007/Style/Image.php @@ -42,7 +42,7 @@ class Image extends AbstractStyle if (!$style instanceof \PhpOffice\PhpWord\Style\Image) { return; } - $this->writeStyle(); + $this->writeStyle($style); } /** @@ -65,10 +65,9 @@ class Image extends AbstractStyle /** * Write style attribute */ - protected function writeStyle() + protected function writeStyle(ImageStyle $style) { $xmlWriter = $this->getXmlWriter(); - $style = $this->getStyle(); // Default style array $styleArray = array( @@ -77,7 +76,7 @@ class Image extends AbstractStyle 'mso-width-relative' => 'margin', 'mso-height-relative' => 'margin', ); - $styleArray = array_merge($styleArray, $this->getElementStyle()); + $styleArray = array_merge($styleArray, $this->getElementStyle($style)); // Absolute/relative positioning $positioning = $style->getPositioning(); @@ -123,9 +122,8 @@ class Image extends AbstractStyle * * @return array */ - private function getElementStyle() + private function getElementStyle(ImageStyle $style) { - $style = $this->getStyle(); $styles = array(); $styleValues = array( 'width' => $style->getWidth(), diff --git a/src/PhpWord/Writer/Word2007/Style/TextBox.php b/src/PhpWord/Writer/Word2007/Style/TextBox.php index 2bbe40e9..d8456ae8 100644 --- a/src/PhpWord/Writer/Word2007/Style/TextBox.php +++ b/src/PhpWord/Writer/Word2007/Style/TextBox.php @@ -35,8 +35,8 @@ class TextBox extends Image if (!$style instanceof \PhpOffice\PhpWord\Style\TextBox) { return; } - $this->writeStyle(); - $this->writeBorder(); + $this->writeStyle($style); + $this->writeBorder($style); } /** @@ -50,6 +50,9 @@ class TextBox extends Image return; } $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\TextBox) { + return; + } $relativePositions = array( TextBoxStyle::POSITION_RELATIVE_TO_MARGIN => 'margin', @@ -88,7 +91,7 @@ class TextBox extends Image public function writeInnerMargin() { $style = $this->getStyle(); - if (!$style->hasInnerMargins()) { + if (!$style instanceof \PhpOffice\PhpWord\Style\TextBox || !$style->hasInnerMargins()) { return; } @@ -100,10 +103,9 @@ class TextBox extends Image /** * Writer border */ - private function writeBorder() + private function writeBorder(TextBoxStyle $style) { $xmlWriter = $this->getXmlWriter(); - $style = $this->getStyle(); // Border size $borderSize = $style->getBorderSize(); From c7e4ed0c182ec0470701cda65e9db0e894063dca Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 11 May 2014 19:41:48 +0700 Subject: [PATCH 02/48] Type check in element writers --- src/PhpWord/Writer/HTML/Element/Container.php | 3 ++ src/PhpWord/Writer/HTML/Element/Footnote.php | 4 ++ src/PhpWord/Writer/HTML/Element/Image.php | 4 ++ src/PhpWord/Writer/HTML/Element/Link.php | 4 ++ src/PhpWord/Writer/HTML/Element/ListItem.php | 4 ++ src/PhpWord/Writer/HTML/Element/Table.php | 4 ++ src/PhpWord/Writer/HTML/Element/Title.php | 4 ++ src/PhpWord/Writer/ODText/Element/Image.php | 3 ++ src/PhpWord/Writer/ODText/Element/Link.php | 3 ++ src/PhpWord/Writer/ODText/Element/Table.php | 3 ++ src/PhpWord/Writer/ODText/Element/Text.php | 3 ++ src/PhpWord/Writer/RTF/Element/Container.php | 3 ++ src/PhpWord/Writer/RTF/Element/Text.php | 21 +++++---- .../Writer/Word2007/Element/CheckBox.php | 3 ++ .../Writer/Word2007/Element/Container.php | 3 ++ .../Writer/Word2007/Element/Footnote.php | 3 ++ src/PhpWord/Writer/Word2007/Element/Image.php | 19 ++++---- src/PhpWord/Writer/Word2007/Element/Link.php | 3 ++ .../Writer/Word2007/Element/ListItem.php | 3 ++ .../Writer/Word2007/Element/ListItemRun.php | 3 ++ .../Writer/Word2007/Element/Object.php | 3 ++ .../Writer/Word2007/Element/PreserveText.php | 3 ++ src/PhpWord/Writer/Word2007/Element/TOC.php | 3 ++ src/PhpWord/Writer/Word2007/Element/Table.php | 16 +++---- src/PhpWord/Writer/Word2007/Element/Text.php | 3 ++ .../Writer/Word2007/Element/TextBox.php | 3 ++ .../Writer/Word2007/Element/TextBreak.php | 3 ++ src/PhpWord/Writer/Word2007/Element/Title.php | 3 ++ .../PhpWord/Tests/Writer/HTML/ElementTest.php | 41 +++++++++++++++++ .../Tests/Writer/ODText/ElementTest.php | 42 +++++++++++++++++ .../PhpWord/Tests/Writer/RTF/ElementTest.php | 41 +++++++++++++++++ .../Tests/Writer/Word2007/ElementTest.php | 45 +++++++++++++++++++ 32 files changed, 281 insertions(+), 25 deletions(-) create mode 100644 tests/PhpWord/Tests/Writer/HTML/ElementTest.php create mode 100644 tests/PhpWord/Tests/Writer/ODText/ElementTest.php create mode 100644 tests/PhpWord/Tests/Writer/RTF/ElementTest.php create mode 100644 tests/PhpWord/Tests/Writer/Word2007/ElementTest.php diff --git a/src/PhpWord/Writer/HTML/Element/Container.php b/src/PhpWord/Writer/HTML/Element/Container.php index 6249426a..c55a7921 100644 --- a/src/PhpWord/Writer/HTML/Element/Container.php +++ b/src/PhpWord/Writer/HTML/Element/Container.php @@ -32,6 +32,9 @@ class Container extends AbstractElement public function write() { $container = $this->element; + if (!$this->element 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 = ''; diff --git a/src/PhpWord/Writer/HTML/Element/Footnote.php b/src/PhpWord/Writer/HTML/Element/Footnote.php index cd1baacb..30398b17 100644 --- a/src/PhpWord/Writer/HTML/Element/Footnote.php +++ b/src/PhpWord/Writer/HTML/Element/Footnote.php @@ -38,6 +38,10 @@ class Footnote extends AbstractElement */ public function write() { + if (!$this->element instanceof \PhpOffice\PhpWord\Element\Footnote) { + return; + } + $noteId = count($this->parentWriter->getNotes()) + 1; $noteMark = $this->noteType . '-' . $this->element->getRelationId(); $content = "{$noteId}"; diff --git a/src/PhpWord/Writer/HTML/Element/Image.php b/src/PhpWord/Writer/HTML/Element/Image.php index 2ae87865..5c2edbb2 100644 --- a/src/PhpWord/Writer/HTML/Element/Image.php +++ b/src/PhpWord/Writer/HTML/Element/Image.php @@ -34,6 +34,10 @@ class Image extends Text */ public function write() { + if (!$this->element instanceof \PhpOffice\PhpWord\Element\Image) { + return; + } + $content = ''; if (!$this->parentWriter->isPdf()) { $imageData = $this->getBase64ImageData($this->element); diff --git a/src/PhpWord/Writer/HTML/Element/Link.php b/src/PhpWord/Writer/HTML/Element/Link.php index 6f1977f8..820a1526 100644 --- a/src/PhpWord/Writer/HTML/Element/Link.php +++ b/src/PhpWord/Writer/HTML/Element/Link.php @@ -31,6 +31,10 @@ class Link extends Text */ public function write() { + if (!$this->element instanceof \PhpOffice\PhpWord\Element\Link) { + return; + } + $content = ''; $content .= $this->writeOpening(); $content .= "element->getTarget()}\">{$this->element->getText()}"; diff --git a/src/PhpWord/Writer/HTML/Element/ListItem.php b/src/PhpWord/Writer/HTML/Element/ListItem.php index 722d920a..8e302b03 100644 --- a/src/PhpWord/Writer/HTML/Element/ListItem.php +++ b/src/PhpWord/Writer/HTML/Element/ListItem.php @@ -31,6 +31,10 @@ class ListItem extends AbstractElement */ public function write() { + if (!$this->element instanceof \PhpOffice\PhpWord\Element\ListItem) { + return; + } + $text = htmlspecialchars($this->element->getTextObject()->getText()); $content = '

' . $text . '

' . PHP_EOL; diff --git a/src/PhpWord/Writer/HTML/Element/Table.php b/src/PhpWord/Writer/HTML/Element/Table.php index a92deea6..bd7795ef 100644 --- a/src/PhpWord/Writer/HTML/Element/Table.php +++ b/src/PhpWord/Writer/HTML/Element/Table.php @@ -31,6 +31,10 @@ class Table extends AbstractElement */ public function write() { + if (!$this->element instanceof \PhpOffice\PhpWord\Element\Table) { + return; + } + $content = ''; $rows = $this->element->getRows(); $rowCount = count($rows); diff --git a/src/PhpWord/Writer/HTML/Element/Title.php b/src/PhpWord/Writer/HTML/Element/Title.php index 2a33a282..64d47ec2 100644 --- a/src/PhpWord/Writer/HTML/Element/Title.php +++ b/src/PhpWord/Writer/HTML/Element/Title.php @@ -31,6 +31,10 @@ class Title extends AbstractElement */ public function write() { + if (!$this->element instanceof \PhpOffice\PhpWord\Element\Title) { + return; + } + $tag = 'h' . $this->element->getDepth(); $text = htmlspecialchars($this->element->getText()); $content = "<{$tag}>{$text}" . PHP_EOL; diff --git a/src/PhpWord/Writer/ODText/Element/Image.php b/src/PhpWord/Writer/ODText/Element/Image.php index 909a9aed..3229b59c 100644 --- a/src/PhpWord/Writer/ODText/Element/Image.php +++ b/src/PhpWord/Writer/ODText/Element/Image.php @@ -33,6 +33,9 @@ class Image extends AbstractElement { $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\Image) { + return; + } $mediaIndex = $element->getMediaIndex(); $target = 'Pictures/' . $element->getTarget(); diff --git a/src/PhpWord/Writer/ODText/Element/Link.php b/src/PhpWord/Writer/ODText/Element/Link.php index 1ccac435..79d3aa24 100644 --- a/src/PhpWord/Writer/ODText/Element/Link.php +++ b/src/PhpWord/Writer/ODText/Element/Link.php @@ -31,6 +31,9 @@ class Link extends AbstractElement { $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\Link) { + return; + } if (!$this->withoutP) { $xmlWriter->startElement('text:p'); // text:p diff --git a/src/PhpWord/Writer/ODText/Element/Table.php b/src/PhpWord/Writer/ODText/Element/Table.php index 411f1f38..19e5b408 100644 --- a/src/PhpWord/Writer/ODText/Element/Table.php +++ b/src/PhpWord/Writer/ODText/Element/Table.php @@ -31,6 +31,9 @@ class Table extends AbstractElement { $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\Table) { + return; + } $rows = $element->getRows(); $rowCount = count($rows); $colCount = $element->countColumns(); diff --git a/src/PhpWord/Writer/ODText/Element/Text.php b/src/PhpWord/Writer/ODText/Element/Text.php index e960dea8..6baf3200 100644 --- a/src/PhpWord/Writer/ODText/Element/Text.php +++ b/src/PhpWord/Writer/ODText/Element/Text.php @@ -31,6 +31,9 @@ class Text extends AbstractElement { $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\Text) { + return; + } $fontStyle = $element->getFontStyle(); $paragraphStyle = $element->getParagraphStyle(); diff --git a/src/PhpWord/Writer/RTF/Element/Container.php b/src/PhpWord/Writer/RTF/Element/Container.php index 57127eb8..4ce4334c 100644 --- a/src/PhpWord/Writer/RTF/Element/Container.php +++ b/src/PhpWord/Writer/RTF/Element/Container.php @@ -32,6 +32,9 @@ class Container extends \PhpOffice\PhpWord\Writer\HTML\Element\Container public function write() { $container = $this->element; + if (!$this->element 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 = ''; diff --git a/src/PhpWord/Writer/RTF/Element/Text.php b/src/PhpWord/Writer/RTF/Element/Text.php index a52b2071..701a5be1 100644 --- a/src/PhpWord/Writer/RTF/Element/Text.php +++ b/src/PhpWord/Writer/RTF/Element/Text.php @@ -17,6 +17,7 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; +use PhpOffice\PhpWord\Element\Text as TextElement; use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Font as FontStyle; use PhpOffice\PhpWord\Writer\RTF\Style\Font as FontStyleWriter; @@ -34,10 +35,14 @@ class Text extends AbstractElement */ public function write() { - $fontStyle = $this->getFontStyle(); + if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) { + return; + } + + $fontStyle = $this->getFontStyle($this->element); $content = ''; - $content .= $this->writeParagraphStyle(); + $content .= $this->writeParagraphStyle($this->element); $content .= $this->writeFontStyleBegin($fontStyle); if ($this->parentWriter->getLastParagraphStyle() != '' || $fontStyle) { $content .= ' '; @@ -57,22 +62,22 @@ class Text extends AbstractElement * * @return string */ - private function writeParagraphStyle() + private function writeParagraphStyle(TextElement $element) { $content = ''; // Get paragraph style - $paragraphStyle = $this->element->getParagraphStyle(); + $paragraphStyle = $element->getParagraphStyle(); if (is_string($paragraphStyle)) { $paragraphStyle = Style::getStyle($paragraphStyle); } // Write style when applicable if ($paragraphStyle && !$this->withoutP) { - if ($this->parentWriter->getLastParagraphStyle() != $this->element->getParagraphStyle()) { + if ($this->parentWriter->getLastParagraphStyle() != $element->getParagraphStyle()) { $styleWriter = new ParagraphStyleWriter($paragraphStyle); $content = $styleWriter->write(); - $this->parentWriter->setLastParagraphStyle($this->element->getParagraphStyle()); + $this->parentWriter->setLastParagraphStyle($element->getParagraphStyle()); } else { $this->parentWriter->setLastParagraphStyle(); } @@ -139,9 +144,9 @@ class Text extends AbstractElement * * @return \PhpOffice\PhpWord\Style\Font */ - private function getFontStyle() + private function getFontStyle(TextElement $element) { - $fontStyle = $this->element->getFontStyle(); + $fontStyle = $element->getFontStyle(); if (is_string($fontStyle)) { $fontStyle = Style::getStyle($fontStyle); } diff --git a/src/PhpWord/Writer/Word2007/Element/CheckBox.php b/src/PhpWord/Writer/Word2007/Element/CheckBox.php index 96e210b5..1d7811cd 100644 --- a/src/PhpWord/Writer/Word2007/Element/CheckBox.php +++ b/src/PhpWord/Writer/Word2007/Element/CheckBox.php @@ -33,6 +33,9 @@ class CheckBox extends Text { $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\CheckBox) { + return; + } $name = htmlspecialchars($element->getName()); $name = String::controlCharacterPHP2OOXML($name); diff --git a/src/PhpWord/Writer/Word2007/Element/Container.php b/src/PhpWord/Writer/Word2007/Element/Container.php index 150cb324..57a27208 100644 --- a/src/PhpWord/Writer/Word2007/Element/Container.php +++ b/src/PhpWord/Writer/Word2007/Element/Container.php @@ -40,6 +40,9 @@ class Container extends AbstractElement { $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; diff --git a/src/PhpWord/Writer/Word2007/Element/Footnote.php b/src/PhpWord/Writer/Word2007/Element/Footnote.php index 646f70ed..d33a0dc9 100644 --- a/src/PhpWord/Writer/Word2007/Element/Footnote.php +++ b/src/PhpWord/Writer/Word2007/Element/Footnote.php @@ -38,6 +38,9 @@ class Footnote extends Text { $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\Footnote) { + return; + } $this->writeOpeningWP(); diff --git a/src/PhpWord/Writer/Word2007/Element/Image.php b/src/PhpWord/Writer/Word2007/Element/Image.php index c7bd8d7d..1b4284d2 100644 --- a/src/PhpWord/Writer/Word2007/Element/Image.php +++ b/src/PhpWord/Writer/Word2007/Element/Image.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; +use PhpOffice\PhpWord\Element\Image as ImageElement; +use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Writer\Word2007\Style\Image as ImageStyleWriter; /** @@ -31,22 +33,24 @@ class Image extends AbstractElement */ public function write() { + $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\Image) { + return; + } if ($element->isWatermark()) { - $this->writeWatermark(); + $this->writeWatermark($xmlWriter, $element); } else { - $this->writeImage(); + $this->writeImage($xmlWriter, $element); } } /** * Write image element */ - private function writeImage() + private function writeImage(XMLWriter $xmlWriter, ImageElement $element) { - $xmlWriter = $this->getXmlWriter(); - $element = $this->getElement(); $rId = $element->getRelationId() + ($element->isInSection() ? 6 : 0); $style = $element->getStyle(); @@ -84,11 +88,8 @@ class Image extends AbstractElement /** * Write watermark element */ - private function writeWatermark() + private function writeWatermark(XMLWriter $xmlWriter, ImageElement $element) { - $xmlWriter = $this->getXmlWriter(); - $element = $this->getElement(); - $rId = $element->getRelationId(); $style = $element->getStyle(); $style->setPositioning('absolute'); diff --git a/src/PhpWord/Writer/Word2007/Element/Link.php b/src/PhpWord/Writer/Word2007/Element/Link.php index 517e276d..3b595b4c 100644 --- a/src/PhpWord/Writer/Word2007/Element/Link.php +++ b/src/PhpWord/Writer/Word2007/Element/Link.php @@ -31,6 +31,9 @@ class Link extends Text { $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\Link) { + return; + } $rId = $element->getRelationId() + ($element->isInSection() ? 6 : 0); diff --git a/src/PhpWord/Writer/Word2007/Element/ListItem.php b/src/PhpWord/Writer/Word2007/Element/ListItem.php index e625114c..7b86efae 100644 --- a/src/PhpWord/Writer/Word2007/Element/ListItem.php +++ b/src/PhpWord/Writer/Word2007/Element/ListItem.php @@ -33,6 +33,9 @@ class ListItem extends AbstractElement { $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\ListItem) { + return; + } $textObject = $element->getTextObject(); diff --git a/src/PhpWord/Writer/Word2007/Element/ListItemRun.php b/src/PhpWord/Writer/Word2007/Element/ListItemRun.php index 4431ffe6..747c2625 100644 --- a/src/PhpWord/Writer/Word2007/Element/ListItemRun.php +++ b/src/PhpWord/Writer/Word2007/Element/ListItemRun.php @@ -33,6 +33,9 @@ class ListItemRun extends AbstractElement { $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\ListItemRun) { + return; + } $xmlWriter->startElement('w:p'); diff --git a/src/PhpWord/Writer/Word2007/Element/Object.php b/src/PhpWord/Writer/Word2007/Element/Object.php index 5f56a704..07580a42 100644 --- a/src/PhpWord/Writer/Word2007/Element/Object.php +++ b/src/PhpWord/Writer/Word2007/Element/Object.php @@ -31,6 +31,9 @@ class Object extends AbstractElement { $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\Object) { + return; + } $rIdObject = $element->getRelationId() + ($element->isInSection() ? 6 : 0); $rIdImage = $element->getImageRelationId() + ($element->isInSection() ? 6 : 0); diff --git a/src/PhpWord/Writer/Word2007/Element/PreserveText.php b/src/PhpWord/Writer/Word2007/Element/PreserveText.php index b3b00d16..97693d9b 100644 --- a/src/PhpWord/Writer/Word2007/Element/PreserveText.php +++ b/src/PhpWord/Writer/Word2007/Element/PreserveText.php @@ -33,6 +33,9 @@ class PreserveText extends Text { $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\PreserveText) { + return; + } $texts = $element->getText(); if (!is_array($texts)) { diff --git a/src/PhpWord/Writer/Word2007/Element/TOC.php b/src/PhpWord/Writer/Word2007/Element/TOC.php index 28d7bc31..98486b01 100644 --- a/src/PhpWord/Writer/Word2007/Element/TOC.php +++ b/src/PhpWord/Writer/Word2007/Element/TOC.php @@ -36,6 +36,9 @@ class TOC extends AbstractElement { $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\TOC) { + return; + } $titles = $element->getTitles(); $writeFieldMark = true; diff --git a/src/PhpWord/Writer/Word2007/Element/Table.php b/src/PhpWord/Writer/Word2007/Element/Table.php index 5c82114a..4ce97d10 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\Shared\XMLWriter; use PhpOffice\PhpWord\Style\Cell as CellStyle; use PhpOffice\PhpWord\Style\Table as TableStyle; use PhpOffice\PhpWord\Writer\Word2007\Style\Cell as CellStyleWriter; @@ -38,6 +39,9 @@ class Table extends AbstractElement { $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\Table) { + return; + } $rows = $element->getRows(); $rowCount = count($rows); @@ -94,7 +98,7 @@ class Table extends AbstractElement // Table rows for ($i = 0; $i < $rowCount; $i++) { - $this->writeRow($rows[$i]); + $this->writeRow($xmlWriter, $rows[$i]); } $xmlWriter->endElement(); } @@ -103,10 +107,8 @@ class Table extends AbstractElement /** * Write row */ - private function writeRow(RowElement $row) + private function writeRow(XMLWriter $xmlWriter, RowElement $row) { - $xmlWriter = $this->getXmlWriter(); - $height = $row->getHeight(); $rowStyle = $row->getStyle(); @@ -132,7 +134,7 @@ class Table extends AbstractElement $xmlWriter->endElement(); } foreach ($row->getCells() as $cell) { - $this->writeCell($cell); + $this->writeCell($xmlWriter, $cell); } $xmlWriter->endElement(); // w:tr } @@ -140,10 +142,8 @@ class Table extends AbstractElement /** * Write cell */ - private function writeCell(CellElement $cell) + private function writeCell(XMLWriter $xmlWriter, CellElement $cell) { - $xmlWriter = $this->getXmlWriter(); - $cellStyle = $cell->getStyle(); $xmlWriter->startElement('w:tc'); diff --git a/src/PhpWord/Writer/Word2007/Element/Text.php b/src/PhpWord/Writer/Word2007/Element/Text.php index d6402dbf..1385e6e6 100644 --- a/src/PhpWord/Writer/Word2007/Element/Text.php +++ b/src/PhpWord/Writer/Word2007/Element/Text.php @@ -35,6 +35,9 @@ class Text extends AbstractElement { $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\Text) { + return; + } $text = htmlspecialchars($element->getText()); $text = String::controlCharacterPHP2OOXML($text); diff --git a/src/PhpWord/Writer/Word2007/Element/TextBox.php b/src/PhpWord/Writer/Word2007/Element/TextBox.php index 6b63c31a..bb1629bc 100644 --- a/src/PhpWord/Writer/Word2007/Element/TextBox.php +++ b/src/PhpWord/Writer/Word2007/Element/TextBox.php @@ -33,6 +33,9 @@ class TextBox extends AbstractElement { $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\TextBox) { + return; + } $style = $element->getStyle(); $styleWriter = new TextBoxStyleWriter($xmlWriter, $style); diff --git a/src/PhpWord/Writer/Word2007/Element/TextBreak.php b/src/PhpWord/Writer/Word2007/Element/TextBreak.php index 05de2917..c74cdc7a 100644 --- a/src/PhpWord/Writer/Word2007/Element/TextBreak.php +++ b/src/PhpWord/Writer/Word2007/Element/TextBreak.php @@ -31,6 +31,9 @@ class TextBreak extends Text { $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\TextBreak) { + return; + } if (!$this->withoutP) { $hasStyle = $element->hasStyle(); diff --git a/src/PhpWord/Writer/Word2007/Element/Title.php b/src/PhpWord/Writer/Word2007/Element/Title.php index c5eda5fd..703188ef 100644 --- a/src/PhpWord/Writer/Word2007/Element/Title.php +++ b/src/PhpWord/Writer/Word2007/Element/Title.php @@ -33,6 +33,9 @@ class Title extends AbstractElement { $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\Title) { + return; + } $bookmarkId = $element->getBookmarkId(); $anchor = '_Toc' . ($bookmarkId + 252634154); diff --git a/tests/PhpWord/Tests/Writer/HTML/ElementTest.php b/tests/PhpWord/Tests/Writer/HTML/ElementTest.php new file mode 100644 index 00000000..d2ed70ea --- /dev/null +++ b/tests/PhpWord/Tests/Writer/HTML/ElementTest.php @@ -0,0 +1,41 @@ +assertEquals('', $object->write()); + } + } +} diff --git a/tests/PhpWord/Tests/Writer/ODText/ElementTest.php b/tests/PhpWord/Tests/Writer/ODText/ElementTest.php new file mode 100644 index 00000000..6354520a --- /dev/null +++ b/tests/PhpWord/Tests/Writer/ODText/ElementTest.php @@ -0,0 +1,42 @@ +write(); + + $this->assertEquals('', $xmlWriter->getData()); + } + } +} diff --git a/tests/PhpWord/Tests/Writer/RTF/ElementTest.php b/tests/PhpWord/Tests/Writer/RTF/ElementTest.php new file mode 100644 index 00000000..9f597c15 --- /dev/null +++ b/tests/PhpWord/Tests/Writer/RTF/ElementTest.php @@ -0,0 +1,41 @@ +assertEquals('', $object->write()); + } + } +} diff --git a/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php b/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php new file mode 100644 index 00000000..0a9eb4ce --- /dev/null +++ b/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php @@ -0,0 +1,45 @@ +write(); + + $this->assertEquals('', $xmlWriter->getData()); + } + } +} From c9179d681f64fac0190bc33ea79f24cd863abf56 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 11 May 2014 22:54:51 +0700 Subject: [PATCH 03/48] Refactor elements and containers --- CHANGELOG.md | 1 + src/PhpWord/Element/AbstractContainer.php | 270 ++++++++---------- src/PhpWord/Element/AbstractElement.php | 29 +- src/PhpWord/Element/Cell.php | 17 +- src/PhpWord/Element/Endnote.php | 6 +- src/PhpWord/Element/Footer.php | 6 +- src/PhpWord/Element/Footnote.php | 6 +- src/PhpWord/Element/Header.php | 7 +- src/PhpWord/Element/ListItemRun.php | 6 +- src/PhpWord/Element/Row.php | 10 +- src/PhpWord/Element/Section.php | 23 +- src/PhpWord/Element/Table.php | 20 +- src/PhpWord/Element/TextBox.php | 6 +- src/PhpWord/Element/TextRun.php | 6 +- src/PhpWord/Element/Title.php | 59 ++-- src/PhpWord/Shared/Html.php | 1 + src/PhpWord/Writer/HTML/Element/Container.php | 2 +- src/PhpWord/Writer/ODText/Style/Font.php | 12 +- src/PhpWord/Writer/RTF/Element/Container.php | 2 +- src/PhpWord/Writer/Word2007/Element/Image.php | 1 - src/PhpWord/Writer/Word2007/Element/TOC.php | 25 +- src/PhpWord/Writer/Word2007/Element/Title.php | 8 +- tests/PhpWord/Tests/Element/CellTest.php | 69 ++--- tests/PhpWord/Tests/Element/RowTest.php | 15 +- tests/PhpWord/Tests/Element/TitleTest.php | 2 +- 25 files changed, 266 insertions(+), 343 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f4cd810..af3ca786 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ This release changed PHPWord license from LGPL 2.1 to LGPL 3. - Static classes `Footnotes`, `Endnotes`, and `TOC` - `Writer\Word2007\Part`: `Numbering::writeNumbering()`, `Settings::writeSettings()`, `WebSettings::writeWebSettings()`, `ContentTypes::writeContentTypes()`, `Styles::writeStyles()`, `Document::writeDocument()` all changed into `write()` - `Writer\Word2007\Part\DocProps`: Split into `Writer\Word2007\Part\DocPropsCore` and `Writer\Word2007\Part\DocPropsApp` +- `Element\Title::getBookmarkId()` replaced by `Element\Title::getRelationId()` ### Miscellaneous diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 86b604f4..39193031 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -35,15 +35,78 @@ abstract class AbstractContainer extends AbstractElement protected $elements = array(); /** - * Set element index and unique id, and add element into elements collection + * Container type section|header|footer|footnote|endnote|cell|textrun|textbox + * + * @var string */ - protected function addElement(AbstractElement $element) + protected $container; + + /** + * Add element + * + * Each element has different number of parameters passed + * + * @param string $elementName + * @return \PhpOffice\PhpWord\Element\AbstractElement + */ + protected function addElement($elementName) { - // $type = str_replace('PhpOffice\\PhpWord\\Element\\', '', get_class($element))); + $elementClass = __NAMESPACE__ . '\\' . $elementName; + $this->checkValidity($elementName); + + // Get arguments + $args = func_get_args(); + $argsCount = func_num_args(); + $withoutP = in_array($this->container, array('TextRun', 'Footnote', 'Endnote', 'ListItemRun')); + if ($withoutP && ($elementName == 'Text' || $elementName == 'PreserveText')) { + $args[3] = null; + } + + // Create element + if ($argsCount == 1) { // Page Break + $element = new $elementClass(); + } elseif ($argsCount == 2) { // TextRun, TextBox, Table, Footnote, Endnote + $element = new $elementClass($args[1]); + } elseif ($argsCount == 3) { // Object, TextBreak, Title + $element = new $elementClass($args[1], $args[2]); + } elseif ($argsCount == 4) { // PreserveText, Text, Image + $element = new $elementClass($args[1], $args[2], $args[3]); + } elseif ($argsCount == 5) { // CheckBox, Link, ListItemRun, TOC + $element = new $elementClass($args[1], $args[2], $args[3], $args[4]); + } elseif ($argsCount == 6) { // ListItem + $element = new $elementClass($args[1], $args[2], $args[3], $args[4], $args[5]); + } + + // Set relation Id for media collection + if (in_array($elementName, array('Link', 'Image', 'Object'))) { + $mediaContainer = $this->getMediaContainer(); + if ($elementName == 'Image') { + $rId = Media::addElement($mediaContainer, strtolower($elementName), $args[1], $element); + } else { + $rId = Media::addElement($mediaContainer, strtolower($elementName), $args[1]); + } + $element->setRelationId($rId); + } + if ($elementName == 'Object') { + $rIdIcon = Media::addElement($mediaContainer, 'image', $element->getIcon(), new Image($element->getIcon())); + $element->setImageRelationId($rIdIcon); + } + + // Set relation Id for other collection + if (in_array($elementName, array('Footnote', 'Endnote', 'Title')) && $this->phpWord instanceof PhpWord) { + $addMethod = "add{$elementName}"; + $rId = $this->phpWord->$addMethod($element); + $element->setRelationId($rId); + } + + // Set other properties and add element into collection + $element->setDocPart($this->getDocPart(), $this->getDocPartId()); $element->setElementIndex($this->countElements() + 1); $element->setElementId(); $element->setPhpWord($this->phpWord); $this->elements[] = $element; + + return $element; } /** @@ -59,62 +122,24 @@ abstract class AbstractContainer extends AbstractElement /** * Count elements * - * @return integer + * @return int */ public function countElements() { return count($this->elements); } - /** - * Add generic element with style - * - * This is how all elements should be added with dependency injection: with - * just one simple $style. Currently this function supports TextRun, Table, - * and TextBox since all other elements have different arguments - * - * @todo Change the function name into something better? - * - * @param string $elementName - * @param mixed $style - * @return \PhpOffice\PhpWord\Element\AbstractElement - */ - private function addGenericElement($elementName, $style) - { - $elementClass = __NAMESPACE__ . '\\' . $elementName; - - $this->checkValidity($elementName); - $element = new $elementClass($style); - $element->setDocPart($this->getDocPart(), $this->getDocPartId()); - $this->addElement($element); - - return $element; - } - /** * Add text/preservetext element * * @param string $text * @param mixed $fontStyle * @param mixed $paragraphStyle - * @param string $elementName Text|PreserveText * @return \PhpOffice\PhpWord\Element\Text|\PhpOffice\PhpWord\Element\PreserveText */ - public function addText($text, $fontStyle = null, $paragraphStyle = null, $elementName = 'Text') + public function addText($text, $fontStyle = null, $paragraphStyle = null) { - $this->checkValidity($elementName); - $elementClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . $elementName; - - // Reset paragraph style for footnote and textrun. They have their own - if (in_array($this->container, array('textrun', 'footnote', 'endnote', 'listitemrun'))) { - $paragraphStyle = null; - } - - $element = new $elementClass($text, $fontStyle, $paragraphStyle); - $element->setDocPart($this->getDocPart(), $this->getDocPartId()); - $this->addElement($element); - - return $element; + return $this->addElement('Text', $text, $fontStyle, $paragraphStyle); } /** @@ -125,7 +150,7 @@ abstract class AbstractContainer extends AbstractElement */ public function addTextRun($paragraphStyle = null) { - return $this->addGenericElement('TextRun', $paragraphStyle); + return $this->addElement('TextRun', $paragraphStyle); } /** @@ -139,18 +164,7 @@ abstract class AbstractContainer extends AbstractElement */ public function addLink($target, $text = null, $fontStyle = null, $paragraphStyle = null) { - $this->checkValidity('Link'); - $elementDocPart = $this->checkElementDocPart(); - - $element = new Link($target, $text, $fontStyle, $paragraphStyle); - $element->setDocPart($this->getDocPart(), $this->getDocPartId()); - - $rId = Media::addElement($elementDocPart, 'link', $target); - $element->setRelationId($rId); - - $this->addElement($element); - - return $element; + return $this->addElement('Link', $target, $text, $fontStyle, $paragraphStyle); } /** @@ -163,7 +177,7 @@ abstract class AbstractContainer extends AbstractElement */ public function addPreserveText($text, $fontStyle = null, $paragraphStyle = null) { - return $this->addText($text, $fontStyle, $paragraphStyle, 'PreserveText'); + return $this->addElement('PreserveText', $text, $fontStyle, $paragraphStyle); } /** @@ -175,12 +189,8 @@ abstract class AbstractContainer extends AbstractElement */ public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null) { - $this->checkValidity('TextBreak'); - for ($i = 1; $i <= $count; $i++) { - $element = new TextBreak($fontStyle, $paragraphStyle); - $element->setDocPart($this->getDocPart(), $this->getDocPartId()); - $this->addElement($element); + $this->addElement('TextBreak', $fontStyle, $paragraphStyle); } } @@ -196,13 +206,7 @@ abstract class AbstractContainer extends AbstractElement */ public function addListItem($text, $depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null) { - $this->checkValidity('ListItem'); - - $element = new ListItem($text, $depth, $fontStyle, $listStyle, $paragraphStyle); - $element->setDocPart($this->getDocPart(), $this->getDocPartId()); - $this->addElement($element); - - return $element; + return $this->addElement('ListItem', $text, $depth, $fontStyle, $listStyle, $paragraphStyle); } /** @@ -216,13 +220,7 @@ abstract class AbstractContainer extends AbstractElement */ public function addListItemRun($depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null) { - $this->checkValidity('ListItemRun'); - - $element = new ListItemRun($depth, $fontStyle, $listStyle, $paragraphStyle); - $element->setDocPart($this->getDocPart(), $this->getDocPartId()); - $this->addElement($element); - - return $element; + return $this->addElement('ListItemRun', $depth, $fontStyle, $listStyle, $paragraphStyle); } /** @@ -234,7 +232,7 @@ abstract class AbstractContainer extends AbstractElement */ public function addTable($style = null) { - return $this->addGenericElement('Table', $style); + return $this->addElement('Table', $style); } /** @@ -242,23 +240,12 @@ abstract class AbstractContainer extends AbstractElement * * @param string $source * @param mixed $style Image style - * @param boolean $isWatermark + * @param bool $isWatermark * @return \PhpOffice\PhpWord\Element\Image */ public function addImage($source, $style = null, $isWatermark = false) { - $this->checkValidity('Image'); - $elementDocPart = $this->checkElementDocPart(); - - $element = new Image($source, $style, $isWatermark); - $element->setDocPart($this->getDocPart(), $this->getDocPartId()); - - $rId = Media::addElement($elementDocPart, 'image', $source, $element); - $element->setRelationId($rId); - - $this->addElement($element); - - return $element; + return $this->addElement('Image', $source, $style, $isWatermark); } /** @@ -269,49 +256,21 @@ abstract class AbstractContainer extends AbstractElement * @param string $source * @param mixed $style * @return \PhpOffice\PhpWord\Element\Object - * @throws \PhpOffice\PhpWord\Exception\Exception */ public function addObject($source, $style = null) { - $this->checkValidity('Object'); - $elementDocPart = $this->checkElementDocPart(); - - $element = new Object($source, $style); - $element->setDocPart($this->getDocPart(), $this->getDocPartId()); - - $rId = Media::addElement($elementDocPart, 'object', $source); - $element->setRelationId($rId); - $rIdIcon = Media::addElement($elementDocPart, 'image', $element->getIcon(), new Image($element->getIcon())); - $element->setImageRelationId($rIdIcon); - - $this->addElement($element); - - return $element; + return $this->addElement('Object', $source, $style); } /** * Add footnote element * * @param mixed $paragraphStyle - * @param string $elementName * @return \PhpOffice\PhpWord\Element\Footnote */ - public function addFootnote($paragraphStyle = null, $elementName = 'Footnote') + public function addFootnote($paragraphStyle = null) { - $this->checkValidity($elementName); - $elementClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . $elementName; - $docPart = strtolower($elementName); - $addMethod = "add{$elementName}"; - - $element = new $elementClass($paragraphStyle); - $element->setDocPart($docPart, $this->getDocPartId()); - if ($this->phpWord instanceof PhpWord) { - $rId = $this->phpWord->$addMethod($element); - $element->setRelationId($rId); - } - $this->addElement($element); - - return $element; + return $this->addElement('Footnote', $paragraphStyle); } /** @@ -322,7 +281,7 @@ abstract class AbstractContainer extends AbstractElement */ public function addEndnote($paragraphStyle = null) { - return $this->addFootnote($paragraphStyle, 'Endnote'); + return $this->addElement('Endnote', $paragraphStyle); } /** @@ -336,13 +295,7 @@ abstract class AbstractContainer extends AbstractElement */ public function addCheckBox($name, $text, $fontStyle = null, $paragraphStyle = null) { - $this->checkValidity('CheckBox'); - - $element = new CheckBox($name, $text, $fontStyle, $paragraphStyle); - $element->setDocPart($this->getDocPart(), $this->getDocPartId()); - $this->addElement($element); - - return $element; + return $this->addElement('CheckBox', $name, $text, $fontStyle, $paragraphStyle); } /** @@ -353,47 +306,50 @@ abstract class AbstractContainer extends AbstractElement */ public function addTextBox($style = null) { - return $this->addGenericElement('TextBox', $style); + return $this->addElement('TextBox', $style); } /** * Check if a method is allowed for the current container * * @param string $method - * @return boolean + * @return bool */ private function checkValidity($method) { // Valid containers for each element - $allContainers = array('section', 'header', 'footer', 'cell', 'textrun', 'footnote', 'endnote', 'textbox', 'listitemrun'); + $allContainers = array( + 'Section', 'Header', 'Footer', 'Footnote', 'Endnote', + 'Cell', 'TextRun', 'TextBox', 'ListItemRun', + ); $validContainers = array( 'Text' => $allContainers, 'Link' => $allContainers, 'TextBreak' => $allContainers, 'Image' => $allContainers, 'Object' => $allContainers, - 'TextRun' => array('section', 'header', 'footer', 'cell', 'textbox'), - 'ListItem' => array('section', 'header', 'footer', 'cell', 'textbox'), - 'ListItemRun' => array('section', 'header', 'footer', 'cell', 'textbox'), - 'Table' => array('section', 'header', 'footer', 'cell', 'textbox'), - 'CheckBox' => array('section', 'header', 'footer', 'cell'), - 'TextBox' => array('section', 'header', 'footer', 'cell'), - 'Footnote' => array('section', 'textrun', 'cell'), - 'Endnote' => array('section', 'textrun', 'cell'), - 'PreserveText' => array('header', 'footer', 'cell'), + 'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'), + 'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'), + 'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'), + 'Table' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'), + 'CheckBox' => array('Section', 'Header', 'Footer', 'Cell'), + 'TextBox' => array('Section', 'Header', 'Footer', 'Cell'), + 'Footnote' => array('Section', 'TextRun', 'Cell'), + 'Endnote' => array('Section', 'TextRun', 'Cell'), + 'PreserveText' => array('Header', 'Footer', 'Cell'), ); // Special condition, e.g. preservetext can only exists in cell when // the cell is located in header or footer $validSubcontainers = array( - 'PreserveText' => array(array('cell'), array('header', 'footer')), - 'Footnote' => array(array('cell', 'textrun'), array('section')), - 'Endnote' => array(array('cell', 'textrun'), array('section')), + 'PreserveText' => array(array('Cell'), array('Header', 'Footer')), + 'Footnote' => array(array('Cell', 'TextRun'), array('Section')), + 'Endnote' => array(array('Cell', 'TextRun'), array('Section')), ); // Check if a method is valid for current container if (array_key_exists($method, $validContainers)) { if (!in_array($this->container, $validContainers[$method])) { - throw new \BadMethodCallException("Cannot put $method in $this->container."); + throw new \BadMethodCallException("Cannot add $method in $this->container."); } } // Check if a method is valid for current container, located in other container @@ -403,7 +359,7 @@ abstract class AbstractContainer extends AbstractElement $allowedDocParts = $rules[1]; foreach ($containers as $container) { if ($this->container == $container && !in_array($this->getDocPart(), $allowedDocParts)) { - throw new \BadMethodCallException("Cannot put $method in $this->container."); + throw new \BadMethodCallException("Cannot add $method in $this->container."); } } } @@ -412,17 +368,21 @@ abstract class AbstractContainer extends AbstractElement } /** - * Return element location in document: section, headerx, or footerx + * Return media element (image, object, link) container name + * + * @return string section|headerx|footerx|footnote|endnote */ - private function checkElementDocPart() + private function getMediaContainer() { - $inOtherPart = in_array($this->container, array('cell', 'textrun', 'textbox', 'listitemrun')); - $docPart = $inOtherPart ? $this->getDocPart() : $this->container; - $docPartId = $inOtherPart ? $this->getDocPartId() : $this->sectionId; - $inHeaderFooter = ($docPart == 'header' || $docPart == 'footer'); - $docPartId = $inHeaderFooter ? $this->getDocPartId() : $docPartId; + $partName = $this->container; + if (in_array($partName, array('Cell', 'TextRun', 'TextBox', 'ListItemRun'))) { + $partName = $this->getDocPart(); + } + if ($partName == 'Header' || $partName == 'Footer') { + $partName .= $this->getDocPartId(); + } - return $inHeaderFooter ? $docPart . $docPartId : $docPart; + return strtolower($partName); } /** diff --git a/src/PhpWord/Element/AbstractElement.php b/src/PhpWord/Element/AbstractElement.php index 7e3151e2..40e65c12 100644 --- a/src/PhpWord/Element/AbstractElement.php +++ b/src/PhpWord/Element/AbstractElement.php @@ -34,13 +34,6 @@ abstract class AbstractElement */ protected $phpWord; - /** - * Container type section|header|footer|cell|textrun|footnote|endnote|textbox - * - * @var string - */ - protected $container; - /** * Section Id * @@ -57,7 +50,7 @@ abstract class AbstractElement * * @var string */ - protected $docPart = 'section'; + protected $docPart = 'Section'; /** * Document part Id @@ -66,21 +59,21 @@ abstract class AbstractElement * because the max number of header/footer in every page is 3, i.e. * AUTO, FIRST, and EVEN (AUTO = ODD) * - * @var integer + * @var int */ protected $docPartId = 1; /** * Index of element in the elements collection (start with 1) * - * @var integer + * @var int */ protected $elementIndex = 1; /** * Unique Id for element * - * @var integer + * @var int */ protected $elementId; @@ -114,7 +107,7 @@ abstract class AbstractElement /** * Get section number * - * @return integer + * @return int */ public function getSectionId() { @@ -125,7 +118,7 @@ abstract class AbstractElement * Set doc part * * @param string $docPart - * @param integer $docPartId + * @param int $docPartId */ public function setDocPart($docPart, $docPartId = 1) { @@ -146,7 +139,7 @@ abstract class AbstractElement /** * Get doc part Id * - * @return integer + * @return int */ public function getDocPartId() { @@ -212,13 +205,13 @@ abstract class AbstractElement } /** - * Check if element is located in section doc part (as opposed to header/footer) + * Check if element is located in Section doc part (as opposed to Header/Footer) * - * @return boolean + * @return bool */ public function isInSection() { - return ($this->docPart == 'section'); + return ($this->docPart == 'Section'); } /** @@ -226,7 +219,7 @@ abstract class AbstractElement * * @param mixed $styleObject Style object * @param mixed $styleValue Style value - * @param boolean $returnObject Always return object + * @param bool $returnObject Always return object */ protected function setStyle($styleObject, $styleValue = null, $returnObject = false) { diff --git a/src/PhpWord/Element/Cell.php b/src/PhpWord/Element/Cell.php index bc9b47b4..ea49bc7b 100644 --- a/src/PhpWord/Element/Cell.php +++ b/src/PhpWord/Element/Cell.php @@ -24,6 +24,11 @@ use PhpOffice\PhpWord\Style\Cell as CellStyle; */ class Cell extends AbstractContainer { + /** + * @var string Container type + */ + protected $container = 'Cell'; + /** * Cell width * @@ -36,22 +41,18 @@ class Cell extends AbstractContainer * * @var \PhpOffice\PhpWord\Style\Cell */ - private $cellStyle; + private $style; /** * Create new instance * - * @param string $docPart section|header|footer - * @param int $docPartId * @param int $width * @param array|\PhpOffice\PhpWord\Style\Cell $style */ - public function __construct($docPart, $docPartId, $width = null, $style = null) + public function __construct($width = null, $style = null) { - $this->container = 'cell'; - $this->setDocPart($docPart, $docPartId); $this->width = $width; - $this->cellStyle = $this->setStyle(new CellStyle(), $style, true); + $this->style = $this->setStyle(new CellStyle(), $style, true); } /** @@ -61,7 +62,7 @@ class Cell extends AbstractContainer */ public function getStyle() { - return $this->cellStyle; + return $this->style; } /** diff --git a/src/PhpWord/Element/Endnote.php b/src/PhpWord/Element/Endnote.php index 03ef3b68..20278898 100644 --- a/src/PhpWord/Element/Endnote.php +++ b/src/PhpWord/Element/Endnote.php @@ -26,6 +26,11 @@ use PhpOffice\PhpWord\Style\Paragraph; */ class Endnote extends Footnote { + /** + * @var string Container type + */ + protected $container = 'Endnote'; + /** * Create new instance * @@ -33,7 +38,6 @@ class Endnote extends Footnote */ public function __construct($paragraphStyle = null) { - $this->container = 'endnote'; $this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle); } } diff --git a/src/PhpWord/Element/Footer.php b/src/PhpWord/Element/Footer.php index 265c2c4c..142ccfda 100644 --- a/src/PhpWord/Element/Footer.php +++ b/src/PhpWord/Element/Footer.php @@ -33,11 +33,9 @@ class Footer extends AbstractContainer const EVEN = 'even'; /** - * Container type - * - * @var string + * @var string Container type */ - protected $container = 'footer'; + protected $container = 'Footer'; /** * Header type diff --git a/src/PhpWord/Element/Footnote.php b/src/PhpWord/Element/Footnote.php index d59a10a5..76311c6b 100644 --- a/src/PhpWord/Element/Footnote.php +++ b/src/PhpWord/Element/Footnote.php @@ -24,6 +24,11 @@ use PhpOffice\PhpWord\Style\Paragraph; */ class Footnote extends AbstractContainer { + /** + * @var string Container type + */ + protected $container = 'Footnote'; + /** * Paragraph style * @@ -38,7 +43,6 @@ class Footnote extends AbstractContainer */ public function __construct($paragraphStyle = null) { - $this->container = 'footnote'; $this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle); } diff --git a/src/PhpWord/Element/Header.php b/src/PhpWord/Element/Header.php index be95936c..feaa86e8 100644 --- a/src/PhpWord/Element/Header.php +++ b/src/PhpWord/Element/Header.php @@ -22,13 +22,10 @@ namespace PhpOffice\PhpWord\Element; */ class Header extends Footer { - /** - * Container type - * - * @var string + * @var string Container type */ - protected $container = 'header'; + protected $container = 'Header'; /** * Add a Watermark Element diff --git a/src/PhpWord/Element/ListItemRun.php b/src/PhpWord/Element/ListItemRun.php index a6dbd932..2a2a51af 100644 --- a/src/PhpWord/Element/ListItemRun.php +++ b/src/PhpWord/Element/ListItemRun.php @@ -25,6 +25,11 @@ use PhpOffice\PhpWord\Style\Paragraph; */ class ListItemRun extends TextRun { + /** + * @var string Container type + */ + protected $container = 'ListItemRun'; + /** * ListItem Style * @@ -49,7 +54,6 @@ class ListItemRun extends TextRun */ public function __construct($depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null) { - $this->container = 'listitemrun'; $this->depth = $depth; // Version >= 0.10.0 will pass numbering style name. Older version will use old method diff --git a/src/PhpWord/Element/Row.php b/src/PhpWord/Element/Row.php index 0eb18784..3aef4539 100644 --- a/src/PhpWord/Element/Row.php +++ b/src/PhpWord/Element/Row.php @@ -50,14 +50,11 @@ class Row extends AbstractElement /** * Create a new table row * - * @param string $docPart - * @param int $docPartId * @param int $height * @param mixed $style */ - public function __construct($docPart, $docPartId, $height = null, $style = null) + public function __construct($height = null, $style = null) { - $this->setDocPart($docPart, $docPartId); $this->height = $height; $this->style = $this->setStyle(new RowStyle(), $style, true); } @@ -67,12 +64,15 @@ class Row extends AbstractElement * * @param int $width * @param mixed $style + * @return \PhpOffice\PhpWord\Element\Cell */ public function addCell($width = null, $style = null) { - $cell = new Cell($this->getDocPart(), $this->getDocPartId(), $width, $style); + $cell = new Cell($width, $style); + $cell->setDocPart($this->getDocPart(), $this->getDocPartId()); $cell->setPhpWord($this->phpWord); $this->cells[] = $cell; + return $cell; } diff --git a/src/PhpWord/Element/Section.php b/src/PhpWord/Element/Section.php index 04ff5aa2..8a2b474f 100644 --- a/src/PhpWord/Element/Section.php +++ b/src/PhpWord/Element/Section.php @@ -26,6 +26,11 @@ use PhpOffice\PhpWord\Style\Section as SectionSettings; */ class Section extends AbstractContainer { + /** + * @var string Container type + */ + protected $container = 'Section'; + /** * Section settings * @@ -55,7 +60,6 @@ class Section extends AbstractContainer */ public function __construct($sectionCount, $settings = null) { - $this->container = 'section'; $this->sectionId = $sectionCount; $this->setDocPart($this->container, $this->sectionId); $this->settings = new SectionSettings(); @@ -98,15 +102,7 @@ class Section extends AbstractContainer */ public function addTitle($text, $depth = 1) { - $title = new Title($text, $depth); - $title->setDocPart($this->getDocPart(), $this->getDocPartId()); - if ($this->phpWord instanceof PhpWord) { - $bookmarkId = $this->phpWord->addTitle($title); - $title->setBookmarkId($bookmarkId); - } - $this->addElement($title); - - return $title; + return $this->addElement('Title', $text, $depth); } /** @@ -114,7 +110,7 @@ class Section extends AbstractContainer */ public function addPageBreak() { - $this->addElement(new PageBreak()); + return $this->addElement('PageBreak'); } /** @@ -128,10 +124,7 @@ class Section extends AbstractContainer */ public function addTOC($fontStyle = null, $tocStyle = null, $minDepth = 1, $maxDepth = 9) { - $toc = new TOC($fontStyle, $tocStyle, $minDepth, $maxDepth); - $this->addElement($toc); - - return $toc; + return $this->addElement('TOC', $fontStyle, $tocStyle, $minDepth, $maxDepth); } /** diff --git a/src/PhpWord/Element/Table.php b/src/PhpWord/Element/Table.php index e87c591f..ee45e36f 100644 --- a/src/PhpWord/Element/Table.php +++ b/src/PhpWord/Element/Table.php @@ -41,7 +41,7 @@ class Table extends AbstractElement /** * Table width * - * @var integer + * @var int */ private $width = null; @@ -59,28 +59,32 @@ class Table extends AbstractElement /** * Add a row * - * @param integer $height + * @param int $height * @param mixed $style + * @return \PhpOffice\PhpWord\Element\Row */ public function addRow($height = null, $style = null) { - $row = new Row($this->getDocPart(), $this->getDocPartId(), $height, $style); + $row = new Row($height, $style); + $row->setDocPart($this->getDocPart(), $this->getDocPartId()); $row->setPhpWord($this->phpWord); $this->rows[] = $row; + return $row; } /** * Add a cell * - * @param integer $width + * @param int $width * @param mixed $style - * @return Cell + * @return \PhpOffice\PhpWord\Element\Cell */ public function addCell($width = null, $style = null) { $index = count($this->rows) - 1; $cell = $this->rows[$index]->addCell($width, $style); + return $cell; } @@ -107,7 +111,7 @@ class Table extends AbstractElement /** * Set table width * - * @param integer $width + * @param int $width */ public function setWidth($width) { @@ -117,7 +121,7 @@ class Table extends AbstractElement /** * Get table width * - * @return integer + * @return int */ public function getWidth() { @@ -127,7 +131,7 @@ class Table extends AbstractElement /** * Get column count * - * @return integer + * @return int */ public function countColumns() { diff --git a/src/PhpWord/Element/TextBox.php b/src/PhpWord/Element/TextBox.php index c3c83d81..06c95181 100644 --- a/src/PhpWord/Element/TextBox.php +++ b/src/PhpWord/Element/TextBox.php @@ -26,6 +26,11 @@ use PhpOffice\PhpWord\Style\TextBox as TextBoxStyle; */ class TextBox extends AbstractContainer { + /** + * @var string Container type + */ + protected $container = 'TextBox'; + /** * TextBox style * @@ -40,7 +45,6 @@ class TextBox extends AbstractContainer */ public function __construct($style = null) { - $this->container = 'textbox'; $this->style = $this->setStyle(new TextBoxStyle(), $style); } diff --git a/src/PhpWord/Element/TextRun.php b/src/PhpWord/Element/TextRun.php index 6c8aa010..75837104 100644 --- a/src/PhpWord/Element/TextRun.php +++ b/src/PhpWord/Element/TextRun.php @@ -24,6 +24,11 @@ use PhpOffice\PhpWord\Style\Paragraph; */ class TextRun extends AbstractContainer { + /** + * @var string Container type + */ + protected $container = 'TextRun'; + /** * Paragraph style * @@ -38,7 +43,6 @@ class TextRun extends AbstractContainer */ public function __construct($paragraphStyle = null) { - $this->container = 'textrun'; $this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle); } diff --git a/src/PhpWord/Element/Title.php b/src/PhpWord/Element/Title.php index 481f061b..3a1b3049 100644 --- a/src/PhpWord/Element/Title.php +++ b/src/PhpWord/Element/Title.php @@ -39,13 +39,6 @@ class Title extends AbstractElement */ private $depth = 1; - /** - * Title Bookmark ID - * - * @var int - */ - private $bookmarkId = 1; - /** * Name of the heading style, e.g. 'Heading1' * @@ -56,7 +49,7 @@ class Title extends AbstractElement /** * Title anchor * - * @var int + * @var string * @deprecated 0.10.0 */ private $anchor; @@ -79,26 +72,6 @@ class Title extends AbstractElement return $this; } - /** - * Set Bookmark ID - * - * @param int $bookmarkId - */ - public function setBookmarkId($bookmarkId) - { - $this->bookmarkId = $bookmarkId; - } - - /** - * Get Anchor - * - * @return int - */ - public function getBookmarkId() - { - return $this->bookmarkId; - } - /** * Get Title Text content * @@ -132,7 +105,7 @@ class Title extends AbstractElement /** * Set Anchor * - * @param int $anchor + * @param string $anchor * @deprecated 0.10.0 * @codeCoverageIgnore */ @@ -144,12 +117,36 @@ class Title extends AbstractElement /** * Get Anchor * - * @return int + * @return string * @deprecated 0.10.0 * @codeCoverageIgnore */ public function getAnchor() { - return '_Toc' . (252634154 + $this->bookmarkId); + return '_Toc' . (252634154 + $this->getRelationId()); + } + + /** + * Set Bookmark ID + * + * @param int $value + * @deprecated 0.11.0 + * @codeCoverageIgnore + */ + public function setBookmarkId($value) + { + $this->setRelationId($value); + } + + /** + * Get bookmark ID + * + * @return int + * @deprecated 0.11.0 + * @codeCoverageIgnore + */ + public function getBookmarkId() + { + return $this->getRelationId(); } } diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 1df56e68..c56e418b 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -207,6 +207,7 @@ class Html case 'li': $cNodes = $node->childNodes; if (count($cNodes) > 0) { + $text = ''; foreach ($cNodes as $cNode) { if ($cNode->nodeName == '#text') { $text = $cNode->nodeValue; diff --git a/src/PhpWord/Writer/HTML/Element/Container.php b/src/PhpWord/Writer/HTML/Element/Container.php index c55a7921..c4bbc2f8 100644 --- a/src/PhpWord/Writer/HTML/Element/Container.php +++ b/src/PhpWord/Writer/HTML/Element/Container.php @@ -32,7 +32,7 @@ class Container extends AbstractElement public function write() { $container = $this->element; - if (!$this->element instanceof \PhpOffice\PhpWord\Element\AbstractContainer) { + if (!$container instanceof \PhpOffice\PhpWord\Element\AbstractContainer) { return; } $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1); diff --git a/src/PhpWord/Writer/ODText/Style/Font.php b/src/PhpWord/Writer/ODText/Style/Font.php index eb96946c..148c8dbc 100644 --- a/src/PhpWord/Writer/ODText/Style/Font.php +++ b/src/PhpWord/Writer/ODText/Style/Font.php @@ -47,18 +47,18 @@ class Font extends AbstractStyle // Name $font = $style->getName(); - $xmlWriter->writeAttributeIf($font, 'style:font-name', $font); - $xmlWriter->writeAttributeIf($font, 'style:font-name-complex', $font); + $xmlWriter->writeAttributeIf($font != '', 'style:font-name', $font); + $xmlWriter->writeAttributeIf($font != '', 'style:font-name-complex', $font); $size = $style->getSize(); // Size - $xmlWriter->writeAttributeIf($size, 'fo:font-size', $size . 'pt'); - $xmlWriter->writeAttributeIf($size, 'style:font-size-asian', $size . 'pt'); - $xmlWriter->writeAttributeIf($size, 'style:font-size-complex', $size . 'pt'); + $xmlWriter->writeAttributeIf(is_numeric($size), 'fo:font-size', $size . 'pt'); + $xmlWriter->writeAttributeIf(is_numeric($size), 'style:font-size-asian', $size . 'pt'); + $xmlWriter->writeAttributeIf(is_numeric($size), 'style:font-size-complex', $size . 'pt'); // Color $color = $style->getColor(); - $xmlWriter->writeAttributeIf($color, 'fo:color', '#' . $color); + $xmlWriter->writeAttributeIf($color != '', 'fo:color', '#' . $color); // Bold & italic $xmlWriter->writeAttributeIf($style->isBold(), 'fo:font-weight', 'bold'); diff --git a/src/PhpWord/Writer/RTF/Element/Container.php b/src/PhpWord/Writer/RTF/Element/Container.php index 4ce4334c..67caba1d 100644 --- a/src/PhpWord/Writer/RTF/Element/Container.php +++ b/src/PhpWord/Writer/RTF/Element/Container.php @@ -32,7 +32,7 @@ class Container extends \PhpOffice\PhpWord\Writer\HTML\Element\Container public function write() { $container = $this->element; - if (!$this->element instanceof \PhpOffice\PhpWord\Element\AbstractContainer) { + if (!$container instanceof \PhpOffice\PhpWord\Element\AbstractContainer) { return; } $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1); diff --git a/src/PhpWord/Writer/Word2007/Element/Image.php b/src/PhpWord/Writer/Word2007/Element/Image.php index 1b4284d2..c41199f8 100644 --- a/src/PhpWord/Writer/Word2007/Element/Image.php +++ b/src/PhpWord/Writer/Word2007/Element/Image.php @@ -51,7 +51,6 @@ class Image extends AbstractElement */ private function writeImage(XMLWriter $xmlWriter, ImageElement $element) { - $rId = $element->getRelationId() + ($element->isInSection() ? 6 : 0); $style = $element->getStyle(); $styleWriter = new ImageStyleWriter($xmlWriter, $style); diff --git a/src/PhpWord/Writer/Word2007/Element/TOC.php b/src/PhpWord/Writer/Word2007/Element/TOC.php index 98486b01..88dd0dbd 100644 --- a/src/PhpWord/Writer/Word2007/Element/TOC.php +++ b/src/PhpWord/Writer/Word2007/Element/TOC.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; +use PhpOffice\PhpWord\Element\TOC as TOCElement; +use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter; use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter; @@ -44,7 +46,7 @@ class TOC extends AbstractElement $writeFieldMark = true; foreach ($titles as $title) { - $this->writeTitle($title, $writeFieldMark); + $this->writeTitle($xmlWriter, $element, $title, $writeFieldMark); if ($writeFieldMark) { $writeFieldMark = false; } @@ -65,23 +67,20 @@ class TOC extends AbstractElement * @param \PhpOffice\PhpWord\Element\Title $title * @param bool $writeFieldMark */ - private function writeTitle($title, $writeFieldMark) + private function writeTitle(XMLWriter $xmlWriter, TOCElement $element, $title, $writeFieldMark) { - $xmlWriter = $this->getXmlWriter(); - $element = $this->getElement(); - $tocStyle = $element->getStyleTOC(); $fontStyle = $element->getStyleFont(); $isObject = ($fontStyle instanceof Font) ? true : false; - $anchor = '_Toc' . ($title->getBookmarkId() + 252634154); + $anchor = '_Toc' . ($title->getRelationId() + 252634154); $indent = ($title->getDepth() - 1) * $tocStyle->getIndent(); $xmlWriter->startElement('w:p'); // Write style and field mark - $this->writeStyle($indent); + $this->writeStyle($xmlWriter, $element, $indent); if ($writeFieldMark) { - $this->writeFieldMark(); + $this->writeFieldMark($xmlWriter, $element); } // Hyperlink @@ -133,11 +132,8 @@ class TOC extends AbstractElement * * @param int $indent */ - private function writeStyle($indent) + private function writeStyle(XMLWriter $xmlWriter, TOCElement $element, $indent) { - $xmlWriter = $this->getXmlWriter(); - $element = $this->getElement(); - $tocStyle = $element->getStyleTOC(); $fontStyle = $element->getStyleFont(); $isObject = ($fontStyle instanceof Font) ? true : false; @@ -178,11 +174,8 @@ class TOC extends AbstractElement /** * Write TOC Field */ - private function writeFieldMark() + private function writeFieldMark(XMLWriter $xmlWriter, TOCElement $element) { - $xmlWriter = $this->getXmlWriter(); - $element = $this->getElement(); - $minDepth = $element->getMinDepth(); $maxDepth = $element->getMaxDepth(); diff --git a/src/PhpWord/Writer/Word2007/Element/Title.php b/src/PhpWord/Writer/Word2007/Element/Title.php index 703188ef..6f2bd314 100644 --- a/src/PhpWord/Writer/Word2007/Element/Title.php +++ b/src/PhpWord/Writer/Word2007/Element/Title.php @@ -37,8 +37,8 @@ class Title extends AbstractElement return; } - $bookmarkId = $element->getBookmarkId(); - $anchor = '_Toc' . ($bookmarkId + 252634154); + $rId = $element->getRelationId(); + $anchor = '_Toc' . ($rId + 252634154); $style = $element->getStyle(); $text = htmlspecialchars($element->getText()); @@ -61,7 +61,7 @@ class Title extends AbstractElement $xmlWriter->endElement(); $xmlWriter->startElement('w:bookmarkStart'); - $xmlWriter->writeAttribute('w:id', $bookmarkId); + $xmlWriter->writeAttribute('w:id', $rId); $xmlWriter->writeAttribute('w:name', $anchor); $xmlWriter->endElement(); @@ -72,7 +72,7 @@ class Title extends AbstractElement $xmlWriter->endElement(); $xmlWriter->startElement('w:bookmarkEnd'); - $xmlWriter->writeAttribute('w:id', $bookmarkId); + $xmlWriter->writeAttribute('w:id', $rId); $xmlWriter->endElement(); $xmlWriter->endElement(); diff --git a/tests/PhpWord/Tests/Element/CellTest.php b/tests/PhpWord/Tests/Element/CellTest.php index 466188f6..af924bc8 100644 --- a/tests/PhpWord/Tests/Element/CellTest.php +++ b/tests/PhpWord/Tests/Element/CellTest.php @@ -31,8 +31,7 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testConstruct() { - $iVal = rand(1, 1000); - $oCell = new Cell('section', $iVal); + $oCell = new Cell(); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Cell', $oCell); $this->assertEquals($oCell->getWidth(), null); @@ -43,8 +42,7 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testConstructWithStyleArray() { - $iVal = rand(1, 1000); - $oCell = new Cell('section', $iVal, null, array('valign' => 'center')); + $oCell = new Cell(null, array('valign' => 'center')); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Cell', $oCell->getStyle()); $this->assertEquals($oCell->getWidth(), null); @@ -55,7 +53,7 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testAddText() { - $oCell = new Cell('section', 1); + $oCell = new Cell(); $element = $oCell->addText('text'); $this->assertCount(1, $oCell->getElements()); @@ -67,7 +65,7 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testAddTextNotUTF8() { - $oCell = new Cell('section', 1); + $oCell = new Cell(); $element = $oCell->addText(utf8_decode('ééé')); $this->assertCount(1, $oCell->getElements()); @@ -80,7 +78,7 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testAddLink() { - $oCell = new Cell('section', 1); + $oCell = new Cell(); $element = $oCell->addLink(utf8_decode('ééé'), utf8_decode('ééé')); $this->assertCount(1, $oCell->getElements()); @@ -92,7 +90,7 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testAddTextBreak() { - $oCell = new Cell('section', 1); + $oCell = new Cell(); $oCell->addTextBreak(); $this->assertCount(1, $oCell->getElements()); @@ -103,7 +101,7 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testAddListItem() { - $oCell = new Cell('section', 1); + $oCell = new Cell(); $element = $oCell->addListItem('text'); $this->assertCount(1, $oCell->getElements()); @@ -116,7 +114,7 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testAddListItemNotUTF8() { - $oCell = new Cell('section', 1); + $oCell = new Cell(); $element = $oCell->addListItem(utf8_decode('ééé')); $this->assertCount(1, $oCell->getElements()); @@ -130,7 +128,7 @@ class CellTest extends \PHPUnit_Framework_TestCase public function testAddImageSection() { $src = __DIR__ . "/../_files/images/earth.jpg"; - $oCell = new Cell('section', 1); + $oCell = new Cell(); $element = $oCell->addImage($src); $this->assertCount(1, $oCell->getElements()); @@ -168,35 +166,7 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testAddImageSectionByUrl() { - $oCell = new Cell('section', 1); - $element = $oCell->addImage( - 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png' - ); - - $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); - } - - /** - * Add image header by URL - */ - public function testAddImageHeaderByUrl() - { - $oCell = new Cell('header', 1); - $element = $oCell->addImage( - 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png' - ); - - $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); - } - - /** - * Add image footer by URL - */ - public function testAddImageFooterByUrl() - { - $oCell = new Cell('footer', 1); + $oCell = new Cell(); $element = $oCell->addImage( 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png' ); @@ -211,7 +181,7 @@ class CellTest extends \PHPUnit_Framework_TestCase public function testAddObjectXLS() { $src = __DIR__ . "/../_files/documents/sheet.xls"; - $oCell = new Cell('section', 1); + $oCell = new Cell(); $element = $oCell->addObject($src); $this->assertCount(1, $oCell->getElements()); @@ -226,7 +196,7 @@ class CellTest extends \PHPUnit_Framework_TestCase public function testAddObjectException() { $src = __DIR__ . "/../_files/xsl/passthrough.xsl"; - $oCell = new Cell('section', 1); + $oCell = new Cell(); $oCell->addObject($src); } @@ -235,7 +205,8 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testAddPreserveText() { - $oCell = new Cell('header', 1); + $oCell = new Cell(); + $oCell->setDocPart('Header', 1); $element = $oCell->addPreserveText('text'); $this->assertCount(1, $oCell->getElements()); @@ -247,7 +218,8 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testAddPreserveTextNotUTF8() { - $oCell = new Cell('header', 1); + $oCell = new Cell(); + $oCell->setDocPart('Header', 1); $element = $oCell->addPreserveText(utf8_decode('ééé')); $this->assertCount(1, $oCell->getElements()); @@ -262,7 +234,8 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testAddPreserveTextException() { - $oCell = new Cell('section', 1); + $oCell = new Cell(); + $oCell->setDocPart('Section', 1); $oCell->addPreserveText('text'); } @@ -271,7 +244,7 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testCreateTextRun() { - $oCell = new Cell('section', 1); + $oCell = new Cell(); $element = $oCell->addTextRun(); $this->assertCount(1, $oCell->getElements()); @@ -283,7 +256,7 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testAddCheckBox() { - $oCell = new Cell('section', 1); + $oCell = new Cell(); $element = $oCell->addCheckBox(utf8_decode('ééé'), utf8_decode('ééé')); $this->assertCount(1, $oCell->getElements()); @@ -295,7 +268,7 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testGetElements() { - $oCell = new Cell('section', 1); + $oCell = new Cell(); $this->assertInternalType('array', $oCell->getElements()); } diff --git a/tests/PhpWord/Tests/Element/RowTest.php b/tests/PhpWord/Tests/Element/RowTest.php index b0de3910..c377bb7c 100644 --- a/tests/PhpWord/Tests/Element/RowTest.php +++ b/tests/PhpWord/Tests/Element/RowTest.php @@ -32,8 +32,7 @@ class RowTest extends \PHPUnit_Framework_TestCase */ public function testConstruct() { - $iVal = rand(1, 1000); - $oRow = new Row('section', $iVal); + $oRow = new Row(); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Row', $oRow); $this->assertEquals($oRow->getHeight(), null); @@ -48,15 +47,9 @@ class RowTest extends \PHPUnit_Framework_TestCase public function testConstructWithParams() { $iVal = rand(1, 1000); - $iVal2 = rand(1, 1000); - $oRow = new Row( - 'section', - $iVal, - $iVal2, - array('borderBottomSize' => 18, 'borderBottomColor' => '0000FF', 'bgColor' => '66BBFF') - ); + $oRow = new Row($iVal, array('borderBottomSize' => 18, 'borderBottomColor' => '0000FF', 'bgColor' => '66BBFF')); - $this->assertEquals($oRow->getHeight(), $iVal2); + $this->assertEquals($oRow->getHeight(), $iVal); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Row', $oRow->getStyle()); } @@ -65,7 +58,7 @@ class RowTest extends \PHPUnit_Framework_TestCase */ public function testAddCell() { - $oRow = new Row('section', 1); + $oRow = new Row(); $element = $oRow->addCell(); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Cell', $element); diff --git a/tests/PhpWord/Tests/Element/TitleTest.php b/tests/PhpWord/Tests/Element/TitleTest.php index 5b54cce6..301d8bec 100644 --- a/tests/PhpWord/Tests/Element/TitleTest.php +++ b/tests/PhpWord/Tests/Element/TitleTest.php @@ -57,6 +57,6 @@ class TitleTest extends \PHPUnit_Framework_TestCase $iVal = rand(1, 1000); $oTitle->setBookmarkId($iVal); - $this->assertEquals($oTitle->getBookmarkId(), $iVal); + $this->assertEquals($oTitle->getRelationId(), $iVal); } } From 55e715b5b1af53976ec02a7d0e93b37535eb82fa Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Mon, 12 May 2014 14:24:13 +0700 Subject: [PATCH 04/48] Add variable type hints --- src/PhpWord/Element/AbstractContainer.php | 11 ++++----- src/PhpWord/Writer/HTML/Element/Footnote.php | 6 +++-- src/PhpWord/Writer/HTML/Element/Image.php | 4 +++- src/PhpWord/Writer/HTML/Element/Text.php | 14 +++++++---- src/PhpWord/Writer/HTML/Style/Image.php | 6 +++-- src/PhpWord/Writer/RTF/Element/Text.php | 24 ++++++++++++------- src/PhpWord/Writer/RTF/Element/TextBreak.php | 4 +++- src/PhpWord/Writer/RTF/Style/Font.php | 8 +++++-- src/PhpWord/Writer/Word2007/Element/Text.php | 6 +++-- .../Writer/Word2007/Part/ContentTypes.php | 4 +++- .../Writer/Word2007/Part/Footnotes.php | 5 ++-- .../Writer/Word2007/Part/RelsDocument.php | 5 +++- src/PhpWord/Writer/Word2007/Part/Settings.php | 2 ++ 13 files changed, 67 insertions(+), 32 deletions(-) diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 39193031..6caa0056 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -35,7 +35,7 @@ abstract class AbstractContainer extends AbstractElement protected $elements = array(); /** - * Container type section|header|footer|footnote|endnote|cell|textrun|textbox + * Container type Section|Header|Footer|Footnote|Endnote|Cell|TextRun|TextBox|ListItemRun * * @var string */ @@ -63,9 +63,7 @@ abstract class AbstractContainer extends AbstractElement } // Create element - if ($argsCount == 1) { // Page Break - $element = new $elementClass(); - } elseif ($argsCount == 2) { // TextRun, TextBox, Table, Footnote, Endnote + if ($argsCount == 2) { // TextRun, TextBox, Table, Footnote, Endnote $element = new $elementClass($args[1]); } elseif ($argsCount == 3) { // Object, TextBreak, Title $element = new $elementClass($args[1], $args[2]); @@ -75,11 +73,13 @@ abstract class AbstractContainer extends AbstractElement $element = new $elementClass($args[1], $args[2], $args[3], $args[4]); } elseif ($argsCount == 6) { // ListItem $element = new $elementClass($args[1], $args[2], $args[3], $args[4], $args[5]); + } else { // Page Break + $element = new $elementClass(); } // Set relation Id for media collection + $mediaContainer = $this->getMediaContainer(); if (in_array($elementName, array('Link', 'Image', 'Object'))) { - $mediaContainer = $this->getMediaContainer(); if ($elementName == 'Image') { $rId = Media::addElement($mediaContainer, strtolower($elementName), $args[1], $element); } else { @@ -228,7 +228,6 @@ abstract class AbstractContainer extends AbstractElement * * @param mixed $style * @return \PhpOffice\PhpWord\Element\Table - * @todo Merge with the same function on Footer */ public function addTable($style = null) { diff --git a/src/PhpWord/Writer/HTML/Element/Footnote.php b/src/PhpWord/Writer/HTML/Element/Footnote.php index 30398b17..ba5ced56 100644 --- a/src/PhpWord/Writer/HTML/Element/Footnote.php +++ b/src/PhpWord/Writer/HTML/Element/Footnote.php @@ -41,12 +41,14 @@ class Footnote extends AbstractElement if (!$this->element instanceof \PhpOffice\PhpWord\Element\Footnote) { return; } + /** @var \PhpOffice\PhpWord\Writer\HTML $parentWriter Scrutinizer type hint */ + $parentWriter = $this->parentWriter; - $noteId = count($this->parentWriter->getNotes()) + 1; + $noteId = count($parentWriter->getNotes()) + 1; $noteMark = $this->noteType . '-' . $this->element->getRelationId(); $content = "{$noteId}"; - $this->parentWriter->addNote($noteId, $noteMark); + $parentWriter->addNote($noteId, $noteMark); return $content; } diff --git a/src/PhpWord/Writer/HTML/Element/Image.php b/src/PhpWord/Writer/HTML/Element/Image.php index 5c2edbb2..1d07acd7 100644 --- a/src/PhpWord/Writer/HTML/Element/Image.php +++ b/src/PhpWord/Writer/HTML/Element/Image.php @@ -37,9 +37,11 @@ class Image extends Text if (!$this->element instanceof \PhpOffice\PhpWord\Element\Image) { return; } + /** @var \PhpOffice\PhpWord\Writer\HTML $parentWriter Scrutinizer type hint */ + $parentWriter = $this->parentWriter; $content = ''; - if (!$this->parentWriter->isPdf()) { + if (!$parentWriter->isPdf()) { $imageData = $this->getBase64ImageData($this->element); if (!is_null($imageData)) { $styleWriter = new ImageStyleWriter($this->element->getStyle()); diff --git a/src/PhpWord/Writer/HTML/Element/Text.php b/src/PhpWord/Writer/HTML/Element/Text.php index 883a8cd3..8ace129c 100644 --- a/src/PhpWord/Writer/HTML/Element/Text.php +++ b/src/PhpWord/Writer/HTML/Element/Text.php @@ -64,12 +64,14 @@ class Text extends AbstractElement */ public function write() { + /** @var \PhpOffice\PhpWord\Element\Text $element Scrutinizer type hint */ + $element = $this->element; $this->getFontStyle(); $content = ''; $content .= $this->writeOpening(); $content .= $this->openingTags; - $content .= htmlspecialchars($this->element->getText()); + $content .= htmlspecialchars($element->getText()); $content .= $this->closingTags; $content .= $this->closingText; $content .= $this->writeClosing(); @@ -140,12 +142,14 @@ class Text extends AbstractElement */ private function getParagraphStyle() { + /** @var \PhpOffice\PhpWord\Element\Text $element Scrutinizer type hint */ + $element = $this->element; $style = ''; - if (method_exists($this->element, 'getParagraphStyle')) { + if (method_exists($element, 'getParagraphStyle')) { return $style; } - $paragraphStyle = $this->element->getParagraphStyle(); + $paragraphStyle = $element->getParagraphStyle(); $pStyleIsObject = ($paragraphStyle instanceof Paragraph); if ($pStyleIsObject) { $styleWriter = new ParagraphStyleWriter($paragraphStyle); @@ -164,8 +168,10 @@ class Text extends AbstractElement */ private function getFontStyle() { + /** @var \PhpOffice\PhpWord\Element\Text $element Scrutinizer type hint */ + $element = $this->element; $style = ''; - $fontStyle = $this->element->getFontStyle(); + $fontStyle = $element->getFontStyle(); $fStyleIsObject = ($fontStyle instanceof Font); if ($fStyleIsObject) { $styleWriter = new FontStyleWriter($fontStyle); diff --git a/src/PhpWord/Writer/HTML/Style/Image.php b/src/PhpWord/Writer/HTML/Style/Image.php index fcf18395..db6ac57b 100644 --- a/src/PhpWord/Writer/HTML/Style/Image.php +++ b/src/PhpWord/Writer/HTML/Style/Image.php @@ -37,8 +37,10 @@ class Image extends AbstractStyle } $css = array(); - $css['width'] = $this->getValueIf($style->getWidth(), $style->getWidth() . 'px'); - $css['height'] = $this->getValueIf($style->getHeight(), $style->getHeight() . 'px'); + $width = $style->getWidth(); + $height = $style->getHeight(); + $css['width'] = $this->getValueIf(is_numeric($width), $width . 'px'); + $css['height'] = $this->getValueIf(is_numeric($height), $height . 'px'); return $this->assembleCss($css); } diff --git a/src/PhpWord/Writer/RTF/Element/Text.php b/src/PhpWord/Writer/RTF/Element/Text.php index 701a5be1..27172df3 100644 --- a/src/PhpWord/Writer/RTF/Element/Text.php +++ b/src/PhpWord/Writer/RTF/Element/Text.php @@ -39,12 +39,15 @@ class Text extends AbstractElement return; } + /** @var \PhpOffice\PhpWord\Style\Font $fontStyle Scrutinizer type hint */ $fontStyle = $this->getFontStyle($this->element); + /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */ + $parentWriter = $this->parentWriter; $content = ''; $content .= $this->writeParagraphStyle($this->element); $content .= $this->writeFontStyleBegin($fontStyle); - if ($this->parentWriter->getLastParagraphStyle() != '' || $fontStyle) { + if ($parentWriter->getLastParagraphStyle() != '' || $fontStyle) { $content .= ' '; } $content .= $this->element->getText(); @@ -64,6 +67,8 @@ class Text extends AbstractElement */ private function writeParagraphStyle(TextElement $element) { + /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */ + $parentWriter = $this->parentWriter; $content = ''; // Get paragraph style @@ -74,15 +79,15 @@ class Text extends AbstractElement // Write style when applicable if ($paragraphStyle && !$this->withoutP) { - if ($this->parentWriter->getLastParagraphStyle() != $element->getParagraphStyle()) { + if ($parentWriter->getLastParagraphStyle() != $element->getParagraphStyle()) { $styleWriter = new ParagraphStyleWriter($paragraphStyle); $content = $styleWriter->write(); - $this->parentWriter->setLastParagraphStyle($element->getParagraphStyle()); + $parentWriter->setLastParagraphStyle($element->getParagraphStyle()); } else { - $this->parentWriter->setLastParagraphStyle(); + $parentWriter->setLastParagraphStyle(); } } else { - $this->parentWriter->setLastParagraphStyle(); + $parentWriter->setLastParagraphStyle(); } return $content; @@ -91,7 +96,7 @@ class Text extends AbstractElement /** * Write font style beginning * - * @param \PhpOffice\PhpWord\Style\Font $style + * @param mixed $style * @return string */ private function writeFontStyleBegin($style) @@ -100,16 +105,19 @@ class Text extends AbstractElement return ''; } + /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */ + $parentWriter = $this->parentWriter; + // Create style writer and set color/name index $styleWriter = new FontStyleWriter($style); if ($style->getColor() != null) { - $colorIndex = array_search($style->getColor(), $this->parentWriter->getColorTable()); + $colorIndex = array_search($style->getColor(), $parentWriter->getColorTable()); if ($colorIndex !== false) { $styleWriter->setColorIndex($colorIndex + 1); } } if ($style->getName() != null) { - $fontIndex = array_search($style->getName(), $this->parentWriter->getFontTable()); + $fontIndex = array_search($style->getName(), $parentWriter->getFontTable()); if ($fontIndex !== false) { $styleWriter->setNameIndex($fontIndex + 1); } diff --git a/src/PhpWord/Writer/RTF/Element/TextBreak.php b/src/PhpWord/Writer/RTF/Element/TextBreak.php index 760f34e8..ff836a88 100644 --- a/src/PhpWord/Writer/RTF/Element/TextBreak.php +++ b/src/PhpWord/Writer/RTF/Element/TextBreak.php @@ -31,7 +31,9 @@ class TextBreak extends AbstractElement */ public function write() { - $this->parentWriter->setLastParagraphStyle(); + /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */ + $parentWriter = $this->parentWriter; + $parentWriter->setLastParagraphStyle(); return '\par' . PHP_EOL; } diff --git a/src/PhpWord/Writer/RTF/Style/Font.php b/src/PhpWord/Writer/RTF/Style/Font.php index 5c14eb3a..49c9889c 100644 --- a/src/PhpWord/Writer/RTF/Style/Font.php +++ b/src/PhpWord/Writer/RTF/Style/Font.php @@ -53,7 +53,9 @@ class Font extends AbstractStyle $content .= '\f' . $this->nameIndex; $content .= $this->getValueIf($style->isBold(), '\b'); $content .= $this->getValueIf($style->isItalic(), '\i'); - $content .= $this->getValueIf($style->getSize(), '\fs' . ($style->getSize() * 2)); + + $size = $style->getSize(); + $content .= $this->getValueIf(is_numeric($size), '\fs' . ($size * 2)); return $content; } @@ -75,7 +77,9 @@ class Font extends AbstractStyle $content .= '\f0'; $content .= $this->getValueIf($style->isBold(), '\b0'); $content .= $this->getValueIf($style->isItalic(), '\i0'); - $content .= $this->getValueIf($style->getSize(), '\fs' . (PhpWord::DEFAULT_FONT_SIZE * 2)); + + $size = $style->getSize(); + $content .= $this->getValueIf(is_numeric($size), '\fs' . (PhpWord::DEFAULT_FONT_SIZE * 2)); return $content; } diff --git a/src/PhpWord/Writer/Word2007/Element/Text.php b/src/PhpWord/Writer/Word2007/Element/Text.php index 1385e6e6..e06bde6b 100644 --- a/src/PhpWord/Writer/Word2007/Element/Text.php +++ b/src/PhpWord/Writer/Word2007/Element/Text.php @@ -92,8 +92,9 @@ class Text extends AbstractElement protected function writeParagraphStyle() { $xmlWriter = $this->getXmlWriter(); - $element = $this->getElement(); + /** @var \PhpOffice\PhpWord\Element\Text $element Scrutinizer type hint */ + $element = $this->getElement(); $paragraphStyle = $element->getParagraphStyle(); $styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle); $styleWriter->setIsInline(true); @@ -106,8 +107,9 @@ class Text extends AbstractElement protected function writeFontStyle() { $xmlWriter = $this->getXmlWriter(); - $element = $this->getElement(); + /** @var \PhpOffice\PhpWord\Element\Text $element Scrutinizer type hint */ + $element = $this->getElement(); $fontStyle = $element->getFontStyle(); $styleWriter = new FontStyleWriter($xmlWriter, $fontStyle); $styleWriter->setIsInline(true); diff --git a/src/PhpWord/Writer/Word2007/Part/ContentTypes.php b/src/PhpWord/Writer/Word2007/Part/ContentTypes.php index 3af62a44..417df64d 100644 --- a/src/PhpWord/Writer/Word2007/Part/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/Part/ContentTypes.php @@ -32,7 +32,9 @@ class ContentTypes extends AbstractPart */ public function write() { - $contentTypes = $this->getParentWriter()->getContentTypes(); + /** @var \PhpOffice\PhpWord\Writer\Word2007 $parentWriter Scrutinizer type hint */ + $parentWriter = $this->getParentWriter(); + $contentTypes = $parentWriter->getContentTypes(); $openXMLPrefix = 'application/vnd.openxmlformats-'; $wordMLPrefix = $openXMLPrefix . 'officedocument.wordprocessingml.'; diff --git a/src/PhpWord/Writer/Word2007/Part/Footnotes.php b/src/PhpWord/Writer/Word2007/Part/Footnotes.php index 1eeabc9c..d7a1caaa 100644 --- a/src/PhpWord/Writer/Word2007/Part/Footnotes.php +++ b/src/PhpWord/Writer/Word2007/Part/Footnotes.php @@ -106,8 +106,9 @@ class Footnotes extends AbstractPart $xmlWriter->endElement(); // w:p $xmlWriter->endElement(); // $this->elementNode - // Content - foreach ($this->elements as $element) { + /** @var array $elements Scrutinizer type hint */ + $elements = $this->elements; + foreach ($elements as $element) { if ($element instanceof Footnote) { $this->writeNote($xmlWriter, $element); } diff --git a/src/PhpWord/Writer/Word2007/Part/RelsDocument.php b/src/PhpWord/Writer/Word2007/Part/RelsDocument.php index e0773a4a..2a58421c 100644 --- a/src/PhpWord/Writer/Word2007/Part/RelsDocument.php +++ b/src/PhpWord/Writer/Word2007/Part/RelsDocument.php @@ -40,7 +40,10 @@ class RelsDocument extends Rels 'fontTable.xml' => 'officeDocument/2006/relationships/fontTable', ); $xmlWriter = $this->getXmlWriter(); - $this->writeRels($xmlWriter, $xmlRels, $this->getParentWriter()->getRelationships()); + + /** @var \PhpOffice\PhpWord\Writer\Word2007 $parentWriter Scrutinizer type hint */ + $parentWriter = $this->getParentWriter(); + $this->writeRels($xmlWriter, $xmlRels, $parentWriter->getRelationships()); return $xmlWriter->getData(); } diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index 862e419e..c296581f 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -126,6 +126,8 @@ class Settings extends AbstractPart $xmlWriter->writeElement($settingKey); } else { $xmlWriter->startElement($settingKey); + + /** @var array $settingValue Scrutinizer type hint */ foreach ($settingValue as $childKey => $childValue) { if ($childKey == '@attributes') { foreach ($childValue as $key => $val) { From e589961e68a7f6e9cb666a87fbce4c545be80431 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Mon, 12 May 2014 22:55:06 +0700 Subject: [PATCH 05/48] #158: Convert UTF8 text to Unicode before writing RTF (support UTF8 in RTF) --- CHANGELOG.md | 3 +- composer.lock | 2 +- src/PhpWord/Shared/String.php | 48 +++++++++++++++++++ src/PhpWord/Style/Paragraph.php | 19 ++++++-- src/PhpWord/Writer/RTF/Element/Container.php | 2 - src/PhpWord/Writer/RTF/Element/Text.php | 23 +++++---- src/PhpWord/Writer/RTF/Element/TextRun.php | 4 +- src/PhpWord/Writer/RTF/Element/Title.php | 4 +- src/PhpWord/Writer/RTF/Style/Font.php | 21 ++++++-- src/PhpWord/Writer/RTF/Style/Paragraph.php | 22 ++++++--- .../Word2007/Element/AbstractElement.php | 14 +++++- .../Writer/Word2007/Element/CheckBox.php | 13 ++--- .../Writer/Word2007/Element/Footnote.php | 2 +- src/PhpWord/Writer/Word2007/Element/Link.php | 2 +- .../Writer/Word2007/Element/PreserveText.php | 9 +--- src/PhpWord/Writer/Word2007/Element/Text.php | 10 ++-- .../Writer/Word2007/Element/TextBreak.php | 2 +- .../Writer/Word2007/Element/TextRun.php | 2 +- src/PhpWord/Writer/Word2007/Element/Title.php | 7 +-- 19 files changed, 144 insertions(+), 65 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af3ca786..55629257 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers ## 0.11.0 - Not yet released -This release changed PHPWord license from LGPL 2.1 to LGPL 3. +This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new relative and absolute positioning for image; new `TextBox` and `ListItemRun` element; refactorings of writer classes into parts, elements, and styles; and ability to add elements to PHPWord object via HTML. ### Features @@ -15,6 +15,7 @@ This release changed PHPWord license from LGPL 2.1 to LGPL 3. - HTML: Ability to add elements to PHPWord object via html - @basjan GH-231 - ListItemRun: New element that can add a list item with inline formatting like a textrun - @basjan GH-235 - Table: Ability to add table inside a cell (nested table) - @ivanlanin GH-149 +- RTF: UTF8 support for RTF: Internal UTF8 text is converted to Unicode before writing - @ivanlanin GH-158 ### Bugfixes diff --git a/composer.lock b/composer.lock index 6d63264a..9bc868ac 100644 --- a/composer.lock +++ b/composer.lock @@ -3,7 +3,7 @@ "This file locks the dependencies of your project to a known state", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" ], - "hash": "6daefa91649add98af3850b0a3f13415", + "hash": "77631436badcf4f49d673498ab6f1916", "packages": [ ], diff --git a/src/PhpWord/Shared/String.php b/src/PhpWord/Shared/String.php index 6ebcc65d..259e904c 100644 --- a/src/PhpWord/Shared/String.php +++ b/src/PhpWord/Shared/String.php @@ -85,6 +85,54 @@ class String return $value; } + /** + * Returns unicode from UTF8 text + * + * @param string $text UTF8 text + * @return string Unicode text + * @since 0.11.0 + * @link http://www.randomchaos.com/documents/?source=php_and_unicode + */ + public static function toUnicode($text) + { + $unicode = array(); + $values = array(); + $lookingFor = 1; + + // Gets unicode for each character + for ($i = 0; $i < strlen($text); $i++) { + $thisValue = ord($text[$i]); + if ($thisValue < 128) { + $unicode[] = $thisValue; + } else { + if (count($values) == 0) { + $lookingFor = $thisValue < 224 ? 2 : 3; + } + $values[] = $thisValue; + if (count($values) == $lookingFor) { + if ($lookingFor == 3) { + $number = (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64); + } else { + $number = (($values[0] % 32) * 64) + ($values[1] % 64); + } + $unicode[] = $number; + $values = array(); + $lookingFor = 1; + } + } + } + + // Converts text with utf8 characters into rtf utf8 entites preserving ascii + $entities = ''; + foreach ($unicode as $value) { + if ($value != 65279) { + $entities .= $value > 127 ? '\uc0{\u' . $value . '}' : chr($value); + } + } + + return $entities; + } + /** * Return name without underscore for < 0.10.0 variable name compatibility * diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index 79a12242..9867afe3 100644 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -25,8 +25,20 @@ use PhpOffice\PhpWord\Shared\String; */ class Paragraph extends AbstractStyle { + /** + * @const int One line height equals 240 twip + */ const LINE_HEIGHT = 240; + /** + * @const string Alignment http://www.schemacentral.com/sc/ooxml/t-w_ST_Jc.html + */ + const ALIGN_LEFT = 'left'; // Align left + const ALIGN_RIGHT = 'right'; // Align right + const ALIGN_CENTER = 'center'; // Align center + const ALIGN_BOTH = 'both'; // Align both + const ALIGN_JUSTIFY = 'justify'; // Alias for align both + /** * Aliases * @@ -147,10 +159,11 @@ class Paragraph extends AbstractStyle */ public function setAlign($value = null) { - if (strtolower($value) == 'justify') { - $value = 'both'; + if (strtolower($value) == self::ALIGN_JUSTIFY) { + $value = self::ALIGN_BOTH; } - $this->align = $value; + $enum = array(self::ALIGN_LEFT, self::ALIGN_RIGHT, self::ALIGN_CENTER, self::ALIGN_BOTH, self::ALIGN_JUSTIFY); + $this->align = $this->setEnumVal($value, $enum, $this->align); return $this; } diff --git a/src/PhpWord/Writer/RTF/Element/Container.php b/src/PhpWord/Writer/RTF/Element/Container.php index 67caba1d..f4b5f2ac 100644 --- a/src/PhpWord/Writer/RTF/Element/Container.php +++ b/src/PhpWord/Writer/RTF/Element/Container.php @@ -44,9 +44,7 @@ class Container extends \PhpOffice\PhpWord\Writer\HTML\Element\Container $writerClass = str_replace('\\Element', '\\Writer\\RTF\\Element', get_class($element)); if (class_exists($writerClass)) { $writer = new $writerClass($this->parentWriter, $element, $withoutP); - $content .= '{'; $content .= $writer->write(); - $content .= '}' . PHP_EOL; } } diff --git a/src/PhpWord/Writer/RTF/Element/Text.php b/src/PhpWord/Writer/RTF/Element/Text.php index 27172df3..51b24941 100644 --- a/src/PhpWord/Writer/RTF/Element/Text.php +++ b/src/PhpWord/Writer/RTF/Element/Text.php @@ -18,6 +18,7 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; use PhpOffice\PhpWord\Element\Text as TextElement; +use PhpOffice\PhpWord\Shared\String; use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Font as FontStyle; use PhpOffice\PhpWord\Writer\RTF\Style\Font as FontStyleWriter; @@ -46,12 +47,17 @@ class Text extends AbstractElement $content = ''; $content .= $this->writeParagraphStyle($this->element); - $content .= $this->writeFontStyleBegin($fontStyle); - if ($parentWriter->getLastParagraphStyle() != '' || $fontStyle) { + $content .= '{'; + $content .= $this->writeFontStyle($fontStyle); + if ($fontStyle || $parentWriter->getLastParagraphStyle() != '') { $content .= ' '; } - $content .= $this->element->getText(); - $content .= $this->writeFontStyleEnd($fontStyle); + $content .= String::toUnicode($this->element->getText()); + $content .= '}'; + + // Remarked to test using closure {} to avoid closing tags + // @since 0.11.0 + // $content .= $this->writeFontStyleClosing($fontStyle); if (!$this->withoutP) { $content .= '\par' . PHP_EOL; @@ -80,9 +86,10 @@ class Text extends AbstractElement // Write style when applicable if ($paragraphStyle && !$this->withoutP) { if ($parentWriter->getLastParagraphStyle() != $element->getParagraphStyle()) { + $parentWriter->setLastParagraphStyle($element->getParagraphStyle()); + $styleWriter = new ParagraphStyleWriter($paragraphStyle); $content = $styleWriter->write(); - $parentWriter->setLastParagraphStyle($element->getParagraphStyle()); } else { $parentWriter->setLastParagraphStyle(); } @@ -99,7 +106,7 @@ class Text extends AbstractElement * @param mixed $style * @return string */ - private function writeFontStyleBegin($style) + private function writeFontStyle($style) { if (!$style instanceof FontStyle) { return ''; @@ -135,14 +142,14 @@ class Text extends AbstractElement * @param \PhpOffice\PhpWord\Style\Font $style * @return string */ - private function writeFontStyleEnd($style) + private function writeFontStyleClosing($style) { if (!$style instanceof FontStyle) { return ''; } $styleWriter = new FontStyleWriter($style); - $content = $styleWriter->writeEnd(); + $content = $styleWriter->writeClosing(); return $content; } diff --git a/src/PhpWord/Writer/RTF/Element/TextRun.php b/src/PhpWord/Writer/RTF/Element/TextRun.php index 174aebd9..8d7324a3 100644 --- a/src/PhpWord/Writer/RTF/Element/TextRun.php +++ b/src/PhpWord/Writer/RTF/Element/TextRun.php @@ -35,10 +35,10 @@ class TextRun extends AbstractElement { $content = ''; - $content .= '\pard\nowidctlpar' . PHP_EOL; + $content .= '{\pard\nowidctlpar'; $writer = new Container($this->parentWriter, $this->element); $content .= $writer->write(); - $content .= '\par' . PHP_EOL; + $content .= '\par}' . PHP_EOL; return $content; } diff --git a/src/PhpWord/Writer/RTF/Element/Title.php b/src/PhpWord/Writer/RTF/Element/Title.php index d78bb6f4..2be72ff8 100644 --- a/src/PhpWord/Writer/RTF/Element/Title.php +++ b/src/PhpWord/Writer/RTF/Element/Title.php @@ -37,8 +37,8 @@ class Title extends AbstractElement $content = ''; - $content .= '\pard\nowidctlpar' . PHP_EOL; - $content .= $this->element->getText(); + $content .= '\pard\nowidctlpar'; + $content .= String::toUnicode($this->element->getText()); $content .= '\par' . PHP_EOL; return $content; diff --git a/src/PhpWord/Writer/RTF/Style/Font.php b/src/PhpWord/Writer/RTF/Style/Font.php index 49c9889c..c20558db 100644 --- a/src/PhpWord/Writer/RTF/Style/Font.php +++ b/src/PhpWord/Writer/RTF/Style/Font.php @@ -18,6 +18,7 @@ namespace PhpOffice\PhpWord\Writer\RTF\Style; use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\Style\Font as FontStyle; /** * RTF font style writer @@ -51,12 +52,17 @@ class Font extends AbstractStyle $content = ''; $content .= '\cf' . $this->colorIndex; $content .= '\f' . $this->nameIndex; - $content .= $this->getValueIf($style->isBold(), '\b'); - $content .= $this->getValueIf($style->isItalic(), '\i'); $size = $style->getSize(); $content .= $this->getValueIf(is_numeric($size), '\fs' . ($size * 2)); + $content .= $this->getValueIf($style->isBold(), '\b'); + $content .= $this->getValueIf($style->isItalic(), '\i'); + $content .= $this->getValueIf($style->getUnderline() != FontStyle::UNDERLINE_NONE, '\ul'); + $content .= $this->getValueIf($style->isStrikethrough(), '\strike'); + $content .= $this->getValueIf($style->isSuperScript(), '\super'); + $content .= $this->getValueIf($style->isSubScript(), '\sub'); + return $content; } @@ -65,7 +71,7 @@ class Font extends AbstractStyle * * @return string */ - public function writeEnd() + public function writeClosing() { $style = $this->getStyle(); if (!$style instanceof \PhpOffice\PhpWord\Style\Font) { @@ -75,12 +81,17 @@ class Font extends AbstractStyle $content = ''; $content .= '\cf0'; $content .= '\f0'; - $content .= $this->getValueIf($style->isBold(), '\b0'); - $content .= $this->getValueIf($style->isItalic(), '\i0'); $size = $style->getSize(); $content .= $this->getValueIf(is_numeric($size), '\fs' . (PhpWord::DEFAULT_FONT_SIZE * 2)); + $content .= $this->getValueIf($style->isBold(), '\b0'); + $content .= $this->getValueIf($style->isItalic(), '\i0'); + $content .= $this->getValueIf($style->getUnderline() != FontStyle::UNDERLINE_NONE, '\ul0'); + $content .= $this->getValueIf($style->isStrikethrough(), '\strike0'); + $content .= $this->getValueIf($style->isSuperScript(), '\super0'); + $content .= $this->getValueIf($style->isSubScript(), '\sub0'); + return $content; } diff --git a/src/PhpWord/Writer/RTF/Style/Paragraph.php b/src/PhpWord/Writer/RTF/Style/Paragraph.php index 512e5774..8abdf2fb 100644 --- a/src/PhpWord/Writer/RTF/Style/Paragraph.php +++ b/src/PhpWord/Writer/RTF/Style/Paragraph.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Writer\RTF\Style; +use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle; + /** * RTF paragraph style writer * @@ -36,15 +38,23 @@ class Paragraph extends AbstractStyle return; } - $content = '\pard\nowidctlpar'; + $alignments = array( + ParagraphStyle::ALIGN_LEFT => '\ql', + ParagraphStyle::ALIGN_RIGHT => '\qr', + ParagraphStyle::ALIGN_CENTER => '\qc', + ParagraphStyle::ALIGN_BOTH => '\qj', + ); - // Alignment $align = $style->getAlign(); - $content .= $this->getValueIf(!is_null($align) && $align == 'center', '\qc'); - - // Spacing $spaceAfter = $style->getSpaceAfter(); - $content .= $this->getValueIf(!is_null($spaceAfter), '\sa' . $spaceAfter); + $spaceBefore = $style->getSpaceBefore(); + + $content = '\pard\nowidctlpar'; + if (isset($alignments[$align])) { + $content .= $alignments[$align]; + } + $content .= $this->getValueIf($spaceBefore !== null, '\sb' . $spaceBefore); + $content .= $this->getValueIf($spaceAfter !== null, '\sa' . $spaceAfter); return $content; } diff --git a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php index 4b38d01f..bbb2c83e 100644 --- a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php +++ b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php @@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; use PhpOffice\PhpWord\Element\AbstractElement as Element; use PhpOffice\PhpWord\Exception\Exception; +use PhpOffice\PhpWord\Shared\String; use PhpOffice\PhpWord\Shared\XMLWriter; /** @@ -77,7 +78,7 @@ abstract class AbstractElement } /** - * Get Element + * Get element * * @return \PhpOffice\PhpWord\Element\AbstractElement */ @@ -89,4 +90,15 @@ abstract class AbstractElement throw new Exception('No element assigned.'); } } + + /** + * Convert text to valid format + * + * @param string $text + * @return string + */ + protected function getText($text) + { + return String::controlCharacterPHP2OOXML(htmlspecialchars($text)); + } } diff --git a/src/PhpWord/Writer/Word2007/Element/CheckBox.php b/src/PhpWord/Writer/Word2007/Element/CheckBox.php index 1d7811cd..ea13b8f9 100644 --- a/src/PhpWord/Writer/Word2007/Element/CheckBox.php +++ b/src/PhpWord/Writer/Word2007/Element/CheckBox.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; -use PhpOffice\PhpWord\Shared\String; - /** * CheckBox element writer * @@ -37,11 +35,6 @@ class CheckBox extends Text return; } - $name = htmlspecialchars($element->getName()); - $name = String::controlCharacterPHP2OOXML($name); - $text = htmlspecialchars($element->getText()); - $text = String::controlCharacterPHP2OOXML($text); - $this->writeOpeningWP(); $xmlWriter->startElement('w:r'); @@ -49,7 +42,7 @@ class CheckBox extends Text $xmlWriter->writeAttribute('w:fldCharType', 'begin'); $xmlWriter->startElement('w:ffData'); $xmlWriter->startElement('w:name'); - $xmlWriter->writeAttribute('w:val', $name); + $xmlWriter->writeAttribute('w:val', $this->getText($element->getName())); $xmlWriter->endElement(); //w:name $xmlWriter->writeAttribute('w:enabled', ''); $xmlWriter->startElement('w:calcOnExit'); @@ -88,10 +81,10 @@ class CheckBox extends Text $xmlWriter->startElement('w:t'); $xmlWriter->writeAttribute('xml:space', 'preserve'); - $xmlWriter->writeRaw($text); + $xmlWriter->writeRaw($this->getText($element->getText())); $xmlWriter->endElement(); // w:t $xmlWriter->endElement(); // w:r - $this->writeEndingWP(); + $this->writeClosingWP(); } } diff --git a/src/PhpWord/Writer/Word2007/Element/Footnote.php b/src/PhpWord/Writer/Word2007/Element/Footnote.php index d33a0dc9..240a8a00 100644 --- a/src/PhpWord/Writer/Word2007/Element/Footnote.php +++ b/src/PhpWord/Writer/Word2007/Element/Footnote.php @@ -55,6 +55,6 @@ class Footnote extends Text $xmlWriter->endElement(); // w:$referenceType $xmlWriter->endElement(); // w:r - $this->writeEndingWP(); + $this->writeClosingWP(); } } diff --git a/src/PhpWord/Writer/Word2007/Element/Link.php b/src/PhpWord/Writer/Word2007/Element/Link.php index 3b595b4c..ec531bac 100644 --- a/src/PhpWord/Writer/Word2007/Element/Link.php +++ b/src/PhpWord/Writer/Word2007/Element/Link.php @@ -53,6 +53,6 @@ class Link extends Text $xmlWriter->endElement(); // w:r $xmlWriter->endElement(); // w:hyperlink - $this->writeEndingWP(); + $this->writeClosingWP(); } } diff --git a/src/PhpWord/Writer/Word2007/Element/PreserveText.php b/src/PhpWord/Writer/Word2007/Element/PreserveText.php index 97693d9b..dd5d9008 100644 --- a/src/PhpWord/Writer/Word2007/Element/PreserveText.php +++ b/src/PhpWord/Writer/Word2007/Element/PreserveText.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; -use PhpOffice\PhpWord\Shared\String; - /** * PreserveText element writer * @@ -76,21 +74,18 @@ class PreserveText extends Text $xmlWriter->endElement(); $xmlWriter->endElement(); } else { - $text = htmlspecialchars($text); - $text = String::controlCharacterPHP2OOXML($text); - $xmlWriter->startElement('w:r'); $this->writeFontStyle(); $xmlWriter->startElement('w:t'); $xmlWriter->writeAttribute('xml:space', 'preserve'); - $xmlWriter->writeRaw($text); + $xmlWriter->writeRaw($this->getText($text)); $xmlWriter->endElement(); $xmlWriter->endElement(); } } - $this->writeEndingWP(); + $this->writeClosingWP(); } } diff --git a/src/PhpWord/Writer/Word2007/Element/Text.php b/src/PhpWord/Writer/Word2007/Element/Text.php index e06bde6b..5cb33a28 100644 --- a/src/PhpWord/Writer/Word2007/Element/Text.php +++ b/src/PhpWord/Writer/Word2007/Element/Text.php @@ -17,7 +17,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; -use PhpOffice\PhpWord\Shared\String; use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter; use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter; @@ -39,9 +38,6 @@ class Text extends AbstractElement return; } - $text = htmlspecialchars($element->getText()); - $text = String::controlCharacterPHP2OOXML($text); - $this->writeOpeningWP(); $xmlWriter->startElement('w:r'); @@ -50,11 +46,11 @@ class Text extends AbstractElement $xmlWriter->startElement('w:t'); $xmlWriter->writeAttribute('xml:space', 'preserve'); - $xmlWriter->writeRaw($text); + $xmlWriter->writeRaw($this->getText($element->getText())); $xmlWriter->endElement(); $xmlWriter->endElement(); // w:r - $this->writeEndingWP(); + $this->writeClosingWP(); } /** @@ -77,7 +73,7 @@ class Text extends AbstractElement /** * Write ending */ - protected function writeEndingWP() + protected function writeClosingWP() { $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/Word2007/Element/TextBreak.php b/src/PhpWord/Writer/Word2007/Element/TextBreak.php index c74cdc7a..227b1b30 100644 --- a/src/PhpWord/Writer/Word2007/Element/TextBreak.php +++ b/src/PhpWord/Writer/Word2007/Element/TextBreak.php @@ -42,7 +42,7 @@ class TextBreak extends Text $xmlWriter->startElement('w:pPr'); $this->writeFontStyle(); $xmlWriter->endElement(); // w:pPr - $this->writeEndingWP(); + $this->writeClosingWP(); } else { $xmlWriter->writeElement('w:p'); } diff --git a/src/PhpWord/Writer/Word2007/Element/TextRun.php b/src/PhpWord/Writer/Word2007/Element/TextRun.php index cb72ab18..330e297c 100644 --- a/src/PhpWord/Writer/Word2007/Element/TextRun.php +++ b/src/PhpWord/Writer/Word2007/Element/TextRun.php @@ -37,6 +37,6 @@ class TextRun extends Text $containerWriter = new Container($xmlWriter, $element); $containerWriter->write(); - $this->writeEndingWP(); + $this->writeClosingWP(); } } diff --git a/src/PhpWord/Writer/Word2007/Element/Title.php b/src/PhpWord/Writer/Word2007/Element/Title.php index 6f2bd314..298bd9b1 100644 --- a/src/PhpWord/Writer/Word2007/Element/Title.php +++ b/src/PhpWord/Writer/Word2007/Element/Title.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; -use PhpOffice\PhpWord\Shared\String; - /** * TextRun element writer * @@ -41,9 +39,6 @@ class Title extends AbstractElement $anchor = '_Toc' . ($rId + 252634154); $style = $element->getStyle(); - $text = htmlspecialchars($element->getText()); - $text = String::controlCharacterPHP2OOXML($text); - $xmlWriter->startElement('w:p'); if (!empty($style)) { @@ -67,7 +62,7 @@ class Title extends AbstractElement $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:t'); - $xmlWriter->writeRaw($text); + $xmlWriter->writeRaw($this->getText($element->getText())); $xmlWriter->endElement(); $xmlWriter->endElement(); From f8f98cccabf58e456730459762ffea12e46d43eb Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Tue, 13 May 2014 01:32:44 +0700 Subject: [PATCH 06/48] #237: Ability to define table width (in percent and twip) and position --- CHANGELOG.md | 1 + samples/Sample_09_Tables.php | 7 +- src/PhpWord/Shared/Html.php | 24 ++-- src/PhpWord/Style/Alignment.php | 78 +++++++++++ src/PhpWord/Style/Paragraph.php | 43 +++--- src/PhpWord/Style/Table.php | 129 +++++++++++++++--- src/PhpWord/Writer/RTF/Element/Text.php | 22 --- src/PhpWord/Writer/RTF/Element/Title.php | 2 + src/PhpWord/Writer/RTF/Style/Font.php | 29 ---- src/PhpWord/Writer/RTF/Style/Paragraph.php | 9 +- .../Writer/Word2007/Style/Alignment.php | 44 ++++++ .../Writer/Word2007/Style/Paragraph.php | 6 +- src/PhpWord/Writer/Word2007/Style/Table.php | 56 +++++--- .../Tests/Writer/Word2007/StyleTest.php | 2 +- 14 files changed, 321 insertions(+), 131 deletions(-) create mode 100644 src/PhpWord/Style/Alignment.php create mode 100644 src/PhpWord/Writer/Word2007/Style/Alignment.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 55629257..7bcc5a2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r - ListItemRun: New element that can add a list item with inline formatting like a textrun - @basjan GH-235 - Table: Ability to add table inside a cell (nested table) - @ivanlanin GH-149 - RTF: UTF8 support for RTF: Internal UTF8 text is converted to Unicode before writing - @ivanlanin GH-158 +- Table: Ability to define table width (in percent and twip) and position - @ivanlanin GH-237 ### Bugfixes diff --git a/samples/Sample_09_Tables.php b/samples/Sample_09_Tables.php index 5f77f8db..6732e336 100644 --- a/samples/Sample_09_Tables.php +++ b/samples/Sample_09_Tables.php @@ -87,11 +87,12 @@ $table->addCell(null, $cellRowContinue); // 4. Nested table $section->addTextBreak(2); -$section->addText('Nested table', $header); +$section->addText('Nested table in a centered and 50% width table.', $header); -$cell = $section->addTable()->addRow()->addCell(); +$table = $section->addTable(array('width' => 50 * 50, 'unit' => 'pct', 'align' => 'center')); +$cell = $table->addRow()->addCell(); $cell->addText('This cell contains nested table.'); -$innerCell = $cell->addTable()->addRow()->addCell(); +$innerCell = $cell->addTable(array('align' => 'center'))->addRow()->addCell(); $innerCell->addText('Inside nested table'); // Save file diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index c56e418b..59795af7 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -27,9 +27,8 @@ class Html * * Note: $stylesheet parameter is removed to avoid PHPMD error for unused parameter * - * @param \PhpOffice\PhpWord\Element\AbstractElement $object Where the parts need to be added + * @param \PhpOffice\PhpWord\Element\AbstractContainer $object Where the parts need to be added * @param string $html the code to parse - * */ public static function addHtml($object, $html) { @@ -53,7 +52,6 @@ class Html * * @param \DOMNode $node Node to check on attributes and to compile a style array * @param array $style is supplied, the inline style attributes are added to the already existing style - * */ protected static function parseInlineStyle($node, $style = array()) { @@ -100,10 +98,9 @@ class Html * parse a node and add a corresponding element to the object * * @param \DOMNode $node node to parse - * @param \PhpOffice\PhpWord\Element\AbstractElement $object object to add an element corresponding with the node + * @param \PhpOffice\PhpWord\Element\AbstractContainer $object object to add an element corresponding with the node * @param array $styles Array with all styles * @param array $data Array to transport data to a next level in the DOM tree, for example level of listitems - * */ protected static function parseNode( $node, @@ -171,17 +168,26 @@ class Html case 'table': $styles['paragraphStyle'] = self::parseInlineStyle($node, $styles['paragraphStyle']); $newobject = $object->addTable(); - // if ($attributes->getNamedItem('width') !== null)$newobject->setWidth($attributes->getNamedItem('width')->value); + // if ($attributes->getNamedItem('width') !== null) { + // $newobject->setWidth($attributes->getNamedItem('width')->value); + // } break; case 'tr': + /** @var \PhpOffice\PhpWord\Element\Table $object Scrutinizer type hint */ $styles['paragraphStyle'] = self::parseInlineStyle($node, $styles['paragraphStyle']); $newobject = $object->addRow(); - // if ($attributes->getNamedItem('height') !== null)$newobject->setHeight($attributes->getNamedItem('height')->value); + // if ($attributes->getNamedItem('height') !== null) { + // $newobject->setHeight($attributes->getNamedItem('height')->value); + // } break; case 'td': + /** @var \PhpOffice\PhpWord\Element\Row $object Scrutinizer type hint */ $styles['paragraphStyle'] = self::parseInlineStyle($node, $styles['paragraphStyle']); - // if ($attributes->getNamedItem('width') !== null)$newobject=$object->addCell($width=$attributes->getNamedItem('width')->value); - // else $newobject=$object->addCell(); + // if ($attributes->getNamedItem('width') !== null) { + // $newobject=$object->addCell($width=$attributes->getNamedItem('width')->value); + // } else { + // $newobject=$object->addCell(); + // } $newobject = $object->addCell(); break; case 'ul': diff --git a/src/PhpWord/Style/Alignment.php b/src/PhpWord/Style/Alignment.php new file mode 100644 index 00000000..3beabe37 --- /dev/null +++ b/src/PhpWord/Style/Alignment.php @@ -0,0 +1,78 @@ +setStyleByArray($style); + } + + /** + * Get alignment + * + * @return string + */ + public function getValue() + { + return $this->value; + } + + /** + * Set alignment + * + * @param string $value + * @return self + */ + public function setValue($value = null) + { + if (strtolower($value) == self::ALIGN_JUSTIFY) { + $value = self::ALIGN_BOTH; + } + $enum = array(self::ALIGN_LEFT, self::ALIGN_RIGHT, self::ALIGN_CENTER, self::ALIGN_BOTH, self::ALIGN_JUSTIFY); + $this->value = $this->setEnumVal($value, $enum, $this->value); + + return $this; + } +} diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index 9867afe3..56609119 100644 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -30,15 +30,6 @@ class Paragraph extends AbstractStyle */ const LINE_HEIGHT = 240; - /** - * @const string Alignment http://www.schemacentral.com/sc/ooxml/t-w_ST_Jc.html - */ - const ALIGN_LEFT = 'left'; // Align left - const ALIGN_RIGHT = 'right'; // Align right - const ALIGN_CENTER = 'center'; // Align center - const ALIGN_BOTH = 'both'; // Align both - const ALIGN_JUSTIFY = 'justify'; // Alias for align both - /** * Aliases * @@ -46,13 +37,6 @@ class Paragraph extends AbstractStyle */ protected $aliases = array('line-height' => 'lineHeight'); - /** - * Paragraph alignment - * - * @var string - */ - private $align; - /** * Text line height * @@ -123,6 +107,21 @@ class Paragraph extends AbstractStyle */ private $spacing; + /** + * Alignment + * + * @var \PhpOffice\PhpWord\Style\Alignment + */ + private $alignment; + + /** + * Create new instance + */ + public function __construct() + { + $this->alignment = new Alignment(); + } + /** * Set Style value * @@ -142,28 +141,24 @@ class Paragraph extends AbstractStyle } /** - * Get Paragraph Alignment + * Get alignment * * @return string */ public function getAlign() { - return $this->align; + return $this->alignment->getValue(); } /** - * Set Paragraph Alignment + * Set alignment * * @param string $value * @return self */ public function setAlign($value = null) { - if (strtolower($value) == self::ALIGN_JUSTIFY) { - $value = self::ALIGN_BOTH; - } - $enum = array(self::ALIGN_LEFT, self::ALIGN_RIGHT, self::ALIGN_CENTER, self::ALIGN_BOTH, self::ALIGN_JUSTIFY); - $this->align = $this->setEnumVal($value, $enum, $this->align); + $this->alignment->setValue($value); return $this; } diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index c9850e73..c077f499 100644 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -22,6 +22,13 @@ namespace PhpOffice\PhpWord\Style; */ class Table extends Border { + /** + * @const string Table width units http://www.schemacentral.com/sc/ooxml/t-w_ST_TblWidth.html + */ + const WIDTH_AUTO = 'auto'; // Automatically determined width + const WIDTH_PERCENT = 'pct'; // Width in fiftieths (1/50) of a percent (1% = 50 unit) + const WIDTH_TWIP = 'dxa'; // Width in twentieths (1/20) of a point (twip) + /** * Style for first row * @@ -92,15 +99,31 @@ class Table extends Border */ private $shading; + /** + * @var \PhpOffice\PhpWord\Style\Alignment Alignment + */ + private $alignment; + + /** + * @var int|float Width value + */ + private $width = 0; + + /** + * @var string Width unit + */ + private $unit = self::WIDTH_AUTO; + /** * Create new table style * - * @param mixed $styleTable - * @param mixed $styleFirstRow + * @param mixed $tableStyle + * @param mixed $firstRowStyle */ - public function __construct($styleTable = null, $styleFirstRow = null) + public function __construct($tableStyle = null, $firstRowStyle = null) { - if (!is_null($styleFirstRow) && is_array($styleFirstRow)) { + $this->alignment = new Alignment(); + if (!is_null($firstRowStyle) && is_array($firstRowStyle)) { $this->firstRow = clone $this; unset($this->firstRow->firstRow); @@ -112,11 +135,11 @@ class Table extends Border unset($this->firstRow->borderInsideVSize); unset($this->firstRow->borderInsideHColor); unset($this->firstRow->borderInsideHSize); - $this->firstRow->setStyleByArray($styleFirstRow); + $this->firstRow->setStyleByArray($firstRowStyle); } - if (!is_null($styleTable) && is_array($styleTable)) { - $this->setStyleByArray($styleTable); + if (!is_null($tableStyle) && is_array($tableStyle)) { + $this->setStyleByArray($tableStyle); } } @@ -408,6 +431,24 @@ class Table extends Border return array($this->cellMarginTop, $this->cellMarginLeft, $this->cellMarginRight, $this->cellMarginBottom); } + /** + * Has margins? + * + * @return bool + */ + public function hasMargins() + { + $hasMargins = false; + $margins = $this->getCellMargin(); + for ($i = 0; $i < count($margins); $i++) { + if (!is_null($margins[$i])) { + $hasMargins = true; + } + } + + return $hasMargins; + } + /** * Get shading * @@ -432,20 +473,72 @@ class Table extends Border } /** - * Has margins? + * Get alignment * - * @return bool + * @return string */ - public function hasMargins() + public function getAlign() { - $hasMargins = false; - $margins = $this->getCellMargin(); - for ($i = 0; $i < count($margins); $i++) { - if (!is_null($margins[$i])) { - $hasMargins = true; - } - } + return $this->alignment->getValue(); + } - return $hasMargins; + /** + * Set alignment + * + * @param string $value + * @return self + */ + public function setAlign($value = null) + { + $this->alignment->setValue($value); + + return $this; + } + + /** + * Get width + * + * @return int|float + */ + public function getWidth() + { + return $this->width; + } + + /** + * Set width + * + * @param int|float $value + * @return self + */ + public function setWidth($value = null) + { + $this->width = $this->setNumericVal($value, $this->width); + + return $this; + } + + /** + * Get width unit + * + * @return string + */ + public function getUnit() + { + return $this->unit; + } + + /** + * Set width unit + * + * @param string $value + * @return self + */ + public function setUnit($value = null) + { + $enum = array(self::WIDTH_AUTO, self::WIDTH_PERCENT, self::WIDTH_TWIP); + $this->unit = $this->setEnumVal($value, $enum, $this->unit); + + return $this; } } diff --git a/src/PhpWord/Writer/RTF/Element/Text.php b/src/PhpWord/Writer/RTF/Element/Text.php index 51b24941..ae97fd3a 100644 --- a/src/PhpWord/Writer/RTF/Element/Text.php +++ b/src/PhpWord/Writer/RTF/Element/Text.php @@ -55,10 +55,6 @@ class Text extends AbstractElement $content .= String::toUnicode($this->element->getText()); $content .= '}'; - // Remarked to test using closure {} to avoid closing tags - // @since 0.11.0 - // $content .= $this->writeFontStyleClosing($fontStyle); - if (!$this->withoutP) { $content .= '\par' . PHP_EOL; } @@ -136,24 +132,6 @@ class Text extends AbstractElement return $content; } - /** - * Write font style ending - * - * @param \PhpOffice\PhpWord\Style\Font $style - * @return string - */ - private function writeFontStyleClosing($style) - { - if (!$style instanceof FontStyle) { - return ''; - } - - $styleWriter = new FontStyleWriter($style); - $content = $styleWriter->writeClosing(); - - return $content; - } - /** * Get font style * diff --git a/src/PhpWord/Writer/RTF/Element/Title.php b/src/PhpWord/Writer/RTF/Element/Title.php index 2be72ff8..d7db8f2d 100644 --- a/src/PhpWord/Writer/RTF/Element/Title.php +++ b/src/PhpWord/Writer/RTF/Element/Title.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; +use PhpOffice\PhpWord\Shared\String; + /** * TextBreak element RTF writer * diff --git a/src/PhpWord/Writer/RTF/Style/Font.php b/src/PhpWord/Writer/RTF/Style/Font.php index c20558db..d14ee9c5 100644 --- a/src/PhpWord/Writer/RTF/Style/Font.php +++ b/src/PhpWord/Writer/RTF/Style/Font.php @@ -66,35 +66,6 @@ class Font extends AbstractStyle return $content; } - /** - * Write end style - * - * @return string - */ - public function writeClosing() - { - $style = $this->getStyle(); - if (!$style instanceof \PhpOffice\PhpWord\Style\Font) { - return; - } - - $content = ''; - $content .= '\cf0'; - $content .= '\f0'; - - $size = $style->getSize(); - $content .= $this->getValueIf(is_numeric($size), '\fs' . (PhpWord::DEFAULT_FONT_SIZE * 2)); - - $content .= $this->getValueIf($style->isBold(), '\b0'); - $content .= $this->getValueIf($style->isItalic(), '\i0'); - $content .= $this->getValueIf($style->getUnderline() != FontStyle::UNDERLINE_NONE, '\ul0'); - $content .= $this->getValueIf($style->isStrikethrough(), '\strike0'); - $content .= $this->getValueIf($style->isSuperScript(), '\super0'); - $content .= $this->getValueIf($style->isSubScript(), '\sub0'); - - return $content; - } - /** * Set font name index * diff --git a/src/PhpWord/Writer/RTF/Style/Paragraph.php b/src/PhpWord/Writer/RTF/Style/Paragraph.php index 8abdf2fb..8811cacf 100644 --- a/src/PhpWord/Writer/RTF/Style/Paragraph.php +++ b/src/PhpWord/Writer/RTF/Style/Paragraph.php @@ -17,6 +17,7 @@ namespace PhpOffice\PhpWord\Writer\RTF\Style; +use PhpOffice\PhpWord\Style\Alignment; use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle; /** @@ -39,10 +40,10 @@ class Paragraph extends AbstractStyle } $alignments = array( - ParagraphStyle::ALIGN_LEFT => '\ql', - ParagraphStyle::ALIGN_RIGHT => '\qr', - ParagraphStyle::ALIGN_CENTER => '\qc', - ParagraphStyle::ALIGN_BOTH => '\qj', + Alignment::ALIGN_LEFT => '\ql', + Alignment::ALIGN_RIGHT => '\qr', + Alignment::ALIGN_CENTER => '\qc', + Alignment::ALIGN_BOTH => '\qj', ); $align = $style->getAlign(); diff --git a/src/PhpWord/Writer/Word2007/Style/Alignment.php b/src/PhpWord/Writer/Word2007/Style/Alignment.php new file mode 100644 index 00000000..cefadb57 --- /dev/null +++ b/src/PhpWord/Writer/Word2007/Style/Alignment.php @@ -0,0 +1,44 @@ +getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Alignment) { + return; + } + $value = $style->getValue(); + if ($value !== null) { + $xmlWriter = $this->getXmlWriter(); + $xmlWriter->startElement('w:jc'); + $xmlWriter->writeAttribute('w:val', $value); + $xmlWriter->endElement(); // w:jc + } + } +} diff --git a/src/PhpWord/Writer/Word2007/Style/Paragraph.php b/src/PhpWord/Writer/Word2007/Style/Paragraph.php index 88a75845..7322fd91 100644 --- a/src/PhpWord/Writer/Word2007/Style/Paragraph.php +++ b/src/PhpWord/Writer/Word2007/Style/Paragraph.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Style; +use PhpOffice\PhpWord\Style\Alignment as AlignmentStyle; + /** * Paragraph style writer * @@ -81,8 +83,8 @@ class Paragraph extends AbstractStyle $xmlWriter->writeElementIf(!is_null($styleName), 'w:pStyle', 'w:val', $styleName); // Alignment - $align = $style->getAlign(); - $xmlWriter->writeElementIf(!is_null($align), 'w:jc', 'w:val', $align); + $styleWriter = new Alignment($xmlWriter, new AlignmentStyle(array('value' => $style->getAlign()))); + $styleWriter->write(); // Pagination $xmlWriter->writeElementIf(!$style->hasWidowControl(), 'w:widowControl', 'w:val', '0'); diff --git a/src/PhpWord/Writer/Word2007/Style/Table.php b/src/PhpWord/Writer/Word2007/Style/Table.php index 0a0241c3..3fbf8497 100644 --- a/src/PhpWord/Writer/Word2007/Style/Table.php +++ b/src/PhpWord/Writer/Word2007/Style/Table.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Style; +use PhpOffice\PhpWord\Style\Alignment as AlignmentStyle; + /** * Table style writer * @@ -42,30 +44,46 @@ class Table extends AbstractStyle } $xmlWriter = $this->getXmlWriter(); - $hasBorders = $style->hasBorders(); + + // w:tblPr $hasMargins = $style->hasMargins(); - if ($hasMargins || $hasBorders) { - $xmlWriter->startElement('w:tblPr'); - if ($hasMargins) { - $mbWriter = new MarginBorder($xmlWriter); - $mbWriter->setSizes($style->getCellMargin()); + $hasBorders = $style->hasBorders(); + $align = $style->getAlign(); - $xmlWriter->startElement('w:tblCellMar'); - $mbWriter->write(); - $xmlWriter->endElement(); // w:tblCellMar - } - if ($hasBorders) { - $mbWriter = new MarginBorder($xmlWriter); - $mbWriter->setSizes($style->getBorderSize()); - $mbWriter->setColors($style->getBorderColor()); + $xmlWriter->startElement('w:tblPr'); - $xmlWriter->startElement('w:tblBorders'); - $mbWriter->write(); - $xmlWriter->endElement(); // w:tblBorders - } - $xmlWriter->endElement(); // 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 diff --git a/tests/PhpWord/Tests/Writer/Word2007/StyleTest.php b/tests/PhpWord/Tests/Writer/Word2007/StyleTest.php index 97a6c6bd..dfcf3fee 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/StyleTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/StyleTest.php @@ -29,7 +29,7 @@ class StyleTest extends \PHPUnit_Framework_TestCase public function testEmptyStyles() { $styles = array( - 'Cell', 'Font', 'Image', 'Indentation', 'LineNumbering', + 'Alignment', 'Cell', 'Font', 'Image', 'Indentation', 'LineNumbering', 'Paragraph', 'Section', 'Shading', 'Spacing', 'Tab', 'Table', 'TextBox' ); foreach ($styles as $style) { From 4b1a16006d44d36e91e676ea76ea02f89927cb90 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Tue, 13 May 2014 02:43:44 +0700 Subject: [PATCH 07/48] #196: Ability to add links and page breaks in RTF --- CHANGELOG.md | 1 + docs/intro.rst | 4 +- docs/src/documentation.md | 4 +- samples/Sample_01_SimpleText.php | 6 ++- src/PhpWord/Writer/RTF/Element/Link.php | 54 +++++++++++++++++++ src/PhpWord/Writer/RTF/Element/PageBreak.php | 36 +++++++++++++ src/PhpWord/Writer/RTF/Element/Title.php | 2 +- .../PhpWord/Tests/Writer/RTF/ElementTest.php | 2 +- 8 files changed, 101 insertions(+), 8 deletions(-) create mode 100644 src/PhpWord/Writer/RTF/Element/Link.php create mode 100644 src/PhpWord/Writer/RTF/Element/PageBreak.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bcc5a2f..8878e3db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r - Table: Ability to add table inside a cell (nested table) - @ivanlanin GH-149 - RTF: UTF8 support for RTF: Internal UTF8 text is converted to Unicode before writing - @ivanlanin GH-158 - Table: Ability to define table width (in percent and twip) and position - @ivanlanin GH-237 +- RTF: Ability to add links and page breaks in RTF - @ivanlanin GH-196 ### Bugfixes diff --git a/docs/intro.rst b/docs/intro.rst index 25a50205..0b0c1dfe 100644 --- a/docs/intro.rst +++ b/docs/intro.rst @@ -73,13 +73,13 @@ Writers +---------------------------+----------------------+--------+-------+-------+--------+-------+ | | Title | ✓ | | | ✓ | ✓ | +---------------------------+----------------------+--------+-------+-------+--------+-------+ -| | Link | ✓ | ✓ | | ✓ | ✓ | +| | Link | ✓ | ✓ | ✓ | ✓ | ✓ | +---------------------------+----------------------+--------+-------+-------+--------+-------+ | | Preserve Text | ✓ | | | | | +---------------------------+----------------------+--------+-------+-------+--------+-------+ | | Text Break | ✓ | ✓ | ✓ | ✓ | ✓ | +---------------------------+----------------------+--------+-------+-------+--------+-------+ -| | Page Break | ✓ | | | | | +| | Page Break | ✓ | | ✓ | | | +---------------------------+----------------------+--------+-------+-------+--------+-------+ | | List | ✓ | | | | | +---------------------------+----------------------+--------+-------+-------+--------+-------+ diff --git a/docs/src/documentation.md b/docs/src/documentation.md index a140c5ba..5b969687 100644 --- a/docs/src/documentation.md +++ b/docs/src/documentation.md @@ -84,10 +84,10 @@ Below are the supported features for each file formats. | **Element Type** | Text | ✓ | ✓ | ✓ | ✓ | ✓ | | | Text Run | ✓ | ✓ | ✓ | ✓ | ✓ | | | Title | ✓ | | | ✓ | ✓ | -| | Link | ✓ | ✓ | | ✓ | ✓ | +| | Link | ✓ | ✓ | ✓ | ✓ | ✓ | | | Preserve Text | ✓ | | | | | | | Text Break | ✓ | ✓ | ✓ | ✓ | ✓ | -| | Page Break | ✓ | | | | | +| | Page Break | ✓ | | ✓ | | | | | List | ✓ | | | | | | | Table | ✓ | ✓ | | ✓ | ✓ | | | Image | ✓ | ✓ | | ✓ | | diff --git a/samples/Sample_01_SimpleText.php b/samples/Sample_01_SimpleText.php index 7202ed7e..311f3350 100644 --- a/samples/Sample_01_SimpleText.php +++ b/samples/Sample_01_SimpleText.php @@ -22,7 +22,8 @@ $section->addTextBreak(2); $section->addText('I am styled by a font style definition.', 'rStyle'); $section->addText('I am styled by a paragraph style definition.', null, 'pStyle'); $section->addText('I am styled by both font and paragraph style.', 'rStyle', 'pStyle'); -$section->addTextBreak(); + +$section->addPageBreak(); // Inline font style $fontStyle['name'] = 'Times New Roman'; @@ -36,10 +37,11 @@ $fontStyle['color'] = 'FF0000'; $fontStyle['fgColor'] = 'yellow'; $fontStyle['smallCaps'] = true; $section->addText('I am inline styled.', $fontStyle); + $section->addTextBreak(); // Link -$section->addLink('http://www.google.com', null, 'NLink'); +$section->addLink('http://www.google.com', 'Google'); $section->addTextBreak(); // Image diff --git a/src/PhpWord/Writer/RTF/Element/Link.php b/src/PhpWord/Writer/RTF/Element/Link.php new file mode 100644 index 00000000..bb87e291 --- /dev/null +++ b/src/PhpWord/Writer/RTF/Element/Link.php @@ -0,0 +1,54 @@ +element; + if (!$element instanceof \PhpOffice\PhpWord\Element\Link) { + return; + } + + $content = ''; + if (!$this->withoutP) { + $content .= '{'; + } + $content .= '{\field {\*\fldinst {HYPERLINK "' . $element->getTarget() . '"}}{\\fldrslt {'; + $content .= String::toUnicode($element->getText()); + $content .= '}}}'; + if (!$this->withoutP) { + $content .= '}'; + } + + return $content; + } +} diff --git a/src/PhpWord/Writer/RTF/Element/PageBreak.php b/src/PhpWord/Writer/RTF/Element/PageBreak.php new file mode 100644 index 00000000..c8d16e06 --- /dev/null +++ b/src/PhpWord/Writer/RTF/Element/PageBreak.php @@ -0,0 +1,36 @@ +element->getText()); $content .= '\par' . PHP_EOL; diff --git a/tests/PhpWord/Tests/Writer/RTF/ElementTest.php b/tests/PhpWord/Tests/Writer/RTF/ElementTest.php index 9f597c15..b63c8a5e 100644 --- a/tests/PhpWord/Tests/Writer/RTF/ElementTest.php +++ b/tests/PhpWord/Tests/Writer/RTF/ElementTest.php @@ -28,7 +28,7 @@ class ElementTest extends \PHPUnit_Framework_TestCase */ public function testUnmatchedElements() { - $styles = array('Container', 'Text', 'Title'); + $styles = array('Container', 'Text', 'Title', 'Link'); foreach ($styles as $style) { $objectClass = 'PhpOffice\\PhpWord\\Writer\\RTF\\Element\\' . $style; $parentWriter = new RTF(); From 8745c5ee303faf8b6e363b2f418fd19f7bde9958 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Tue, 13 May 2014 11:05:12 +0700 Subject: [PATCH 08/48] Change behaviour of `set...` function of boolean properties; Some bug fixing based on Scrutinizer; New `Row` Word2007 style writer --- .scrutinizer.yml | 2 +- CHANGELOG.md | 1 + phpmd.xml.dist | 7 +- samples/Sample_09_Tables.php | 2 +- samples/Sample_25_TextBox.php | 4 +- src/PhpWord/Shared/Html.php | 8 ++- src/PhpWord/Style/AbstractStyle.php | 12 ++-- src/PhpWord/Style/Font.php | 19 +++--- src/PhpWord/Style/Image.php | 41 ++++++++--- src/PhpWord/Style/Paragraph.php | 6 +- src/PhpWord/Style/Row.php | 6 +- src/PhpWord/Writer/HTML/Element/Container.php | 11 ++- src/PhpWord/Writer/RTF/Element/Container.php | 26 +------ src/PhpWord/Writer/RTF/Style/Paragraph.php | 1 - .../Writer/Word2007/Element/Container.php | 16 ++--- src/PhpWord/Writer/Word2007/Element/Image.php | 9 +-- .../Writer/Word2007/Element/Object.php | 12 ++-- src/PhpWord/Writer/Word2007/Element/Table.php | 46 +++++-------- .../Writer/Word2007/Element/TextBox.php | 8 +-- src/PhpWord/Writer/Word2007/Style/Cell.php | 25 +++++++ src/PhpWord/Writer/Word2007/Style/Image.php | 50 +++++++++----- src/PhpWord/Writer/Word2007/Style/Row.php | 68 +++++++++++++++++++ tests/PhpWord/Tests/Style/FontTest.php | 15 ++-- .../Writer/Word2007/Part/DocumentTest.php | 2 +- .../Tests/Writer/Word2007/StyleTest.php | 2 +- 25 files changed, 251 insertions(+), 148 deletions(-) create mode 100644 src/PhpWord/Writer/Word2007/Style/Row.php 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; From 7ae8c3cb81708d459f91e0ad2c55e4e04d3db16b Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Tue, 13 May 2014 14:54:40 +0700 Subject: [PATCH 09/48] Refactoring: Word2007 table and marginborder style writer --- phpmd.xml.dist | 9 +- src/PhpWord/Settings.php | 2 +- src/PhpWord/Style/AbstractStyle.php | 4 +- src/PhpWord/Writer/HTML/Element/Container.php | 1 - src/PhpWord/Writer/RTF/Element/Text.php | 2 +- src/PhpWord/Writer/Word2007/Element/Table.php | 88 ++++---- .../Writer/Word2007/Style/MarginBorder.php | 58 ++++-- src/PhpWord/Writer/Word2007/Style/Row.php | 2 - src/PhpWord/Writer/Word2007/Style/Table.php | 196 ++++++++++-------- 9 files changed, 205 insertions(+), 157 deletions(-) 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; + } } From 1ee43da4de7895201c8f038c085c6f3a666b437d Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Tue, 13 May 2014 23:36:33 +0700 Subject: [PATCH 10/48] #196: RTF link styling --- src/PhpWord/Element/ListItem.php | 32 +++-- src/PhpWord/Writer/RTF.php | 4 +- .../Writer/RTF/Element/AbstractElement.php | 132 ++++++++++++++++++ src/PhpWord/Writer/RTF/Element/Link.php | 20 ++- src/PhpWord/Writer/RTF/Element/ListItem.php | 29 ++++ src/PhpWord/Writer/RTF/Element/Text.php | 112 ++------------- src/PhpWord/Writer/RTF/Element/TextRun.php | 10 +- src/PhpWord/Writer/RTF/Element/Title.php | 25 +--- src/PhpWord/Writer/RTF/Style/Font.php | 2 +- src/PhpWord/Writer/Word2007/Element/Table.php | 1 - src/PhpWord/Writer/Word2007/Style/Table.php | 3 - 11 files changed, 214 insertions(+), 156 deletions(-) create mode 100644 src/PhpWord/Writer/RTF/Element/ListItem.php diff --git a/src/PhpWord/Element/ListItem.php b/src/PhpWord/Element/ListItem.php index 1042362c..59bd8391 100644 --- a/src/PhpWord/Element/ListItem.php +++ b/src/PhpWord/Element/ListItem.php @@ -26,27 +26,26 @@ use PhpOffice\PhpWord\Style\ListItem as ListItemStyle; class ListItem extends AbstractElement { /** - * ListItem Style + * Element style * * @var \PhpOffice\PhpWord\Style\ListItem */ private $style; /** - * Textrun + * Text object * - * @var Text + * @var \PhpOffice\PhpWord\Element\Text */ private $textObject; /** - * ListItem Depth + * Depth * * @var int */ private $depth; - /** * Create a new ListItem * @@ -70,7 +69,9 @@ class ListItem extends AbstractElement } /** - * Get ListItem style + * Get style + * + * @return \PhpOffice\PhpWord\Style\ListItem */ public function getStyle() { @@ -78,7 +79,9 @@ class ListItem extends AbstractElement } /** - * Get ListItem TextRun + * Get Text object + * + * @return \PhpOffice\PhpWord\Element\Text */ public function getTextObject() { @@ -86,10 +89,23 @@ class ListItem extends AbstractElement } /** - * Get ListItem depth + * Get depth + * + * @return int */ public function getDepth() { return $this->depth; } + + /** + * Get text + * + * @return string + * @since 0.11.0 + */ + public function getText() + { + return $this->textObject->getText(); + } } diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index 434ffd67..27329db2 100644 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -226,7 +226,7 @@ class RTF extends AbstractWriter implements WriterInterface $elements = $section->getElements(); foreach ($elements as $element) { - if ($element instanceof Text) { + if (method_exists($element, 'getFontStyle')) { $fontStyle = $element->getFontStyle(); if ($fontStyle instanceof Font) { @@ -284,7 +284,7 @@ class RTF extends AbstractWriter implements WriterInterface $elements = $section->getElements(); foreach ($elements as $element) { - if ($element instanceof Text) { + if (method_exists($element, 'getFontStyle')) { $fontStyle = $element->getFontStyle(); if ($fontStyle instanceof Font) { diff --git a/src/PhpWord/Writer/RTF/Element/AbstractElement.php b/src/PhpWord/Writer/RTF/Element/AbstractElement.php index fbf8114e..e6e42c18 100644 --- a/src/PhpWord/Writer/RTF/Element/AbstractElement.php +++ b/src/PhpWord/Writer/RTF/Element/AbstractElement.php @@ -17,6 +17,13 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; +use PhpOffice\PhpWord\Shared\String; +use PhpOffice\PhpWord\Style; +use PhpOffice\PhpWord\Style\Font as FontStyle; +use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle; +use PhpOffice\PhpWord\Writer\RTF\Style\Font as FontStyleWriter; +use PhpOffice\PhpWord\Writer\RTF\Style\Paragraph as ParagraphStyleWriter; + /** * Abstract RTF element writer * @@ -24,4 +31,129 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; */ class AbstractElement extends \PhpOffice\PhpWord\Writer\HTML\Element\AbstractElement { + /** + * Font style + * + * @var \PhpWord\PhpOffice\Style\Font + */ + private $fontStyle; + + /** + * Paragraph style + * + * @var \PhpWord\PhpOffice\Style\Paragraph + */ + private $paragraphStyle; + + /** + * Get font and paragraph styles + */ + protected function getStyles() + { + /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */ + $parentWriter = $this->parentWriter; + + // Font style + if (method_exists($this->element, 'getFontStyle')) { + $this->fontStyle = $this->element->getFontStyle(); + if (is_string($this->fontStyle)) { + $this->fontStyle = Style::getStyle($this->fontStyle); + } + } + + // Paragraph style + if (method_exists($this->element, 'getParagraphStyle')) { + $this->paragraphStyle = $this->element->getParagraphStyle(); + if (is_string($this->paragraphStyle)) { + $this->paragraphStyle = Style::getStyle($this->paragraphStyle); + } + + if ($this->paragraphStyle !== null && !$this->withoutP) { + if ($parentWriter->getLastParagraphStyle() != $this->element->getParagraphStyle()) { + $parentWriter->setLastParagraphStyle($this->element->getParagraphStyle()); + } else { + $parentWriter->setLastParagraphStyle(); + $this->paragraphStyle = null; + } + } else { + $parentWriter->setLastParagraphStyle(); + $this->paragraphStyle = null; + } + } + } + + /** + * Write opening + * + * @return string + */ + protected function writeOpening() + { + if ($this->withoutP || !$this->paragraphStyle instanceof ParagraphStyle) { + return; + } + + $styleWriter = new ParagraphStyleWriter($this->paragraphStyle); + return $styleWriter->write(); + } + + /** + * Write text + * + * @param string $text + * @return string + */ + protected function writeText($text) + { + return String::toUnicode($text); + } + + /** + * Write closing + * + * @return string + */ + protected function writeClosing() + { + if ($this->withoutP) { + return; + } + + return '\par' . PHP_EOL; + } + + /** + * Write font style + * + * @return string + */ + protected function writeFontStyle() + { + if (!$this->fontStyle instanceof FontStyle) { + return ''; + } + + /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */ + $parentWriter = $this->parentWriter; + + // Create style writer and set color/name index + $styleWriter = new FontStyleWriter($this->fontStyle); + if ($this->fontStyle->getColor() != null) { + $colorIndex = array_search($this->fontStyle->getColor(), $parentWriter->getColorTable()); + if ($colorIndex !== false) { + $styleWriter->setColorIndex($colorIndex + 1); + } + } + if ($this->fontStyle->getName() != null) { + $fontIndex = array_search($this->fontStyle->getName(), $parentWriter->getFontTable()); + if ($fontIndex !== false) { + $styleWriter->setNameIndex($fontIndex + 1); + } + } + + // Write style + $content = $styleWriter->write(); + + return $content; + } } diff --git a/src/PhpWord/Writer/RTF/Element/Link.php b/src/PhpWord/Writer/RTF/Element/Link.php index bb87e291..684f6e65 100644 --- a/src/PhpWord/Writer/RTF/Element/Link.php +++ b/src/PhpWord/Writer/RTF/Element/Link.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; -use PhpOffice\PhpWord\Shared\String; - /** * Link element RTF writer * @@ -33,21 +31,19 @@ class Link extends AbstractElement */ public function write() { - $element = $this->element; - if (!$element instanceof \PhpOffice\PhpWord\Element\Link) { + if (!$this->element instanceof \PhpOffice\PhpWord\Element\Link) { return; } + $this->getStyles(); + $content = ''; - if (!$this->withoutP) { - $content .= '{'; - } - $content .= '{\field {\*\fldinst {HYPERLINK "' . $element->getTarget() . '"}}{\\fldrslt {'; - $content .= String::toUnicode($element->getText()); + $content .= $this->writeOpening(); + $content .= '{\field {\*\fldinst {HYPERLINK "' . $this->element->getTarget() . '"}}{\\fldrslt {'; + $content .= $this->writeFontStyle(); + $content .= $this->writeText($this->element->getText()); $content .= '}}}'; - if (!$this->withoutP) { - $content .= '}'; - } + $content .= $this->writeClosing(); return $content; } diff --git a/src/PhpWord/Writer/RTF/Element/ListItem.php b/src/PhpWord/Writer/RTF/Element/ListItem.php new file mode 100644 index 00000000..96375a4a --- /dev/null +++ b/src/PhpWord/Writer/RTF/Element/ListItem.php @@ -0,0 +1,29 @@ +element instanceof \PhpOffice\PhpWord\Element\Text) { + $elementClass = str_replace('\\Writer\\RTF', '', get_class($this)); + if (!$this->element instanceof $elementClass) { return; } - /** @var \PhpOffice\PhpWord\Style\Font $fontStyle Scrutinizer type hint */ - $fontStyle = $this->getFontStyle($this->element); - /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */ - $parentWriter = $this->parentWriter; + $this->getStyles(); $content = ''; - $content .= $this->writeParagraphStyle($this->element); + $content .= $this->writeOpening(); $content .= '{'; - $content .= $this->writeFontStyle($fontStyle); - if ($fontStyle || $parentWriter->getLastParagraphStyle() != '') { - $content .= ' '; - } - $content .= String::toUnicode($this->element->getText()); + $content .= $this->writeFontStyle(); + $content .= $this->writeText($this->element->getText()); $content .= '}'; - - if (!$this->withoutP) { - $content .= '\par' . PHP_EOL; - } + $content .= $this->writeClosing(); return $content; } - - /** - * Write paragraph style - * - * @return string - */ - private function writeParagraphStyle(TextElement $element) - { - /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */ - $parentWriter = $this->parentWriter; - $content = ''; - - // Get paragraph style - $paragraphStyle = $element->getParagraphStyle(); - if (is_string($paragraphStyle)) { - $paragraphStyle = Style::getStyle($paragraphStyle); - } - - // Write style when applicable - if ($paragraphStyle && !$this->withoutP) { - if ($parentWriter->getLastParagraphStyle() != $element->getParagraphStyle()) { - $parentWriter->setLastParagraphStyle($element->getParagraphStyle()); - - $styleWriter = new ParagraphStyleWriter($paragraphStyle); - $content = $styleWriter->write(); - } else { - $parentWriter->setLastParagraphStyle(); - } - } else { - $parentWriter->setLastParagraphStyle(); - } - - return $content; - } - - /** - * Write font style beginning - * - * @param \PhpOffice\PhpWord\Style\Font $style - * @return string - */ - private function writeFontStyle($style) - { - if (!$style instanceof FontStyle) { - return ''; - } - - /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */ - $parentWriter = $this->parentWriter; - - // Create style writer and set color/name index - $styleWriter = new FontStyleWriter($style); - if ($style->getColor() != null) { - $colorIndex = array_search($style->getColor(), $parentWriter->getColorTable()); - if ($colorIndex !== false) { - $styleWriter->setColorIndex($colorIndex + 1); - } - } - if ($style->getName() != null) { - $fontIndex = array_search($style->getName(), $parentWriter->getFontTable()); - if ($fontIndex !== false) { - $styleWriter->setNameIndex($fontIndex + 1); - } - } - - // Write style - $content = $styleWriter->write(); - - return $content; - } - - /** - * Get font style - * - * @return \PhpOffice\PhpWord\Style\Font - */ - private function getFontStyle(TextElement $element) - { - $fontStyle = $element->getFontStyle(); - if (is_string($fontStyle)) { - $fontStyle = Style::getStyle($fontStyle); - } - - return $fontStyle; - } } diff --git a/src/PhpWord/Writer/RTF/Element/TextRun.php b/src/PhpWord/Writer/RTF/Element/TextRun.php index 8d7324a3..8d70366c 100644 --- a/src/PhpWord/Writer/RTF/Element/TextRun.php +++ b/src/PhpWord/Writer/RTF/Element/TextRun.php @@ -33,12 +33,14 @@ class TextRun extends AbstractElement */ public function write() { - $content = ''; - - $content .= '{\pard\nowidctlpar'; $writer = new Container($this->parentWriter, $this->element); + + $content = ''; + $content .= $this->writeOpening(); + $content .= '{'; $content .= $writer->write(); - $content .= '\par}' . PHP_EOL; + $content .= '}'; + $content .= $this->writeClosing(); return $content; } diff --git a/src/PhpWord/Writer/RTF/Element/Title.php b/src/PhpWord/Writer/RTF/Element/Title.php index 78bb3c2e..998bcedc 100644 --- a/src/PhpWord/Writer/RTF/Element/Title.php +++ b/src/PhpWord/Writer/RTF/Element/Title.php @@ -20,29 +20,10 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; use PhpOffice\PhpWord\Shared\String; /** - * TextBreak element RTF writer + * Title element RTF writer; extends from text * - * @since 0.10.0 + * @since 0.11.0 */ -class Title extends AbstractElement +class Title extends Text { - /** - * Write element - * - * @return string - */ - public function write() - { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Title) { - return; - } - - $content = ''; - - $content .= '\pard\nowidctlpar '; - $content .= String::toUnicode($this->element->getText()); - $content .= '\par' . PHP_EOL; - - return $content; - } } diff --git a/src/PhpWord/Writer/RTF/Style/Font.php b/src/PhpWord/Writer/RTF/Style/Font.php index d14ee9c5..b70c089f 100644 --- a/src/PhpWord/Writer/RTF/Style/Font.php +++ b/src/PhpWord/Writer/RTF/Style/Font.php @@ -63,7 +63,7 @@ class Font extends AbstractStyle $content .= $this->getValueIf($style->isSuperScript(), '\super'); $content .= $this->getValueIf($style->isSubScript(), '\sub'); - return $content; + return $content . ' '; } /** diff --git a/src/PhpWord/Writer/Word2007/Element/Table.php b/src/PhpWord/Writer/Word2007/Element/Table.php index 6349172a..b936d697 100644 --- a/src/PhpWord/Writer/Word2007/Element/Table.php +++ b/src/PhpWord/Writer/Word2007/Element/Table.php @@ -23,7 +23,6 @@ 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; -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; diff --git a/src/PhpWord/Writer/Word2007/Style/Table.php b/src/PhpWord/Writer/Word2007/Style/Table.php index d996df00..590e819b 100644 --- a/src/PhpWord/Writer/Word2007/Style/Table.php +++ b/src/PhpWord/Writer/Word2007/Style/Table.php @@ -147,9 +147,6 @@ class Table extends AbstractStyle /** * Write shading - * - * @param int $width - * @param string $unit */ private function writeShading(XMLWriter $xmlWriter, TableStyle $style) { From e03e121e8550700e6fc4c1399b889932ba75d503 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Wed, 14 May 2014 00:42:05 +0700 Subject: [PATCH 11/48] #196: RTF link styling fixes --- src/PhpWord/Writer/RTF.php | 1 - .../Writer/RTF/Element/AbstractElement.php | 17 ++++++++++------- src/PhpWord/Writer/RTF/Element/ListItem.php | 2 -- src/PhpWord/Writer/RTF/Element/Text.php | 11 +++++------ src/PhpWord/Writer/RTF/Element/Title.php | 2 -- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index 27329db2..8670cb40 100644 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -17,7 +17,6 @@ namespace PhpOffice\PhpWord\Writer; -use PhpOffice\PhpWord\Element\Text; use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Shared\Drawing; diff --git a/src/PhpWord/Writer/RTF/Element/AbstractElement.php b/src/PhpWord/Writer/RTF/Element/AbstractElement.php index e6e42c18..d6b48ca8 100644 --- a/src/PhpWord/Writer/RTF/Element/AbstractElement.php +++ b/src/PhpWord/Writer/RTF/Element/AbstractElement.php @@ -51,26 +51,29 @@ class AbstractElement extends \PhpOffice\PhpWord\Writer\HTML\Element\AbstractEle protected function getStyles() { /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */ + /** @var \PhpOffice\PhpWord\Element\Text $element Scrutinizer type hint */ + $parentWriter = $this->parentWriter; + $element = $this->element; // Font style - if (method_exists($this->element, 'getFontStyle')) { - $this->fontStyle = $this->element->getFontStyle(); + if (method_exists($element, 'getFontStyle')) { + $this->fontStyle = $element->getFontStyle(); if (is_string($this->fontStyle)) { $this->fontStyle = Style::getStyle($this->fontStyle); } } // Paragraph style - if (method_exists($this->element, 'getParagraphStyle')) { - $this->paragraphStyle = $this->element->getParagraphStyle(); + if (method_exists($element, 'getParagraphStyle')) { + $this->paragraphStyle = $element->getParagraphStyle(); if (is_string($this->paragraphStyle)) { $this->paragraphStyle = Style::getStyle($this->paragraphStyle); } if ($this->paragraphStyle !== null && !$this->withoutP) { - if ($parentWriter->getLastParagraphStyle() != $this->element->getParagraphStyle()) { - $parentWriter->setLastParagraphStyle($this->element->getParagraphStyle()); + if ($parentWriter->getLastParagraphStyle() != $element->getParagraphStyle()) { + $parentWriter->setLastParagraphStyle($element->getParagraphStyle()); } else { $parentWriter->setLastParagraphStyle(); $this->paragraphStyle = null; @@ -147,7 +150,7 @@ class AbstractElement extends \PhpOffice\PhpWord\Writer\HTML\Element\AbstractEle if ($this->fontStyle->getName() != null) { $fontIndex = array_search($this->fontStyle->getName(), $parentWriter->getFontTable()); if ($fontIndex !== false) { - $styleWriter->setNameIndex($fontIndex + 1); + $styleWriter->setNameIndex($fontIndex); } } diff --git a/src/PhpWord/Writer/RTF/Element/ListItem.php b/src/PhpWord/Writer/RTF/Element/ListItem.php index 96375a4a..b795143c 100644 --- a/src/PhpWord/Writer/RTF/Element/ListItem.php +++ b/src/PhpWord/Writer/RTF/Element/ListItem.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; -use PhpOffice\PhpWord\Shared\String; - /** * ListItem element RTF writer; extends from text * diff --git a/src/PhpWord/Writer/RTF/Element/Text.php b/src/PhpWord/Writer/RTF/Element/Text.php index 7cefd965..cee1b157 100644 --- a/src/PhpWord/Writer/RTF/Element/Text.php +++ b/src/PhpWord/Writer/RTF/Element/Text.php @@ -17,10 +17,6 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; -use PhpOffice\PhpWord\Element\Text as TextElement; -use PhpOffice\PhpWord\Shared\String; -use PhpOffice\PhpWord\Style; - /** * Text element RTF writer * @@ -35,8 +31,11 @@ class Text extends AbstractElement */ public function write() { + /** @var \PhpOffice\PhpWord\Element\Text $element Scrutinizer type hint */ + $elementClass = str_replace('\\Writer\\RTF', '', get_class($this)); - if (!$this->element instanceof $elementClass) { + $element = $this->element; + if (!$element instanceof $elementClass) { return; } @@ -46,7 +45,7 @@ class Text extends AbstractElement $content .= $this->writeOpening(); $content .= '{'; $content .= $this->writeFontStyle(); - $content .= $this->writeText($this->element->getText()); + $content .= $this->writeText($element->getText()); $content .= '}'; $content .= $this->writeClosing(); diff --git a/src/PhpWord/Writer/RTF/Element/Title.php b/src/PhpWord/Writer/RTF/Element/Title.php index 998bcedc..b9645a68 100644 --- a/src/PhpWord/Writer/RTF/Element/Title.php +++ b/src/PhpWord/Writer/RTF/Element/Title.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; -use PhpOffice\PhpWord\Shared\String; - /** * Title element RTF writer; extends from text * From 6ba6ed40fc6ef42f47aaf7bbc954db4635a57eda Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Wed, 14 May 2014 08:40:21 +0700 Subject: [PATCH 12/48] Reactivate php_sim in Scrutinizer and fix some minor issues --- .scrutinizer.yml | 3 ++- src/PhpWord/Shared/XMLReader.php | 21 ++++++++++--------- .../Writer/RTF/Element/AbstractElement.php | 4 ++-- src/PhpWord/Writer/RTF/Element/Text.php | 3 +-- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index c7d3c9e5..40cc99a4 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -18,7 +18,8 @@ tools: enabled: true timeout: 900 php_sim: - min_mass: 16 + enabled: true + min_mass: 30 php_pdepend: true php_analyzer: true sensiolabs_security_checker: true diff --git a/src/PhpWord/Shared/XMLReader.php b/src/PhpWord/Shared/XMLReader.php index 415526ee..1b9553cb 100644 --- a/src/PhpWord/Shared/XMLReader.php +++ b/src/PhpWord/Shared/XMLReader.php @@ -96,7 +96,7 @@ class XMLReader * @param string $path * @return \DOMElement|null */ - public function getElement($path, \DOMElement $contextNode) + public function getElement($path, \DOMElement $contextNode = null) { $elements = $this->getElements($path, $contextNode); if ($elements->length > 0) { @@ -113,16 +113,17 @@ class XMLReader * @param string $path * @return string|null */ - public function getAttribute($attribute, \DOMElement $contextNode, $path = null) + public function getAttribute($attribute, \DOMElement $contextNode = null, $path = null) { - if (is_null($path)) { - $return = $contextNode->getAttribute($attribute); - } else { + $return = null; + if ($path !== null) { $elements = $this->getElements($path, $contextNode); if ($elements->length > 0) { $return = $elements->item(0)->getAttribute($attribute); - } else { - $return = null; + } + } else { + if ($contextNode !== null) { + $return = $contextNode->getAttribute($attribute); } } @@ -135,7 +136,7 @@ class XMLReader * @param string $path * @return string|null */ - public function getValue($path, \DOMElement $contextNode) + public function getValue($path, \DOMElement $contextNode = null) { $elements = $this->getElements($path, $contextNode); if ($elements->length > 0) { @@ -151,7 +152,7 @@ class XMLReader * @param string $path * @return integer */ - public function countElements($path, \DOMElement $contextNode) + public function countElements($path, \DOMElement $contextNode = null) { $elements = $this->getElements($path, $contextNode); @@ -164,7 +165,7 @@ class XMLReader * @param string $path * @return boolean */ - public function elementExists($path, \DOMElement $contextNode) + public function elementExists($path, \DOMElement $contextNode = null) { return $this->getElements($path, $contextNode)->length > 0; } diff --git a/src/PhpWord/Writer/RTF/Element/AbstractElement.php b/src/PhpWord/Writer/RTF/Element/AbstractElement.php index d6b48ca8..89bcfd37 100644 --- a/src/PhpWord/Writer/RTF/Element/AbstractElement.php +++ b/src/PhpWord/Writer/RTF/Element/AbstractElement.php @@ -51,9 +51,9 @@ class AbstractElement extends \PhpOffice\PhpWord\Writer\HTML\Element\AbstractEle protected function getStyles() { /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */ - /** @var \PhpOffice\PhpWord\Element\Text $element Scrutinizer type hint */ - $parentWriter = $this->parentWriter; + + /** @var \PhpOffice\PhpWord\Element\Text $element Scrutinizer type hint */ $element = $this->element; // Font style diff --git a/src/PhpWord/Writer/RTF/Element/Text.php b/src/PhpWord/Writer/RTF/Element/Text.php index cee1b157..abdf888d 100644 --- a/src/PhpWord/Writer/RTF/Element/Text.php +++ b/src/PhpWord/Writer/RTF/Element/Text.php @@ -32,9 +32,8 @@ class Text extends AbstractElement public function write() { /** @var \PhpOffice\PhpWord\Element\Text $element Scrutinizer type hint */ - - $elementClass = str_replace('\\Writer\\RTF', '', get_class($this)); $element = $this->element; + $elementClass = str_replace('\\Writer\\RTF', '', get_class($this)); if (!$element instanceof $elementClass) { return; } From 7b673af176a715b603f290b697b9f9b57c1996fa Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Wed, 14 May 2014 13:39:06 +0200 Subject: [PATCH 13/48] QA : PHP CodeSniffer (PEAR to Composer) --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a7b16860..a201a033 100644 --- a/composer.json +++ b/composer.json @@ -38,7 +38,8 @@ }, "require-dev": { "phpunit/phpunit": "3.7.*", - "phpdocumentor/phpdocumentor":"2.*" + "phpdocumentor/phpdocumentor":"2.*", + "squizlabs/php_codesniffer": "1.*" }, "suggest": { "ext-gd2": "Required to add images", From e94e2738e3e774e74967897874f521c8b26b074c Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Wed, 14 May 2014 13:40:30 +0200 Subject: [PATCH 14/48] QA : PHP CodeSniffer (PEAR to Composer) --- .travis.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 190c0a3c..4ddf3f54 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,10 +27,8 @@ before_script: # - php composer.phar install --prefer-source - composer self-update - composer require dompdf/dompdf:0.6.* + - composer update --prefer-source --dev - composer install --prefer-source --dev - ## PHP_CodeSniffer - - pyrus install pear/PHP_CodeSniffer - - phpenv rehash ## PHP Copy/Paste Detector - curl -o phpcpd.phar https://phar.phpunit.de/phpcpd.phar ## PHP Mess Detector @@ -48,7 +46,7 @@ before_script: script: ## PHP_CodeSniffer - - phpcs src/ tests/ --standard=PSR2 -n --ignore=src/PhpWord/Shared/PCLZip + - ./vendor/bin/phpcs src/ tests/ --standard=PSR2 -n --ignore=src/PhpWord/Shared/PCLZip ## PHP Copy/Paste Detector - php phpcpd.phar src/ tests/ --verbose ## PHP Mess Detector From 04a62f3255a9d99670cd70ed4dae08ae29e46daa Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Wed, 14 May 2014 19:41:44 +0700 Subject: [PATCH 15/48] QA: Code cleanup and some additional tests --- src/PhpWord/DocumentProperties.php | 6 ++- src/PhpWord/Element/AbstractContainer.php | 4 ++ src/PhpWord/Element/AbstractElement.php | 1 + src/PhpWord/Element/Image.php | 2 + src/PhpWord/Element/Object.php | 1 + src/PhpWord/Element/PreserveText.php | 2 +- src/PhpWord/Element/Section.php | 1 - src/PhpWord/PhpWord.php | 6 +-- src/PhpWord/Reader/ODText/AbstractPart.php | 2 + src/PhpWord/Reader/Word2007/AbstractPart.php | 12 ++++-- src/PhpWord/Reader/Word2007/Document.php | 8 ++++ src/PhpWord/Reader/Word2007/Numbering.php | 2 + src/PhpWord/Shared/Html.php | 6 ++- src/PhpWord/Shared/XMLReader.php | 7 ++++ src/PhpWord/Shared/ZipArchive.php | 6 ++- src/PhpWord/Style.php | 6 +-- src/PhpWord/Style/AbstractStyle.php | 5 +++ src/PhpWord/Style/Cell.php | 2 + src/PhpWord/Style/Font.php | 2 + src/PhpWord/Style/Paragraph.php | 13 +++++- src/PhpWord/Style/Section.php | 2 +- src/PhpWord/Writer/AbstractWriter.php | 2 + src/PhpWord/Writer/HTML.php | 2 +- .../Writer/HTML/Element/AbstractElement.php | 2 + src/PhpWord/Writer/HTML/Element/Container.php | 2 +- src/PhpWord/Writer/HTML/Element/Footnote.php | 4 +- src/PhpWord/Writer/HTML/Element/Image.php | 5 ++- src/PhpWord/Writer/HTML/Element/Link.php | 2 +- src/PhpWord/Writer/HTML/Element/ListItem.php | 2 +- src/PhpWord/Writer/HTML/Element/Table.php | 2 +- src/PhpWord/Writer/HTML/Element/Text.php | 8 ++-- src/PhpWord/Writer/HTML/Element/Title.php | 2 +- .../Writer/HTML/Style/AbstractStyle.php | 2 +- src/PhpWord/Writer/HTML/Style/Font.php | 2 +- src/PhpWord/Writer/HTML/Style/Image.php | 2 +- src/PhpWord/Writer/HTML/Style/Paragraph.php | 2 +- src/PhpWord/Writer/ODText/Element/Text.php | 2 + .../Writer/ODText/Part/AbstractPart.php | 2 +- src/PhpWord/Writer/ODText/Part/Content.php | 2 +- src/PhpWord/Writer/ODText/Part/Meta.php | 2 - src/PhpWord/Writer/PDF.php | 2 + src/PhpWord/Writer/PDF/AbstractRenderer.php | 3 ++ src/PhpWord/Writer/PDF/DomPDF.php | 1 + src/PhpWord/Writer/RTF.php | 2 +- .../Writer/RTF/Element/AbstractElement.php | 16 ++++---- src/PhpWord/Writer/RTF/Element/Link.php | 2 +- src/PhpWord/Writer/RTF/Element/Text.php | 4 +- src/PhpWord/Writer/RTF/Element/TextBreak.php | 2 +- src/PhpWord/Writer/RTF/Element/TextRun.php | 2 - .../Writer/RTF/Style/AbstractStyle.php | 2 - src/PhpWord/Writer/RTF/Style/Font.php | 3 +- src/PhpWord/Writer/RTF/Style/Paragraph.php | 2 +- src/PhpWord/Writer/Word2007.php | 2 + .../Word2007/Element/AbstractElement.php | 9 ++-- src/PhpWord/Writer/Word2007/Element/TOC.php | 8 +++- src/PhpWord/Writer/Word2007/Element/Text.php | 4 +- .../Writer/Word2007/Part/ContentTypes.php | 18 ++++---- .../Writer/Word2007/Part/DocPropsApp.php | 2 - .../Writer/Word2007/Part/DocPropsCore.php | 2 - src/PhpWord/Writer/Word2007/Part/Document.php | 1 - .../Writer/Word2007/Part/Footnotes.php | 2 +- .../Writer/Word2007/Part/Numbering.php | 2 +- src/PhpWord/Writer/Word2007/Part/Rels.php | 1 + .../Writer/Word2007/Part/RelsDocument.php | 2 +- src/PhpWord/Writer/Word2007/Part/Settings.php | 2 +- src/PhpWord/Writer/Word2007/Part/Styles.php | 3 +- .../Writer/Word2007/Style/AbstractStyle.php | 1 + src/PhpWord/Writer/Word2007/Style/Image.php | 1 + .../Writer/Word2007/Style/MarginBorder.php | 1 + src/PhpWord/Writer/Word2007/Style/Table.php | 1 + .../PhpWord/Tests/Writer/HTML/ElementTest.php | 10 ++--- tests/PhpWord/Tests/Writer/HTMLTest.php | 1 + .../Tests/Writer/ODText/ElementTest.php | 10 ++--- .../PhpWord/Tests/Writer/RTF/ElementTest.php | 10 ++--- tests/PhpWord/Tests/Writer/RTFTest.php | 2 +- .../Tests/Writer/Word2007/ElementTest.php | 10 ++--- .../Writer/Word2007/Part/DocumentTest.php | 17 ++++++-- .../Tests/Writer/Word2007/PartTest.php | 41 +++++++++++++++++++ 78 files changed, 234 insertions(+), 115 deletions(-) create mode 100644 tests/PhpWord/Tests/Writer/Word2007/PartTest.php diff --git a/src/PhpWord/DocumentProperties.php b/src/PhpWord/DocumentProperties.php index a2d869b0..17d57c1d 100644 --- a/src/PhpWord/DocumentProperties.php +++ b/src/PhpWord/DocumentProperties.php @@ -416,8 +416,9 @@ class DocumentProperties { if ($this->isCustomPropertySet($propertyName)) { return $this->customProperties[$propertyName]['value']; + } else { + return null; } - } /** @@ -430,8 +431,9 @@ class DocumentProperties { if ($this->isCustomPropertySet($propertyName)) { return $this->customProperties[$propertyName]['type']; + } else { + return null; } - } /** diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 6caa0056..5afddc5f 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -313,6 +313,7 @@ abstract class AbstractContainer extends AbstractElement * * @param string $method * @return bool + * @throws \BadMethodCallException */ private function checkValidity($method) { @@ -389,6 +390,7 @@ abstract class AbstractContainer extends AbstractElement * * @param string $src * @param mixed $style + * @return \PhpOffice\PhpWord\Element\Image * @deprecated 0.9.0 * @codeCoverageIgnore */ @@ -401,6 +403,7 @@ abstract class AbstractContainer extends AbstractElement * Create textrun element * * @param mixed $paragraphStyle + * @return \PhpOffice\PhpWord\Element\TextRun * @deprecated 0.10.0 * @codeCoverageIgnore */ @@ -413,6 +416,7 @@ abstract class AbstractContainer extends AbstractElement * Create footnote element * * @param mixed $paragraphStyle + * @return \PhpOffice\PhpWord\Element\Footnote * @deprecated 0.10.0 * @codeCoverageIgnore */ diff --git a/src/PhpWord/Element/AbstractElement.php b/src/PhpWord/Element/AbstractElement.php index 40e65c12..45311364 100644 --- a/src/PhpWord/Element/AbstractElement.php +++ b/src/PhpWord/Element/AbstractElement.php @@ -220,6 +220,7 @@ abstract class AbstractElement * @param mixed $styleObject Style object * @param mixed $styleValue Style value * @param bool $returnObject Always return object + * @return mixed */ protected function setStyle($styleObject, $styleValue = null, $returnObject = false) { diff --git a/src/PhpWord/Element/Image.php b/src/PhpWord/Element/Image.php index 6cfd6176..44829ba5 100644 --- a/src/PhpWord/Element/Image.php +++ b/src/PhpWord/Element/Image.php @@ -283,6 +283,8 @@ class Image extends AbstractElement * Check memory image, supported type, image functions, and proportional width/height * * @param string $source + * @throws \PhpOffice\PhpWord\Exception\InvalidImageException + * @throws \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException */ private function checkImage($source) { diff --git a/src/PhpWord/Element/Object.php b/src/PhpWord/Element/Object.php index a87da019..a63c1869 100644 --- a/src/PhpWord/Element/Object.php +++ b/src/PhpWord/Element/Object.php @@ -58,6 +58,7 @@ class Object extends AbstractElement * * @param string $source * @param mixed $style + * @throws \PhpOffice\PhpWord\Exception\InvalidObjectException */ public function __construct($source, $style = null) { diff --git a/src/PhpWord/Element/PreserveText.php b/src/PhpWord/Element/PreserveText.php index 004ffbc3..100385c9 100644 --- a/src/PhpWord/Element/PreserveText.php +++ b/src/PhpWord/Element/PreserveText.php @@ -54,7 +54,7 @@ class PreserveText extends AbstractElement * @param string $text * @param mixed $fontStyle * @param mixed $paragraphStyle - * @return $this + * @return self */ public function __construct($text = null, $fontStyle = null, $paragraphStyle = null) { diff --git a/src/PhpWord/Element/Section.php b/src/PhpWord/Element/Section.php index 8a2b474f..cfa101cf 100644 --- a/src/PhpWord/Element/Section.php +++ b/src/PhpWord/Element/Section.php @@ -18,7 +18,6 @@ namespace PhpOffice\PhpWord\Element; use PhpOffice\PhpWord\Exception\Exception; -use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Style\Section as SectionSettings; /** diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index 50890ad3..a56578b0 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -301,13 +301,13 @@ class PhpWord /** * Adds a heading style definition to styles.xml * - * @param int $titleCount + * @param int $depth * @param mixed $fontStyle * @param mixed $paragraphStyle */ - public function addTitleStyle($titleCount, $fontStyle, $paragraphStyle = null) + public function addTitleStyle($depth, $fontStyle, $paragraphStyle = null) { - Style::addTitleStyle($titleCount, $fontStyle, $paragraphStyle); + Style::addTitleStyle($depth, $fontStyle, $paragraphStyle); } /** diff --git a/src/PhpWord/Reader/ODText/AbstractPart.php b/src/PhpWord/Reader/ODText/AbstractPart.php index 8ffb9f1b..44884922 100644 --- a/src/PhpWord/Reader/ODText/AbstractPart.php +++ b/src/PhpWord/Reader/ODText/AbstractPart.php @@ -27,6 +27,8 @@ abstract class AbstractPart extends \PhpOffice\PhpWord\Reader\Word2007\AbstractP /** * Read w:r (override) * + * @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader + * @param \DOMElement $domNode * @param mixed $parent * @param string $docPart * @param mixed $paragraphStyle diff --git a/src/PhpWord/Reader/Word2007/AbstractPart.php b/src/PhpWord/Reader/Word2007/AbstractPart.php index a6efb329..34088b17 100644 --- a/src/PhpWord/Reader/Word2007/AbstractPart.php +++ b/src/PhpWord/Reader/Word2007/AbstractPart.php @@ -138,12 +138,14 @@ abstract class AbstractPart /** * Read w:pPr * + * @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader + * @param \DOMElement $domNode * @return array|null */ protected function readParagraphStyle(XMLReader $xmlReader, \DOMElement $domNode) { if (!$xmlReader->elementExists('w:pPr', $domNode)) { - return; + return ''; } $style = array(); @@ -200,19 +202,21 @@ abstract class AbstractPart /** * Read w:rPr * + * @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader + * @param \DOMElement $domNode * @return array|null */ protected function readFontStyle(XMLReader $xmlReader, \DOMElement $domNode) { if (is_null($domNode)) { - return; + return null; } // Hyperlink has an extra w:r child if ($domNode->nodeName == 'w:hyperlink') { $domNode = $xmlReader->getElement('w:r', $domNode); } if (!$xmlReader->elementExists('w:rPr', $domNode)) { - return; + return null; } $style = array(); @@ -272,6 +276,8 @@ abstract class AbstractPart /** * Read w:tblPr * + * @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader + * @param \DOMElement $domNode * @return string|array|null * @todo Capture w:tblStylePr w:type="firstRow" */ diff --git a/src/PhpWord/Reader/Word2007/Document.php b/src/PhpWord/Reader/Word2007/Document.php index d38b3fb9..52456cf7 100644 --- a/src/PhpWord/Reader/Word2007/Document.php +++ b/src/PhpWord/Reader/Word2007/Document.php @@ -121,6 +121,8 @@ class Document extends AbstractPart /** * Read w:p * + * @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader + * @param \DOMElement $domNode * @param mixed $parent * @param string $docPart * @@ -209,6 +211,8 @@ class Document extends AbstractPart /** * Read w:tbl * + * @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader + * @param \DOMElement $domNode * @param mixed $parent * @param string $docPart */ @@ -266,6 +270,8 @@ class Document extends AbstractPart /** * Read w:sectPr * + * @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader + * @param \DOMElement $domNode * @return array|null */ private function readSectionStyle(XMLReader $xmlReader, \DOMElement $domNode) @@ -326,6 +332,8 @@ class Document extends AbstractPart /** * Read w:tcPr * + * @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader + * @param \DOMElement $domNode * @return array|null */ private function readCellStyle(XMLReader $xmlReader, \DOMElement $domNode) diff --git a/src/PhpWord/Reader/Word2007/Numbering.php b/src/PhpWord/Reader/Word2007/Numbering.php index 68a36d57..81939ae8 100644 --- a/src/PhpWord/Reader/Word2007/Numbering.php +++ b/src/PhpWord/Reader/Word2007/Numbering.php @@ -87,6 +87,8 @@ class Numbering extends AbstractPart /** * Read numbering level definition from w:abstractNum and w:num * + * @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader + * @param \DOMElement $subnode * @param integer $levelId * @return array */ diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 78f1d99d..501d2404 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -52,6 +52,7 @@ class Html * * @param \DOMNode $node Node to check on attributes and to compile a style array * @param array $style is supplied, the inline style attributes are added to the already existing style + * @return array */ protected static function parseInlineStyle($node, $style = array()) { @@ -91,6 +92,7 @@ class Html } } } + return $style; } @@ -173,7 +175,7 @@ class Html // } break; case 'tr': - /** @var \PhpOffice\PhpWord\Element\Table $object Scrutinizer type hint */ + /** @var \PhpOffice\PhpWord\Element\Table $object Type hint */ $styles['paragraphStyle'] = self::parseInlineStyle($node, $styles['paragraphStyle']); $newobject = $object->addRow(); // if ($attributes->getNamedItem('height') !== null) { @@ -181,7 +183,7 @@ class Html // } break; case 'td': - /** @var \PhpOffice\PhpWord\Element\Row $object Scrutinizer type hint */ + /** @var \PhpOffice\PhpWord\Element\Row $object Type hint */ $styles['paragraphStyle'] = self::parseInlineStyle($node, $styles['paragraphStyle']); // if ($attributes->getNamedItem('width') !== null) { // $newobject=$object->addCell($width=$attributes->getNamedItem('width')->value); diff --git a/src/PhpWord/Shared/XMLReader.php b/src/PhpWord/Shared/XMLReader.php index 1b9553cb..6b118c56 100644 --- a/src/PhpWord/Shared/XMLReader.php +++ b/src/PhpWord/Shared/XMLReader.php @@ -47,6 +47,7 @@ class XMLReader * @param string $zipFile * @param string $xmlFile * @return \DOMDocument|false + * @throws \PhpOffice\PhpWord\Exception\Exception */ public function getDomFromZip($zipFile, $xmlFile) { @@ -76,6 +77,7 @@ class XMLReader * Get elements * * @param string $path + * @param \DOMElement $contextNode * @return \DOMNodeList */ public function getElements($path, \DOMElement $contextNode = null) @@ -94,6 +96,7 @@ class XMLReader * Get element * * @param string $path + * @param \DOMElement $contextNode * @return \DOMElement|null */ public function getElement($path, \DOMElement $contextNode = null) @@ -110,6 +113,7 @@ class XMLReader * Get element attribute * * @param string $attribute + * @param \DOMElement $contextNode * @param string $path * @return string|null */ @@ -134,6 +138,7 @@ class XMLReader * Get element value * * @param string $path + * @param \DOMElement $contextNode * @return string|null */ public function getValue($path, \DOMElement $contextNode = null) @@ -150,6 +155,7 @@ class XMLReader * Count elements * * @param string $path + * @param \DOMElement $contextNode * @return integer */ public function countElements($path, \DOMElement $contextNode = null) @@ -163,6 +169,7 @@ class XMLReader * Element exists * * @param string $path + * @param \DOMElement $contextNode * @return boolean */ public function elementExists($path, \DOMElement $contextNode = null) diff --git a/src/PhpWord/Shared/ZipArchive.php b/src/PhpWord/Shared/ZipArchive.php index 8c9f10b8..84d6ab85 100644 --- a/src/PhpWord/Shared/ZipArchive.php +++ b/src/PhpWord/Shared/ZipArchive.php @@ -84,8 +84,9 @@ class ZipArchive /** * Add a new file to the zip archive (emulate \ZipArchive) * - * @param string $filename Directory/Name of the file to add to the zip archive + * @param string $filename Directory/Name of the file to add to the zip archive * @param string $localname Directory/Name of the file added to the zip + * @return bool */ public function addFile($filename, $localname = null) { @@ -117,7 +118,8 @@ class ZipArchive * Add a new file to the zip archive from a string of raw data (emulate \ZipArchive) * * @param string $localname Directory/Name of the file to add to the zip archive - * @param string $contents String of data to add to the zip archive + * @param string $contents String of data to add to the zip archive + * @return bool */ public function addFromString($localname, $contents) { diff --git a/src/PhpWord/Style.php b/src/PhpWord/Style.php index 25e5785b..ef3310d7 100644 --- a/src/PhpWord/Style.php +++ b/src/PhpWord/Style.php @@ -83,13 +83,13 @@ class Style /** * Add title style * - * @param int $titleCount + * @param int $depth * @param array $fontStyle * @param array $paragraphStyle */ - public static function addTitleStyle($titleCount, $fontStyle, $paragraphStyle = null) + public static function addTitleStyle($depth, $fontStyle, $paragraphStyle = null) { - self::setStyleValues("Heading_{$titleCount}", new Font('title', $paragraphStyle), $fontStyle); + self::setStyleValues("Heading_{$depth}", new Font('title', $paragraphStyle), $fontStyle); } /** diff --git a/src/PhpWord/Style/AbstractStyle.php b/src/PhpWord/Style/AbstractStyle.php index 5be042bd..0ac71ab2 100644 --- a/src/PhpWord/Style/AbstractStyle.php +++ b/src/PhpWord/Style/AbstractStyle.php @@ -227,6 +227,8 @@ abstract class AbstractStyle * @param mixed $value * @param array $enum * @param mixed $default + * @return mixed + * @throws \InvalidArgumentException */ protected function setEnumVal($value = null, $enum = array(), $default = null) { @@ -245,11 +247,13 @@ abstract class AbstractStyle * @param mixed $value * @param string $styleName * @param mixed $style + * @return mixed */ protected function setObjectVal($value, $styleName, &$style) { $styleClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . $styleName; if (is_array($value)) { + /** @var \PhpOffice\PhpWord\Style\AbstractStyle $style Type hint */ if (!$style instanceof $styleClass) { $style = new $styleClass(); } @@ -265,6 +269,7 @@ abstract class AbstractStyle * Set style using associative array * * @param array $style + * @return self * @deprecated 0.11.0 * @codeCoverageIgnore */ diff --git a/src/PhpWord/Style/Cell.php b/src/PhpWord/Style/Cell.php index e44ffa38..95ed13b4 100644 --- a/src/PhpWord/Style/Cell.php +++ b/src/PhpWord/Style/Cell.php @@ -146,6 +146,8 @@ class Cell extends Border { if (!is_null($this->shading)) { return $this->shading->getFill(); + } else { + return null; } } diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index 4fe70068..a4d56dd4 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -562,6 +562,8 @@ class Font extends AbstractStyle { if (!is_null($this->shading)) { return $this->shading->getFill(); + } else { + return null; } } diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index 9c8e0c5b..8609b5ab 100644 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -127,6 +127,7 @@ class Paragraph extends AbstractStyle * * @param string $key * @param mixed $value + * @return self */ public function setStyleValue($key, $value) { @@ -172,6 +173,8 @@ class Paragraph extends AbstractStyle { if (!is_null($this->spacing)) { return $this->spacing->getBefore(); + } else { + return null; } } @@ -195,6 +198,8 @@ class Paragraph extends AbstractStyle { if (!is_null($this->spacing)) { return $this->spacing->getAfter(); + } else { + return null; } } @@ -218,6 +223,8 @@ class Paragraph extends AbstractStyle { if (!is_null($this->spacing)) { return $this->spacing->getLine(); + } else { + return null; } } @@ -246,7 +253,7 @@ class Paragraph extends AbstractStyle * Set the line height * * @param int|float|string $lineHeight - * @return $this + * @return self * @throws \PhpOffice\PhpWord\Exception\InvalidStyleException */ public function setLineHeight($lineHeight) @@ -273,6 +280,8 @@ class Paragraph extends AbstractStyle { if (!is_null($this->indentation)) { return $this->indentation->getLeft(); + } else { + return null; } } @@ -296,6 +305,8 @@ class Paragraph extends AbstractStyle { if (!is_null($this->indentation)) { return $this->indentation->getHanging(); + } else { + return null; } } diff --git a/src/PhpWord/Style/Section.php b/src/PhpWord/Style/Section.php index 8a0c071d..d47703c2 100644 --- a/src/PhpWord/Style/Section.php +++ b/src/PhpWord/Style/Section.php @@ -420,7 +420,7 @@ class Section extends Border * Set page numbering start * * @param null|int $pageNumberingStart - * @return $this + * @return self */ public function setPageNumberingStart($pageNumberingStart = null) { diff --git a/src/PhpWord/Writer/AbstractWriter.php b/src/PhpWord/Writer/AbstractWriter.php index f1a30026..bc1c1bf9 100644 --- a/src/PhpWord/Writer/AbstractWriter.php +++ b/src/PhpWord/Writer/AbstractWriter.php @@ -149,6 +149,7 @@ abstract class AbstractWriter implements WriterInterface * @param bool $value * @param string $directory * @return self + * @throws \PhpOffice\PhpWord\Exception\Exception */ public function setUseDiskCaching($value = false, $directory = null) { @@ -257,6 +258,7 @@ abstract class AbstractWriter implements WriterInterface * * @param string $filename * @return mixed ZipArchive object + * @throws \PhpOffice\PhpWord\Exception\Exception */ protected function getZipArchive($filename) { diff --git a/src/PhpWord/Writer/HTML.php b/src/PhpWord/Writer/HTML.php index f871bc18..6c1a534f 100644 --- a/src/PhpWord/Writer/HTML.php +++ b/src/PhpWord/Writer/HTML.php @@ -19,9 +19,9 @@ namespace PhpOffice\PhpWord\Writer; use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; -use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Writer\HTML\Element\Container; use PhpOffice\PhpWord\Writer\HTML\Element\TextRun as TextRunWriter; use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter; diff --git a/src/PhpWord/Writer/HTML/Element/AbstractElement.php b/src/PhpWord/Writer/HTML/Element/AbstractElement.php index 8da09414..a5dee60a 100644 --- a/src/PhpWord/Writer/HTML/Element/AbstractElement.php +++ b/src/PhpWord/Writer/HTML/Element/AbstractElement.php @@ -51,6 +51,8 @@ abstract class AbstractElement /** * Create new instance * + * @param \PhpOffice\PhpWord\Writer\AbstractWriter $parentWriter + * @param \PhpOffice\PhpWord\Element\AbstractElement $element * @param bool $withoutP */ public function __construct(AbstractWriter $parentWriter, Element $element, $withoutP = false) diff --git a/src/PhpWord/Writer/HTML/Element/Container.php b/src/PhpWord/Writer/HTML/Element/Container.php index 54ec7ee6..e9b2d869 100644 --- a/src/PhpWord/Writer/HTML/Element/Container.php +++ b/src/PhpWord/Writer/HTML/Element/Container.php @@ -40,7 +40,7 @@ class Container extends AbstractElement { $container = $this->element; if (!$container instanceof \PhpOffice\PhpWord\Element\AbstractContainer) { - return; + return ''; } $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1); $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false; diff --git a/src/PhpWord/Writer/HTML/Element/Footnote.php b/src/PhpWord/Writer/HTML/Element/Footnote.php index ba5ced56..f59545de 100644 --- a/src/PhpWord/Writer/HTML/Element/Footnote.php +++ b/src/PhpWord/Writer/HTML/Element/Footnote.php @@ -39,9 +39,9 @@ class Footnote extends AbstractElement public function write() { if (!$this->element instanceof \PhpOffice\PhpWord\Element\Footnote) { - return; + return ''; } - /** @var \PhpOffice\PhpWord\Writer\HTML $parentWriter Scrutinizer type hint */ + /** @var \PhpOffice\PhpWord\Writer\HTML $parentWriter Type hint */ $parentWriter = $this->parentWriter; $noteId = count($parentWriter->getNotes()) + 1; diff --git a/src/PhpWord/Writer/HTML/Element/Image.php b/src/PhpWord/Writer/HTML/Element/Image.php index 1d07acd7..361babf3 100644 --- a/src/PhpWord/Writer/HTML/Element/Image.php +++ b/src/PhpWord/Writer/HTML/Element/Image.php @@ -35,9 +35,9 @@ class Image extends Text public function write() { if (!$this->element instanceof \PhpOffice\PhpWord\Element\Image) { - return; + return ''; } - /** @var \PhpOffice\PhpWord\Writer\HTML $parentWriter Scrutinizer type hint */ + /** @var \PhpOffice\PhpWord\Writer\HTML $parentWriter Type hint */ $parentWriter = $this->parentWriter; $content = ''; @@ -59,6 +59,7 @@ class Image extends Text /** * Get Base64 image data * + * @param \PhpOffice\PhpWord\Element\Image $element * @return string|null */ private function getBase64ImageData(ImageElement $element) diff --git a/src/PhpWord/Writer/HTML/Element/Link.php b/src/PhpWord/Writer/HTML/Element/Link.php index 820a1526..d1256eb0 100644 --- a/src/PhpWord/Writer/HTML/Element/Link.php +++ b/src/PhpWord/Writer/HTML/Element/Link.php @@ -32,7 +32,7 @@ class Link extends Text public function write() { if (!$this->element instanceof \PhpOffice\PhpWord\Element\Link) { - return; + return ''; } $content = ''; diff --git a/src/PhpWord/Writer/HTML/Element/ListItem.php b/src/PhpWord/Writer/HTML/Element/ListItem.php index 8e302b03..79a3b393 100644 --- a/src/PhpWord/Writer/HTML/Element/ListItem.php +++ b/src/PhpWord/Writer/HTML/Element/ListItem.php @@ -32,7 +32,7 @@ class ListItem extends AbstractElement public function write() { if (!$this->element instanceof \PhpOffice\PhpWord\Element\ListItem) { - return; + return ''; } $text = htmlspecialchars($this->element->getTextObject()->getText()); diff --git a/src/PhpWord/Writer/HTML/Element/Table.php b/src/PhpWord/Writer/HTML/Element/Table.php index bd7795ef..fc5f0d30 100644 --- a/src/PhpWord/Writer/HTML/Element/Table.php +++ b/src/PhpWord/Writer/HTML/Element/Table.php @@ -32,7 +32,7 @@ class Table extends AbstractElement public function write() { if (!$this->element instanceof \PhpOffice\PhpWord\Element\Table) { - return; + return ''; } $content = ''; diff --git a/src/PhpWord/Writer/HTML/Element/Text.php b/src/PhpWord/Writer/HTML/Element/Text.php index 8ace129c..44740f72 100644 --- a/src/PhpWord/Writer/HTML/Element/Text.php +++ b/src/PhpWord/Writer/HTML/Element/Text.php @@ -53,7 +53,7 @@ class Text extends AbstractElement /** * Closing tag * - * @var strings + * @var string */ private $closingTags = ''; @@ -64,7 +64,7 @@ class Text extends AbstractElement */ public function write() { - /** @var \PhpOffice\PhpWord\Element\Text $element Scrutinizer type hint */ + /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */ $element = $this->element; $this->getFontStyle(); @@ -142,7 +142,7 @@ class Text extends AbstractElement */ private function getParagraphStyle() { - /** @var \PhpOffice\PhpWord\Element\Text $element Scrutinizer type hint */ + /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */ $element = $this->element; $style = ''; if (method_exists($element, 'getParagraphStyle')) { @@ -168,7 +168,7 @@ class Text extends AbstractElement */ private function getFontStyle() { - /** @var \PhpOffice\PhpWord\Element\Text $element Scrutinizer type hint */ + /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */ $element = $this->element; $style = ''; $fontStyle = $element->getFontStyle(); diff --git a/src/PhpWord/Writer/HTML/Element/Title.php b/src/PhpWord/Writer/HTML/Element/Title.php index 64d47ec2..20747bf9 100644 --- a/src/PhpWord/Writer/HTML/Element/Title.php +++ b/src/PhpWord/Writer/HTML/Element/Title.php @@ -32,7 +32,7 @@ class Title extends AbstractElement public function write() { if (!$this->element instanceof \PhpOffice\PhpWord\Element\Title) { - return; + return ''; } $tag = 'h' . $this->element->getDepth(); diff --git a/src/PhpWord/Writer/HTML/Style/AbstractStyle.php b/src/PhpWord/Writer/HTML/Style/AbstractStyle.php index 3b6d99c1..cd37174a 100644 --- a/src/PhpWord/Writer/HTML/Style/AbstractStyle.php +++ b/src/PhpWord/Writer/HTML/Style/AbstractStyle.php @@ -56,7 +56,7 @@ abstract class AbstractStyle public function getStyle() { if (!$this->style instanceof Style && !is_array($this->style)) { - return; + return ''; } return $this->style; diff --git a/src/PhpWord/Writer/HTML/Style/Font.php b/src/PhpWord/Writer/HTML/Style/Font.php index 33c23f7a..dc95eb1a 100644 --- a/src/PhpWord/Writer/HTML/Style/Font.php +++ b/src/PhpWord/Writer/HTML/Style/Font.php @@ -36,7 +36,7 @@ class Font extends AbstractStyle { $style = $this->getStyle(); if (!$style instanceof \PhpOffice\PhpWord\Style\Font) { - return; + return ''; } $css = array(); diff --git a/src/PhpWord/Writer/HTML/Style/Image.php b/src/PhpWord/Writer/HTML/Style/Image.php index db6ac57b..13be3665 100644 --- a/src/PhpWord/Writer/HTML/Style/Image.php +++ b/src/PhpWord/Writer/HTML/Style/Image.php @@ -33,7 +33,7 @@ class Image extends AbstractStyle { $style = $this->getStyle(); if (!$style instanceof \PhpOffice\PhpWord\Style\Image) { - return; + return ''; } $css = array(); diff --git a/src/PhpWord/Writer/HTML/Style/Paragraph.php b/src/PhpWord/Writer/HTML/Style/Paragraph.php index cc3ad785..4a77e496 100644 --- a/src/PhpWord/Writer/HTML/Style/Paragraph.php +++ b/src/PhpWord/Writer/HTML/Style/Paragraph.php @@ -35,7 +35,7 @@ class Paragraph extends AbstractStyle { $style = $this->getStyle(); if (!$style instanceof \PhpOffice\PhpWord\Style\Paragraph) { - return; + return ''; } $css = array(); diff --git a/src/PhpWord/Writer/ODText/Element/Text.php b/src/PhpWord/Writer/ODText/Element/Text.php index 6baf3200..4fc53bfe 100644 --- a/src/PhpWord/Writer/ODText/Element/Text.php +++ b/src/PhpWord/Writer/ODText/Element/Text.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Writer\ODText\Element; +use PhpOffice\PhpWord\Exception\Exception; + /** * Text element writer * diff --git a/src/PhpWord/Writer/ODText/Part/AbstractPart.php b/src/PhpWord/Writer/ODText/Part/AbstractPart.php index c5a3b0c6..8b53f26b 100644 --- a/src/PhpWord/Writer/ODText/Part/AbstractPart.php +++ b/src/PhpWord/Writer/ODText/Part/AbstractPart.php @@ -19,8 +19,8 @@ namespace PhpOffice\PhpWord\Writer\ODText\Part; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Shared\XMLWriter; -use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style; +use PhpOffice\PhpWord\Style\Font; /** * ODText writer part abstract diff --git a/src/PhpWord/Writer/ODText/Part/Content.php b/src/PhpWord/Writer/ODText/Part/Content.php index f5ae1883..bebc5b86 100644 --- a/src/PhpWord/Writer/ODText/Part/Content.php +++ b/src/PhpWord/Writer/ODText/Part/Content.php @@ -23,8 +23,8 @@ use PhpOffice\PhpWord\Media; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Style\Font; -use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Style; +use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Writer\ODText\Element\Container; /** diff --git a/src/PhpWord/Writer/ODText/Part/Meta.php b/src/PhpWord/Writer/ODText/Part/Meta.php index 6ed58213..0d2a761a 100644 --- a/src/PhpWord/Writer/ODText/Part/Meta.php +++ b/src/PhpWord/Writer/ODText/Part/Meta.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\ODText\Part; -use PhpOffice\PhpWord\PhpWord; - /** * ODText meta part writer: meta.xml */ diff --git a/src/PhpWord/Writer/PDF.php b/src/PhpWord/Writer/PDF.php index 45e8a412..fc4599c9 100644 --- a/src/PhpWord/Writer/PDF.php +++ b/src/PhpWord/Writer/PDF.php @@ -37,6 +37,7 @@ class PDF * Instantiate a new renderer of the configured type within this container class * * @param \PhpOffice\PhpWord\PhpWord $phpWord + * @throws \PhpOffice\PhpWord\Exception\Exception */ public function __construct(PhpWord $phpWord) { @@ -62,6 +63,7 @@ class PDF * @param string $name Renderer library method name * @param mixed[] $arguments Array of arguments to pass to the renderer method * @return mixed Returned data from the PDF renderer wrapper method + * @throws \PhpOffice\PhpWord\Exception\Exception */ public function __call($name, $arguments) { diff --git a/src/PhpWord/Writer/PDF/AbstractRenderer.php b/src/PhpWord/Writer/PDF/AbstractRenderer.php index 342cdf58..4feefdae 100644 --- a/src/PhpWord/Writer/PDF/AbstractRenderer.php +++ b/src/PhpWord/Writer/PDF/AbstractRenderer.php @@ -90,10 +90,12 @@ abstract class AbstractRenderer extends \PhpOffice\PhpWord\Writer\HTML * 'arialunicid0-japanese' * * @param string $fontName + * @return self */ public function setFont($fontName) { $this->font = $fontName; + return $this; } @@ -146,6 +148,7 @@ abstract class AbstractRenderer extends \PhpOffice\PhpWord\Writer\HTML * * @param string $pFilename Name of the file to save as * @return resource + * @throws \PhpOffice\PhpWord\Exception\Exception */ protected function prepareForSave($pFilename = null) { diff --git a/src/PhpWord/Writer/PDF/DomPDF.php b/src/PhpWord/Writer/PDF/DomPDF.php index cf18d7a5..2b8f951f 100644 --- a/src/PhpWord/Writer/PDF/DomPDF.php +++ b/src/PhpWord/Writer/PDF/DomPDF.php @@ -30,6 +30,7 @@ class DomPDF extends AbstractRenderer implements \PhpOffice\PhpWord\Writer\Write * Create new instance * * @param PhpWord $phpWord PhpWord object + * @throws \PhpOffice\PhpWord\Exception\Exception */ public function __construct(PhpWord $phpWord) { diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index 8670cb40..888166e6 100644 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -145,7 +145,7 @@ class RTF extends AbstractWriter implements WriterInterface // Set the color tbl group $content .= '{\colortbl '; - foreach ($this->colorTable as $idx => $color) { + foreach ($this->colorTable as $color) { $arrColor = Drawing::htmlToRGB($color); $content .= ';\red' . $arrColor[0] . '\green' . $arrColor[1] . '\blue' . $arrColor[2] . ''; } diff --git a/src/PhpWord/Writer/RTF/Element/AbstractElement.php b/src/PhpWord/Writer/RTF/Element/AbstractElement.php index 89bcfd37..e04aff3c 100644 --- a/src/PhpWord/Writer/RTF/Element/AbstractElement.php +++ b/src/PhpWord/Writer/RTF/Element/AbstractElement.php @@ -18,8 +18,8 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; use PhpOffice\PhpWord\Shared\String; -use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Font as FontStyle; +use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle; use PhpOffice\PhpWord\Writer\RTF\Style\Font as FontStyleWriter; use PhpOffice\PhpWord\Writer\RTF\Style\Paragraph as ParagraphStyleWriter; @@ -34,14 +34,14 @@ class AbstractElement extends \PhpOffice\PhpWord\Writer\HTML\Element\AbstractEle /** * Font style * - * @var \PhpWord\PhpOffice\Style\Font + * @var \PhpOffice\PhpWord\Style\Font */ private $fontStyle; /** * Paragraph style * - * @var \PhpWord\PhpOffice\Style\Paragraph + * @var \PhpOffice\PhpWord\Style\Paragraph */ private $paragraphStyle; @@ -50,10 +50,10 @@ class AbstractElement extends \PhpOffice\PhpWord\Writer\HTML\Element\AbstractEle */ protected function getStyles() { - /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */ + /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Type hint */ $parentWriter = $this->parentWriter; - /** @var \PhpOffice\PhpWord\Element\Text $element Scrutinizer type hint */ + /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */ $element = $this->element; // Font style @@ -93,7 +93,7 @@ class AbstractElement extends \PhpOffice\PhpWord\Writer\HTML\Element\AbstractEle protected function writeOpening() { if ($this->withoutP || !$this->paragraphStyle instanceof ParagraphStyle) { - return; + return ''; } $styleWriter = new ParagraphStyleWriter($this->paragraphStyle); @@ -119,7 +119,7 @@ class AbstractElement extends \PhpOffice\PhpWord\Writer\HTML\Element\AbstractEle protected function writeClosing() { if ($this->withoutP) { - return; + return ''; } return '\par' . PHP_EOL; @@ -136,7 +136,7 @@ class AbstractElement extends \PhpOffice\PhpWord\Writer\HTML\Element\AbstractEle return ''; } - /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */ + /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Type hint */ $parentWriter = $this->parentWriter; // Create style writer and set color/name index diff --git a/src/PhpWord/Writer/RTF/Element/Link.php b/src/PhpWord/Writer/RTF/Element/Link.php index 684f6e65..22b08588 100644 --- a/src/PhpWord/Writer/RTF/Element/Link.php +++ b/src/PhpWord/Writer/RTF/Element/Link.php @@ -32,7 +32,7 @@ class Link extends AbstractElement public function write() { if (!$this->element instanceof \PhpOffice\PhpWord\Element\Link) { - return; + return ''; } $this->getStyles(); diff --git a/src/PhpWord/Writer/RTF/Element/Text.php b/src/PhpWord/Writer/RTF/Element/Text.php index abdf888d..38ef4c94 100644 --- a/src/PhpWord/Writer/RTF/Element/Text.php +++ b/src/PhpWord/Writer/RTF/Element/Text.php @@ -31,11 +31,11 @@ class Text extends AbstractElement */ public function write() { - /** @var \PhpOffice\PhpWord\Element\Text $element Scrutinizer type hint */ + /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */ $element = $this->element; $elementClass = str_replace('\\Writer\\RTF', '', get_class($this)); if (!$element instanceof $elementClass) { - return; + return ''; } $this->getStyles(); diff --git a/src/PhpWord/Writer/RTF/Element/TextBreak.php b/src/PhpWord/Writer/RTF/Element/TextBreak.php index ff836a88..4449be65 100644 --- a/src/PhpWord/Writer/RTF/Element/TextBreak.php +++ b/src/PhpWord/Writer/RTF/Element/TextBreak.php @@ -31,7 +31,7 @@ class TextBreak extends AbstractElement */ public function write() { - /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */ + /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Type hint */ $parentWriter = $this->parentWriter; $parentWriter->setLastParagraphStyle(); diff --git a/src/PhpWord/Writer/RTF/Element/TextRun.php b/src/PhpWord/Writer/RTF/Element/TextRun.php index 8d70366c..e7563716 100644 --- a/src/PhpWord/Writer/RTF/Element/TextRun.php +++ b/src/PhpWord/Writer/RTF/Element/TextRun.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; -use PhpOffice\PhpWord\Writer\RTF\Element\Container; - /** * TextRun element RTF writer * diff --git a/src/PhpWord/Writer/RTF/Style/AbstractStyle.php b/src/PhpWord/Writer/RTF/Style/AbstractStyle.php index 55d6588e..6ad31e41 100644 --- a/src/PhpWord/Writer/RTF/Style/AbstractStyle.php +++ b/src/PhpWord/Writer/RTF/Style/AbstractStyle.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\RTF\Style; -use PhpOffice\PhpWord\Style\AbstractStyle as Style; - /** * Abstract RTF style writer * diff --git a/src/PhpWord/Writer/RTF/Style/Font.php b/src/PhpWord/Writer/RTF/Style/Font.php index b70c089f..e28cb691 100644 --- a/src/PhpWord/Writer/RTF/Style/Font.php +++ b/src/PhpWord/Writer/RTF/Style/Font.php @@ -17,7 +17,6 @@ namespace PhpOffice\PhpWord\Writer\RTF\Style; -use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Style\Font as FontStyle; /** @@ -46,7 +45,7 @@ class Font extends AbstractStyle { $style = $this->getStyle(); if (!$style instanceof \PhpOffice\PhpWord\Style\Font) { - return; + return ''; } $content = ''; diff --git a/src/PhpWord/Writer/RTF/Style/Paragraph.php b/src/PhpWord/Writer/RTF/Style/Paragraph.php index 26c62f02..e5f5d85e 100644 --- a/src/PhpWord/Writer/RTF/Style/Paragraph.php +++ b/src/PhpWord/Writer/RTF/Style/Paragraph.php @@ -35,7 +35,7 @@ class Paragraph extends AbstractStyle { $style = $this->getStyle(); if (!$style instanceof \PhpOffice\PhpWord\Style\Paragraph) { - return; + return ''; } $alignments = array( diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index 44317bd2..d55cfff5 100644 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -88,6 +88,7 @@ class Word2007 extends AbstractWriter implements WriterInterface * Save document by name * * @param string $filename + * @throws \PhpOffice\PhpWord\Exception\Exception */ public function save($filename = null) { @@ -191,6 +192,7 @@ class Word2007 extends AbstractWriter implements WriterInterface /** * Add header/footer content * + * @param \PhpOffice\PhpWord\Element\Section $section * @param mixed $objZip * @param string $elmType header|footer * @param integer $rId diff --git a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php index bbb2c83e..be3e8463 100644 --- a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php +++ b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php @@ -58,6 +58,8 @@ abstract class AbstractElement /** * Create new instance * + * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param \PhpOffice\PhpWord\Element\AbstractElement $element * @param bool $withoutP */ public function __construct(XMLWriter $xmlWriter, Element $element, $withoutP = false) @@ -81,14 +83,11 @@ abstract class AbstractElement * Get element * * @return \PhpOffice\PhpWord\Element\AbstractElement + * @throws \PhpOffice\PhpWord\Exception\Exception */ protected function getElement() { - if (!is_null($this->element)) { - return $this->element; - } else { - throw new Exception('No element assigned.'); - } + return $this->element; } /** diff --git a/src/PhpWord/Writer/Word2007/Element/TOC.php b/src/PhpWord/Writer/Word2007/Element/TOC.php index 88dd0dbd..00da355e 100644 --- a/src/PhpWord/Writer/Word2007/Element/TOC.php +++ b/src/PhpWord/Writer/Word2007/Element/TOC.php @@ -64,6 +64,8 @@ class TOC extends AbstractElement /** * Write title * + * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param \PhpOffice\PhpWord\Element\TOC $element * @param \PhpOffice\PhpWord\Element\Title $title * @param bool $writeFieldMark */ @@ -130,6 +132,8 @@ class TOC extends AbstractElement /** * Write style * + * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param \PhpOffice\PhpWord\Element\TOC $element * @param int $indent */ private function writeStyle(XMLWriter $xmlWriter, TOCElement $element, $indent) @@ -141,8 +145,8 @@ class TOC extends AbstractElement $xmlWriter->startElement('w:pPr'); // Paragraph - if ($isObject && !is_null($fontStyle->getParagraphStyle())) { - $styleWriter = new ParagraphStyleWriter($xmlWriter, $fontStyle->getParagraphStyle()); + if ($isObject && !is_null($fontStyle->getParagraph())) { + $styleWriter = new ParagraphStyleWriter($xmlWriter, $fontStyle->getParagraph()); $styleWriter->write(); } diff --git a/src/PhpWord/Writer/Word2007/Element/Text.php b/src/PhpWord/Writer/Word2007/Element/Text.php index 5cb33a28..87a56dd2 100644 --- a/src/PhpWord/Writer/Word2007/Element/Text.php +++ b/src/PhpWord/Writer/Word2007/Element/Text.php @@ -89,7 +89,7 @@ class Text extends AbstractElement { $xmlWriter = $this->getXmlWriter(); - /** @var \PhpOffice\PhpWord\Element\Text $element Scrutinizer type hint */ + /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */ $element = $this->getElement(); $paragraphStyle = $element->getParagraphStyle(); $styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle); @@ -104,7 +104,7 @@ class Text extends AbstractElement { $xmlWriter = $this->getXmlWriter(); - /** @var \PhpOffice\PhpWord\Element\Text $element Scrutinizer type hint */ + /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */ $element = $this->getElement(); $fontStyle = $element->getFontStyle(); $styleWriter = new FontStyleWriter($xmlWriter, $fontStyle); diff --git a/src/PhpWord/Writer/Word2007/Part/ContentTypes.php b/src/PhpWord/Writer/Word2007/Part/ContentTypes.php index 417df64d..c97654be 100644 --- a/src/PhpWord/Writer/Word2007/Part/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/Part/ContentTypes.php @@ -32,7 +32,7 @@ class ContentTypes extends AbstractPart */ public function write() { - /** @var \PhpOffice\PhpWord\Writer\Word2007 $parentWriter Scrutinizer type hint */ + /** @var \PhpOffice\PhpWord\Writer\Word2007 $parentWriter Type hint */ $parentWriter = $this->getParentWriter(); $contentTypes = $parentWriter->getContentTypes(); @@ -82,16 +82,12 @@ class ContentTypes extends AbstractPart private function writeContentType(XMLWriter $xmlWriter, $parts, $isDefault) { foreach ($parts as $partName => $contentType) { - if ($partName != '' && $contentType != '') { - $partType = $isDefault ? 'Default' : 'Override'; - $partAttribute = $isDefault ? 'Extension' : 'PartName'; - $xmlWriter->startElement($partType); - $xmlWriter->writeAttribute($partAttribute, $partName); - $xmlWriter->writeAttribute('ContentType', $contentType); - $xmlWriter->endElement(); - } else { - throw new Exception("Invalid parameters passed."); - } + $partType = $isDefault ? 'Default' : 'Override'; + $partAttribute = $isDefault ? 'Extension' : 'PartName'; + $xmlWriter->startElement($partType); + $xmlWriter->writeAttribute($partAttribute, $partName); + $xmlWriter->writeAttribute('ContentType', $contentType); + $xmlWriter->endElement(); } } } diff --git a/src/PhpWord/Writer/Word2007/Part/DocPropsApp.php b/src/PhpWord/Writer/Word2007/Part/DocPropsApp.php index 17d38a10..1e6549c5 100644 --- a/src/PhpWord/Writer/Word2007/Part/DocPropsApp.php +++ b/src/PhpWord/Writer/Word2007/Part/DocPropsApp.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; -use PhpOffice\PhpWord\PhpWord; - /** * Word2007 extended document properties part writer: docProps/app.xml * diff --git a/src/PhpWord/Writer/Word2007/Part/DocPropsCore.php b/src/PhpWord/Writer/Word2007/Part/DocPropsCore.php index 51400846..2b3bce5a 100644 --- a/src/PhpWord/Writer/Word2007/Part/DocPropsCore.php +++ b/src/PhpWord/Writer/Word2007/Part/DocPropsCore.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; -use PhpOffice\PhpWord\PhpWord; - /** * Word2007 core document properties part writer: docProps/core.xml * diff --git a/src/PhpWord/Writer/Word2007/Part/Document.php b/src/PhpWord/Writer/Word2007/Part/Document.php index 0f8a16af..99570338 100644 --- a/src/PhpWord/Writer/Word2007/Part/Document.php +++ b/src/PhpWord/Writer/Word2007/Part/Document.php @@ -18,7 +18,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; use PhpOffice\PhpWord\Element\Section; -use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Writer\Word2007\Element\Container; use PhpOffice\PhpWord\Writer\Word2007\Style\Section as SectionStyleWriter; diff --git a/src/PhpWord/Writer/Word2007/Part/Footnotes.php b/src/PhpWord/Writer/Word2007/Part/Footnotes.php index d7a1caaa..903e5fe5 100644 --- a/src/PhpWord/Writer/Word2007/Part/Footnotes.php +++ b/src/PhpWord/Writer/Word2007/Part/Footnotes.php @@ -106,7 +106,7 @@ class Footnotes extends AbstractPart $xmlWriter->endElement(); // w:p $xmlWriter->endElement(); // $this->elementNode - /** @var array $elements Scrutinizer type hint */ + /** @var array $elements Type hint */ $elements = $this->elements; foreach ($elements as $element) { if ($element instanceof Footnote) { diff --git a/src/PhpWord/Writer/Word2007/Part/Numbering.php b/src/PhpWord/Writer/Word2007/Part/Numbering.php index 2678ac55..6310fbf1 100644 --- a/src/PhpWord/Writer/Word2007/Part/Numbering.php +++ b/src/PhpWord/Writer/Word2007/Part/Numbering.php @@ -18,9 +18,9 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; use PhpOffice\PhpWord\Shared\XMLWriter; -use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Numbering as NumberingStyle; use PhpOffice\PhpWord\Style\NumberingLevel; +use PhpOffice\PhpWord\Style; /** * Word2007 numbering part writer: word/numbering.xml diff --git a/src/PhpWord/Writer/Word2007/Part/Rels.php b/src/PhpWord/Writer/Word2007/Part/Rels.php index 44e62013..af57a768 100644 --- a/src/PhpWord/Writer/Word2007/Part/Rels.php +++ b/src/PhpWord/Writer/Word2007/Part/Rels.php @@ -97,6 +97,7 @@ class Rels extends AbstractPart * @param string $type Relationship type * @param string $target Relationship target * @param string $targetMode Relationship target mode + * @throws \PhpOffice\PhpWord\Exception\Exception */ private function writeRel(XMLWriter $xmlWriter, $relId, $type, $target, $targetMode = '') { diff --git a/src/PhpWord/Writer/Word2007/Part/RelsDocument.php b/src/PhpWord/Writer/Word2007/Part/RelsDocument.php index 2a58421c..744e14f9 100644 --- a/src/PhpWord/Writer/Word2007/Part/RelsDocument.php +++ b/src/PhpWord/Writer/Word2007/Part/RelsDocument.php @@ -41,7 +41,7 @@ class RelsDocument extends Rels ); $xmlWriter = $this->getXmlWriter(); - /** @var \PhpOffice\PhpWord\Writer\Word2007 $parentWriter Scrutinizer type hint */ + /** @var \PhpOffice\PhpWord\Writer\Word2007 $parentWriter Type hint */ $parentWriter = $this->getParentWriter(); $this->writeRels($xmlWriter, $xmlRels, $parentWriter->getRelationships()); diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index c296581f..80a1b427 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -127,7 +127,7 @@ class Settings extends AbstractPart } else { $xmlWriter->startElement($settingKey); - /** @var array $settingValue Scrutinizer type hint */ + /** @var array $settingValue Type hint */ foreach ($settingValue as $childKey => $childValue) { if ($childKey == '@attributes') { foreach ($childValue as $key => $val) { diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index 13e07e71..09ee43ed 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -19,9 +19,9 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Shared\XMLWriter; -use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; +use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Table; use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter; use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter; @@ -155,6 +155,7 @@ class Styles extends AbstractPart * Write default font and other default styles * * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param \PhpOffice\PhpWord\PhpWord $phpWord * @param array $styles */ private function writeDefaultStyles(XMLWriter $xmlWriter, PhpWord $phpWord, $styles) diff --git a/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php b/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php index 06c35117..ae97b62e 100644 --- a/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php +++ b/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php @@ -49,6 +49,7 @@ abstract class AbstractStyle /** * Create new instance * + * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter * @param string|\PhpOffice\PhpWord\Style\AbstractStyle $style */ public function __construct(XMLWriter $xmlWriter, $style = null) diff --git a/src/PhpWord/Writer/Word2007/Style/Image.php b/src/PhpWord/Writer/Word2007/Style/Image.php index ecc3fb7b..5dde00c8 100644 --- a/src/PhpWord/Writer/Word2007/Style/Image.php +++ b/src/PhpWord/Writer/Word2007/Style/Image.php @@ -136,6 +136,7 @@ class Image extends AbstractStyle /** * Get element style * + * @param \PhpOffice\PhpWord\Style\Image $style * @return array */ private function getElementStyle(ImageStyle $style) diff --git a/src/PhpWord/Writer/Word2007/Style/MarginBorder.php b/src/PhpWord/Writer/Word2007/Style/MarginBorder.php index b5c05ca9..23143ef5 100644 --- a/src/PhpWord/Writer/Word2007/Style/MarginBorder.php +++ b/src/PhpWord/Writer/Word2007/Style/MarginBorder.php @@ -71,6 +71,7 @@ class MarginBorder extends AbstractStyle /** * Write side * + * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter * @param string $side * @param int $width * @param string $color diff --git a/src/PhpWord/Writer/Word2007/Style/Table.php b/src/PhpWord/Writer/Word2007/Style/Table.php index 590e819b..68a074ea 100644 --- a/src/PhpWord/Writer/Word2007/Style/Table.php +++ b/src/PhpWord/Writer/Word2007/Style/Table.php @@ -85,6 +85,7 @@ class Table extends AbstractStyle /** * Write width * + * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter * @param int $width * @param string $unit */ diff --git a/tests/PhpWord/Tests/Writer/HTML/ElementTest.php b/tests/PhpWord/Tests/Writer/HTML/ElementTest.php index d2ed70ea..e12193e8 100644 --- a/tests/PhpWord/Tests/Writer/HTML/ElementTest.php +++ b/tests/PhpWord/Tests/Writer/HTML/ElementTest.php @@ -28,12 +28,12 @@ class ElementTest extends \PHPUnit_Framework_TestCase */ public function testUnmatchedElements() { - $styles = array('Container', 'Footnote', 'Image', 'Link', 'ListItem', 'Table', 'Title'); - foreach ($styles as $style) { - $objectClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Element\\' . $style; + $elements = array('Container', 'Footnote', 'Image', 'Link', 'ListItem', 'Table', 'Title'); + foreach ($elements as $element) { + $objectClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Element\\' . $element; $parentWriter = new HTML(); - $element = new \PhpOffice\PhpWord\Element\PageBreak(); - $object = new $objectClass($parentWriter, $element); + $newElement = new \PhpOffice\PhpWord\Element\PageBreak(); + $object = new $objectClass($parentWriter, $newElement); $this->assertEquals('', $object->write()); } diff --git a/tests/PhpWord/Tests/Writer/HTMLTest.php b/tests/PhpWord/Tests/Writer/HTMLTest.php index 1c2a5049..34e9d2bd 100644 --- a/tests/PhpWord/Tests/Writer/HTMLTest.php +++ b/tests/PhpWord/Tests/Writer/HTMLTest.php @@ -64,6 +64,7 @@ class HTMLTest extends \PHPUnit_Framework_TestCase $docProps = $phpWord->getDocumentProperties(); $docProps->setTitle('HTML Test'); + $phpWord->addTitleStyle(1, array('bold' => true)); $phpWord->addFontStyle('Font', array('name' => 'Verdana', 'size' => 11, 'color' => 'FF0000', 'fgColor' => 'FF0000')); $phpWord->addParagraphStyle('Paragraph', array('align' => 'center')); diff --git a/tests/PhpWord/Tests/Writer/ODText/ElementTest.php b/tests/PhpWord/Tests/Writer/ODText/ElementTest.php index 6354520a..c5fdfb41 100644 --- a/tests/PhpWord/Tests/Writer/ODText/ElementTest.php +++ b/tests/PhpWord/Tests/Writer/ODText/ElementTest.php @@ -28,12 +28,12 @@ class ElementTest extends \PHPUnit_Framework_TestCase */ public function testUnmatchedElements() { - $styles = array('Image', 'Link', 'Table', 'Text'); - foreach ($styles as $style) { - $objectClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Element\\' . $style; + $elements = array('Image', 'Link', 'Table', 'Text'); + foreach ($elements as $element) { + $objectClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Element\\' . $element; $xmlWriter = new XMLWriter(); - $element = new \PhpOffice\PhpWord\Element\PageBreak(); - $object = new $objectClass($xmlWriter, $element); + $newElement = new \PhpOffice\PhpWord\Element\PageBreak(); + $object = new $objectClass($xmlWriter, $newElement); $object->write(); $this->assertEquals('', $xmlWriter->getData()); diff --git a/tests/PhpWord/Tests/Writer/RTF/ElementTest.php b/tests/PhpWord/Tests/Writer/RTF/ElementTest.php index b63c8a5e..a31117e6 100644 --- a/tests/PhpWord/Tests/Writer/RTF/ElementTest.php +++ b/tests/PhpWord/Tests/Writer/RTF/ElementTest.php @@ -28,12 +28,12 @@ class ElementTest extends \PHPUnit_Framework_TestCase */ public function testUnmatchedElements() { - $styles = array('Container', 'Text', 'Title', 'Link'); - foreach ($styles as $style) { - $objectClass = 'PhpOffice\\PhpWord\\Writer\\RTF\\Element\\' . $style; + $elements = array('Container', 'Text', 'Title', 'Link'); + foreach ($elements as $element) { + $objectClass = 'PhpOffice\\PhpWord\\Writer\\RTF\\Element\\' . $element; $parentWriter = new RTF(); - $element = new \PhpOffice\PhpWord\Element\PageBreak(); - $object = new $objectClass($parentWriter, $element); + $newElement = new \PhpOffice\PhpWord\Element\PageBreak(); + $object = new $objectClass($parentWriter, $newElement); $this->assertEquals('', $object->write()); } diff --git a/tests/PhpWord/Tests/Writer/RTFTest.php b/tests/PhpWord/Tests/Writer/RTFTest.php index ad5a13e1..a8b10bb5 100644 --- a/tests/PhpWord/Tests/Writer/RTFTest.php +++ b/tests/PhpWord/Tests/Writer/RTFTest.php @@ -59,7 +59,7 @@ class RTFTest extends \PHPUnit_Framework_TestCase $phpWord = new PhpWord(); $phpWord->addFontStyle('Font', array('name' => 'Verdana', 'size' => 11, - 'color' => 'FF0000', 'fgColor' => 'FF0000')); + 'color' => 'FF0000', 'fgColor' => '00FF00')); $phpWord->addParagraphStyle('Paragraph', array('align' => 'center')); $section = $phpWord->addSection(); $section->addText('Test 1', 'Font', 'Paragraph'); diff --git a/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php b/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php index 0a9eb4ce..4d1d7ce2 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php @@ -28,15 +28,15 @@ class ElementTest extends \PHPUnit_Framework_TestCase */ public function testUnmatchedElements() { - $styles = array( + $elements = array( 'CheckBox', 'Container', 'Footnote', 'Image', 'Link', 'ListItem', 'ListItemRun', 'Object', 'PreserveText', 'Table', 'Text', 'TextBox', 'TextBreak', 'Title', 'TOC' ); - foreach ($styles as $style) { - $objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $style; + foreach ($elements as $element) { + $objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $element; $xmlWriter = new XMLWriter(); - $element = new \PhpOffice\PhpWord\Element\PageBreak(); - $object = new $objectClass($xmlWriter, $element); + $newElement = new \PhpOffice\PhpWord\Element\PageBreak(); + $object = new $objectClass($xmlWriter, $newElement); $object->write(); $this->assertEquals('', $xmlWriter->getData()); diff --git a/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php b/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php index 7798f30e..ae0fe1d2 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php @@ -72,6 +72,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase $section->addListItem('List Item 1', 0); $section->addListItem('List Item 2', 0); $section->addListItem('List Item 3', 0); + $section = $phpWord->addSection(); $section->addTitle('Title 2', 2); $section->addObject($objectSrc); @@ -80,6 +81,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase 'posHorizontalRel' => 'margin', 'posVerticalRel' => 'margin', 'innerMargin' => 10, 'borderSize' => 1, 'borderColor' => '#FF0')); $section->addTextBox(array('wrappingStyle' => 'tight', 'positioning' => 'absolute', 'align' => 'center')); + $section->addListItemRun()->addText('List item run 1'); $doc = TestHelperDOCX::getDocument($phpWord); @@ -113,22 +115,27 @@ class DocumentTest extends \PHPUnit_Framework_TestCase { $objectSrc = __DIR__ . "/../../../_files/documents/sheet.xls"; + $tabs = array(new \PhpOffice\PhpWord\Style\Tab('right', 9090)); $phpWord = new PhpWord(); - $phpWord->addParagraphStyle('pStyle', array('align' => 'center')); // Style #1 + $phpWord->addParagraphStyle('pStyle', array('align' => 'center', 'tabs' => $tabs)); // Style #1 $phpWord->addFontStyle('fStyle', array('size' => '20', 'bold' => true, 'allCaps' => true)); // Style #2 $phpWord->addTitleStyle(1, array('color' => '333333', 'doubleStrikethrough' => true)); // Style #3 + $phpWord->addTableStyle('tStyle', array('borderSize' => 1)); $fontStyle = new Font('text', array('align' => 'center')); + $section = $phpWord->addSection(); - $section->addListItem('List Item', 0, null, null, 'pStyle'); // Style #4 + $section->addListItem('List Item', 0, null, null, 'pStyle'); // Style #5 $section->addObject($objectSrc, array('align' => 'center')); $section->addTOC($fontStyle); $section->addTitle('Title 1', 1); $section->addTOC('fStyle'); + $table = $section->addTable('tStyle'); + $table->setWidth(100); $doc = TestHelperDOCX::getDocument($phpWord); // List item $element = $doc->getElement('/w:document/w:body/w:p[1]/w:pPr/w:numPr/w:numId'); - $this->assertEquals(4, $element->getAttribute('w:val')); + $this->assertEquals(5, $element->getAttribute('w:val')); // Object $element = $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:object/o:OLEObject'); @@ -497,6 +504,10 @@ class DocumentTest extends \PHPUnit_Framework_TestCase $table->addCell(40); $table->addCell(40); + $table->addRow(); + $cell = $table->addCell(200, array('borderRightColor' => 'FF0000')); + $cell->getStyle()->setGridSpan(5); + $doc = TestHelperDOCX::getDocument($phpWord); $element = $doc->getElement('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:gridSpan'); diff --git a/tests/PhpWord/Tests/Writer/Word2007/PartTest.php b/tests/PhpWord/Tests/Writer/Word2007/PartTest.php new file mode 100644 index 00000000..8748169d --- /dev/null +++ b/tests/PhpWord/Tests/Writer/Word2007/PartTest.php @@ -0,0 +1,41 @@ +setMedia(array(array('foo' => 'bar'))); + $object->write(); + } +} From 4edf8ed9b4961f1d06ad7d6d0bacfe3e2e714787 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Wed, 14 May 2014 20:18:35 +0700 Subject: [PATCH 16/48] Fix unit tests --- tests/PhpWord/Tests/Element/CellTest.php | 2 +- tests/PhpWord/Tests/Element/FooterTest.php | 2 +- tests/PhpWord/Tests/Element/HeaderTest.php | 2 +- tests/PhpWord/Tests/Writer/Word2007/PartTest.php | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/PhpWord/Tests/Element/CellTest.php b/tests/PhpWord/Tests/Element/CellTest.php index af924bc8..aa07ec03 100644 --- a/tests/PhpWord/Tests/Element/CellTest.php +++ b/tests/PhpWord/Tests/Element/CellTest.php @@ -168,7 +168,7 @@ class CellTest extends \PHPUnit_Framework_TestCase { $oCell = new Cell(); $element = $oCell->addImage( - 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png' + 'http://php.net/images/logos/php-med-trans-light.gif' ); $this->assertCount(1, $oCell->getElements()); diff --git a/tests/PhpWord/Tests/Element/FooterTest.php b/tests/PhpWord/Tests/Element/FooterTest.php index d2a76705..c5d04b41 100644 --- a/tests/PhpWord/Tests/Element/FooterTest.php +++ b/tests/PhpWord/Tests/Element/FooterTest.php @@ -119,7 +119,7 @@ class FooterTest extends \PHPUnit_Framework_TestCase { $oFooter = new Footer(1); $element = $oFooter->addImage( - 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png' + 'http://php.net/images/logos/php-med-trans-light.gif' ); $this->assertCount(1, $oFooter->getElements()); diff --git a/tests/PhpWord/Tests/Element/HeaderTest.php b/tests/PhpWord/Tests/Element/HeaderTest.php index 5b64b111..796b24f0 100644 --- a/tests/PhpWord/Tests/Element/HeaderTest.php +++ b/tests/PhpWord/Tests/Element/HeaderTest.php @@ -128,7 +128,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase { $oHeader = new Header(1); $element = $oHeader->addImage( - 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png' + 'http://php.net/images/logos/php-med-trans-light.gif' ); $this->assertCount(1, $oHeader->getElements()); diff --git a/tests/PhpWord/Tests/Writer/Word2007/PartTest.php b/tests/PhpWord/Tests/Writer/Word2007/PartTest.php index 8748169d..c3d29331 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/PartTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/PartTest.php @@ -14,7 +14,7 @@ * @copyright 2010-2014 PHPWord contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ -namespace PhpOffice\PhpWord\Tests\Writer\Word2007\Part; +namespace PhpOffice\PhpWord\Tests\Writer\Word2007; use PhpOffice\PhpWord\Writer\Word2007\Part\RelsPart; @@ -35,7 +35,7 @@ class PartTest extends \PHPUnit_Framework_TestCase public function testRelsWriteRelException() { $object = new RelsPart(); - $object->setMedia(array(array('foo' => 'bar'))); + $object->setMedia(array(array('type' => '', 'target' => ''))); $object->write(); } } From e40b377fc8ac55bc48ee1f557c1f7b12ac3f95df Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Wed, 14 May 2014 20:36:15 +0700 Subject: [PATCH 17/48] Remove PHP 5.3.3 and 5.6 from Travis allowed failures and update composer.lock file. --- .travis.yml | 2 -- composer.lock | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4ddf3f54..96d8bbee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,8 +10,6 @@ php: matrix: allow_failures: - - php: 5.3.3 - - php: 5.6 - php: hhvm env: diff --git a/composer.lock b/composer.lock index 9bc868ac..aab180be 100644 --- a/composer.lock +++ b/composer.lock @@ -3,7 +3,7 @@ "This file locks the dependencies of your project to a known state", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" ], - "hash": "77631436badcf4f49d673498ab6f1916", + "hash": "d46ea4154e935e4be01ffbad0a67bab2", "packages": [ ], @@ -1863,6 +1863,81 @@ ], "time": "2012-12-21 11:40:51" }, + { + "name": "squizlabs/php_codesniffer", + "version": "1.5.3", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "396178ada8499ec492363587f037125bf7b07fcc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/396178ada8499ec492363587f037125bf7b07fcc", + "reference": "396178ada8499ec492363587f037125bf7b07fcc", + "shasum": "" + }, + "require": { + "ext-tokenizer": "*", + "php": ">=5.1.2" + }, + "suggest": { + "phpunit/php-timer": "dev-master" + }, + "bin": [ + "scripts/phpcs" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-phpcs-fixer": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "CodeSniffer.php", + "CodeSniffer/CLI.php", + "CodeSniffer/Exception.php", + "CodeSniffer/File.php", + "CodeSniffer/Report.php", + "CodeSniffer/Reporting.php", + "CodeSniffer/Sniff.php", + "CodeSniffer/Tokens.php", + "CodeSniffer/Reports/", + "CodeSniffer/CommentParser/", + "CodeSniffer/Tokenizers/", + "CodeSniffer/DocGenerators/", + "CodeSniffer/Standards/AbstractPatternSniff.php", + "CodeSniffer/Standards/AbstractScopeSniff.php", + "CodeSniffer/Standards/AbstractVariableSniff.php", + "CodeSniffer/Standards/IncorrectPatternException.php", + "CodeSniffer/Standards/Generic/Sniffs/", + "CodeSniffer/Standards/MySource/Sniffs/", + "CodeSniffer/Standards/PEAR/Sniffs/", + "CodeSniffer/Standards/PSR1/Sniffs/", + "CodeSniffer/Standards/PSR2/Sniffs/", + "CodeSniffer/Standards/Squiz/Sniffs/", + "CodeSniffer/Standards/Zend/Sniffs/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "lead" + } + ], + "description": "PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "http://www.squizlabs.com/php-codesniffer", + "keywords": [ + "phpcs", + "standards" + ], + "time": "2014-05-01 03:07:07" + }, { "name": "symfony/config", "version": "v2.4.4", From 27fa3ba2337de7be653d516ca688b460c8c481e4 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 15 May 2014 01:13:22 +0700 Subject: [PATCH 18/48] Remove fontStyle parameter from ListItemRun and various small fixes --- .scrutinizer.yml | 1 - CHANGELOG.md | 1 + src/PhpWord/Element/AbstractContainer.php | 5 +- src/PhpWord/Element/ListItemRun.php | 3 +- src/PhpWord/Element/Title.php | 56 ----- src/PhpWord/Style/Border.php | 140 +++++------ src/PhpWord/Style/Table.php | 229 ++++++++++-------- src/PhpWord/Writer/HTML/Element/Text.php | 2 +- .../Word2007/Element/AbstractElement.php | 2 - .../Writer/Word2007/Part/ContentTypes.php | 2 - src/PhpWord/Writer/Word2007/Style/Cell.php | 2 +- src/PhpWord/Writer/Word2007/Style/Section.php | 2 +- src/PhpWord/Writer/Word2007/Style/Table.php | 4 +- 13 files changed, 204 insertions(+), 245 deletions(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 40cc99a4..ab53c762 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -9,7 +9,6 @@ tools: enabled: true config: standard: PSR2 - php_cpd: true php_mess_detector: enabled: true config: diff --git a/CHANGELOG.md b/CHANGELOG.md index 45b35a21..3bed3012 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r - RTF: UTF8 support for RTF: Internal UTF8 text is converted to Unicode before writing - @ivanlanin GH-158 - Table: Ability to define table width (in percent and twip) and position - @ivanlanin GH-237 - RTF: Ability to add links and page breaks in RTF - @ivanlanin GH-196 +- ListItemRun: Remove fontStyle parameter because ListItemRun is inherited from TextRun and TextRun doesn't have fontStyle - @ivanlanin ### Bugfixes diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 5afddc5f..81dbedc2 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -213,14 +213,13 @@ abstract class AbstractContainer extends AbstractElement * Add listitemrun element * * @param int $depth - * @param mixed $fontStyle * @param mixed $listStyle * @param mixed $paragraphStyle * @return \PhpOffice\PhpWord\Element\ListItemRun */ - public function addListItemRun($depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null) + public function addListItemRun($depth = 0, $listStyle = null, $paragraphStyle = null) { - return $this->addElement('ListItemRun', $depth, $fontStyle, $listStyle, $paragraphStyle); + return $this->addElement('ListItemRun', $depth, $listStyle, $paragraphStyle); } /** diff --git a/src/PhpWord/Element/ListItemRun.php b/src/PhpWord/Element/ListItemRun.php index 2a2a51af..fb219f91 100644 --- a/src/PhpWord/Element/ListItemRun.php +++ b/src/PhpWord/Element/ListItemRun.php @@ -48,11 +48,10 @@ class ListItemRun extends TextRun * Create a new ListItem * * @param int $depth - * @param mixed $fontStyle * @param array|string|null $listStyle * @param mixed $paragraphStyle */ - public function __construct($depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null) + public function __construct($depth = 0, $listStyle = null, $paragraphStyle = null) { $this->depth = $depth; diff --git a/src/PhpWord/Element/Title.php b/src/PhpWord/Element/Title.php index 3a1b3049..d5206879 100644 --- a/src/PhpWord/Element/Title.php +++ b/src/PhpWord/Element/Title.php @@ -46,14 +46,6 @@ class Title extends AbstractElement */ private $style; - /** - * Title anchor - * - * @var string - * @deprecated 0.10.0 - */ - private $anchor; - /** * Create a new Title Element * @@ -101,52 +93,4 @@ class Title extends AbstractElement { return $this->style; } - - /** - * Set Anchor - * - * @param string $anchor - * @deprecated 0.10.0 - * @codeCoverageIgnore - */ - public function setAnchor($anchor) - { - $this->anchor = $anchor; - } - - /** - * Get Anchor - * - * @return string - * @deprecated 0.10.0 - * @codeCoverageIgnore - */ - public function getAnchor() - { - return '_Toc' . (252634154 + $this->getRelationId()); - } - - /** - * Set Bookmark ID - * - * @param int $value - * @deprecated 0.11.0 - * @codeCoverageIgnore - */ - public function setBookmarkId($value) - { - $this->setRelationId($value); - } - - /** - * Get bookmark ID - * - * @return int - * @deprecated 0.11.0 - * @codeCoverageIgnore - */ - public function getBookmarkId() - { - return $this->getRelationId(); - } } diff --git a/src/PhpWord/Style/Border.php b/src/PhpWord/Style/Border.php index f7c479fe..f0fd8650 100644 --- a/src/PhpWord/Style/Border.php +++ b/src/PhpWord/Style/Border.php @@ -78,22 +78,6 @@ class Border extends AbstractStyle */ protected $borderBottomColor; - /** - * Set border size - * - * @param int|float $value - * @return self - */ - public function setBorderSize($value = null) - { - $this->setBorderTopSize($value); - $this->setBorderLeftSize($value); - $this->setBorderRightSize($value); - $this->setBorderBottomSize($value); - - return $this; - } - /** * Get border size * @@ -110,17 +94,17 @@ class Border extends AbstractStyle } /** - * Set border color + * Set border size * - * @param string $value + * @param int|float $value * @return self */ - public function setBorderColor($value = null) + public function setBorderSize($value = null) { - $this->setBorderTopColor($value); - $this->setBorderLeftColor($value); - $this->setBorderRightColor($value); - $this->setBorderBottomColor($value); + $this->setBorderTopSize($value); + $this->setBorderLeftSize($value); + $this->setBorderRightSize($value); + $this->setBorderBottomSize($value); return $this; } @@ -141,14 +125,17 @@ class Border extends AbstractStyle } /** - * Set border top size + * Set border color * - * @param int|float $value + * @param string $value * @return self */ - public function setBorderTopSize($value = null) + public function setBorderColor($value = null) { - $this->borderTopSize = $value; + $this->setBorderTopColor($value); + $this->setBorderLeftColor($value); + $this->setBorderRightColor($value); + $this->setBorderBottomColor($value); return $this; } @@ -163,6 +150,29 @@ class Border extends AbstractStyle return $this->borderTopSize; } + /** + * Set border top size + * + * @param int|float $value + * @return self + */ + public function setBorderTopSize($value = null) + { + $this->borderTopSize = $this->setNumericVal($value, $this->borderTopSize); + + return $this; + } + + /** + * Get border top color + * + * @return string + */ + public function getBorderTopColor() + { + return $this->borderTopColor; + } + /** * Set border top color * @@ -177,13 +187,13 @@ class Border extends AbstractStyle } /** - * Get border top color + * Get border left size * - * @return string + * @return int|float */ - public function getBorderTopColor() + public function getBorderLeftSize() { - return $this->borderTopColor; + return $this->borderLeftSize; } /** @@ -194,19 +204,19 @@ class Border extends AbstractStyle */ public function setBorderLeftSize($value = null) { - $this->borderLeftSize = $value; + $this->borderLeftSize = $this->setNumericVal($value, $this->borderLeftSize); return $this; } /** - * Get border left size + * Get border left color * - * @return int|float + * @return string */ - public function getBorderLeftSize() + public function getBorderLeftColor() { - return $this->borderLeftSize; + return $this->borderLeftColor; } /** @@ -223,13 +233,13 @@ class Border extends AbstractStyle } /** - * Get border left color + * Get border right size * - * @return string + * @return int|float */ - public function getBorderLeftColor() + public function getBorderRightSize() { - return $this->borderLeftColor; + return $this->borderRightSize; } /** @@ -240,19 +250,19 @@ class Border extends AbstractStyle */ public function setBorderRightSize($value = null) { - $this->borderRightSize = $value; + $this->borderRightSize = $this->setNumericVal($value, $this->borderRightSize); return $this; } /** - * Get border right size + * Get border right color * - * @return int|float + * @return string */ - public function getBorderRightSize() + public function getBorderRightColor() { - return $this->borderRightSize; + return $this->borderRightColor; } /** @@ -269,13 +279,13 @@ class Border extends AbstractStyle } /** - * Get border right color + * Get border bottom size * - * @return string + * @return int|float */ - public function getBorderRightColor() + public function getBorderBottomSize() { - return $this->borderRightColor; + return $this->borderBottomSize; } /** @@ -286,19 +296,19 @@ class Border extends AbstractStyle */ public function setBorderBottomSize($value = null) { - $this->borderBottomSize = $value; + $this->borderBottomSize = $this->setNumericVal($value, $this->borderBottomSize); return $this; } /** - * Get border bottom size + * Get border bottom color * - * @return int|float + * @return string */ - public function getBorderBottomSize() + public function getBorderBottomColor() { - return $this->borderBottomSize; + return $this->borderBottomColor; } /** @@ -315,30 +325,14 @@ class Border extends AbstractStyle } /** - * Get border bottom color - * - * @return string - */ - public function getBorderBottomColor() - { - return $this->borderBottomColor; - } - - /** - * Has borders? + * Check if any of the border is not null * * @return bool */ - public function hasBorders() + public function hasBorder() { - $hasBorders = false; $borders = $this->getBorderSize(); - for ($i = 0; $i < count($borders); $i++) { - if (!is_null($borders[$i])) { - $hasBorders = true; - } - } - return $hasBorders; + return $borders !== array_filter($borders, 'is_null'); } } diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index c077f499..9d5cca87 100644 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -34,35 +34,35 @@ class Table extends Border * * @var \PhpOffice\PhpWord\Style\Table */ - private $firstRow = null; + private $firstRow; /** * Cell margin top * * @var int */ - private $cellMarginTop = null; + private $cellMarginTop; /** * Cell margin left * * @var int */ - private $cellMarginLeft = null; + private $cellMarginLeft; /** * Cell margin right * * @var int */ - private $cellMarginRight = null; + private $cellMarginRight; /** * Cell margin bottom * * @var int */ - private $cellMarginBottom = null; + private $cellMarginBottom; /** * Border size inside horizontal @@ -123,9 +123,12 @@ class Table extends Border public function __construct($tableStyle = null, $firstRowStyle = null) { $this->alignment = new Alignment(); - if (!is_null($firstRowStyle) && is_array($firstRowStyle)) { - $this->firstRow = clone $this; + if ($tableStyle !== null && is_array($tableStyle)) { + $this->setStyleByArray($tableStyle); + } + if ($firstRowStyle !== null && is_array($firstRowStyle)) { + $this->firstRow = clone $this; unset($this->firstRow->firstRow); unset($this->firstRow->cellMarginBottom); unset($this->firstRow->cellMarginTop); @@ -137,10 +140,6 @@ class Table extends Border unset($this->firstRow->borderInsideHSize); $this->firstRow->setStyleByArray($firstRowStyle); } - - if (!is_null($tableStyle) && is_array($tableStyle)) { - $this->setStyleByArray($tableStyle); - } } /** @@ -162,36 +161,20 @@ class Table extends Border { if (!is_null($this->shading)) { return $this->shading->getFill(); - } else { - return null; } + + return null; } /** * Set background * * @param string $value - * @return \PhpOffice\PhpWord\Style\Table + * @return self */ public function setBgColor($value = null) { $this->setShading(array('fill' => $value)); - } - - /** - * Set TLRBHV Border Size - * - * @param int $value Border size in eighths of a point (1/8 point) - * @return self - */ - public function setBorderSize($value = null) - { - $this->setBorderTopSize($value); - $this->setBorderLeftSize($value); - $this->setBorderRightSize($value); - $this->setBorderBottomSize($value); - $this->setBorderInsideHSize($value); - $this->setBorderInsideVSize($value); return $this; } @@ -214,19 +197,19 @@ class Table extends Border } /** - * Set TLRBHV Border Color + * Set TLRBHV Border Size * - * @param string $value + * @param int $value Border size in eighths of a point (1/8 point) * @return self */ - public function setBorderColor($value = null) + public function setBorderSize($value = null) { - $this->setBorderTopColor($value); - $this->setBorderLeftColor($value); - $this->setBorderRightColor($value); - $this->setBorderBottomColor($value); - $this->setBorderInsideHColor($value); - $this->setBorderInsideVColor($value); + $this->setBorderTopSize($value); + $this->setBorderLeftSize($value); + $this->setBorderRightSize($value); + $this->setBorderBottomSize($value); + $this->setBorderInsideHSize($value); + $this->setBorderInsideVSize($value); return $this; } @@ -249,13 +232,21 @@ class Table extends Border } /** - * Set border size inside horizontal + * Set TLRBHV Border Color * - * @param int $value + * @param string $value + * @return self */ - public function setBorderInsideHSize($value = null) + public function setBorderColor($value = null) { - $this->borderInsideHSize = $value; + $this->setBorderTopColor($value); + $this->setBorderLeftColor($value); + $this->setBorderRightColor($value); + $this->setBorderBottomColor($value); + $this->setBorderInsideHColor($value); + $this->setBorderInsideVColor($value); + + return $this; } /** @@ -265,37 +256,20 @@ class Table extends Border */ public function getBorderInsideHSize() { - return (isset($this->borderInsideHSize)) ? $this->borderInsideHSize : null; + return isset($this->borderInsideHSize) ? $this->borderInsideHSize : null; } /** - * Set border size inside vertical + * Set border size inside horizontal * * @param int $value + * @return self */ - public function setBorderInsideVSize($value = null) + public function setBorderInsideHSize($value = null) { - $this->borderInsideVSize = $value; - } + $this->borderInsideHSize = $this->setNumericVal($value, $this->borderInsideHSize); - /** - * Get border size inside vertical - * - * @return int - */ - public function getBorderInsideVSize() - { - return (isset($this->borderInsideVSize)) ? $this->borderInsideVSize : null; - } - - /** - * Set border color inside horizontal - * - * @param string $value - */ - public function setBorderInsideHColor($value = null) - { - $this->borderInsideHColor = $value; + return $this; } /** @@ -305,17 +279,43 @@ class Table extends Border */ public function getBorderInsideHColor() { - return (isset($this->borderInsideHColor)) ? $this->borderInsideHColor : null; + return isset($this->borderInsideHColor) ? $this->borderInsideHColor : null; } /** - * Set border color inside vertical + * Set border color inside horizontal * * @param string $value + * @return self */ - public function setBorderInsideVColor($value = null) + public function setBorderInsideHColor($value = null) { - $this->borderInsideVColor = $value; + $this->borderInsideHColor = $value ; + + return $this; + } + + /** + * Get border size inside vertical + * + * @return int + */ + public function getBorderInsideVSize() + { + return isset($this->borderInsideVSize) ? $this->borderInsideVSize : null; + } + + /** + * Set border size inside vertical + * + * @param int $value + * @return self + */ + public function setBorderInsideVSize($value = null) + { + $this->borderInsideVSize = $this->setNumericVal($value, $this->borderInsideVSize); + + return $this; } /** @@ -325,17 +325,20 @@ class Table extends Border */ public function getBorderInsideVColor() { - return (isset($this->borderInsideVColor)) ? $this->borderInsideVColor : null; + return isset($this->borderInsideVColor) ? $this->borderInsideVColor : null; } /** - * Set cell margin top + * Set border color inside vertical * - * @param int $value + * @param string $value + * @return self */ - public function setCellMarginTop($value = null) + public function setBorderInsideVColor($value = null) { - $this->cellMarginTop = $value; + $this->borderInsideVColor = $value; + + return $this; } /** @@ -349,13 +352,16 @@ class Table extends Border } /** - * Set cell margin left + * Set cell margin top * * @param int $value + * @return self */ - public function setCellMarginLeft($value = null) + public function setCellMarginTop($value = null) { - $this->cellMarginLeft = $value; + $this->cellMarginTop = $this->setNumericVal($value, $this->cellMarginTop); + + return $this; } /** @@ -369,13 +375,16 @@ class Table extends Border } /** - * Set cell margin right + * Set cell margin left * * @param int $value + * @return self */ - public function setCellMarginRight($value = null) + public function setCellMarginLeft($value = null) { - $this->cellMarginRight = $value; + $this->cellMarginLeft = $this->setNumericVal($value, $this->cellMarginLeft); + + return $this; } /** @@ -389,13 +398,16 @@ class Table extends Border } /** - * Set cell margin bottom + * Set cell margin right * * @param int $value + * @return self */ - public function setCellMarginBottom($value = null) + public function setCellMarginRight($value = null) { - $this->cellMarginBottom = $value; + $this->cellMarginRight = $this->setNumericVal($value, $this->cellMarginRight); + + return $this; } /** @@ -409,16 +421,16 @@ class Table extends Border } /** - * Set TLRB cell margin + * Set cell margin bottom * - * @param int $value Margin in twips + * @param int $value + * @return self */ - public function setCellMargin($value = null) + public function setCellMarginBottom($value = null) { - $this->setCellMarginTop($value); - $this->setCellMarginLeft($value); - $this->setCellMarginRight($value); - $this->setCellMarginBottom($value); + $this->cellMarginBottom = $this->setNumericVal($value, $this->cellMarginBottom); + + return $this; } /** @@ -428,25 +440,40 @@ class Table extends Border */ public function getCellMargin() { - return array($this->cellMarginTop, $this->cellMarginLeft, $this->cellMarginRight, $this->cellMarginBottom); + return array( + $this->cellMarginTop, + $this->cellMarginLeft, + $this->cellMarginRight, + $this->cellMarginBottom + ); } /** - * Has margins? + * Set TLRB cell margin + * + * @param int $value Margin in twips + * @return self + */ + public function setCellMargin($value = null) + { + $this->setCellMarginTop($value); + $this->setCellMarginLeft($value); + $this->setCellMarginRight($value); + $this->setCellMarginBottom($value); + + return $this; + } + + /** + * Check if any of the margin is not null * * @return bool */ - public function hasMargins() + public function hasMargin() { - $hasMargins = false; $margins = $this->getCellMargin(); - for ($i = 0; $i < count($margins); $i++) { - if (!is_null($margins[$i])) { - $hasMargins = true; - } - } - return $hasMargins; + return $margins !== array_filter($margins, 'is_null'); } /** diff --git a/src/PhpWord/Writer/HTML/Element/Text.php b/src/PhpWord/Writer/HTML/Element/Text.php index 44740f72..cbabc645 100644 --- a/src/PhpWord/Writer/HTML/Element/Text.php +++ b/src/PhpWord/Writer/HTML/Element/Text.php @@ -145,7 +145,7 @@ class Text extends AbstractElement /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */ $element = $this->element; $style = ''; - if (method_exists($element, 'getParagraphStyle')) { + if (!method_exists($element, 'getParagraphStyle')) { return $style; } diff --git a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php index be3e8463..82465afe 100644 --- a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php +++ b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php @@ -18,7 +18,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; use PhpOffice\PhpWord\Element\AbstractElement as Element; -use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Shared\String; use PhpOffice\PhpWord\Shared\XMLWriter; @@ -83,7 +82,6 @@ abstract class AbstractElement * Get element * * @return \PhpOffice\PhpWord\Element\AbstractElement - * @throws \PhpOffice\PhpWord\Exception\Exception */ protected function getElement() { diff --git a/src/PhpWord/Writer/Word2007/Part/ContentTypes.php b/src/PhpWord/Writer/Word2007/Part/ContentTypes.php index c97654be..f8cb2f26 100644 --- a/src/PhpWord/Writer/Word2007/Part/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/Part/ContentTypes.php @@ -17,7 +17,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; -use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Shared\XMLWriter; /** @@ -77,7 +76,6 @@ class ContentTypes extends AbstractPart * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter XML Writer * @param array $parts * @param boolean $isDefault - * @throws \PhpOffice\PhpWord\Exception\Exception */ private function writeContentType(XMLWriter $xmlWriter, $parts, $isDefault) { diff --git a/src/PhpWord/Writer/Word2007/Style/Cell.php b/src/PhpWord/Writer/Word2007/Style/Cell.php index ce7efb3b..ae2668ae 100644 --- a/src/PhpWord/Writer/Word2007/Style/Cell.php +++ b/src/PhpWord/Writer/Word2007/Style/Cell.php @@ -59,7 +59,7 @@ class Cell extends AbstractStyle $xmlWriter->writeElementIf(!is_null($vAlign), 'w:vAlign', 'w:val', $vAlign); // Border - if ($style->hasBorders()) { + if ($style->hasBorder()) { $xmlWriter->startElement('w:tcBorders'); $styleWriter = new MarginBorder($xmlWriter); diff --git a/src/PhpWord/Writer/Word2007/Style/Section.php b/src/PhpWord/Writer/Word2007/Style/Section.php index 61658e03..5758e3a8 100644 --- a/src/PhpWord/Writer/Word2007/Style/Section.php +++ b/src/PhpWord/Writer/Word2007/Style/Section.php @@ -66,7 +66,7 @@ class Section extends AbstractStyle $xmlWriter->endElement(); // Borders - if ($style->hasBorders()) { + if ($style->hasBorder()) { $xmlWriter->startElement('w:pgBorders'); $xmlWriter->writeAttribute('w:offsetFrom', 'page'); diff --git a/src/PhpWord/Writer/Word2007/Style/Table.php b/src/PhpWord/Writer/Word2007/Style/Table.php index 68a074ea..f6babe6d 100644 --- a/src/PhpWord/Writer/Word2007/Style/Table.php +++ b/src/PhpWord/Writer/Word2007/Style/Table.php @@ -102,7 +102,7 @@ class Table extends AbstractStyle */ private function writeMargin(XMLWriter $xmlWriter, TableStyle $style) { - if ($style->hasMargins()) { + if ($style->hasMargin()) { $xmlWriter->startElement('w:tblCellMar'); $styleWriter = new MarginBorder($xmlWriter); @@ -118,7 +118,7 @@ class Table extends AbstractStyle */ private function writeBorder(XMLWriter $xmlWriter, TableStyle $style) { - if ($style->hasBorders()) { + if ($style->hasBorder()) { $xmlWriter->startElement('w:tblBorders'); $styleWriter = new MarginBorder($xmlWriter); From abbf60a3be97eddfb4d4f58478b8c70a8c934984 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 15 May 2014 10:24:20 +0700 Subject: [PATCH 19/48] Increase minimum mass for php_sim and some test fixes --- .scrutinizer.yml | 2 +- src/PhpWord/Style/Table.php | 17 +++++++------- .../PhpWord/Tests/Element/ListItemRunTest.php | 23 +++++++------------ tests/PhpWord/Tests/Element/TitleTest.php | 12 ---------- 4 files changed, 18 insertions(+), 36 deletions(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index ab53c762..ded9a39f 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -18,7 +18,7 @@ tools: timeout: 900 php_sim: enabled: true - min_mass: 30 + min_mass: 40 php_pdepend: true php_analyzer: true sensiolabs_security_checker: true diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index 9d5cca87..4559e677 100644 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -123,23 +123,24 @@ class Table extends Border public function __construct($tableStyle = null, $firstRowStyle = null) { $this->alignment = new Alignment(); - if ($tableStyle !== null && is_array($tableStyle)) { - $this->setStyleByArray($tableStyle); - } if ($firstRowStyle !== null && is_array($firstRowStyle)) { $this->firstRow = clone $this; unset($this->firstRow->firstRow); - unset($this->firstRow->cellMarginBottom); + unset($this->firstRow->borderInsideHSize); + unset($this->firstRow->borderInsideHColor); + unset($this->firstRow->borderInsideVSize); + unset($this->firstRow->borderInsideVColor); unset($this->firstRow->cellMarginTop); unset($this->firstRow->cellMarginLeft); unset($this->firstRow->cellMarginRight); - unset($this->firstRow->borderInsideVColor); - unset($this->firstRow->borderInsideVSize); - unset($this->firstRow->borderInsideHColor); - unset($this->firstRow->borderInsideHSize); + unset($this->firstRow->cellMarginBottom); $this->firstRow->setStyleByArray($firstRowStyle); } + + if ($tableStyle !== null && is_array($tableStyle)) { + $this->setStyleByArray($tableStyle); + } } /** diff --git a/tests/PhpWord/Tests/Element/ListItemRunTest.php b/tests/PhpWord/Tests/Element/ListItemRunTest.php index 5b3f72c8..c034a8f8 100644 --- a/tests/PhpWord/Tests/Element/ListItemRunTest.php +++ b/tests/PhpWord/Tests/Element/ListItemRunTest.php @@ -44,7 +44,7 @@ class ListItemRunTest extends \PHPUnit_Framework_TestCase */ public function testConstructString() { - $oListItemRun = new ListItemRun(0, null, null, 'pStyle'); + $oListItemRun = new ListItemRun(0, null, 'pStyle'); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItemRun', $oListItemRun); $this->assertCount(0, $oListItemRun->getElements()); @@ -56,29 +56,22 @@ class ListItemRunTest extends \PHPUnit_Framework_TestCase */ public function testConstructArray() { - $oListItemRun = new ListItemRun(0, null, null, array('spacing' => 100)); + $oListItemRun = new ListItemRun(0, null, array('spacing' => 100)); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItemRun', $oListItemRun); $this->assertCount(0, $oListItemRun->getElements()); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oListItemRun->getParagraphStyle()); } - + /** * Get style */ public function testStyle() { - $oListItemRun = new ListItemRun( - 1, - null, - array('listType' => \PhpOffice\PhpWord\Style\ListItem::TYPE_NUMBER) - ); - + $oListItemRun = new ListItemRun(1, array('listType' => \PhpOffice\PhpWord\Style\ListItem::TYPE_NUMBER)); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\ListItem', $oListItemRun->getStyle()); - $this->assertEquals( - $oListItemRun->getStyle()->getListType(), - \PhpOffice\PhpWord\Style\ListItem::TYPE_NUMBER - ); + $this->assertEquals($oListItemRun->getStyle()->getListType(), \PhpOffice\PhpWord\Style\ListItem::TYPE_NUMBER); } /** * getDepth @@ -87,10 +80,10 @@ class ListItemRunTest extends \PHPUnit_Framework_TestCase { $iVal = rand(1, 1000); $oListItemRun = new ListItemRun($iVal); - + $this->assertEquals($oListItemRun->getDepth(), $iVal); } - + /** * Add text */ diff --git a/tests/PhpWord/Tests/Element/TitleTest.php b/tests/PhpWord/Tests/Element/TitleTest.php index 301d8bec..ca65c8eb 100644 --- a/tests/PhpWord/Tests/Element/TitleTest.php +++ b/tests/PhpWord/Tests/Element/TitleTest.php @@ -47,16 +47,4 @@ class TitleTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oTitle->getStyle(), null); } - - /** - * Get bookmark Id - */ - public function testBookmarkID() - { - $oTitle = new Title('text'); - - $iVal = rand(1, 1000); - $oTitle->setBookmarkId($iVal); - $this->assertEquals($oTitle->getRelationId(), $iVal); - } } From 1c3735fc080645324924a4a2639ea7e8378007ff Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 15 May 2014 11:46:28 +0700 Subject: [PATCH 20/48] Refactor table and font styles to reduce duplication --- .scrutinizer.yml | 1 - src/PhpWord/Style/Font.php | 47 ++++------ src/PhpWord/Style/Table.php | 93 ++++++++++++------- .../Tests/Writer/Word2007/Part/StylesTest.php | 19 ++-- 4 files changed, 85 insertions(+), 75 deletions(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index ded9a39f..a35530a9 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -17,7 +17,6 @@ tools: enabled: true timeout: 900 php_sim: - enabled: true min_mass: 40 php_pdepend: true php_analyzer: true diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index a4d56dd4..f56a4789 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -404,10 +404,7 @@ class Font extends AbstractStyle */ public function setSuperScript($value = true) { - $this->superScript = $this->setBoolVal($value, $this->superScript); - $this->toggleFalse($this->subScript, $this->superScript); - - return $this; + return $this->setPairedProperty($this->superScript, $this->subScript, $value); } /** @@ -428,10 +425,7 @@ class Font extends AbstractStyle */ public function setSubScript($value = true) { - $this->subScript = $this->setBoolVal($value, $this->subScript); - $this->toggleFalse($this->subScript, $this->superScript); - - return $this; + return $this->setPairedProperty($this->subScript, $this->superScript, $value); } /** @@ -452,10 +446,7 @@ class Font extends AbstractStyle */ public function setStrikethrough($value = true) { - $this->strikethrough = $this->setBoolVal($value, $this->strikethrough); - $this->toggleFalse($this->doubleStrikethrough, $this->strikethrough); - - return $this; + return $this->setPairedProperty($this->strikethrough, $this->doubleStrikethrough, $value); } /** @@ -476,10 +467,7 @@ class Font extends AbstractStyle */ public function setDoubleStrikethrough($value = true) { - $this->doubleStrikethrough = $this->setBoolVal($value, $this->doubleStrikethrough); - $this->toggleFalse($this->strikethrough, $this->doubleStrikethrough); - - return $this; + return $this->setPairedProperty($this->doubleStrikethrough, $this->strikethrough, $value); } /** @@ -500,10 +488,7 @@ class Font extends AbstractStyle */ public function setSmallCaps($value = true) { - $this->smallCaps = $this->setBoolVal($value, $this->smallCaps); - $this->toggleFalse($this->allCaps, $this->smallCaps); - - return $this; + return $this->setPairedProperty($this->smallCaps, $this->allCaps, $value); } /** @@ -524,10 +509,7 @@ class Font extends AbstractStyle */ public function setAllCaps($value = true) { - $this->allCaps = $this->setBoolVal($value, $this->allCaps); - $this->toggleFalse($this->smallCaps, $this->allCaps); - - return $this; + return $this->setPairedProperty($this->allCaps, $this->smallCaps, $value); } /** @@ -648,16 +630,21 @@ class Font extends AbstractStyle } /** - * Toggle $target property to false when $source true + * Set $property value and set $pairProperty = false when $value = true * - * @param bool|null $target Target property - * @param bool $sourceValue + * @param bool $property + * @param bool $pair + * @param bool $value + * @return self */ - private function toggleFalse(&$target, $sourceValue) + private function setPairedProperty(&$property, &$pairProperty, $value) { - if ($sourceValue == true) { - $target = false; + $property = $this->setBoolVal($value, $property); + if ($value == true) { + $pairProperty = false; } + + return $this; } /** diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index 4559e677..32821793 100644 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -124,7 +124,8 @@ class Table extends Border { $this->alignment = new Alignment(); - if ($firstRowStyle !== null && is_array($firstRowStyle)) { + // Clone first row from table style, but with certain properties disabled + if ($firstRowStyle !== null && is_array($firstRowStyle)) { $this->firstRow = clone $this; unset($this->firstRow->firstRow); unset($this->firstRow->borderInsideHSize); @@ -257,7 +258,7 @@ class Table extends Border */ public function getBorderInsideHSize() { - return isset($this->borderInsideHSize) ? $this->borderInsideHSize : null; + return $this->getTableOnlyProperty('borderInsideHSize'); } /** @@ -268,9 +269,7 @@ class Table extends Border */ public function setBorderInsideHSize($value = null) { - $this->borderInsideHSize = $this->setNumericVal($value, $this->borderInsideHSize); - - return $this; + return $this->setTableOnlyProperty('borderInsideHSize', $value); } /** @@ -280,7 +279,7 @@ class Table extends Border */ public function getBorderInsideHColor() { - return isset($this->borderInsideHColor) ? $this->borderInsideHColor : null; + return $this->getTableOnlyProperty('borderInsideHColor'); } /** @@ -291,9 +290,7 @@ class Table extends Border */ public function setBorderInsideHColor($value = null) { - $this->borderInsideHColor = $value ; - - return $this; + return $this->setTableOnlyProperty('borderInsideHColor', $value, false); } /** @@ -303,7 +300,7 @@ class Table extends Border */ public function getBorderInsideVSize() { - return isset($this->borderInsideVSize) ? $this->borderInsideVSize : null; + return $this->getTableOnlyProperty('borderInsideVSize'); } /** @@ -314,9 +311,7 @@ class Table extends Border */ public function setBorderInsideVSize($value = null) { - $this->borderInsideVSize = $this->setNumericVal($value, $this->borderInsideVSize); - - return $this; + return $this->setTableOnlyProperty('borderInsideVSize', $value); } /** @@ -326,7 +321,7 @@ class Table extends Border */ public function getBorderInsideVColor() { - return isset($this->borderInsideVColor) ? $this->borderInsideVColor : null; + return $this->getTableOnlyProperty('borderInsideVColor'); } /** @@ -337,9 +332,7 @@ class Table extends Border */ public function setBorderInsideVColor($value = null) { - $this->borderInsideVColor = $value; - - return $this; + return $this->setTableOnlyProperty('borderInsideVColor', $value, false); } /** @@ -349,7 +342,7 @@ class Table extends Border */ public function getCellMarginTop() { - return $this->cellMarginTop; + return $this->getTableOnlyProperty('cellMarginTop'); } /** @@ -360,9 +353,7 @@ class Table extends Border */ public function setCellMarginTop($value = null) { - $this->cellMarginTop = $this->setNumericVal($value, $this->cellMarginTop); - - return $this; + return $this->setTableOnlyProperty('cellMarginTop', $value); } /** @@ -372,7 +363,7 @@ class Table extends Border */ public function getCellMarginLeft() { - return $this->cellMarginLeft; + return $this->getTableOnlyProperty('cellMarginLeft'); } /** @@ -383,9 +374,7 @@ class Table extends Border */ public function setCellMarginLeft($value = null) { - $this->cellMarginLeft = $this->setNumericVal($value, $this->cellMarginLeft); - - return $this; + return $this->setTableOnlyProperty('cellMarginLeft', $value); } /** @@ -395,7 +384,7 @@ class Table extends Border */ public function getCellMarginRight() { - return $this->cellMarginRight; + return $this->getTableOnlyProperty('cellMarginRight'); } /** @@ -406,9 +395,7 @@ class Table extends Border */ public function setCellMarginRight($value = null) { - $this->cellMarginRight = $this->setNumericVal($value, $this->cellMarginRight); - - return $this; + return $this->setTableOnlyProperty('cellMarginRight', $value); } /** @@ -418,7 +405,7 @@ class Table extends Border */ public function getCellMarginBottom() { - return $this->cellMarginBottom; + return $this->getTableOnlyProperty('cellMarginBottom'); } /** @@ -429,9 +416,7 @@ class Table extends Border */ public function setCellMarginBottom($value = null) { - $this->cellMarginBottom = $this->setNumericVal($value, $this->cellMarginBottom); - - return $this; + return $this->setTableOnlyProperty('cellMarginBottom', $value); } /** @@ -569,4 +554,46 @@ class Table extends Border return $this; } + + /** + * Get table style only property by checking if firstRow is set + * + * This is necessary since firstRow style is cloned from table style but + * without certain properties activated, e.g. margins + * + * @param string $property + * @return int|string|null + */ + private function getTableOnlyProperty($property) + { + if (isset($this->firstRow)) { + return $this->$property; + } + + return null; + } + + /** + * Set table style only property by checking if firstRow is set + * + * This is necessary since firstRow style is cloned from table style but + * without certain properties activated, e.g. margins + * + * @param string $property + * @param int|string $value + * @param bool $isNumeric + * @return self + */ + private function setTableOnlyProperty($property, $value, $isNumeric = true) + { + if (isset($this->firstRow)) { + if ($isNumeric) { + $this->$property = $this->setNumericVal($value, $this->$property); + } else { + $this->$property = $value; + } + } + + return $this; + } } diff --git a/tests/PhpWord/Tests/Writer/Word2007/Part/StylesTest.php b/tests/PhpWord/Tests/Writer/Word2007/Part/StylesTest.php index 795d3e8a..103caa81 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/Part/StylesTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/Part/StylesTest.php @@ -49,22 +49,19 @@ class StylesTest extends \PHPUnit_Framework_TestCase $rStyle = array('size' => 20); $tStyle = array( 'bgColor' => 'FF0000', - 'cellMarginTop' => 120, - 'cellMarginBottom' => 120, - 'cellMarginLeft' => 120, - 'cellMarginRight' => 120, - 'borderTopSize' => 120, - 'borderBottomSize' => 120, - 'borderLeftSize' => 120, - 'borderRightSize' => 120, - 'borderInsideHSize' => 120, - 'borderInsideVSize' => 120, + 'cellMargin' => 120, + 'borderSize' => 120, + ); + $firstRowStyle = array( + 'bgColor' => '0000FF', + 'borderSize' => 120, + 'borderColor' => '00FF00', ); $phpWord->setDefaultParagraphStyle($pStyle); $phpWord->addParagraphStyle('Base Style', $pBase); $phpWord->addParagraphStyle('New Style', $pNew); $phpWord->addFontStyle('New Style', $rStyle, $pStyle); - $phpWord->addTableStyle('Table Style', $tStyle, $tStyle); + $phpWord->addTableStyle('Table Style', $tStyle, $firstRowStyle); $phpWord->addTitleStyle(1, $rStyle, $pStyle); $doc = TestHelperDOCX::getDocument($phpWord); From 4d9e4062c34df16cb50d64a2f92089856fbf626c Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 15 May 2014 14:41:08 +0700 Subject: [PATCH 21/48] QA: Scrutinizer dedup --- src/PhpWord/Reader/AbstractReader.php | 24 ++++---- src/PhpWord/Reader/ODText/AbstractPart.php | 3 +- src/PhpWord/Reader/ReaderInterface.php | 8 +-- src/PhpWord/Style/Font.php | 2 +- src/PhpWord/Style/Table.php | 46 +++++++++------ src/PhpWord/Writer/AbstractWriter.php | 2 +- src/PhpWord/Writer/HTML.php | 14 ++--- src/PhpWord/Writer/HTML/Element/Container.php | 4 +- src/PhpWord/Writer/HTML/Element/Image.php | 5 +- src/PhpWord/Writer/HTML/Style/Font.php | 2 +- src/PhpWord/Writer/ODText.php | 4 -- .../Writer/ODText/Element/AbstractElement.php | 4 +- .../Writer/ODText/Element/Container.php | 4 +- .../Writer/ODText/Part/AbstractPart.php | 3 +- .../Writer/ODText/Style/AbstractStyle.php | 4 +- src/PhpWord/Writer/PDF/AbstractRenderer.php | 23 ++++---- src/PhpWord/Writer/PDF/DomPDF.php | 9 +-- src/PhpWord/Writer/RTF.php | 58 ++++++------------- .../Writer/RTF/Element/AbstractElement.php | 3 +- src/PhpWord/Writer/RTF/Element/Container.php | 4 +- .../Writer/RTF/Style/AbstractStyle.php | 4 +- src/PhpWord/Writer/RTF/Style/Font.php | 2 +- src/PhpWord/Writer/Word2007.php | 10 ++-- .../Writer/Word2007/Element/Container.php | 3 +- src/PhpWord/Writer/Word2007/Element/Image.php | 2 +- src/PhpWord/Writer/Word2007/Element/TOC.php | 2 +- src/PhpWord/Writer/Word2007/Element/Table.php | 2 +- src/PhpWord/Writer/Word2007/Style/Cell.php | 2 +- src/PhpWord/Writer/Word2007/Style/Image.php | 4 +- src/PhpWord/Writer/Word2007/Style/Section.php | 2 +- src/PhpWord/Writer/Word2007/Style/Table.php | 4 +- src/PhpWord/Writer/Word2007/Style/TextBox.php | 6 +- src/PhpWord/Writer/WriterInterface.php | 4 +- tests/PhpWord/Tests/Writer/ODTextTest.php | 12 ---- tests/PhpWord/Tests/Writer/RTFTest.php | 12 ---- tests/PhpWord/Tests/Writer/Word2007Test.php | 12 ---- 36 files changed, 136 insertions(+), 173 deletions(-) diff --git a/src/PhpWord/Reader/AbstractReader.php b/src/PhpWord/Reader/AbstractReader.php index 73d18666..a243c5d2 100644 --- a/src/PhpWord/Reader/AbstractReader.php +++ b/src/PhpWord/Reader/AbstractReader.php @@ -54,47 +54,47 @@ abstract class AbstractReader implements ReaderInterface /** * Set read data only * - * @param bool $pValue + * @param bool $value * @return self */ - public function setReadDataOnly($pValue = true) + public function setReadDataOnly($value = true) { - $this->readDataOnly = $pValue; + $this->readDataOnly = $value; return $this; } /** * Open file for reading * - * @param string $pFilename + * @param string $filename * @return resource * @throws \PhpOffice\PhpWord\Exception\Exception */ - protected function openFile($pFilename) + protected function openFile($filename) { // Check if file exists - if (!file_exists($pFilename) || !is_readable($pFilename)) { - throw new Exception("Could not open " . $pFilename . " for reading! File does not exist."); + if (!file_exists($filename) || !is_readable($filename)) { + throw new Exception("Could not open " . $filename . " for reading! File does not exist."); } // Open file - $this->fileHandle = fopen($pFilename, 'r'); + $this->fileHandle = fopen($filename, 'r'); if ($this->fileHandle === false) { - throw new Exception("Could not open file " . $pFilename . " for reading."); + throw new Exception("Could not open file " . $filename . " for reading."); } } /** * Can the current ReaderInterface read the file? * - * @param string $pFilename + * @param string $filename * @return bool */ - public function canRead($pFilename) + public function canRead($filename) { // Check if file exists try { - $this->openFile($pFilename); + $this->openFile($filename); } catch (Exception $e) { return false; } diff --git a/src/PhpWord/Reader/ODText/AbstractPart.php b/src/PhpWord/Reader/ODText/AbstractPart.php index 44884922..7a91e12d 100644 --- a/src/PhpWord/Reader/ODText/AbstractPart.php +++ b/src/PhpWord/Reader/ODText/AbstractPart.php @@ -18,11 +18,12 @@ namespace PhpOffice\PhpWord\Reader\ODText; use PhpOffice\PhpWord\Shared\XMLReader; +use PhpOffice\PhpWord\Reader\Word2007\AbstractPart as Word2007AbstractPart; /** * Abstract part reader */ -abstract class AbstractPart extends \PhpOffice\PhpWord\Reader\Word2007\AbstractPart +abstract class AbstractPart extends Word2007AbstractPart { /** * Read w:r (override) diff --git a/src/PhpWord/Reader/ReaderInterface.php b/src/PhpWord/Reader/ReaderInterface.php index f207aa6f..df663197 100644 --- a/src/PhpWord/Reader/ReaderInterface.php +++ b/src/PhpWord/Reader/ReaderInterface.php @@ -25,15 +25,15 @@ interface ReaderInterface /** * Can the current ReaderInterface read the file? * - * @param string $pFilename + * @param string $filename * @return boolean */ - public function canRead($pFilename); + public function canRead($filename); /** * Loads PhpWord from file * - * @param string $pFilename + * @param string $filename */ - public function load($pFilename); + public function load($filename); } diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index f56a4789..5440f512 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -633,7 +633,7 @@ class Font extends AbstractStyle * Set $property value and set $pairProperty = false when $value = true * * @param bool $property - * @param bool $pair + * @param bool $pairProperty * @param bool $value * @return self */ diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index 32821793..90e8282f 100644 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -29,12 +29,19 @@ class Table extends Border const WIDTH_PERCENT = 'pct'; // Width in fiftieths (1/50) of a percent (1% = 50 unit) const WIDTH_TWIP = 'dxa'; // Width in twentieths (1/20) of a point (twip) + /** + * Is this a first row style? + * + * @var bool + */ + private $isFirstRow = false; + /** * Style for first row * * @var \PhpOffice\PhpWord\Style\Table */ - private $firstRow; + private $firstRowStyle; /** * Cell margin top @@ -126,17 +133,18 @@ class Table extends Border // Clone first row from table style, but with certain properties disabled if ($firstRowStyle !== null && is_array($firstRowStyle)) { - $this->firstRow = clone $this; - unset($this->firstRow->firstRow); - unset($this->firstRow->borderInsideHSize); - unset($this->firstRow->borderInsideHColor); - unset($this->firstRow->borderInsideVSize); - unset($this->firstRow->borderInsideVColor); - unset($this->firstRow->cellMarginTop); - unset($this->firstRow->cellMarginLeft); - unset($this->firstRow->cellMarginRight); - unset($this->firstRow->cellMarginBottom); - $this->firstRow->setStyleByArray($firstRowStyle); + $this->firstRowStyle = clone $this; + $this->firstRowStyle->isFirstRow = true; + unset($this->firstRowStyle->firstRowStyle); + unset($this->firstRowStyle->borderInsideHSize); + unset($this->firstRowStyle->borderInsideHColor); + unset($this->firstRowStyle->borderInsideVSize); + unset($this->firstRowStyle->borderInsideVColor); + unset($this->firstRowStyle->cellMarginTop); + unset($this->firstRowStyle->cellMarginLeft); + unset($this->firstRowStyle->cellMarginRight); + unset($this->firstRowStyle->cellMarginBottom); + $this->firstRowStyle->setStyleByArray($firstRowStyle); } if ($tableStyle !== null && is_array($tableStyle)) { @@ -145,13 +153,13 @@ class Table extends Border } /** - * Get First Row Style + * Set first row * * @return \PhpOffice\PhpWord\Style\Table */ public function getFirstRow() { - return $this->firstRow; + return $this->firstRowStyle; } /** @@ -556,7 +564,7 @@ class Table extends Border } /** - * Get table style only property by checking if firstRow is set + * Get table style only property by checking if it's a firstRow * * This is necessary since firstRow style is cloned from table style but * without certain properties activated, e.g. margins @@ -566,7 +574,7 @@ class Table extends Border */ private function getTableOnlyProperty($property) { - if (isset($this->firstRow)) { + if ($this->isFirstRow === false) { return $this->$property; } @@ -574,7 +582,7 @@ class Table extends Border } /** - * Set table style only property by checking if firstRow is set + * Set table style only property by checking if it's a firstRow * * This is necessary since firstRow style is cloned from table style but * without certain properties activated, e.g. margins @@ -586,8 +594,8 @@ class Table extends Border */ private function setTableOnlyProperty($property, $value, $isNumeric = true) { - if (isset($this->firstRow)) { - if ($isNumeric) { + if ($this->isFirstRow === false) { + if ($isNumeric === true) { $this->$property = $this->setNumericVal($value, $this->$property); } else { $this->$property = $value; diff --git a/src/PhpWord/Writer/AbstractWriter.php b/src/PhpWord/Writer/AbstractWriter.php index bc1c1bf9..628dd045 100644 --- a/src/PhpWord/Writer/AbstractWriter.php +++ b/src/PhpWord/Writer/AbstractWriter.php @@ -335,7 +335,7 @@ abstract class AbstractWriter implements WriterInterface $source = substr($source, 6); list($zipFilename, $imageFilename) = explode('#', $source); - $zipClass = \PhpOffice\PhpWord\Settings::getZipClass(); + $zipClass = Settings::getZipClass(); $zip = new $zipClass(); if ($zip->open($zipFilename) !== false) { if ($zip->locateName($imageFilename)) { diff --git a/src/PhpWord/Writer/HTML.php b/src/PhpWord/Writer/HTML.php index 6c1a534f..db1723aa 100644 --- a/src/PhpWord/Writer/HTML.php +++ b/src/PhpWord/Writer/HTML.php @@ -62,14 +62,10 @@ class HTML extends AbstractWriter implements WriterInterface * Save PhpWord to file * * @param string $filename - * @throws Exception + * @throws \PhpOffice\PhpWord\Exception\Exception */ public function save($filename = null) { - if (is_null($this->phpWord)) { - throw new Exception('PhpWord object unassigned.'); - } - $this->setTempDir(sys_get_temp_dir() . '/PHPWordWriter/'); $hFile = fopen($filename, 'w'); if ($hFile !== false) { @@ -111,7 +107,8 @@ class HTML extends AbstractWriter implements WriterInterface */ private function writeHead() { - $properties = $this->getPhpWord()->getDocumentProperties(); + $phpWord = $this->getPhpWord(); + $properties = $phpWord->getDocumentProperties(); $propertiesMapping = array( 'creator' => 'author', 'title' => '', @@ -171,13 +168,14 @@ class HTML extends AbstractWriter implements WriterInterface */ private function writeStyles() { + $phpWord = $this->getPhpWord(); $css = '