From c28f28ea25cd626cd5560d475f377134b2aa70cc Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Tue, 6 May 2014 21:22:24 +0700 Subject: [PATCH] Refactoring for code quality improvement (based on Scrutinizer) --- src/PhpWord/Element/AbstractContainer.php | 8 +- src/PhpWord/Element/Title.php | 2 +- src/PhpWord/Style/TOC.php | 4 +- src/PhpWord/Template.php | 2 +- src/PhpWord/Writer/RTF/Element/Text.php | 109 ++++++++----- src/PhpWord/Writer/Word2007/Element/Table.php | 105 ++++++------ .../Writer/Word2007/Part/Numbering.php | 149 +++++++++--------- src/PhpWord/Writer/Word2007/Part/Rels.php | 2 +- .../Writer/Word2007/Style/AbstractStyle.php | 23 +++ src/PhpWord/Writer/Word2007/Style/Font.php | 81 +++------- src/PhpWord/Writer/Word2007/Style/Image.php | 14 +- .../Writer/Word2007/Style/Paragraph.php | 48 ++---- 12 files changed, 282 insertions(+), 265 deletions(-) diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 7de4a428..4ac6c22b 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -39,8 +39,8 @@ abstract class AbstractContainer extends AbstractElement */ protected function addElement(AbstractElement $element) { - $type = get_class($element); - $type = str_replace('PhpOffice\\PhpWord\\Element\\', '', $type); + // $type = get_class($element); + // $type = str_replace('PhpOffice\\PhpWord\\Element\\', '', $type); $element->setElementIndex($this->countElements() + 1); $element->setElementId(); $element->setPhpWord($this->phpWord); @@ -254,11 +254,11 @@ abstract class AbstractContainer extends AbstractElement $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); } - $element->setDocPart($docPart, $this->getDocPartId()); - $element->setRelationId($rId); $this->addElement($element); return $element; diff --git a/src/PhpWord/Element/Title.php b/src/PhpWord/Element/Title.php index 481f061b..a2dbba8b 100644 --- a/src/PhpWord/Element/Title.php +++ b/src/PhpWord/Element/Title.php @@ -138,7 +138,7 @@ class Title extends AbstractElement */ public function setAnchor($anchor) { - $this->anchor = $anchor; + $anchor = null; } /** diff --git a/src/PhpWord/Style/TOC.php b/src/PhpWord/Style/TOC.php index 13a70294..6ec4318f 100644 --- a/src/PhpWord/Style/TOC.php +++ b/src/PhpWord/Style/TOC.php @@ -45,7 +45,7 @@ class TOC extends Tab */ public function __construct() { - parent::__construct(self::TAB_STOP_RIGHT, 9062, self::TABLEADER_DOT); + parent::__construct(self::TAB_STOP_RIGHT, 9062, self::TAB_LEADER_DOT); } /** @@ -83,7 +83,7 @@ class TOC extends Tab * * @param string $value */ - public function setTabLeader($value = self::TABLEADER_DOT) + public function setTabLeader($value = self::TAB_LEADER_DOT) { $this->setLeader($value); } diff --git a/src/PhpWord/Template.php b/src/PhpWord/Template.php index faf270f1..68550cfe 100644 --- a/src/PhpWord/Template.php +++ b/src/PhpWord/Template.php @@ -192,7 +192,7 @@ class Template // Check if there's a cell spanning multiple rows. if (preg_match('##', $xmlRow)) { - $extraRowStart = $rowEnd; + // $extraRowStart = $rowEnd; $extraRowEnd = $rowEnd; while (true) { $extraRowStart = $this->findRowStart($extraRowEnd + 1); diff --git a/src/PhpWord/Writer/RTF/Element/Text.php b/src/PhpWord/Writer/RTF/Element/Text.php index 56c922a4..83ade9d2 100644 --- a/src/PhpWord/Writer/RTF/Element/Text.php +++ b/src/PhpWord/Writer/RTF/Element/Text.php @@ -18,7 +18,7 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Style\Font; +use PhpOffice\PhpWord\Style\Font as FontStyle; use PhpOffice\PhpWord\Style; /** @@ -64,56 +64,81 @@ class Text extends Element $this->parentWriter->setLastParagraphStyle(); } - if ($fontStyle instanceof Font) { - if ($fontStyle->getColor() != null) { - $idxColor = array_search($fontStyle->getColor(), $this->parentWriter->getColorTable()); - if ($idxColor !== false) { - $rtfText .= '\cf' . ($idxColor + 1); - } - } else { - $rtfText .= '\cf0'; - } - if ($fontStyle->getName() != null) { - $idxFont = array_search($fontStyle->getName(), $this->parentWriter->getFontTable()); - if ($idxFont !== false) { - $rtfText .= '\f' . $idxFont; - } - } else { - $rtfText .= '\f0'; - } - if ($fontStyle->isBold()) { - $rtfText .= '\b'; - } - if ($fontStyle->isItalic()) { - $rtfText .= '\i'; - } - if ($fontStyle->getSize()) { - $rtfText .= '\fs' . ($fontStyle->getSize() * 2); - } + if ($fontStyle instanceof FontStyle) { + $rtfText .= $this->writeFontStyleBegin($fontStyle); } if ($this->parentWriter->getLastParagraphStyle() != '' || $fontStyle) { $rtfText .= ' '; } $rtfText .= $this->element->getText(); - - if ($fontStyle instanceof Font) { - $rtfText .= '\cf0'; - $rtfText .= '\f0'; - - if ($fontStyle->isBold()) { - $rtfText .= '\b0'; - } - if ($fontStyle->isItalic()) { - $rtfText .= '\i0'; - } - if ($fontStyle->getSize()) { - $rtfText .= '\fs' . (PhpWord::DEFAULT_FONT_SIZE * 2); - } + if ($fontStyle instanceof FontStyle) { + $rtfText .= $this->writeFontStyleEnd($fontStyle); } - if (!$this->withoutP) { $rtfText .= '\par' . PHP_EOL; } + + return $rtfText; + } + + /** + * Write font style beginning + * + * @return string + */ + private function writeFontStyleBegin(FontStyle $style) + { + $rtfText = ''; + if ($style->getColor() != null) { + $idxColor = array_search($style->getColor(), $this->parentWriter->getColorTable()); + if ($idxColor !== false) { + $rtfText .= '\cf' . ($idxColor + 1); + } + } else { + $rtfText .= '\cf0'; + } + if ($style->getName() != null) { + $idxFont = array_search($style->getName(), $this->parentWriter->getFontTable()); + if ($idxFont !== false) { + $rtfText .= '\f' . $idxFont; + } + } else { + $rtfText .= '\f0'; + } + if ($style->isBold()) { + $rtfText .= '\b'; + } + if ($style->isItalic()) { + $rtfText .= '\i'; + } + if ($style->getSize()) { + $rtfText .= '\fs' . ($style->getSize() * 2); + } + + return $rtfText; + } + + /** + * Write font style ending + * + * @return string + */ + private function writeFontStyleEnd(FontStyle $style) + { + $rtfText = ''; + $rtfText .= '\cf0'; + $rtfText .= '\f0'; + + if ($style->isBold()) { + $rtfText .= '\b0'; + } + if ($style->isItalic()) { + $rtfText .= '\i0'; + } + if ($style->getSize()) { + $rtfText .= '\fs' . (PhpWord::DEFAULT_FONT_SIZE * 2); + } + return $rtfText; } } diff --git a/src/PhpWord/Writer/Word2007/Element/Table.php b/src/PhpWord/Writer/Word2007/Element/Table.php index c8c65816..07863979 100644 --- a/src/PhpWord/Writer/Word2007/Element/Table.php +++ b/src/PhpWord/Writer/Word2007/Element/Table.php @@ -17,7 +17,9 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; -use PhpOffice\PhpWord\Style\Cell; +use PhpOffice\PhpWord\Element\Cell as CellElement; +use PhpOffice\PhpWord\Element\Row as RowElement; +use PhpOffice\PhpWord\Style\Cell as CellStyle; use PhpOffice\PhpWord\Style\Table as TableStyle; use PhpOffice\PhpWord\Writer\Word2007\Style\Cell as CellStyleWriter; use PhpOffice\PhpWord\Writer\Word2007\Style\Table as TableStyleWriter; @@ -89,51 +91,66 @@ class Table extends Element // Table rows for ($i = 0; $i < $rowCount; $i++) { - $row = $rows[$i]; - $height = $row->getHeight(); - $rowStyle = $row->getStyle(); - - $this->xmlWriter->startElement('w:tr'); - if (!is_null($height) || $rowStyle->isTblHeader() || $rowStyle->isCantSplit()) { - $this->xmlWriter->startElement('w:trPr'); - if (!is_null($height)) { - $this->xmlWriter->startElement('w:trHeight'); - $this->xmlWriter->writeAttribute('w:val', $height); - $this->xmlWriter->writeAttribute('w:hRule', ($rowStyle->isExactHeight() ? 'exact' : 'atLeast')); - $this->xmlWriter->endElement(); - } - if ($rowStyle->isTblHeader()) { - $this->xmlWriter->startElement('w:tblHeader'); - $this->xmlWriter->writeAttribute('w:val', '1'); - $this->xmlWriter->endElement(); - } - if ($rowStyle->isCantSplit()) { - $this->xmlWriter->startElement('w:cantSplit'); - $this->xmlWriter->writeAttribute('w:val', '1'); - $this->xmlWriter->endElement(); - } - $this->xmlWriter->endElement(); - } - foreach ($row->getCells() as $cell) { - $cellStyle = $cell->getStyle(); - $width = $cell->getWidth(); - $this->xmlWriter->startElement('w:tc'); - $this->xmlWriter->startElement('w:tcPr'); - $this->xmlWriter->startElement('w:tcW'); - $this->xmlWriter->writeAttribute('w:w', $width); - $this->xmlWriter->writeAttribute('w:type', 'dxa'); - $this->xmlWriter->endElement(); // w:tcW - if ($cellStyle instanceof Cell) { - $styleWriter = new CellStyleWriter($this->xmlWriter, $cellStyle); - $styleWriter->write(); - } - $this->xmlWriter->endElement(); // w:tcPr - $this->parentWriter->writeContainerElements($this->xmlWriter, $cell); - $this->xmlWriter->endElement(); // w:tc - } - $this->xmlWriter->endElement(); // w:tr + $this->writeRow($rows[$i]); } $this->xmlWriter->endElement(); } } + + /** + * Write row + */ + private function writeRow(RowElement $row) + { + $height = $row->getHeight(); + $rowStyle = $row->getStyle(); + + $this->xmlWriter->startElement('w:tr'); + if (!is_null($height) || $rowStyle->isTblHeader() || $rowStyle->isCantSplit()) { + $this->xmlWriter->startElement('w:trPr'); + if (!is_null($height)) { + $this->xmlWriter->startElement('w:trHeight'); + $this->xmlWriter->writeAttribute('w:val', $height); + $this->xmlWriter->writeAttribute('w:hRule', ($rowStyle->isExactHeight() ? 'exact' : 'atLeast')); + $this->xmlWriter->endElement(); + } + if ($rowStyle->isTblHeader()) { + $this->xmlWriter->startElement('w:tblHeader'); + $this->xmlWriter->writeAttribute('w:val', '1'); + $this->xmlWriter->endElement(); + } + if ($rowStyle->isCantSplit()) { + $this->xmlWriter->startElement('w:cantSplit'); + $this->xmlWriter->writeAttribute('w:val', '1'); + $this->xmlWriter->endElement(); + } + $this->xmlWriter->endElement(); + } + foreach ($row->getCells() as $cell) { + $this->writeCell($cell); + } + $this->xmlWriter->endElement(); // w:tr + } + + /** + * Write cell + */ + private function writeCell(CellElement $cell) + { + $cellStyle = $cell->getStyle(); + + $this->xmlWriter->startElement('w:tc'); + $this->xmlWriter->startElement('w:tcPr'); + $this->xmlWriter->startElement('w:tcW'); + $this->xmlWriter->writeAttribute('w:w', $cell->getWidth()); + $this->xmlWriter->writeAttribute('w:type', 'dxa'); + $this->xmlWriter->endElement(); // w:tcW + if ($cellStyle instanceof CellStyle) { + $styleWriter = new CellStyleWriter($this->xmlWriter, $cellStyle); + $styleWriter->write(); + } + $this->xmlWriter->endElement(); // w:tcPr + $this->parentWriter->writeContainerElements($this->xmlWriter, $cell); + $this->xmlWriter->endElement(); // w:tc + } } diff --git a/src/PhpWord/Writer/Word2007/Part/Numbering.php b/src/PhpWord/Writer/Word2007/Part/Numbering.php index 6f042b4a..2678ac55 100644 --- a/src/PhpWord/Writer/Word2007/Part/Numbering.php +++ b/src/PhpWord/Writer/Word2007/Part/Numbering.php @@ -17,9 +17,10 @@ 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 @@ -66,76 +67,8 @@ class Numbering extends AbstractPart $xmlWriter->endElement(); // w:multiLevelType if (is_array($levels)) { - foreach ($levels as $levelNum => $levelObject) { - if ($levelObject instanceof NumberingLevel) { - $tabPos = $levelObject->getTabPos(); - $left = $levelObject->getLeft(); - $hanging = $levelObject->getHanging(); - $font = $levelObject->getFont(); - $hint = $levelObject->getHint(); - - $xmlWriter->startElement('w:lvl'); - $xmlWriter->writeAttribute('w:ilvl', $levelNum); - - // Numbering level properties - $properties = array( - 'start' => 'start', - 'format' => 'numFmt', - 'restart' => 'lvlRestart', - 'suffix' => 'suff', - 'text' => 'lvlText', - 'align' => 'lvlJc' - ); - foreach ($properties as $property => $nodeName) { - $getMethod = "get{$property}"; - if (!is_null($levelObject->$getMethod())) { - $xmlWriter->startElement("w:{$nodeName}"); - $xmlWriter->writeAttribute('w:val', $levelObject->$getMethod()); - $xmlWriter->endElement(); // w:start - } - } - - // Paragraph styles - if (!is_null($tabPos) || !is_null($left) || !is_null($hanging)) { - $xmlWriter->startElement('w:pPr'); - if (!is_null($tabPos)) { - $xmlWriter->startElement('w:tabs'); - $xmlWriter->startElement('w:tab'); - $xmlWriter->writeAttribute('w:val', 'num'); - $xmlWriter->writeAttribute('w:pos', $tabPos); - $xmlWriter->endElement(); // w:tab - $xmlWriter->endElement(); // w:tabs - } - if (!is_null($left) || !is_null($hanging)) { - $xmlWriter->startElement('w:ind'); - if (!is_null($left)) { - $xmlWriter->writeAttribute('w:left', $left); - } - if (!is_null($hanging)) { - $xmlWriter->writeAttribute('w:hanging', $hanging); - } - $xmlWriter->endElement(); // w:ind - } - $xmlWriter->endElement(); // w:pPr - } - - // Font styles - if (!is_null($font) || !is_null($hint)) { - $xmlWriter->startElement('w:rPr'); - $xmlWriter->startElement('w:rFonts'); - if (!is_null($font)) { - $xmlWriter->writeAttribute('w:ascii', $font); - $xmlWriter->writeAttribute('w:hAnsi', $font); - $xmlWriter->writeAttribute('w:cs', $font); - } - if (!is_null($hint)) { - $xmlWriter->writeAttribute('w:hint', $hint); - } - $xmlWriter->endElement(); // w:rFonts - $xmlWriter->endElement(); // w:rPr - } - $xmlWriter->endElement(); // w:lvl - } + foreach ($levels as $level) { + $this->writeLevel($xmlWriter, $level); } } $xmlWriter->endElement(); // w:abstractNum @@ -159,6 +92,80 @@ class Numbering extends AbstractPart return $xmlWriter->getData(); } + /** + * Write level + */ + private function writeLevel(XMLWriter $xmlWriter, NumberingLevel $level) + { + $tabPos = $level->getTabPos(); + $left = $level->getLeft(); + $hanging = $level->getHanging(); + $font = $level->getFont(); + $hint = $level->getHint(); + + $xmlWriter->startElement('w:lvl'); + $xmlWriter->writeAttribute('w:ilvl', $level->getLevel()); + + // Numbering level properties + $properties = array( + 'start' => 'start', + 'format' => 'numFmt', + 'restart' => 'lvlRestart', + 'suffix' => 'suff', + 'text' => 'lvlText', + 'align' => 'lvlJc' + ); + foreach ($properties as $property => $nodeName) { + $getMethod = "get{$property}"; + if (!is_null($level->$getMethod())) { + $xmlWriter->startElement("w:{$nodeName}"); + $xmlWriter->writeAttribute('w:val', $level->$getMethod()); + $xmlWriter->endElement(); // w:start + } + } + + // Paragraph styles + if (!is_null($tabPos) || !is_null($left) || !is_null($hanging)) { + $xmlWriter->startElement('w:pPr'); + if (!is_null($tabPos)) { + $xmlWriter->startElement('w:tabs'); + $xmlWriter->startElement('w:tab'); + $xmlWriter->writeAttribute('w:val', 'num'); + $xmlWriter->writeAttribute('w:pos', $tabPos); + $xmlWriter->endElement(); // w:tab + $xmlWriter->endElement(); // w:tabs + } + if (!is_null($left) || !is_null($hanging)) { + $xmlWriter->startElement('w:ind'); + if (!is_null($left)) { + $xmlWriter->writeAttribute('w:left', $left); + } + if (!is_null($hanging)) { + $xmlWriter->writeAttribute('w:hanging', $hanging); + } + $xmlWriter->endElement(); // w:ind + } + $xmlWriter->endElement(); // w:pPr + } + + // Font styles + if (!is_null($font) || !is_null($hint)) { + $xmlWriter->startElement('w:rPr'); + $xmlWriter->startElement('w:rFonts'); + if (!is_null($font)) { + $xmlWriter->writeAttribute('w:ascii', $font); + $xmlWriter->writeAttribute('w:hAnsi', $font); + $xmlWriter->writeAttribute('w:cs', $font); + } + if (!is_null($hint)) { + $xmlWriter->writeAttribute('w:hint', $hint); + } + $xmlWriter->endElement(); // w:rFonts + $xmlWriter->endElement(); // w:rPr + } + $xmlWriter->endElement(); // w:lvl + } + /** * Get random hexadecimal number value * diff --git a/src/PhpWord/Writer/Word2007/Part/Rels.php b/src/PhpWord/Writer/Word2007/Part/Rels.php index 20076c4e..22480ce2 100644 --- a/src/PhpWord/Writer/Word2007/Part/Rels.php +++ b/src/PhpWord/Writer/Word2007/Part/Rels.php @@ -78,10 +78,10 @@ class Rels extends AbstractPart foreach ($mediaRels as $mediaRel) { $mediaType = $mediaRel['type']; $type = array_key_exists($mediaType, $mapping) ? $mapping[$mediaType] : $mediaType; - $type = "officeDocument/2006/relationships/{$type}"; $target = array_key_exists($mediaType, $targetPaths) ? $targetPaths[$mediaType] : ''; $target .= $mediaRel['target']; $targetMode = ($type == 'hyperlink') ? 'External' : ''; + $type = "officeDocument/2006/relationships/{$type}"; $this->writeRel($xmlWriter, $relId++, $type, $target, $targetMode); } diff --git a/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php b/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php index 397adae7..41bbed3d 100644 --- a/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php +++ b/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php @@ -73,4 +73,27 @@ abstract class AbstractStyle return $value * $unit; } } + + /** + * Write element when ... + * + * @param bool $condition + * @param string $element + * @param string $attribute + * @param string $value + */ + protected function writeElementIf($condition, $element, $attribute = null, $value = null) + { + if (!$condition) { + return; + } + + if (is_null($attribute)) { + $this->xmlWriter->writeElement($element, $value); + } else { + $this->xmlWriter->startElement($element); + $this->xmlWriter->writeAttribute($attribute, $value); + $this->xmlWriter->endElement(); + } + } } diff --git a/src/PhpWord/Writer/Word2007/Style/Font.php b/src/PhpWord/Writer/Word2007/Style/Font.php index cd235825..b127da31 100644 --- a/src/PhpWord/Writer/Word2007/Style/Font.php +++ b/src/PhpWord/Writer/Word2007/Style/Font.php @@ -62,6 +62,8 @@ class Font extends AbstractStyle $font = $this->style->getName(); $color = $this->style->getColor(); $size = $this->style->getSize(); + $underline = $this->style->getUnderline(); + $fgColor = $this->style->getFgColor(); $this->xmlWriter->startElement('w:rPr'); @@ -80,56 +82,32 @@ class Font extends AbstractStyle } // Color - if ($color != PhpWord::DEFAULT_FONT_COLOR) { - $this->xmlWriter->startElement('w:color'); - $this->xmlWriter->writeAttribute('w:val', $color); - $this->xmlWriter->endElement(); - } + $this->writeElementIf($color != PhpWord::DEFAULT_FONT_COLOR, 'w:color', 'w:val', $color); + $this->writeElementIf($size != PhpWord::DEFAULT_FONT_SIZE, 'w:sz', 'w:val', $size * 2); + $this->writeElementIf($size != PhpWord::DEFAULT_FONT_SIZE, 'w:szCs', 'w:val', $size * 2); - // Size - if ($size != PhpWord::DEFAULT_FONT_SIZE) { - $this->xmlWriter->startElement('w:sz'); - $this->xmlWriter->writeAttribute('w:val', $size * 2); - $this->xmlWriter->endElement(); - $this->xmlWriter->startElement('w:szCs'); - $this->xmlWriter->writeAttribute('w:val', $size * 2); - $this->xmlWriter->endElement(); - } + // Bold, italic + $this->writeElementIf($this->style->isBold(), 'w:b'); + $this->writeElementIf($this->style->isItalic(), 'w:i'); + $this->writeElementIf($this->style->isItalic(), 'w:iCs'); - // Bold - if ($this->style->isBold()) { - $this->xmlWriter->writeElement('w:b', null); - } + // Strikethrough, double strikethrough + $this->writeElementIf($this->style->isStrikethrough(), 'w:strike'); + $this->writeElementIf($this->style->isDoubleStrikethrough(), 'w:dstrike'); - // Italic - if ($this->style->isItalic()) { - $this->xmlWriter->writeElement('w:i', null); - $this->xmlWriter->writeElement('w:iCs', null); - } + // Small caps, all caps + $this->writeElementIf($this->style->isSmallCaps(), 'w:smallCaps'); + $this->writeElementIf($this->style->isAllCaps(), 'w:caps'); // Underline - if ($this->style->getUnderline() != 'none') { - $this->xmlWriter->startElement('w:u'); - $this->xmlWriter->writeAttribute('w:val', $this->style->getUnderline()); - $this->xmlWriter->endElement(); - } - - // Strikethrough - if ($this->style->isStrikethrough()) { - $this->xmlWriter->writeElement('w:strike', null); - } - - // Double strikethrough - if ($this->style->isDoubleStrikethrough()) { - $this->xmlWriter->writeElement('w:dstrike', null); - } + $this->writeElementIf($underline != 'none', 'w:u', 'w:val', $underline); // Foreground-Color - if (!is_null($this->style->getFgColor())) { - $this->xmlWriter->startElement('w:highlight'); - $this->xmlWriter->writeAttribute('w:val', $this->style->getFgColor()); - $this->xmlWriter->endElement(); - } + $this->writeElementIf(!is_null($fgColor), 'w:highlight', 'w:val', $fgColor); + + // Superscript/subscript + $this->writeElementIf($this->style->isSuperScript(), 'w:vertAlign', 'w:val', 'superscript'); + $this->writeElementIf($this->style->isSubScript(), 'w:vertAlign', 'w:val', 'subscript'); // Background-Color if (!is_null($this->style->getShading())) { @@ -137,23 +115,6 @@ class Font extends AbstractStyle $styleWriter->write(); } - // Superscript/subscript - if ($this->style->isSuperScript() || $this->style->isSubScript()) { - $this->xmlWriter->startElement('w:vertAlign'); - $this->xmlWriter->writeAttribute('w:val', $this->style->isSuperScript() ? 'superscript' : 'subscript'); - $this->xmlWriter->endElement(); - } - - // Small caps - if ($this->style->isSmallCaps()) { - $this->xmlWriter->writeElement('w:smallCaps', null); - } - - // All caps - if ($this->style->isAllCaps()) { - $this->xmlWriter->writeElement('w:caps', null); - } - $this->xmlWriter->endElement(); } diff --git a/src/PhpWord/Writer/Word2007/Style/Image.php b/src/PhpWord/Writer/Word2007/Style/Image.php index a048da3f..b5e23e9a 100644 --- a/src/PhpWord/Writer/Word2007/Style/Image.php +++ b/src/PhpWord/Writer/Word2007/Style/Image.php @@ -38,7 +38,7 @@ class Image extends AbstractStyle */ public function write() { - if (!($this->style instanceof \PhpOffice\PhpWord\Style\Image)) { + if (!$this->style instanceof \PhpOffice\PhpWord\Style\Image) { return; } @@ -52,7 +52,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($this->style)); // Absolute/relative positioning $styleArray['position'] = $positioning; @@ -110,14 +110,14 @@ class Image extends AbstractStyle * * @return array */ - private function getElementStyle() + private function getElementStyle(ImageStyle $style) { $styles = array(); $styleValues = array( - 'width' => $this->style->getWidth(), - 'height' => $this->style->getHeight(), - 'margin-top' => $this->style->getMarginTop(), - 'margin-left' => $this->style->getMarginLeft() + 'width' => $style->getWidth(), + 'height' => $style->getHeight(), + 'margin-top' => $style->getMarginTop(), + 'margin-left' => $style->getMarginLeft() ); foreach ($styleValues as $key => $value) { if (!is_null($value) && $value != '') { diff --git a/src/PhpWord/Writer/Word2007/Style/Paragraph.php b/src/PhpWord/Writer/Word2007/Style/Paragraph.php index 7398ad60..af66abed 100644 --- a/src/PhpWord/Writer/Word2007/Style/Paragraph.php +++ b/src/PhpWord/Writer/Word2007/Style/Paragraph.php @@ -68,53 +68,37 @@ class Paragraph extends AbstractStyle return; } + $align = $this->style->getAlign(); + $indentation = $this->style->getIndentation(); + $spacing = $this->style->getSpace(); + $tabs = $this->style->getTabs(); + if (!$this->withoutPPR) { $this->xmlWriter->startElement('w:pPr'); } // Alignment - if (!is_null($this->style->getAlign())) { - $this->xmlWriter->startElement('w:jc'); - $this->xmlWriter->writeAttribute('w:val', $this->style->getAlign()); - $this->xmlWriter->endElement(); - } + $this->writeElementIf(!is_null($align), 'w:jc', 'w:val', $align); + + // Pagination + $this->writeElementIf(!$this->style->hasWidowControl(), 'w:widowControl', 'w:val', '0'); + $this->writeElementIf($this->style->isKeepNext(), 'w:keepNext', 'w:val', '1'); + $this->writeElementIf($this->style->isKeepLines(), 'w:keepLines', 'w:val', '1'); + $this->writeElementIf($this->style->hasPageBreakBefore(), 'w:pageBreakBefore', 'w:val', '1'); // Indentation - if (!is_null($this->style->getIndentation())) { - $styleWriter = new Indentation($this->xmlWriter, $this->style->getIndentation()); + if (!is_null($indentation)) { + $styleWriter = new Indentation($this->xmlWriter, $indentation); $styleWriter->write(); } // Spacing - if (!is_null($this->style->getSpace())) { - $styleWriter = new Spacing($this->xmlWriter, $this->style->getSpace()); + if (!is_null($spacing)) { + $styleWriter = new Spacing($this->xmlWriter, $spacing); $styleWriter->write(); } - // Pagination - if (!$this->style->hasWidowControl()) { - $this->xmlWriter->startElement('w:widowControl'); - $this->xmlWriter->writeAttribute('w:val', '0'); - $this->xmlWriter->endElement(); - } - if ($this->style->isKeepNext()) { - $this->xmlWriter->startElement('w:keepNext'); - $this->xmlWriter->writeAttribute('w:val', '1'); - $this->xmlWriter->endElement(); - } - if ($this->style->isKeepLines()) { - $this->xmlWriter->startElement('w:keepLines'); - $this->xmlWriter->writeAttribute('w:val', '1'); - $this->xmlWriter->endElement(); - } - if ($this->style->hasPageBreakBefore()) { - $this->xmlWriter->startElement('w:pageBreakBefore'); - $this->xmlWriter->writeAttribute('w:val', '1'); - $this->xmlWriter->endElement(); - } - // Tabs - $tabs = $this->style->getTabs(); if (!empty($tabs)) { $this->xmlWriter->startElement("w:tabs"); foreach ($tabs as $tab) {