From 88560de60173c21253e0d571d881c12ef63183ff Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Wed, 7 May 2014 09:47:02 +0700 Subject: [PATCH] Refactor writers --- .scrutinizer.yml | 2 +- src/PhpWord/Element/AbstractContainer.php | 7 +- src/PhpWord/Element/Section.php | 3 +- src/PhpWord/Element/Text.php | 4 +- src/PhpWord/Element/TextBreak.php | 14 +- src/PhpWord/Shared/XMLWriter.php | 35 ++ src/PhpWord/Style/AbstractStyle.php | 67 ++- src/PhpWord/Style/Border.php | 18 + src/PhpWord/Style/Cell.php | 17 +- src/PhpWord/Style/Font.php | 458 +++++++++--------- src/PhpWord/Style/Paragraph.php | 77 +-- src/PhpWord/Style/Section.php | 11 +- src/PhpWord/Style/Table.php | 31 +- src/PhpWord/Writer/HTML.php | 24 +- src/PhpWord/Writer/HTML/Element/Element.php | 13 +- src/PhpWord/Writer/ODText.php | 52 +- .../Writer/ODText/Element/AbstractElement.php | 27 ++ .../Writer/ODText/Element/Container.php | 27 ++ src/PhpWord/Writer/ODText/Element/Element.php | 87 ---- src/PhpWord/Writer/ODText/Element/Image.php | 47 +- src/PhpWord/Writer/ODText/Element/Link.php | 21 +- src/PhpWord/Writer/ODText/Element/Table.php | 54 +-- src/PhpWord/Writer/ODText/Element/Text.php | 34 +- .../Writer/ODText/Element/TextBreak.php | 10 +- src/PhpWord/Writer/ODText/Element/TextRun.php | 30 +- src/PhpWord/Writer/ODText/Part/Content.php | 11 +- src/PhpWord/Writer/ODText/Part/Styles.php | 2 +- src/PhpWord/Writer/ODText/Style/Font.php | 59 +-- src/PhpWord/Writer/ODText/Style/Paragraph.php | 31 +- src/PhpWord/Writer/PDF.php | 2 +- src/PhpWord/Writer/RTF.php | 29 +- src/PhpWord/Writer/RTF/Element/Element.php | 13 +- src/PhpWord/Writer/Word2007.php | 102 ++-- .../{Element.php => AbstractElement.php} | 58 ++- .../Writer/Word2007/Element/CheckBox.php | 114 ++--- .../Writer/Word2007/Element/Container.php | 58 +++ .../Writer/Word2007/Element/Footnote.php | 37 +- src/PhpWord/Writer/Word2007/Element/Image.php | 90 ++-- src/PhpWord/Writer/Word2007/Element/Link.php | 50 +- .../Writer/Word2007/Element/ListItem.php | 44 +- .../Writer/Word2007/Element/Object.php | 77 ++- .../Writer/Word2007/Element/PageBreak.php | 18 +- .../Writer/Word2007/Element/PreserveText.php | 87 ++-- src/PhpWord/Writer/Word2007/Element/TOC.php | 166 ++++--- src/PhpWord/Writer/Word2007/Element/Table.php | 104 ++-- src/PhpWord/Writer/Word2007/Element/Text.php | 93 +++- .../Writer/Word2007/Element/TextBreak.php | 40 +- .../Writer/Word2007/Element/TextRun.php | 19 +- src/PhpWord/Writer/Word2007/Element/Title.php | 62 +-- .../Writer/Word2007/Part/AbstractPart.php | 55 +-- src/PhpWord/Writer/Word2007/Part/Document.php | 6 +- src/PhpWord/Writer/Word2007/Part/Footer.php | 5 +- .../Writer/Word2007/Part/Footnotes.php | 4 +- src/PhpWord/Writer/Word2007/Part/Rels.php | 18 +- .../Writer/Word2007/Style/AbstractStyle.php | 55 ++- src/PhpWord/Writer/Word2007/Style/Cell.php | 76 ++- src/PhpWord/Writer/Word2007/Style/Font.php | 80 +-- src/PhpWord/Writer/Word2007/Style/Image.php | 26 +- .../Writer/Word2007/Style/Indentation.php | 21 +- .../Writer/Word2007/Style/LineNumbering.php | 15 +- .../Writer/Word2007/Style/MarginBorder.php | 18 +- .../Writer/Word2007/Style/Paragraph.php | 48 +- src/PhpWord/Writer/Word2007/Style/Section.php | 76 ++- src/PhpWord/Writer/Word2007/Style/Shading.php | 13 +- src/PhpWord/Writer/Word2007/Style/Spacing.php | 26 +- src/PhpWord/Writer/Word2007/Style/Tab.php | 13 +- src/PhpWord/Writer/Word2007/Style/Table.php | 64 +-- tests/PhpWord/Tests/Style/FontTest.php | 3 +- 68 files changed, 1569 insertions(+), 1589 deletions(-) create mode 100644 src/PhpWord/Writer/ODText/Element/AbstractElement.php create mode 100644 src/PhpWord/Writer/ODText/Element/Container.php delete mode 100644 src/PhpWord/Writer/ODText/Element/Element.php rename src/PhpWord/Writer/Word2007/Element/{Element.php => AbstractElement.php} (57%) create mode 100644 src/PhpWord/Writer/Word2007/Element/Container.php diff --git a/.scrutinizer.yml b/.scrutinizer.yml index d6a58c0f..ab5f8620 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -9,7 +9,7 @@ tools: enabled: true timeout: 900 php_sim: - min_mass: 25 + min_mass: 30 php_pdepend: true php_analyzer: true sensiolabs_security_checker: true diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 4ac6c22b..7d317972 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -39,8 +39,7 @@ abstract class AbstractContainer extends AbstractElement */ protected function addElement(AbstractElement $element) { - // $type = get_class($element); - // $type = str_replace('PhpOffice\\PhpWord\\Element\\', '', $type); + // $type = basename(get_class($element)); $element->setElementIndex($this->countElements() + 1); $element->setElementId(); $element->setPhpWord($this->phpWord); @@ -79,7 +78,7 @@ abstract class AbstractContainer extends AbstractElement public function addText($text, $fontStyle = null, $paragraphStyle = null, $elementName = 'Text') { $this->checkValidity($elementName); - $elementClass = 'PhpOffice\\PhpWord\\Element\\' . $elementName; + $elementClass = dirname(get_class($this)) . '\\' . $elementName; // Reset paragraph style for footnote and textrun. They have their own if (in_array($this->container, array('textrun', 'footnote', 'endnote'))) { @@ -249,7 +248,7 @@ abstract class AbstractContainer extends AbstractElement public function addFootnote($paragraphStyle = null, $elementName = 'Footnote') { $this->checkValidity($elementName); - $elementClass = 'PhpOffice\\PhpWord\\Element\\' . $elementName; + $elementClass = dirname(get_class($this)) . '\\' . $elementName; $docPart = strtolower($elementName); $addMethod = "add{$elementName}"; diff --git a/src/PhpWord/Element/Section.php b/src/PhpWord/Element/Section.php index 1ccdccc8..639f752e 100644 --- a/src/PhpWord/Element/Section.php +++ b/src/PhpWord/Element/Section.php @@ -223,8 +223,7 @@ class Section extends AbstractContainer private function addHeaderFooter($type = Header::AUTO, $header = true) { $collectionArray = $header ? 'headers' : 'footers'; - $containerClass = 'PhpOffice\\PhpWord\\Element\\'; - $containerClass .= ($header ? 'Header' : 'Footer'); + $containerClass = dirname(get_class($this)) . '\\' . ($header ? 'Header' : 'Footer'); $collection = &$this->$collectionArray; if (in_array($type, array(Header::AUTO, Header::FIRST, Header::EVEN))) { diff --git a/src/PhpWord/Element/Text.php b/src/PhpWord/Element/Text.php index f992b07c..52fccb3f 100644 --- a/src/PhpWord/Element/Text.php +++ b/src/PhpWord/Element/Text.php @@ -75,7 +75,7 @@ class Text extends AbstractElement $this->setParagraphStyle($paragraphStyle); } elseif (is_array($style)) { $this->fontStyle = new Font('text', $paragraphStyle); - $this->fontStyle->setArrayStyle($style); + $this->fontStyle->setStyleByArray($style); } elseif (null === $style) { $this->fontStyle = new Font('text', $paragraphStyle); } else { @@ -106,7 +106,7 @@ class Text extends AbstractElement { if (is_array($style)) { $this->paragraphStyle = new Paragraph; - $this->paragraphStyle->setArrayStyle($style); + $this->paragraphStyle->setStyleByArray($style); } elseif ($style instanceof Paragraph) { $this->paragraphStyle = $style; } elseif (null === $style) { diff --git a/src/PhpWord/Element/TextBreak.php b/src/PhpWord/Element/TextBreak.php index 02518373..aa6ab582 100644 --- a/src/PhpWord/Element/TextBreak.php +++ b/src/PhpWord/Element/TextBreak.php @@ -69,7 +69,7 @@ class TextBreak extends AbstractElement $this->setParagraphStyle($paragraphStyle); } elseif (is_array($style)) { $this->fontStyle = new Font('text', $paragraphStyle); - $this->fontStyle->setArrayStyle($style); + $this->fontStyle->setStyleByArray($style); } else { $this->fontStyle = $style; $this->setParagraphStyle($paragraphStyle); @@ -97,7 +97,7 @@ class TextBreak extends AbstractElement { if (is_array($style)) { $this->paragraphStyle = new Paragraph; - $this->paragraphStyle->setArrayStyle($style); + $this->paragraphStyle->setStyleByArray($style); } elseif ($style instanceof Paragraph) { $this->paragraphStyle = $style; } else { @@ -115,4 +115,14 @@ class TextBreak extends AbstractElement { return $this->paragraphStyle; } + + /** + * Has font/paragraph style defined + * + * @return bool + */ + public function hasStyle() + { + return !is_null($this->fontStyle) || !is_null($this->paragraphStyle); + } } diff --git a/src/PhpWord/Shared/XMLWriter.php b/src/PhpWord/Shared/XMLWriter.php index 41723002..a16fe0b4 100644 --- a/src/PhpWord/Shared/XMLWriter.php +++ b/src/PhpWord/Shared/XMLWriter.php @@ -149,4 +149,39 @@ class XMLWriter return $this->text($text); } + + /** + * Write element if ... + * + * @param bool $condition + * @param string $element + * @param string $attribute + * @param string $value + */ + public function writeElementIf($condition, $element, $attribute = null, $value = null) + { + if ($condition) { + if (is_null($attribute)) { + $this->xmlWriter->writeElement($element, $value); + } else { + $this->xmlWriter->startElement($element); + $this->xmlWriter->writeAttribute($attribute, $value); + $this->xmlWriter->endElement(); + } + } + } + + /** + * Write attribute if ... + * + * @param bool $condition + * @param string $attribute + * @param string $value + */ + public function writeAttributeIf($condition, $attribute, $value) + { + if ($condition) { + $this->xmlWriter->writeAttribute($attribute, $value); + } + } } diff --git a/src/PhpWord/Style/AbstractStyle.php b/src/PhpWord/Style/AbstractStyle.php index caceb46a..f43aba90 100644 --- a/src/PhpWord/Style/AbstractStyle.php +++ b/src/PhpWord/Style/AbstractStyle.php @@ -38,10 +38,17 @@ abstract class AbstractStyle * * This number starts from one and defined in Style::setStyleValues() * - * @var integer|null + * @var int|null */ protected $index; + /** + * Aliases + * + * @var array + */ + protected $aliases = array(); + /** * Get style name * @@ -68,7 +75,7 @@ abstract class AbstractStyle /** * Get index number * - * @return integer|null + * @return int|null */ public function getIndex() { @@ -78,7 +85,7 @@ abstract class AbstractStyle /** * Set index number * - * @param integer|null $value + * @param int|null $value * @return self */ public function setIndex($value = null) @@ -102,6 +109,9 @@ abstract class AbstractStyle */ public function setStyleValue($key, $value) { + if (isset($this->aliases[$key])) { + $key = $this->aliases[$key]; + } $method = 'set' . String::removeUnderscorePrefix($key); if (method_exists($this, $method)) { $this->$method($value); @@ -150,6 +160,9 @@ abstract class AbstractStyle */ protected function setBoolVal($value, $default = null) { + if (is_string($value)) { + $value = (bool)$value; + } if (!is_bool($value)) { $value = $default; } @@ -161,8 +174,8 @@ abstract class AbstractStyle * Set numeric value * * @param mixed $value - * @param integer|float|null $default - * @return integer|float|null + * @param int|float|null $default + * @return int|float|null */ protected function setNumericVal($value, $default = null) { @@ -177,11 +190,14 @@ abstract class AbstractStyle * Set integer value * * @param mixed $value - * @param integer|null $default - * @return integer|null + * @param int|null $default + * @return int|null */ protected function setIntVal($value, $default = null) { + if (is_string($value)) { + $value = intval($value); + } if (!is_int($value)) { $value = $default; } @@ -198,6 +214,9 @@ abstract class AbstractStyle */ protected function setFloatVal($value, $default = null) { + if (is_string($value)) { + $value = floatval($value); + } if (!is_float($value)) { $value = $default; } @@ -220,4 +239,38 @@ abstract class AbstractStyle return $value; } + + /** + * Set object value + * + * @param mixed $value + * @param string $styleName + * @param mixed $style + */ + protected function setObjectVal($value, $styleName, &$style) + { + $styleClass = dirname(get_class($this)) . '\\' . $styleName; + if (is_array($value)) { + if (!$style instanceof $styleClass) { + $style = new $styleClass(); + } + $style->setStyleByArray($value); + } else { + $style = $value; + } + + return $style; + } + + /** + * Set style using associative array + * + * @param array $style + * @deprecated 0.11.0 + * @codeCoverageIgnore + */ + public function setArrayStyle(array $style = array()) + { + return $this->setStyleByArray($style); + } } diff --git a/src/PhpWord/Style/Border.php b/src/PhpWord/Style/Border.php index 433ee265..f7c479fe 100644 --- a/src/PhpWord/Style/Border.php +++ b/src/PhpWord/Style/Border.php @@ -323,4 +323,22 @@ class Border extends AbstractStyle { return $this->borderBottomColor; } + + /** + * Has borders? + * + * @return bool + */ + public function hasBorders() + { + $hasBorders = false; + $borders = $this->getBorderSize(); + for ($i = 0; $i < count($borders); $i++) { + if (!is_null($borders[$i])) { + $hasBorders = true; + } + } + + return $hasBorders; + } } diff --git a/src/PhpWord/Style/Cell.php b/src/PhpWord/Style/Cell.php index 669df6b0..31b7e307 100644 --- a/src/PhpWord/Style/Cell.php +++ b/src/PhpWord/Style/Cell.php @@ -42,7 +42,7 @@ class Cell extends Border * * @var string */ - private $valign; + private $vAlign; /** * Text Direction @@ -80,7 +80,7 @@ class Cell extends Border */ public function getVAlign() { - return $this->valign; + return $this->vAlign; } /** @@ -90,7 +90,7 @@ class Cell extends Border */ public function setVAlign($value = null) { - $this->valign = $value; + $this->vAlign = $value; } /** @@ -183,19 +183,12 @@ class Cell extends Border /** * Set shading * - * @param array $value + * @param mixed $value * @return self */ public function setShading($value = null) { - if (is_array($value)) { - if (!$this->shading instanceof Shading) { - $this->shading = new Shading(); - } - $this->shading->setStyleByArray($value); - } else { - $this->shading = null; - } + $this->setObjectVal($value, 'Shading', $this->shading); return $this; } diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index 7605312a..f0ee893e 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -70,6 +70,13 @@ class Font extends AbstractStyle const FGCOLOR_LIGHTGRAY = 'lightGray'; const FGCOLOR_BLACK = 'black'; + /** + * Aliases + * + * @var array + */ + protected $aliases = array('line-height' => 'lineHeight'); + /** * Font style type * @@ -77,13 +84,6 @@ class Font extends AbstractStyle */ private $type; - /** - * Paragraph style - * - * @var \PhpOffice\PhpWord\Style\Paragraph - */ - private $paragraphStyle; - /** * Font name * @@ -91,6 +91,13 @@ class Font extends AbstractStyle */ private $name = PhpWord::DEFAULT_FONT_NAME; + /** + * Font Content Type + * + * @var string + */ + private $hint = PhpWord::DEFAULT_FONT_CONTENT_TYPE; + /** * Font size * @@ -98,6 +105,13 @@ class Font extends AbstractStyle */ private $size = PhpWord::DEFAULT_FONT_SIZE; + /** + * Font color + * + * @var string + */ + private $color = PhpWord::DEFAULT_FONT_COLOR; + /** * Bold * @@ -112,6 +126,13 @@ class Font extends AbstractStyle */ private $italic = false; + /** + * Undeline + * + * @var string + */ + private $underline = self::UNDERLINE_NONE; + /** * Superscript * @@ -126,13 +147,6 @@ class Font extends AbstractStyle */ private $subScript = false; - /** - * Undeline - * - * @var string - */ - private $underline = self::UNDERLINE_NONE; - /** * Strikethrough * @@ -147,40 +161,6 @@ class Font extends AbstractStyle */ private $doubleStrikethrough = false; - /** - * Font color - * - * @var string - */ - private $color = PhpWord::DEFAULT_FONT_COLOR; - - /** - * Foreground/highlight - * - * @var string - */ - private $fgColor = null; - - /** - * Text line height - * - * @var int - */ - - /** - * Text line height - * - * @var int - */ - private $lineHeight = 1.0; - - /** - * Font Content Type - * - * @var string - */ - private $hint = PhpWord::DEFAULT_FONT_CONTENT_TYPE; - /** * Small caps * @@ -197,6 +177,26 @@ class Font extends AbstractStyle */ private $allCaps = false; + /** + * Foreground/highlight + * + * @var string + */ + private $fgColor; + + /** + * Text line height + * + * @var int + */ + + /** + * Paragraph style + * + * @var \PhpOffice\PhpWord\Style\Paragraph + */ + private $paragraph; + /** * Shading * @@ -208,39 +208,12 @@ class Font extends AbstractStyle * Create new font style * * @param string $type Type of font - * @param array $paragraphStyle Paragraph styles definition + * @param array $paragraph Paragraph styles definition */ - public function __construct($type = 'text', $paragraphStyle = null) + public function __construct($type = 'text', $paragraph = null) { $this->type = $type; - - if ($paragraphStyle instanceof Paragraph) { - $this->paragraphStyle = $paragraphStyle; - } elseif (is_array($paragraphStyle)) { - $this->paragraphStyle = new Paragraph; - $this->paragraphStyle->setArrayStyle($paragraphStyle); - } else { - $this->paragraphStyle = $paragraphStyle; - } - } - - /** - * Set style using associative array - * - * @param array $style - * @return $this - */ - public function setArrayStyle(array $style = array()) - { - foreach ($style as $key => $value) { - if ($key === 'line-height') { - $this->setLineHeight($value); - null; - } - $this->setStyleValue($key, $value); - } - - return $this; + $this->setParagraph($paragraph); } /** @@ -266,6 +239,28 @@ class Font extends AbstractStyle return $this; } + /** + * Get Font Content Type + * + * @return string + */ + public function getHint() + { + return $this->hint; + } + + /** + * Set Font Content Type + * + * @param string $value + * @return self + */ + public function setHint($value = PhpWord::DEFAULT_FONT_CONTENT_TYPE) + { + $this->hint = $this->setNonEmptyVal($value, PhpWord::DEFAULT_FONT_CONTENT_TYPE); + + return $this; + } /** * Get font size @@ -290,6 +285,29 @@ class Font extends AbstractStyle return $this; } + /** + * Get font color + * + * @return string + */ + public function getColor() + { + return $this->color; + } + + /** + * Set font color + * + * @param string $value + * @return self + */ + public function setColor($value = PhpWord::DEFAULT_FONT_COLOR) + { + $this->color = $this->setNonEmptyVal($value, PhpWord::DEFAULT_FONT_COLOR); + + return $this; + } + /** * Get bold * @@ -336,58 +354,6 @@ class Font extends AbstractStyle return $this; } - /** - * Get superscript - * - * @return bool - */ - public function isSuperScript() - { - return $this->superScript; - } - - /** - * Set superscript - * - * @param bool $value - * @return self - */ - public function setSuperScript($value = false) - { - $this->superScript = $this->setBoolVal($value, $this->superScript); - if ($this->superScript) { - $this->subScript = false; - } - - return $this; - } - - /** - * Get subscript - * - * @return bool - */ - public function isSubScript() - { - return $this->subScript; - } - - /** - * Set subscript - * - * @param bool $value - * @return self - */ - public function setSubScript($value = false) - { - $this->subScript = $this->setBoolVal($value, $this->subScript); - if ($this->subScript) { - $this->superScript = false; - } - - return $this; - } - /** * Get underline * @@ -411,6 +377,57 @@ class Font extends AbstractStyle return $this; } + /** + * Get superscript + * + * @return bool + */ + public function isSuperScript() + { + return $this->superScript; + } + + /** + * Set superscript + * + * @param bool $value + * @return self + */ + public function setSuperScript($value = false) + { + $this->superScript = $this->setBoolVal($value, $this->superScript); + $this->toggleFalse($this->subScript, $this->superScript); + + return $this; + } + + /** + * Get subscript + * + * @return bool + */ + public function isSubScript() + { + return $this->subScript; + } + + /** + * Set subscript + * + * @param bool $value + * @return self + */ + public function setSubScript($value = false) + { + $this->subScript = $this->setBoolVal($value, $this->subScript); + $this->toggleFalse($this->subScript, $this->superScript); + if ($this->subScript) { + $this->superScript = false; + } + + return $this; + } + /** * Get strikethrough * @@ -430,9 +447,7 @@ class Font extends AbstractStyle public function setStrikethrough($value = false) { $this->strikethrough = $this->setBoolVal($value, $this->strikethrough); - if ($this->strikethrough) { - $this->doubleStrikethrough = false; - } + $this->toggleFalse($this->doubleStrikethrough, $this->strikethrough); return $this; } @@ -456,32 +471,55 @@ class Font extends AbstractStyle public function setDoubleStrikethrough($value = false) { $this->doubleStrikethrough = $this->setBoolVal($value, $this->doubleStrikethrough); - if ($this->doubleStrikethrough) { - $this->strikethrough = false; - } + $this->toggleFalse($this->strikethrough, $this->doubleStrikethrough); return $this; } /** - * Get font color + * Get small caps * - * @return string + * @return bool */ - public function getColor() + public function isSmallCaps() { - return $this->color; + return $this->smallCaps; } /** - * Set font color + * Set small caps * - * @param string $value + * @param bool $value * @return self */ - public function setColor($value = PhpWord::DEFAULT_FONT_COLOR) + public function setSmallCaps($value = false) { - $this->color = $this->setNonEmptyVal($value, PhpWord::DEFAULT_FONT_COLOR); + $this->smallCaps = $this->setBoolVal($value, $this->smallCaps); + $this->toggleFalse($this->allCaps, $this->smallCaps); + + return $this; + } + + /** + * Get all caps + * + * @return bool + */ + public function isAllCaps() + { + return $this->allCaps; + } + + /** + * Set all caps + * + * @param bool $value + * @return self + */ + public function setAllCaps($value = false) + { + $this->allCaps = $this->setBoolVal($value, $this->allCaps); + $this->toggleFalse($this->smallCaps, $this->allCaps); return $this; } @@ -542,38 +580,6 @@ class Font extends AbstractStyle return $this->type; } - /** - * Get paragraph style - * - * @return \PhpOffice\PhpWord\Style\Paragraph - */ - public function getParagraphStyle() - { - return $this->paragraphStyle; - } - - /** - * Set lineheight - * - * @param int|float|string $lineHeight - * @return $this - * @throws \PhpOffice\PhpWord\Exception\InvalidStyleException - */ - public function setLineHeight($lineHeight) - { - if (is_string($lineHeight)) { - $lineHeight = floatval(preg_replace('/[^0-9\.\,]/', '', $lineHeight)); - } - - if ((!is_integer($lineHeight) && !is_float($lineHeight)) || !$lineHeight) { - throw new InvalidStyleException('Line height must be a valid number'); - } - - $this->lineHeight = $lineHeight; - $this->getParagraphStyle()->setLineHeight($lineHeight); - return $this; - } - /** * Get line height * @@ -581,80 +587,41 @@ class Font extends AbstractStyle */ public function getLineHeight() { - return $this->lineHeight; + return $this->getParagraph()->getLineHeight(); } /** - * Get Font Content Type + * Set lineheight * - * @return string - */ - public function getHint() - { - return $this->hint; - } - - /** - * Set Font Content Type - * - * @param string $value + * @param int|float|string $value * @return self */ - public function setHint($value = PhpWord::DEFAULT_FONT_CONTENT_TYPE) + public function setLineHeight($value) { - $this->hint = $this->setNonEmptyVal($value, PhpWord::DEFAULT_FONT_CONTENT_TYPE); + $this->setParagraph(array('lineHeight' => $value)); return $this; } /** - * Get small caps + * Get paragraph style * - * @return bool + * @return \PhpOffice\PhpWord\Style\Paragraph */ - public function isSmallCaps() + public function getParagraph() { - return $this->smallCaps; + return $this->paragraph; } /** - * Set small caps + * Set shading * - * @param bool $value + * @param mixed $value * @return self */ - public function setSmallCaps($value = false) + public function setParagraph($value = null) { - $this->smallCaps = $this->setBoolVal($value, $this->smallCaps); - if ($this->smallCaps) { - $this->allCaps = false; - } - - return $this; - } - - /** - * Get all caps - * - * @return bool - */ - public function isAllCaps() - { - return $this->allCaps; - } - - /** - * Set all caps - * - * @param bool $value - * @return self - */ - public function setAllCaps($value = false) - { - $this->allCaps = $this->setBoolVal($value, $this->allCaps); - if ($this->allCaps) { - $this->smallCaps = false; - } + $this->setObjectVal($value, 'Paragraph', $this->paragraph); return $this; } @@ -672,23 +639,29 @@ class Font extends AbstractStyle /** * Set shading * - * @param array $value + * @param mixed $value * @return self */ public function setShading($value = null) { - if (is_array($value)) { - if (!$this->shading instanceof Shading) { - $this->shading = new Shading(); - } - $this->shading->setStyleByArray($value); - } else { - $this->shading = null; - } + $this->setObjectVal($value, 'Shading', $this->shading); return $this; } + /** + * Toggle $target value to false when $source true + * + * @param \PhpOffice\PhpWord\Style\AbstractStyle $target + * @param bool $sourceValue + */ + private function toggleFalse(&$target, $sourceValue) + { + if ($sourceValue == true) { + $target = false; + } + } + /** * Get bold * @@ -743,4 +716,15 @@ class Font extends AbstractStyle { return $this->isStrikethrough(); } + + /** + * Get paragraph style + * + * @deprecated 0.11.0 + * @codeCoverageIgnore + */ + public function getParagraphStyle() + { + return $this->getParagraph(); + } } diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index bd768670..b04646ea 100644 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -27,6 +27,13 @@ class Paragraph extends AbstractStyle { const LINE_HEIGHT = 240; + /** + * Aliases + * + * @var array + */ + protected $aliases = array('line-height' => 'lineHeight'); + /** * Paragraph alignment * @@ -104,24 +111,6 @@ class Paragraph extends AbstractStyle */ private $spacing; - /** - * Set style by array - * - * @param array $style - * @return $this - */ - public function setArrayStyle(array $style = array()) - { - foreach ($style as $key => $value) { - if ($key === 'line-height') { - null; - } - $this->setStyleValue($key, $value); - } - - return $this; - } - /** * Set Style value * @@ -135,9 +124,6 @@ class Paragraph extends AbstractStyle $value = $value * 720; } elseif ($key == 'spacing') { $value += 240; // because line height of 1 matches 240 twips - } elseif ($key === 'line-height') { - $this->setLineHeight($value); - return; } $method = 'set' . $key; if (method_exists($this, $method)) { @@ -168,6 +154,7 @@ class Paragraph extends AbstractStyle $value = 'both'; } $this->align = $value; + return $this; } @@ -362,6 +349,7 @@ class Paragraph extends AbstractStyle public function setBasedOn($value = 'Normal') { $this->basedOn = $value; + return $this; } @@ -384,6 +372,7 @@ class Paragraph extends AbstractStyle public function setNext($value = null) { $this->next = $value; + return $this; } @@ -405,10 +394,8 @@ class Paragraph extends AbstractStyle */ public function setWidowControl($value = true) { - if (!is_bool($value)) { - $value = true; - } - $this->widowControl = $value; + $this->widowControl = $this->setBoolVal($value, $this->widowControl); + return $this; } @@ -430,10 +417,8 @@ class Paragraph extends AbstractStyle */ public function setKeepNext($value = false) { - if (!is_bool($value)) { - $value = false; - } - $this->keepNext = $value; + $this->keepNext = $this->setBoolVal($value, $this->keepNext); + return $this; } @@ -455,10 +440,8 @@ class Paragraph extends AbstractStyle */ public function setKeepLines($value = false) { - if (!is_bool($value)) { - $value = false; - } - $this->keepLines = $value; + $this->keepLines = $this->setBoolVal($value, $this->keepLines); + return $this; } @@ -480,10 +463,8 @@ class Paragraph extends AbstractStyle */ public function setPageBreakBefore($value = false) { - if (!is_bool($value)) { - $value = false; - } - $this->pageBreakBefore = $value; + $this->pageBreakBefore = $this->setBoolVal($value, $this->pageBreakBefore); + return $this; } @@ -500,19 +481,12 @@ class Paragraph extends AbstractStyle /** * Set shading * - * @param array $value + * @param mixed $value * @return self */ public function setIndentation($value = null) { - if (is_array($value)) { - if (!$this->indentation instanceof Indentation) { - $this->indentation = new Indentation(); - } - $this->indentation->setStyleByArray($value); - } else { - $this->indentation = null; - } + $this->setObjectVal($value, 'Indentation', $this->indentation); return $this; } @@ -531,20 +505,13 @@ class Paragraph extends AbstractStyle /** * Set shading * - * @param array $value + * @param mixed $value * @return self * @todo Rename to setSpacing in 1.0 */ public function setSpace($value = null) { - if (is_array($value)) { - if (!$this->spacing instanceof Spacing) { - $this->spacing = new Spacing(); - } - $this->spacing->setStyleByArray($value); - } else { - $this->spacing = null; - } + $this->setObjectVal($value, 'Spacing', $this->spacing); return $this; } diff --git a/src/PhpWord/Style/Section.php b/src/PhpWord/Style/Section.php index cc458f06..7306c6a6 100644 --- a/src/PhpWord/Style/Section.php +++ b/src/PhpWord/Style/Section.php @@ -509,19 +509,12 @@ class Section extends Border /** * Set line numbering * - * @param array $value + * @param mixed $value * @return self */ public function setLineNumbering($value = null) { - if (is_array($value)) { - if (!$this->lineNumbering instanceof LineNumbering) { - $this->lineNumbering = new LineNumbering($value); - } - $this->lineNumbering->setStyleByArray($value); - } else { - $this->lineNumbering = null; - } + $this->setObjectVal($value, 'LineNumbering', $this->lineNumbering); return $this; } diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index d636b765..9173d0d7 100644 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -425,41 +425,16 @@ class Table extends Border /** * Set shading * - * @param array $value + * @param mixed $value * @return self */ public function setShading($value = null) { - if (is_array($value)) { - if (!$this->shading instanceof Shading) { - $this->shading = new Shading(); - } - $this->shading->setStyleByArray($value); - } else { - $this->shading = null; - } + $this->setObjectVal($value, 'Shading', $this->shading); return $this; } - /** - * Has borders? - * - * @return bool - */ - public function hasBorders() - { - $hasBorders = false; - $borders = $this->getBorderSize(); - for ($i = 0; $i < 6; $i++) { - if (!is_null($borders[$i])) { - $hasBorders = true; - } - } - - return $hasBorders; - } - /** * Has margins? * @@ -469,7 +444,7 @@ class Table extends Border { $hasMargins = false; $margins = $this->getCellMargin(); - for ($i = 0; $i < 4; $i++) { + for ($i = 0; $i < count($margins); $i++) { if (!is_null($margins[$i])) { $hasMargins = true; } diff --git a/src/PhpWord/Writer/HTML.php b/src/PhpWord/Writer/HTML.php index d8ecd31f..e28d7733 100644 --- a/src/PhpWord/Writer/HTML.php +++ b/src/PhpWord/Writer/HTML.php @@ -67,19 +67,19 @@ class HTML extends AbstractWriter implements WriterInterface */ public function save($filename = null) { - if (!is_null($this->getPhpWord())) { - $this->setTempDir(sys_get_temp_dir() . '/PHPWordWriter/'); - $hFile = fopen($filename, 'w'); - if ($hFile !== false) { - fwrite($hFile, $this->writeDocument()); - fclose($hFile); - } else { - throw new Exception("Can't open file"); - } - $this->clearTempDir(); - } else { - throw new Exception("No PHPWord assigned."); + 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) { + fwrite($hFile, $this->writeDocument()); + fclose($hFile); + } else { + throw new Exception("Can't open file"); + } + $this->clearTempDir(); } /** diff --git a/src/PhpWord/Writer/HTML/Element/Element.php b/src/PhpWord/Writer/HTML/Element/Element.php index bd8d38b6..b0d25b44 100644 --- a/src/PhpWord/Writer/HTML/Element/Element.php +++ b/src/PhpWord/Writer/HTML/Element/Element.php @@ -72,14 +72,13 @@ class Element */ public function write() { - $html = ''; - $elmName = str_replace('PhpOffice\\PhpWord\\Element\\', '', get_class($this->element)); - $elmWriterClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Element\\' . $elmName; - if (class_exists($elmWriterClass) === true) { - $elmWriter = new $elmWriterClass($this->parentWriter, $this->element, $this->withoutP); - $html = $elmWriter->write(); + $content = ''; + $writerClass = dirname(get_class($this)) . '\\' . basename(get_class($this->element)); + if (class_exists($writerClass)) { + $writer = new $writerClass($this->parentWriter, $this->element, $this->withoutP); + $content = $writer->write(); } - return $html; + return $content; } } diff --git a/src/PhpWord/Writer/ODText.php b/src/PhpWord/Writer/ODText.php index fa290206..72c4a0b5 100644 --- a/src/PhpWord/Writer/ODText.php +++ b/src/PhpWord/Writer/ODText.php @@ -47,7 +47,7 @@ class ODText extends AbstractWriter implements WriterInterface 'Manifest' => 'META-INF/manifest.xml', ); foreach (array_keys($this->parts) as $partName) { - $partClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Part\\' . $partName; + $partClass = get_class($this) . '\\Part\\' . $partName; if (class_exists($partClass)) { $partObject = new $partClass(); $partObject->setParentWriter($this); @@ -67,31 +67,31 @@ class ODText extends AbstractWriter implements WriterInterface */ public function save($filename = null) { - if (!is_null($this->phpWord)) { - $filename = $this->getTempFile($filename); - $objZip = $this->getZipArchive($filename); - - // Add section media files - $sectionMedia = Media::getElements('section'); - if (!empty($sectionMedia)) { - $this->addFilesToPackage($objZip, $sectionMedia); - } - - // Write parts - foreach ($this->parts as $partName => $fileName) { - if ($fileName != '') { - $objZip->addFromString($fileName, $this->getWriterPart($partName)->write()); - } - } - - // Close file - if ($objZip->close() === false) { - throw new Exception("Could not close zip file $filename."); - } - - $this->cleanupTempFile(); - } else { - throw new Exception("PhpWord object unassigned."); + if (is_null($this->phpWord)) { + throw new Exception('PhpWord object unassigned.'); } + + $filename = $this->getTempFile($filename); + $objZip = $this->getZipArchive($filename); + + // Add section media files + $sectionMedia = Media::getElements('section'); + if (!empty($sectionMedia)) { + $this->addFilesToPackage($objZip, $sectionMedia); + } + + // Write parts + foreach ($this->parts as $partName => $fileName) { + if ($fileName != '') { + $objZip->addFromString($fileName, $this->getWriterPart($partName)->write()); + } + } + + // Close file + if ($objZip->close() === false) { + throw new Exception("Could not close zip file $filename."); + } + + $this->cleanupTempFile(); } } diff --git a/src/PhpWord/Writer/ODText/Element/AbstractElement.php b/src/PhpWord/Writer/ODText/Element/AbstractElement.php new file mode 100644 index 00000000..42f86af4 --- /dev/null +++ b/src/PhpWord/Writer/ODText/Element/AbstractElement.php @@ -0,0 +1,27 @@ +xmlWriter = $xmlWriter; - $this->parentWriter = $parentWriter; - $this->element = $element; - $this->withoutP = $withoutP; - } - - /** - * Write element - * - * @return string - */ - public function write() - { - $elmName = str_replace('PhpOffice\\PhpWord\\Element\\', '', get_class($this->element)); - $elmWriterClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Element\\' . $elmName; - if (class_exists($elmWriterClass) === true) { - $elmWriter = new $elmWriterClass($this->xmlWriter, $this->parentWriter, $this->element, $this->withoutP); - $elmWriter->write(); - } - } -} diff --git a/src/PhpWord/Writer/ODText/Element/Image.php b/src/PhpWord/Writer/ODText/Element/Image.php index ffe2d094..909a9aed 100644 --- a/src/PhpWord/Writer/ODText/Element/Image.php +++ b/src/PhpWord/Writer/ODText/Element/Image.php @@ -24,43 +24,42 @@ use PhpOffice\PhpWord\Shared\Drawing; * * @since 0.10.0 */ -class Image extends Element +class Image extends AbstractElement { /** * Write element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Image) { - return; - } + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); - $mediaIndex = $this->element->getMediaIndex(); - $target = 'Pictures/' . $this->element->getTarget(); - $style = $this->element->getStyle(); + $mediaIndex = $element->getMediaIndex(); + $target = 'Pictures/' . $element->getTarget(); + $style = $element->getStyle(); $width = Drawing::pixelsToCentimeters($style->getWidth()); $height = Drawing::pixelsToCentimeters($style->getHeight()); - $this->xmlWriter->startElement('text:p'); - $this->xmlWriter->writeAttribute('text:style-name', 'Standard'); + $xmlWriter->startElement('text:p'); + $xmlWriter->writeAttribute('text:style-name', 'Standard'); - $this->xmlWriter->startElement('draw:frame'); - $this->xmlWriter->writeAttribute('draw:style-name', 'fr' . $mediaIndex); - $this->xmlWriter->writeAttribute('draw:name', $this->element->getElementId()); - $this->xmlWriter->writeAttribute('text:anchor-type', 'as-char'); - $this->xmlWriter->writeAttribute('svg:width', $width . 'cm'); - $this->xmlWriter->writeAttribute('svg:height', $height . 'cm'); - $this->xmlWriter->writeAttribute('draw:z-index', $mediaIndex); + $xmlWriter->startElement('draw:frame'); + $xmlWriter->writeAttribute('draw:style-name', 'fr' . $mediaIndex); + $xmlWriter->writeAttribute('draw:name', $element->getElementId()); + $xmlWriter->writeAttribute('text:anchor-type', 'as-char'); + $xmlWriter->writeAttribute('svg:width', $width . 'cm'); + $xmlWriter->writeAttribute('svg:height', $height . 'cm'); + $xmlWriter->writeAttribute('draw:z-index', $mediaIndex); - $this->xmlWriter->startElement('draw:image'); - $this->xmlWriter->writeAttribute('xlink:href', $target); - $this->xmlWriter->writeAttribute('xlink:type', 'simple'); - $this->xmlWriter->writeAttribute('xlink:show', 'embed'); - $this->xmlWriter->writeAttribute('xlink:actuate', 'onLoad'); - $this->xmlWriter->endElement(); // draw:image + $xmlWriter->startElement('draw:image'); + $xmlWriter->writeAttribute('xlink:href', $target); + $xmlWriter->writeAttribute('xlink:type', 'simple'); + $xmlWriter->writeAttribute('xlink:show', 'embed'); + $xmlWriter->writeAttribute('xlink:actuate', 'onLoad'); + $xmlWriter->endElement(); // draw:image - $this->xmlWriter->endElement(); // draw:frame + $xmlWriter->endElement(); // draw:frame - $this->xmlWriter->endElement(); // text:p + $xmlWriter->endElement(); // text:p } } diff --git a/src/PhpWord/Writer/ODText/Element/Link.php b/src/PhpWord/Writer/ODText/Element/Link.php index 4ff094d2..1ccac435 100644 --- a/src/PhpWord/Writer/ODText/Element/Link.php +++ b/src/PhpWord/Writer/ODText/Element/Link.php @@ -22,29 +22,28 @@ namespace PhpOffice\PhpWord\Writer\ODText\Element; * * @since 0.10.0 */ -class Link extends Element +class Link extends AbstractElement { /** * Write element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Link) { - return; - } + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); if (!$this->withoutP) { - $this->xmlWriter->startElement('text:p'); // text:p + $xmlWriter->startElement('text:p'); // text:p } - $this->xmlWriter->startElement('text:a'); - $this->xmlWriter->writeAttribute('xlink:type', 'simple'); - $this->xmlWriter->writeAttribute('xlink:href', $this->element->getTarget()); - $this->xmlWriter->writeRaw($this->element->getText()); - $this->xmlWriter->endElement(); // text:a + $xmlWriter->startElement('text:a'); + $xmlWriter->writeAttribute('xlink:type', 'simple'); + $xmlWriter->writeAttribute('xlink:href', $element->getTarget()); + $xmlWriter->writeRaw($element->getText()); + $xmlWriter->endElement(); // text:a if (!$this->withoutP) { - $this->xmlWriter->endElement(); // text:p + $xmlWriter->endElement(); // text:p } } } diff --git a/src/PhpWord/Writer/ODText/Element/Table.php b/src/PhpWord/Writer/ODText/Element/Table.php index dbe5b14c..d8664fac 100644 --- a/src/PhpWord/Writer/ODText/Element/Table.php +++ b/src/PhpWord/Writer/ODText/Element/Table.php @@ -25,50 +25,42 @@ use PhpOffice\PhpWord\Writer\ODText\Element\Element as ElementWriter; * * @since 0.10.0 */ -class Table extends Element +class Table extends AbstractElement { /** * Write element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Table) { - return; - } - - $rows = $this->element->getRows(); + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); + $rows = $element->getRows(); $rowCount = count($rows); - $colCount = $this->element->countColumns(); - if ($rowCount > 0) { - $this->xmlWriter->startElement('table:table'); - $this->xmlWriter->writeAttribute('table:name', $this->element->getElementId()); - $this->xmlWriter->writeAttribute('table:style', $this->element->getElementId()); + $colCount = $element->countColumns(); - $this->xmlWriter->startElement('table:table-column'); - $this->xmlWriter->writeAttribute('table:number-columns-repeated', $colCount); - $this->xmlWriter->endElement(); // table:table-column + if ($rowCount > 0) { + $xmlWriter->startElement('table:table'); + $xmlWriter->writeAttribute('table:name', $element->getElementId()); + $xmlWriter->writeAttribute('table:style', $element->getElementId()); + + $xmlWriter->startElement('table:table-column'); + $xmlWriter->writeAttribute('table:number-columns-repeated', $colCount); + $xmlWriter->endElement(); // table:table-column foreach ($rows as $row) { - $this->xmlWriter->startElement('table:table-row'); + $xmlWriter->startElement('table:table-row'); foreach ($row->getCells() as $cell) { - $this->xmlWriter->startElement('table:table-cell'); - $this->xmlWriter->writeAttribute('office:value-type', 'string'); - $elements = $cell->getElements(); - if (count($elements) > 0) { - foreach ($elements as $element) { - $elementWriter = new ElementWriter($this->xmlWriter, $this->parentWriter, $element); - $elementWriter->write(); - } - } else { - $element = new TextBreakElement(); - $elementWriter = new ElementWriter($this->xmlWriter, $this->parentWriter, $element); - $elementWriter->write(); - } - $this->xmlWriter->endElement(); // table:table-cell + $xmlWriter->startElement('table:table-cell'); + $xmlWriter->writeAttribute('office:value-type', 'string'); + + $containerWriter = new Container($xmlWriter, $cell); + $containerWriter->write(); + + $xmlWriter->endElement(); // table:table-cell } - $this->xmlWriter->endElement(); // table:table-row + $xmlWriter->endElement(); // table:table-row } - $this->xmlWriter->endElement(); // table:table + $xmlWriter->endElement(); // table:table } } } diff --git a/src/PhpWord/Writer/ODText/Element/Text.php b/src/PhpWord/Writer/ODText/Element/Text.php index ee2fafab..e960dea8 100644 --- a/src/PhpWord/Writer/ODText/Element/Text.php +++ b/src/PhpWord/Writer/ODText/Element/Text.php @@ -22,19 +22,17 @@ namespace PhpOffice\PhpWord\Writer\ODText\Element; * * @since 0.10.0 */ -class Text extends Element +class Text extends AbstractElement { /** * Write element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) { - return; - } - - $fontStyle = $this->element->getFontStyle(); - $paragraphStyle = $this->element->getParagraphStyle(); + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); + $fontStyle = $element->getFontStyle(); + $paragraphStyle = $element->getParagraphStyle(); // @todo Commented for TextRun. Should really checkout this value // $fStyleIsObject = ($fontStyle instanceof Font) ? true : false; @@ -45,31 +43,31 @@ class Text extends Element throw new Exception('PhpWord : $fStyleIsObject wouldn\'t be an object'); } else { if (!$this->withoutP) { - $this->xmlWriter->startElement('text:p'); // text:p + $xmlWriter->startElement('text:p'); // text:p } if (empty($fontStyle)) { if (empty($paragraphStyle)) { - $this->xmlWriter->writeAttribute('text:style-name', 'P1'); + $xmlWriter->writeAttribute('text:style-name', 'P1'); } elseif (is_string($paragraphStyle)) { - $this->xmlWriter->writeAttribute('text:style-name', $paragraphStyle); + $xmlWriter->writeAttribute('text:style-name', $paragraphStyle); } - $this->xmlWriter->writeRaw($this->element->getText()); + $xmlWriter->writeRaw($element->getText()); } else { if (empty($paragraphStyle)) { - $this->xmlWriter->writeAttribute('text:style-name', 'Standard'); + $xmlWriter->writeAttribute('text:style-name', 'Standard'); } elseif (is_string($paragraphStyle)) { - $this->xmlWriter->writeAttribute('text:style-name', $paragraphStyle); + $xmlWriter->writeAttribute('text:style-name', $paragraphStyle); } // text:span - $this->xmlWriter->startElement('text:span'); + $xmlWriter->startElement('text:span'); if (is_string($fontStyle)) { - $this->xmlWriter->writeAttribute('text:style-name', $fontStyle); + $xmlWriter->writeAttribute('text:style-name', $fontStyle); } - $this->xmlWriter->writeRaw($this->element->getText()); - $this->xmlWriter->endElement(); + $xmlWriter->writeRaw($element->getText()); + $xmlWriter->endElement(); } if (!$this->withoutP) { - $this->xmlWriter->endElement(); // text:p + $xmlWriter->endElement(); // text:p } } } diff --git a/src/PhpWord/Writer/ODText/Element/TextBreak.php b/src/PhpWord/Writer/ODText/Element/TextBreak.php index 8e9b83fa..ef3186f4 100644 --- a/src/PhpWord/Writer/ODText/Element/TextBreak.php +++ b/src/PhpWord/Writer/ODText/Element/TextBreak.php @@ -22,15 +22,17 @@ namespace PhpOffice\PhpWord\Writer\ODText\Element; * * @since 0.10.0 */ -class TextBreak extends Element +class TextBreak extends AbstractElement { /** * Write element */ public function write() { - $this->xmlWriter->startElement('text:p'); - $this->xmlWriter->writeAttribute('text:style-name', 'Standard'); - $this->xmlWriter->endElement(); + $xmlWriter = $this->getXmlWriter(); + + $xmlWriter->startElement('text:p'); + $xmlWriter->writeAttribute('text:style-name', 'Standard'); + $xmlWriter->endElement(); } } diff --git a/src/PhpWord/Writer/ODText/Element/TextRun.php b/src/PhpWord/Writer/ODText/Element/TextRun.php index b03fc353..52240e8f 100644 --- a/src/PhpWord/Writer/ODText/Element/TextRun.php +++ b/src/PhpWord/Writer/ODText/Element/TextRun.php @@ -17,38 +17,26 @@ namespace PhpOffice\PhpWord\Writer\ODText\Element; -use PhpOffice\PhpWord\Element\Link as LinkElement; -use PhpOffice\PhpWord\Element\Text as TextElement; - /** * TextRun element writer * * @since 0.10.0 */ -class TextRun extends Element +class TextRun extends AbstractElement { /** * Write element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\TextRun) { - return; - } + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); - $elements = $this->element->getElements(); - $this->xmlWriter->startElement('text:p'); - if (count($elements) > 0) { - foreach ($elements as $element) { - if ($element instanceof TextElement) { - $elementWriter = new Text($this->xmlWriter, $this->parentWriter, $element, true); - $elementWriter->write(); - } elseif ($element instanceof LinkElement) { - $elementWriter = new Link($this->xmlWriter, $this->parentWriter, $element, true); - $elementWriter->write(); - } - } - } - $this->xmlWriter->endElement(); + $xmlWriter->startElement('text:p'); + + $containerWriter = new Container($xmlWriter, $element); + $containerWriter->write(); + + $xmlWriter->endElement(); } } diff --git a/src/PhpWord/Writer/ODText/Part/Content.php b/src/PhpWord/Writer/ODText/Part/Content.php index c607bb2b..d5f1353c 100644 --- a/src/PhpWord/Writer/ODText/Part/Content.php +++ b/src/PhpWord/Writer/ODText/Part/Content.php @@ -25,7 +25,7 @@ use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Style; -use PhpOffice\PhpWord\Writer\ODText\Element\Element as ElementWriter; +use PhpOffice\PhpWord\Writer\ODText\Element\Container; /** * ODText content part writer: content.xml @@ -73,12 +73,9 @@ class Content extends AbstractPart $sectionCount = count($sections); if ($sectionCount > 0) { foreach ($sections as $section) { - $elements = $section->getElements(); // $xmlWriter->startElement('text:section'); - foreach ($elements as $element) { - $elementWriter = new ElementWriter($xmlWriter, $this, $element, false); - $elementWriter->write(); - } + $containerWriter = new Container($xmlWriter, $section); + $containerWriter->write(); // $xmlWriter->endElement(); // text:section } } @@ -104,7 +101,7 @@ class Content extends AbstractPart if (preg_match('#^T[0-9]+$#', $styleName) != 0 || preg_match('#^P[0-9]+$#', $styleName) != 0 ) { - $styleClass = str_replace('Style', 'Writer\\ODText\\Style', get_class($style)); + $styleClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Style\\' . basename(get_class($style)); if (class_exists($styleClass)) { $styleWriter = new $styleClass($xmlWriter, $style); $styleWriter->setIsAuto(true); diff --git a/src/PhpWord/Writer/ODText/Part/Styles.php b/src/PhpWord/Writer/ODText/Part/Styles.php index cc47dbb4..bc4bf4f3 100644 --- a/src/PhpWord/Writer/ODText/Part/Styles.php +++ b/src/PhpWord/Writer/ODText/Part/Styles.php @@ -91,7 +91,7 @@ class Styles extends AbstractPart if (preg_match('#^T[0-9]+$#', $styleName) == 0 && preg_match('#^P[0-9]+$#', $styleName) == 0 ) { - $styleClass = str_replace('Style', 'Writer\\ODText\\Style', get_class($style)); + $styleClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Style\\' . basename(get_class($style)); if (class_exists($styleClass)) { $styleWriter = new $styleClass($xmlWriter, $style); $styleWriter->write(); diff --git a/src/PhpWord/Writer/ODText/Style/Font.php b/src/PhpWord/Writer/ODText/Style/Font.php index 74bb5ec9..1a6d4fc0 100644 --- a/src/PhpWord/Writer/ODText/Style/Font.php +++ b/src/PhpWord/Writer/ODText/Style/Font.php @@ -34,37 +34,40 @@ class Font extends AbstractStyle */ public function write() { - if (!($this->style instanceof \PhpOffice\PhpWord\Style\Font)) { + if (is_null($style = $this->getStyle())) { return; } + $xmlWriter = $this->getXmlWriter(); - $this->xmlWriter->startElement('style:style'); - $this->xmlWriter->writeAttribute('style:name', $this->style->getStyleName()); - $this->xmlWriter->writeAttribute('style:family', 'text'); - $this->xmlWriter->startElement('style:text-properties'); - if ($this->style->getName()) { - $this->xmlWriter->writeAttribute('style:font-name', $this->style->getName()); - $this->xmlWriter->writeAttribute('style:font-name-complex', $this->style->getName()); - } - if ($this->style->getSize()) { - $this->xmlWriter->writeAttribute('fo:font-size', ($this->style->getSize()) . 'pt'); - $this->xmlWriter->writeAttribute('style:font-size-asian', ($this->style->getSize()) . 'pt'); - $this->xmlWriter->writeAttribute('style:font-size-complex', ($this->style->getSize()) . 'pt'); - } - if ($this->style->getColor()) { - $this->xmlWriter->writeAttribute('fo:color', '#' . $this->style->getColor()); - } - if ($this->style->isItalic()) { - $this->xmlWriter->writeAttribute('fo:font-style', 'italic'); - $this->xmlWriter->writeAttribute('style:font-style-asian', 'italic'); - $this->xmlWriter->writeAttribute('style:font-style-complex', 'italic'); - } - if ($this->style->isBold()) { - $this->xmlWriter->writeAttribute('fo:font-weight', 'bold'); - $this->xmlWriter->writeAttribute('style:font-weight-asian', 'bold'); - } - $this->xmlWriter->endElement(); // style:text-properties - $this->xmlWriter->endElement(); // style:style + $xmlWriter->startElement('style:style'); + $xmlWriter->writeAttribute('style:name', $style->getStyleName()); + $xmlWriter->writeAttribute('style:family', 'text'); + $xmlWriter->startElement('style:text-properties'); + + // Name + $font = $style->getName(); + $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'); + + // Color + $color = $style->getColor(); + $xmlWriter->writeAttributeIf($color, 'fo:color', '#' . $color); + + // Bold & italic + $xmlWriter->writeAttributeIf($style->isBold(), 'fo:font-weight', 'bold'); + $xmlWriter->writeAttributeIf($style->isBold(), 'style:font-weight-asian', 'bold'); + $xmlWriter->writeAttributeIf($style->isItalic(), 'fo:font-style', 'italic'); + $xmlWriter->writeAttributeIf($style->isItalic(), 'style:font-style-asian', 'italic'); + $xmlWriter->writeAttributeIf($style->isItalic(), 'style:font-style-complex', 'italic'); + + $xmlWriter->endElement(); // style:text-properties + $xmlWriter->endElement(); // style:style } /** diff --git a/src/PhpWord/Writer/ODText/Style/Paragraph.php b/src/PhpWord/Writer/ODText/Style/Paragraph.php index d5156365..143627cd 100644 --- a/src/PhpWord/Writer/ODText/Style/Paragraph.php +++ b/src/PhpWord/Writer/ODText/Style/Paragraph.php @@ -34,32 +34,33 @@ class Paragraph extends AbstractStyle */ public function write() { - if (!($this->style instanceof \PhpOffice\PhpWord\Style\Paragraph)) { + if (is_null($style = $this->getStyle())) { return; } + $xmlWriter = $this->getXmlWriter(); - $marginTop = is_null($this->style->getSpaceBefore()) ? '0' : round(17.6 / $this->style->getSpaceBefore(), 2); - $marginBottom = is_null($this->style->getSpaceAfter()) ? '0' : round(17.6 / $this->style->getSpaceAfter(), 2); + $marginTop = is_null($style->getSpaceBefore()) ? '0' : round(17.6 / $style->getSpaceBefore(), 2); + $marginBottom = is_null($style->getSpaceAfter()) ? '0' : round(17.6 / $style->getSpaceAfter(), 2); - $this->xmlWriter->startElement('style:style'); - $this->xmlWriter->writeAttribute('style:name', $this->style->getStyleName()); - $this->xmlWriter->writeAttribute('style:family', 'paragraph'); + $xmlWriter->startElement('style:style'); + $xmlWriter->writeAttribute('style:name', $style->getStyleName()); + $xmlWriter->writeAttribute('style:family', 'paragraph'); if ($this->isAuto) { - $this->xmlWriter->writeAttribute('style:parent-style-name', 'Standard'); - $this->xmlWriter->writeAttribute('style:master-page-name', 'Standard'); + $xmlWriter->writeAttribute('style:parent-style-name', 'Standard'); + $xmlWriter->writeAttribute('style:master-page-name', 'Standard'); } - $this->xmlWriter->startElement('style:paragraph-properties'); + $xmlWriter->startElement('style:paragraph-properties'); if ($this->isAuto) { - $this->xmlWriter->writeAttribute('style:page-number', 'auto'); + $xmlWriter->writeAttribute('style:page-number', 'auto'); } else { - $this->xmlWriter->writeAttribute('fo:margin-top', $marginTop . 'cm'); - $this->xmlWriter->writeAttribute('fo:margin-bottom', $marginBottom . 'cm'); - $this->xmlWriter->writeAttribute('fo:text-align', $this->style->getAlign()); + $xmlWriter->writeAttribute('fo:margin-top', $marginTop . 'cm'); + $xmlWriter->writeAttribute('fo:margin-bottom', $marginBottom . 'cm'); + $xmlWriter->writeAttribute('fo:text-align', $style->getAlign()); } - $this->xmlWriter->endElement(); //style:paragraph-properties + $xmlWriter->endElement(); //style:paragraph-properties - $this->xmlWriter->endElement(); //style:style + $xmlWriter->endElement(); //style:style } /** diff --git a/src/PhpWord/Writer/PDF.php b/src/PhpWord/Writer/PDF.php index be253ec3..45e8a412 100644 --- a/src/PhpWord/Writer/PDF.php +++ b/src/PhpWord/Writer/PDF.php @@ -52,7 +52,7 @@ class PDF set_include_path(get_include_path() . PATH_SEPARATOR . $pdfLibraryPath); } - $rendererName = 'PhpOffice\\PhpWord\\Writer\\PDF\\' . $pdfLibraryName; + $rendererName = get_class($this) . '\\' . $pdfLibraryName; $this->renderer = new $rendererName($phpWord); } diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index 4a63fdcc..829a6d81 100644 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -66,25 +66,24 @@ class RTF extends AbstractWriter implements WriterInterface /** * Save PhpWord to file * - * @param string $pFilename + * @param string $filename * @throws \PhpOffice\PhpWord\Exception\Exception */ - public function save($pFilename = null) + public function save($filename = null) { - if (!is_null($this->phpWord)) { - $pFilename = $this->getTempFile($pFilename); - - $hFile = fopen($pFilename, 'w'); - if ($hFile !== false) { - fwrite($hFile, $this->writeDocument()); - fclose($hFile); - } else { - throw new Exception("Can't open file"); - } - $this->cleanupTempFile(); - } else { - throw new Exception("PhpWord object unassigned."); + if (is_null($this->phpWord)) { + throw new Exception('PhpWord object unassigned.'); } + + $filename = $this->getTempFile($filename); + $hFile = fopen($filename, 'w'); + if ($hFile !== false) { + fwrite($hFile, $this->writeDocument()); + fclose($hFile); + } else { + throw new Exception("Can't open file"); + } + $this->cleanupTempFile(); } /** diff --git a/src/PhpWord/Writer/RTF/Element/Element.php b/src/PhpWord/Writer/RTF/Element/Element.php index fc192ffe..f4d09e36 100644 --- a/src/PhpWord/Writer/RTF/Element/Element.php +++ b/src/PhpWord/Writer/RTF/Element/Element.php @@ -67,14 +67,13 @@ class Element */ public function write() { - $rtfText = ''; - $elmName = str_replace('PhpOffice\\PhpWord\\Element\\', '', get_class($this->element)); - $elmWriterClass = 'PhpOffice\\PhpWord\\Writer\\RTF\\Element\\' . $elmName; - if (class_exists($elmWriterClass) === true) { - $elmWriter = new $elmWriterClass($this->parentWriter, $this->element, $this->withoutP); - $rtfText = $elmWriter->write(); + $content = ''; + $writerClass = dirname(get_class($this)) . '\\' . basename(get_class($this->element)); + if (class_exists($writerClass)) { + $writer = new $writerClass($this->parentWriter, $this->element, $this->withoutP); + $content = $writer->write(); } - return $rtfText; + return $content; } } diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index 07eef989..44317bd2 100644 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -72,7 +72,7 @@ class Word2007 extends AbstractWriter implements WriterInterface 'Endnotes' => '', ); foreach (array_keys($this->parts) as $partName) { - $partClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Part\\' . $partName; + $partClass = get_class($this) . '\\Part\\' . $partName; if (class_exists($partClass)) { $partObject = new $partClass(); $partObject->setParentWriter($this); @@ -91,57 +91,57 @@ class Word2007 extends AbstractWriter implements WriterInterface */ public function save($filename = null) { - if (!is_null($this->phpWord)) { - $filename = $this->getTempFile($filename); - $objZip = $this->getZipArchive($filename); - - // Content types - $this->contentTypes['default'] = array( - 'rels' => 'application/vnd.openxmlformats-package.relationships+xml', - 'xml' => 'application/xml', - ); - - // Add section media files - $sectionMedia = Media::getElements('section'); - if (!empty($sectionMedia)) { - $this->addFilesToPackage($objZip, $sectionMedia); - $this->registerContentTypes($sectionMedia); - foreach ($sectionMedia as $element) { - $this->relationships[] = $element; - } - } - - // Add header/footer media files & relations - $this->addHeaderFooterMedia($objZip, 'header'); - $this->addHeaderFooterMedia($objZip, 'footer'); - - // Add header/footer contents - $rId = Media::countElements('section') + 6; // @see Rels::writeDocRels for 6 first elements - $sections = $this->phpWord->getSections(); - foreach ($sections as $section) { - $this->addHeaderFooterContent($section, $objZip, 'header', $rId); - $this->addHeaderFooterContent($section, $objZip, 'footer', $rId); - } - - $this->addNotes($objZip, $rId, 'footnote'); - $this->addNotes($objZip, $rId, 'endnote'); - - // Write parts - foreach ($this->parts as $partName => $fileName) { - if ($fileName != '') { - $objZip->addFromString($fileName, $this->getWriterPart($partName)->write()); - } - } - - // Close file - if ($objZip->close() === false) { - throw new Exception("Could not close zip file $filename."); - } - - $this->cleanupTempFile(); - } else { - throw new Exception("PhpWord object unassigned."); + if (is_null($this->phpWord)) { + throw new Exception('PhpWord object unassigned.'); } + + $filename = $this->getTempFile($filename); + $objZip = $this->getZipArchive($filename); + + // Content types + $this->contentTypes['default'] = array( + 'rels' => 'application/vnd.openxmlformats-package.relationships+xml', + 'xml' => 'application/xml', + ); + + // Add section media files + $sectionMedia = Media::getElements('section'); + if (!empty($sectionMedia)) { + $this->addFilesToPackage($objZip, $sectionMedia); + $this->registerContentTypes($sectionMedia); + foreach ($sectionMedia as $element) { + $this->relationships[] = $element; + } + } + + // Add header/footer media files & relations + $this->addHeaderFooterMedia($objZip, 'header'); + $this->addHeaderFooterMedia($objZip, 'footer'); + + // Add header/footer contents + $rId = Media::countElements('section') + 6; // @see Rels::writeDocRels for 6 first elements + $sections = $this->phpWord->getSections(); + foreach ($sections as $section) { + $this->addHeaderFooterContent($section, $objZip, 'header', $rId); + $this->addHeaderFooterContent($section, $objZip, 'footer', $rId); + } + + $this->addNotes($objZip, $rId, 'footnote'); + $this->addNotes($objZip, $rId, 'endnote'); + + // Write parts + foreach ($this->parts as $partName => $fileName) { + if ($fileName != '') { + $objZip->addFromString($fileName, $this->getWriterPart($partName)->write()); + } + } + + // Close file + if ($objZip->close() === false) { + throw new Exception("Could not close zip file $filename."); + } + + $this->cleanupTempFile(); } /** diff --git a/src/PhpWord/Writer/Word2007/Element/Element.php b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php similarity index 57% rename from src/PhpWord/Writer/Word2007/Element/Element.php rename to src/PhpWord/Writer/Word2007/Element/AbstractElement.php index 89d92ac5..67cffdc6 100644 --- a/src/PhpWord/Writer/Word2007/Element/Element.php +++ b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php @@ -17,37 +17,30 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; -use PhpOffice\PhpWord\Element\AbstractElement; +use PhpOffice\PhpWord\Element\AbstractElement as Element; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Shared\XMLWriter; -use PhpOffice\PhpWord\Writer\Word2007\Part\AbstractPart; /** - * Generic element writer + * Abstract element writer * * @since 0.10.0 */ -class Element +abstract class AbstractElement { /** * XML writer * * @var \PhpOffice\PhpWord\Shared\XMLWriter */ - protected $xmlWriter; - - /** - * Parent writer - * - * @var \PhpOffice\PhpWord\Writer\Word2007\Part\AbstractPart - */ - protected $parentWriter; + private $xmlWriter; /** * Element * * @var \PhpOffice\PhpWord\Element\AbstractElement */ - protected $element; + private $element; /** * Without paragraph @@ -56,32 +49,49 @@ class Element */ protected $withoutP = false; + /** + * Write element + */ + abstract public function write(); + /** * Create new instance * - * @param \PhpOffice\PhpWord\Element\AbstractElement $element * @param bool $withoutP */ - public function __construct(XMLWriter $xmlWriter, AbstractPart $parentWriter, $element, $withoutP = false) + public function __construct(XMLWriter $xmlWriter, Element $element, $withoutP = false) { $this->xmlWriter = $xmlWriter; - $this->parentWriter = $parentWriter; $this->element = $element; $this->withoutP = $withoutP; } /** - * Write element + * Get XML Writer * - * @return string + * @return \PhpOffice\PhpWord\Shared\XMLWriter */ - public function write() + protected function getXmlWriter() { - $elmName = str_replace('PhpOffice\\PhpWord\\Element\\', '', get_class($this->element)); - $elmWriterClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $elmName; - if (class_exists($elmWriterClass) === true) { - $elmWriter = new $elmWriterClass($this->xmlWriter, $this->parentWriter, $this->element, $this->withoutP); - $elmWriter->write(); + return $this->xmlWriter; + } + + /** + * Get Element + * + * @return \PhpOffice\PhpWord\Element\AbstractElement + */ + protected function getElement() + { + if (!is_null($this->element)) { + $elementClass = 'PhpOffice\\PhpWord\\Element\\' . basename(get_class($this->element)); + if ($this->element instanceof $elementClass) { + return $this->element; + } else { + throw new Exception('No valid element assigned.'); + } + } else { + throw new Exception('No element assigned.'); } } } diff --git a/src/PhpWord/Writer/Word2007/Element/CheckBox.php b/src/PhpWord/Writer/Word2007/Element/CheckBox.php index ef8c166d..96e210b5 100644 --- a/src/PhpWord/Writer/Word2007/Element/CheckBox.php +++ b/src/PhpWord/Writer/Word2007/Element/CheckBox.php @@ -18,91 +18,77 @@ 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; /** * CheckBox element writer * * @since 0.10.0 */ -class CheckBox extends Element +class CheckBox extends Text { /** * Write element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\CheckBox) { - return; - } + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); - $name = htmlspecialchars($this->element->getName()); + $name = htmlspecialchars($element->getName()); $name = String::controlCharacterPHP2OOXML($name); - $text = htmlspecialchars($this->element->getText()); + $text = htmlspecialchars($element->getText()); $text = String::controlCharacterPHP2OOXML($text); - $fontStyle = $this->element->getFontStyle(); - $paragraphStyle = $this->element->getParagraphStyle(); - if (!$this->withoutP) { - $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle); - $styleWriter->setIsInline(true); + $this->writeOpeningWP(); - $this->xmlWriter->startElement('w:p'); - $styleWriter->write(); - } + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'begin'); + $xmlWriter->startElement('w:ffData'); + $xmlWriter->startElement('w:name'); + $xmlWriter->writeAttribute('w:val', $name); + $xmlWriter->endElement(); //w:name + $xmlWriter->writeAttribute('w:enabled', ''); + $xmlWriter->startElement('w:calcOnExit'); + $xmlWriter->writeAttribute('w:val', '0'); + $xmlWriter->endElement(); //w:calcOnExit + $xmlWriter->startElement('w:checkBox'); + $xmlWriter->writeAttribute('w:sizeAuto', ''); + $xmlWriter->startElement('w:default'); + $xmlWriter->writeAttribute('w:val', 0); + $xmlWriter->endElement(); //w:default + $xmlWriter->endElement(); //w:checkBox + $xmlWriter->endElement(); // w:ffData + $xmlWriter->endElement(); // w:fldChar + $xmlWriter->endElement(); // w:r - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:fldChar'); - $this->xmlWriter->writeAttribute('w:fldCharType', 'begin'); - $this->xmlWriter->startElement('w:ffData'); - $this->xmlWriter->startElement('w:name'); - $this->xmlWriter->writeAttribute('w:val', $name); - $this->xmlWriter->endElement(); //w:name - $this->xmlWriter->writeAttribute('w:enabled', ''); - $this->xmlWriter->startElement('w:calcOnExit'); - $this->xmlWriter->writeAttribute('w:val', '0'); - $this->xmlWriter->endElement(); //w:calcOnExit - $this->xmlWriter->startElement('w:checkBox'); - $this->xmlWriter->writeAttribute('w:sizeAuto', ''); - $this->xmlWriter->startElement('w:default'); - $this->xmlWriter->writeAttribute('w:val', 0); - $this->xmlWriter->endElement(); //w:default - $this->xmlWriter->endElement(); //w:checkBox - $this->xmlWriter->endElement(); // w:ffData - $this->xmlWriter->endElement(); // w:fldChar - $this->xmlWriter->endElement(); // w:r + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:instrText'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->writeRaw(' FORMCHECKBOX '); + $xmlWriter->endElement();// w:instrText + $xmlWriter->endElement(); // w:r + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'seperate'); + $xmlWriter->endElement();// w:fldChar + $xmlWriter->endElement(); // w:r + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'end'); + $xmlWriter->endElement();// w:fldChar + $xmlWriter->endElement(); // w:r - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:instrText'); - $this->xmlWriter->writeAttribute('xml:space', 'preserve'); - $this->xmlWriter->writeRaw(' FORMCHECKBOX '); - $this->xmlWriter->endElement();// w:instrText - $this->xmlWriter->endElement(); // w:r - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:fldChar'); - $this->xmlWriter->writeAttribute('w:fldCharType', 'seperate'); - $this->xmlWriter->endElement();// w:fldChar - $this->xmlWriter->endElement(); // w:r - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:fldChar'); - $this->xmlWriter->writeAttribute('w:fldCharType', 'end'); - $this->xmlWriter->endElement();// w:fldChar - $this->xmlWriter->endElement(); // w:r + $xmlWriter->startElement('w:r'); - $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle); - $styleWriter->setIsInline(true); + $this->writeFontStyle(); - $this->xmlWriter->startElement('w:r'); - $styleWriter->write(); - $this->xmlWriter->startElement('w:t'); - $this->xmlWriter->writeAttribute('xml:space', 'preserve'); - $this->xmlWriter->writeRaw($text); - $this->xmlWriter->endElement(); // w:t - $this->xmlWriter->endElement(); // w:r + $xmlWriter->startElement('w:t'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->writeRaw($text); + $xmlWriter->endElement(); // w:t + $xmlWriter->endElement(); // w:r - if (!$this->withoutP) { - $this->xmlWriter->endElement(); // w:p - } + $this->writeEndingWP(); } } diff --git a/src/PhpWord/Writer/Word2007/Element/Container.php b/src/PhpWord/Writer/Word2007/Element/Container.php new file mode 100644 index 00000000..7baf7e3b --- /dev/null +++ b/src/PhpWord/Writer/Word2007/Element/Container.php @@ -0,0 +1,58 @@ +getXmlWriter(); + $element = $this->getElement(); + + // Loop through subelements + $containerClass = basename(get_class($element)); + $subelements = $element->getElements(); + $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false; + if (count($subelements) > 0) { + foreach ($subelements as $subelement) { + $writerClass = dirname(get_class($this)) . '\\' . basename(get_class($subelement)); + if (class_exists($writerClass)) { + $writer = new $writerClass($xmlWriter, $subelement, $withoutP); + $writer->write(); + } + } + } else { + // Special case for Cell: They have to contain a TextBreak at least + if ($containerClass == 'Cell') { + $writerClass = dirname(get_class($this)) . '\\TextBreak'; + $writer = new $writerClass($xmlWriter, new TextBreakElement(), $withoutP); + $writer->write(); + } + } + } +} diff --git a/src/PhpWord/Writer/Word2007/Element/Footnote.php b/src/PhpWord/Writer/Word2007/Element/Footnote.php index f1f276aa..646f70ed 100644 --- a/src/PhpWord/Writer/Word2007/Element/Footnote.php +++ b/src/PhpWord/Writer/Word2007/Element/Footnote.php @@ -22,7 +22,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; * * @since 0.10.0 */ -class Footnote extends Element +class Footnote extends Text { /** * Reference type footnoteReference|endnoteReference @@ -36,25 +36,22 @@ class Footnote extends Element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Footnote) { - return; - } + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); - if (!$this->withoutP) { - $this->xmlWriter->startElement('w:p'); - } - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:rPr'); - $this->xmlWriter->startElement('w:rStyle'); - $this->xmlWriter->writeAttribute('w:val', ucfirst($this->referenceType)); - $this->xmlWriter->endElement(); // w:rStyle - $this->xmlWriter->endElement(); // w:rPr - $this->xmlWriter->startElement("w:{$this->referenceType}"); - $this->xmlWriter->writeAttribute('w:id', $this->element->getRelationId()); - $this->xmlWriter->endElement(); // w:$referenceType - $this->xmlWriter->endElement(); // w:r - if (!$this->withoutP) { - $this->xmlWriter->endElement(); // w:p - } + $this->writeOpeningWP(); + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:rPr'); + $xmlWriter->startElement('w:rStyle'); + $xmlWriter->writeAttribute('w:val', ucfirst($this->referenceType)); + $xmlWriter->endElement(); // w:rStyle + $xmlWriter->endElement(); // w:rPr + $xmlWriter->startElement("w:{$this->referenceType}"); + $xmlWriter->writeAttribute('w:id', $element->getRelationId()); + $xmlWriter->endElement(); // w:$referenceType + $xmlWriter->endElement(); // w:r + + $this->writeEndingWP(); } } diff --git a/src/PhpWord/Writer/Word2007/Element/Image.php b/src/PhpWord/Writer/Word2007/Element/Image.php index 6b948f1e..c7bd8d7d 100644 --- a/src/PhpWord/Writer/Word2007/Element/Image.php +++ b/src/PhpWord/Writer/Word2007/Element/Image.php @@ -24,14 +24,16 @@ use PhpOffice\PhpWord\Writer\Word2007\Style\Image as ImageStyleWriter; * * @since 0.10.0 */ -class Image extends Element +class Image extends AbstractElement { /** * Write element */ public function write() { - if ($this->element->isWatermark()) { + $element = $this->getElement(); + + if ($element->isWatermark()) { $this->writeWatermark(); } else { $this->writeImage(); @@ -43,41 +45,40 @@ class Image extends Element */ private function writeImage() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Image) { - return; - } + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); - $rId = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0); - $style = $this->element->getStyle(); - $styleWriter = new ImageStyleWriter($this->xmlWriter, $style); + $rId = $element->getRelationId() + ($element->isInSection() ? 6 : 0); + $style = $element->getStyle(); + $styleWriter = new ImageStyleWriter($xmlWriter, $style); if (!$this->withoutP) { - $this->xmlWriter->startElement('w:p'); + $xmlWriter->startElement('w:p'); if (!is_null($style->getAlign())) { - $this->xmlWriter->startElement('w:pPr'); - $this->xmlWriter->startElement('w:jc'); - $this->xmlWriter->writeAttribute('w:val', $style->getAlign()); - $this->xmlWriter->endElement(); // w:jc - $this->xmlWriter->endElement(); // w:pPr + $xmlWriter->startElement('w:pPr'); + $xmlWriter->startElement('w:jc'); + $xmlWriter->writeAttribute('w:val', $style->getAlign()); + $xmlWriter->endElement(); // w:jc + $xmlWriter->endElement(); // w:pPr } } - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:pict'); - $this->xmlWriter->startElement('v:shape'); - $this->xmlWriter->writeAttribute('type', '#_x0000_t75'); + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:pict'); + $xmlWriter->startElement('v:shape'); + $xmlWriter->writeAttribute('type', '#_x0000_t75'); $styleWriter->write(); - $this->xmlWriter->startElement('v:imagedata'); - $this->xmlWriter->writeAttribute('r:id', 'rId' . $rId); - $this->xmlWriter->writeAttribute('o:title', ''); - $this->xmlWriter->endElement(); // v:imagedata + $xmlWriter->startElement('v:imagedata'); + $xmlWriter->writeAttribute('r:id', 'rId' . $rId); + $xmlWriter->writeAttribute('o:title', ''); + $xmlWriter->endElement(); // v:imagedata $styleWriter->writeW10Wrap(); - $this->xmlWriter->endElement(); // v:shape - $this->xmlWriter->endElement(); // w:pict - $this->xmlWriter->endElement(); // w:r + $xmlWriter->endElement(); // v:shape + $xmlWriter->endElement(); // w:pict + $xmlWriter->endElement(); // w:r if (!$this->withoutP) { - $this->xmlWriter->endElement(); // w:p + $xmlWriter->endElement(); // w:p } } /** @@ -85,24 +86,27 @@ class Image extends Element */ private function writeWatermark() { - $rId = $this->element->getRelationId(); - $style = $this->element->getStyle(); - $style->setPositioning('absolute'); - $styleWriter = new ImageStyleWriter($this->xmlWriter, $style); + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); - $this->xmlWriter->startElement('w:p'); - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:pict'); - $this->xmlWriter->startElement('v:shape'); - $this->xmlWriter->writeAttribute('type', '#_x0000_t75'); + $rId = $element->getRelationId(); + $style = $element->getStyle(); + $style->setPositioning('absolute'); + $styleWriter = new ImageStyleWriter($xmlWriter, $style); + + $xmlWriter->startElement('w:p'); + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:pict'); + $xmlWriter->startElement('v:shape'); + $xmlWriter->writeAttribute('type', '#_x0000_t75'); $styleWriter->write(); - $this->xmlWriter->startElement('v:imagedata'); - $this->xmlWriter->writeAttribute('r:id', 'rId' . $rId); - $this->xmlWriter->writeAttribute('o:title', ''); - $this->xmlWriter->endElement(); // v:imagedata - $this->xmlWriter->endElement(); // v:shape - $this->xmlWriter->endElement(); // w:pict - $this->xmlWriter->endElement(); // w:r - $this->xmlWriter->endElement(); // w:p + $xmlWriter->startElement('v:imagedata'); + $xmlWriter->writeAttribute('r:id', 'rId' . $rId); + $xmlWriter->writeAttribute('o:title', ''); + $xmlWriter->endElement(); // v:imagedata + $xmlWriter->endElement(); // v:shape + $xmlWriter->endElement(); // w:pict + $xmlWriter->endElement(); // w:r + $xmlWriter->endElement(); // w:p } } diff --git a/src/PhpWord/Writer/Word2007/Element/Link.php b/src/PhpWord/Writer/Word2007/Element/Link.php index 2e53b1a8..517e276d 100644 --- a/src/PhpWord/Writer/Word2007/Element/Link.php +++ b/src/PhpWord/Writer/Word2007/Element/Link.php @@ -17,53 +17,39 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; -use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter; -use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter; - /** * Link element writer * * @since 0.10.0 */ -class Link extends Element +class Link extends Text { /** * Write link element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Link) { - return; - } + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); - $rId = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0); - $fontStyle = $this->element->getFontStyle(); - $paragraphStyle = $this->element->getParagraphStyle(); + $rId = $element->getRelationId() + ($element->isInSection() ? 6 : 0); - if (!$this->withoutP) { - $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle); - $styleWriter->setIsInline(true); + $this->writeOpeningWP(); - $this->xmlWriter->startElement('w:p'); - $styleWriter->write(); - } + $xmlWriter->startElement('w:hyperlink'); + $xmlWriter->writeAttribute('r:id', 'rId' . $rId); + $xmlWriter->writeAttribute('w:history', '1'); + $xmlWriter->startElement('w:r'); - $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle); - $styleWriter->setIsInline(true); + $this->writeFontStyle(); - $this->xmlWriter->startElement('w:hyperlink'); - $this->xmlWriter->writeAttribute('r:id', 'rId' . $rId); - $this->xmlWriter->writeAttribute('w:history', '1'); - $this->xmlWriter->startElement('w:r'); - $styleWriter->write(); - $this->xmlWriter->startElement('w:t'); - $this->xmlWriter->writeAttribute('xml:space', 'preserve'); - $this->xmlWriter->writeRaw($this->element->getText()); - $this->xmlWriter->endElement(); // w:t - $this->xmlWriter->endElement(); // w:r - $this->xmlWriter->endElement(); // w:hyperlink - if (!$this->withoutP) { - $this->xmlWriter->endElement(); // w:p - } + $xmlWriter->startElement('w:t'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->writeRaw($element->getText()); + $xmlWriter->endElement(); // w:t + $xmlWriter->endElement(); // w:r + $xmlWriter->endElement(); // w:hyperlink + + $this->writeEndingWP(); } } diff --git a/src/PhpWord/Writer/Word2007/Element/ListItem.php b/src/PhpWord/Writer/Word2007/Element/ListItem.php index e4394862..e625114c 100644 --- a/src/PhpWord/Writer/Word2007/Element/ListItem.php +++ b/src/PhpWord/Writer/Word2007/Element/ListItem.php @@ -17,7 +17,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; -use PhpOffice\PhpWord\Writer\Word2007\Element\Element as ElementWriter; use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter; /** @@ -25,42 +24,41 @@ use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter; * * @since 0.10.0 */ -class ListItem extends Element +class ListItem extends AbstractElement { /** * Write list item element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\ListItem) { - return; - } + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); - $textObject = $this->element->getTextObject(); - $depth = $this->element->getDepth(); - $numId = $this->element->getStyle()->getNumId(); - $paragraphStyle = $textObject->getParagraphStyle(); - $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle); + $textObject = $element->getTextObject(); + + $styleWriter = new ParagraphStyleWriter($xmlWriter, $textObject->getParagraphStyle()); $styleWriter->setWithoutPPR(true); $styleWriter->setIsInline(true); - $this->xmlWriter->startElement('w:p'); + $xmlWriter->startElement('w:p'); - $this->xmlWriter->startElement('w:pPr'); + $xmlWriter->startElement('w:pPr'); $styleWriter->write(); - $this->xmlWriter->startElement('w:numPr'); - $this->xmlWriter->startElement('w:ilvl'); - $this->xmlWriter->writeAttribute('w:val', $depth); - $this->xmlWriter->endElement(); // w:ilvl - $this->xmlWriter->startElement('w:numId'); - $this->xmlWriter->writeAttribute('w:val', $numId); - $this->xmlWriter->endElement(); // w:numId - $this->xmlWriter->endElement(); // w:numPr - $this->xmlWriter->endElement(); // w:pPr - $elementWriter = new ElementWriter($this->xmlWriter, $this->parentWriter, $textObject, true); + $xmlWriter->startElement('w:numPr'); + $xmlWriter->startElement('w:ilvl'); + $xmlWriter->writeAttribute('w:val', $element->getDepth()); + $xmlWriter->endElement(); // w:ilvl + $xmlWriter->startElement('w:numId'); + $xmlWriter->writeAttribute('w:val', $element->getStyle()->getNumId()); + $xmlWriter->endElement(); // w:numId + $xmlWriter->endElement(); // w:numPr + + $xmlWriter->endElement(); // w:pPr + + $elementWriter = new Text($xmlWriter, $textObject, true); $elementWriter->write(); - $this->xmlWriter->endElement(); // w:p + $xmlWriter->endElement(); // w:p } } diff --git a/src/PhpWord/Writer/Word2007/Element/Object.php b/src/PhpWord/Writer/Word2007/Element/Object.php index db90d78c..5f56a704 100644 --- a/src/PhpWord/Writer/Word2007/Element/Object.php +++ b/src/PhpWord/Writer/Word2007/Element/Object.php @@ -22,60 +22,59 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; * * @since 0.10.0 */ -class Object extends Element +class Object extends AbstractElement { /** * Write object element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Object) { - return; - } + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); - $rIdObject = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0); - $rIdImage = $this->element->getImageRelationId() + ($this->element->isInSection() ? 6 : 0); + $rIdObject = $element->getRelationId() + ($element->isInSection() ? 6 : 0); + $rIdImage = $element->getImageRelationId() + ($element->isInSection() ? 6 : 0); $shapeId = md5($rIdObject . '_' . $rIdImage); - $objectId = $this->element->getRelationId() + 1325353440; - $style = $this->element->getStyle(); + $objectId = $element->getRelationId() + 1325353440; + $style = $element->getStyle(); $align = $style->getAlign(); if (!$this->withoutP) { - $this->xmlWriter->startElement('w:p'); + $xmlWriter->startElement('w:p'); } if (!is_null($align)) { - $this->xmlWriter->startElement('w:pPr'); - $this->xmlWriter->startElement('w:jc'); - $this->xmlWriter->writeAttribute('w:val', $align); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:pPr'); + $xmlWriter->startElement('w:jc'); + $xmlWriter->writeAttribute('w:val', $align); + $xmlWriter->endElement(); + $xmlWriter->endElement(); } - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:object'); - $this->xmlWriter->writeAttribute('w:dxaOrig', '249'); - $this->xmlWriter->writeAttribute('w:dyaOrig', '160'); - $this->xmlWriter->startElement('v:shape'); - $this->xmlWriter->writeAttribute('id', $shapeId); - $this->xmlWriter->writeAttribute('type', '#_x0000_t75'); - $this->xmlWriter->writeAttribute('style', 'width:104px;height:67px'); - $this->xmlWriter->writeAttribute('o:ole', ''); - $this->xmlWriter->startElement('v:imagedata'); - $this->xmlWriter->writeAttribute('r:id', 'rId' . $rIdImage); - $this->xmlWriter->writeAttribute('o:title', ''); - $this->xmlWriter->endElement(); // v:imagedata - $this->xmlWriter->endElement(); // v:shape - $this->xmlWriter->startElement('o:OLEObject'); - $this->xmlWriter->writeAttribute('Type', 'Embed'); - $this->xmlWriter->writeAttribute('ProgID', 'Package'); - $this->xmlWriter->writeAttribute('ShapeID', $shapeId); - $this->xmlWriter->writeAttribute('DrawAspect', 'Icon'); - $this->xmlWriter->writeAttribute('ObjectID', '_' . $objectId); - $this->xmlWriter->writeAttribute('r:id', 'rId' . $rIdObject); - $this->xmlWriter->endElement(); // o:OLEObject - $this->xmlWriter->endElement(); // w:object - $this->xmlWriter->endElement(); // w:r + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:object'); + $xmlWriter->writeAttribute('w:dxaOrig', '249'); + $xmlWriter->writeAttribute('w:dyaOrig', '160'); + $xmlWriter->startElement('v:shape'); + $xmlWriter->writeAttribute('id', $shapeId); + $xmlWriter->writeAttribute('type', '#_x0000_t75'); + $xmlWriter->writeAttribute('style', 'width:104px;height:67px'); + $xmlWriter->writeAttribute('o:ole', ''); + $xmlWriter->startElement('v:imagedata'); + $xmlWriter->writeAttribute('r:id', 'rId' . $rIdImage); + $xmlWriter->writeAttribute('o:title', ''); + $xmlWriter->endElement(); // v:imagedata + $xmlWriter->endElement(); // v:shape + $xmlWriter->startElement('o:OLEObject'); + $xmlWriter->writeAttribute('Type', 'Embed'); + $xmlWriter->writeAttribute('ProgID', 'Package'); + $xmlWriter->writeAttribute('ShapeID', $shapeId); + $xmlWriter->writeAttribute('DrawAspect', 'Icon'); + $xmlWriter->writeAttribute('ObjectID', '_' . $objectId); + $xmlWriter->writeAttribute('r:id', 'rId' . $rIdObject); + $xmlWriter->endElement(); // o:OLEObject + $xmlWriter->endElement(); // w:object + $xmlWriter->endElement(); // w:r if (!$this->withoutP) { - $this->xmlWriter->endElement(); // w:p + $xmlWriter->endElement(); // w:p } } } diff --git a/src/PhpWord/Writer/Word2007/Element/PageBreak.php b/src/PhpWord/Writer/Word2007/Element/PageBreak.php index 47d6d335..fb285ac6 100644 --- a/src/PhpWord/Writer/Word2007/Element/PageBreak.php +++ b/src/PhpWord/Writer/Word2007/Element/PageBreak.php @@ -22,19 +22,21 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; * * @since 0.10.0 */ -class PageBreak extends Element +class PageBreak extends AbstractElement { /** * Write element */ public function write() { - $this->xmlWriter->startElement('w:p'); - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:br'); - $this->xmlWriter->writeAttribute('w:type', 'page'); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $xmlWriter = $this->getXmlWriter(); + + $xmlWriter->startElement('w:p'); + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:br'); + $xmlWriter->writeAttribute('w:type', 'page'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + $xmlWriter->endElement(); } } diff --git a/src/PhpWord/Writer/Word2007/Element/PreserveText.php b/src/PhpWord/Writer/Word2007/Element/PreserveText.php index 63c03899..b3b00d16 100644 --- a/src/PhpWord/Writer/Word2007/Element/PreserveText.php +++ b/src/PhpWord/Writer/Word2007/Element/PreserveText.php @@ -18,85 +18,76 @@ 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; /** * PreserveText element writer * * @since 0.10.0 */ -class PreserveText extends Element +class PreserveText extends Text { /** * Write preserve text element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\PreserveText) { - return; - } + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); - $fontStyle = $this->element->getFontStyle(); - $paragraphStyle = $this->element->getParagraphStyle(); - $texts = $this->element->getText(); + $texts = $element->getText(); if (!is_array($texts)) { $texts = array($texts); } - $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle); - $styleWriter->setIsInline(true); - - $this->xmlWriter->startElement('w:p'); - $styleWriter->write(); + $this->writeOpeningWP(); foreach ($texts as $text) { if (substr($text, 0, 1) == '{') { $text = substr($text, 1, -1); - $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle); - $styleWriter->setIsInline(true); - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:fldChar'); - $this->xmlWriter->writeAttribute('w:fldCharType', 'begin'); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'begin'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); - $this->xmlWriter->startElement('w:r'); - $styleWriter->write(); - $this->xmlWriter->startElement('w:instrText'); - $this->xmlWriter->writeAttribute('xml:space', 'preserve'); - $this->xmlWriter->writeRaw($text); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:fldChar'); - $this->xmlWriter->writeAttribute('w:fldCharType', 'separate'); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $this->writeFontStyle(); - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:fldChar'); - $this->xmlWriter->writeAttribute('w:fldCharType', 'end'); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:instrText'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->writeRaw($text); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'separate'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'end'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); } else { $text = htmlspecialchars($text); $text = String::controlCharacterPHP2OOXML($text); - $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle); - $styleWriter->setIsInline(true); - $this->xmlWriter->startElement('w:r'); - $styleWriter->write(); - $this->xmlWriter->startElement('w:t'); - $this->xmlWriter->writeAttribute('xml:space', 'preserve'); - $this->xmlWriter->writeRaw($text); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:r'); + + $this->writeFontStyle(); + + $xmlWriter->startElement('w:t'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->writeRaw($text); + $xmlWriter->endElement(); + $xmlWriter->endElement(); } } - $this->xmlWriter->endElement(); // p + $this->writeEndingWP(); } } diff --git a/src/PhpWord/Writer/Word2007/Element/TOC.php b/src/PhpWord/Writer/Word2007/Element/TOC.php index 6f1e2cdf..28d7bc31 100644 --- a/src/PhpWord/Writer/Word2007/Element/TOC.php +++ b/src/PhpWord/Writer/Word2007/Element/TOC.php @@ -27,18 +27,17 @@ use PhpOffice\PhpWord\Writer\Word2007\Style\Tab as TabStyleWriter; * * @since 0.10.0 */ -class TOC extends Element +class TOC extends AbstractElement { /** * Write element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\TOC) { - return; - } + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); - $titles = $this->element->getTitles(); + $titles = $element->getTitles(); $writeFieldMark = true; foreach ($titles as $title) { @@ -48,13 +47,13 @@ class TOC extends Element } } - $this->xmlWriter->startElement('w:p'); - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:fldChar'); - $this->xmlWriter->writeAttribute('w:fldCharType', 'end'); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:p'); + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'end'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + $xmlWriter->endElement(); } /** @@ -65,13 +64,16 @@ class TOC extends Element */ private function writeTitle($title, $writeFieldMark) { - $tocStyle = $this->element->getStyleTOC(); - $fontStyle = $this->element->getStyleFont(); + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); + + $tocStyle = $element->getStyleTOC(); + $fontStyle = $element->getStyleFont(); $isObject = ($fontStyle instanceof Font) ? true : false; $anchor = '_Toc' . ($title->getBookmarkId() + 252634154); $indent = ($title->getDepth() - 1) * $tocStyle->getIndent(); - $this->xmlWriter->startElement('w:p'); + $xmlWriter->startElement('w:p'); // Write style and field mark $this->writeStyle($indent); @@ -80,47 +82,47 @@ class TOC extends Element } // Hyperlink - $this->xmlWriter->startElement('w:hyperlink'); - $this->xmlWriter->writeAttribute('w:anchor', $anchor); - $this->xmlWriter->writeAttribute('w:history', '1'); + $xmlWriter->startElement('w:hyperlink'); + $xmlWriter->writeAttribute('w:anchor', $anchor); + $xmlWriter->writeAttribute('w:history', '1'); // Title text - $this->xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:r'); if ($isObject) { - $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle); + $styleWriter = new FontStyleWriter($xmlWriter, $fontStyle); $styleWriter->write(); } - $this->xmlWriter->startElement('w:t'); - $this->xmlWriter->writeRaw($title->getText()); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); // w:r + $xmlWriter->startElement('w:t'); + $xmlWriter->writeRaw($title->getText()); + $xmlWriter->endElement(); + $xmlWriter->endElement(); // w:r - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->writeElement('w:tab', null); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:r'); + $xmlWriter->writeElement('w:tab', null); + $xmlWriter->endElement(); - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:fldChar'); - $this->xmlWriter->writeAttribute('w:fldCharType', 'begin'); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'begin'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:instrText'); - $this->xmlWriter->writeAttribute('xml:space', 'preserve'); - $this->xmlWriter->writeRaw('PAGEREF ' . $anchor . ' \h'); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:instrText'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->writeRaw('PAGEREF ' . $anchor . ' \h'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:fldChar'); - $this->xmlWriter->writeAttribute('w:fldCharType', 'end'); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'end'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); - $this->xmlWriter->endElement(); // w:hyperlink + $xmlWriter->endElement(); // w:hyperlink - $this->xmlWriter->endElement(); // w:p + $xmlWriter->endElement(); // w:p } /** @@ -130,41 +132,44 @@ class TOC extends Element */ private function writeStyle($indent) { - $tocStyle = $this->element->getStyleTOC(); - $fontStyle = $this->element->getStyleFont(); + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); + + $tocStyle = $element->getStyleTOC(); + $fontStyle = $element->getStyleFont(); $isObject = ($fontStyle instanceof Font) ? true : false; - $this->xmlWriter->startElement('w:pPr'); + $xmlWriter->startElement('w:pPr'); // Paragraph if ($isObject && !is_null($fontStyle->getParagraphStyle())) { - $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $fontStyle->getParagraphStyle()); + $styleWriter = new ParagraphStyleWriter($xmlWriter, $fontStyle->getParagraphStyle()); $styleWriter->write(); } // Font if (!empty($fontStyle) && !$isObject) { - $this->xmlWriter->startElement('w:rPr'); - $this->xmlWriter->startElement('w:rStyle'); - $this->xmlWriter->writeAttribute('w:val', $fontStyle); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); // w:rPr + $xmlWriter->startElement('w:rPr'); + $xmlWriter->startElement('w:rStyle'); + $xmlWriter->writeAttribute('w:val', $fontStyle); + $xmlWriter->endElement(); + $xmlWriter->endElement(); // w:rPr } // Tab - $this->xmlWriter->startElement('w:tabs'); - $styleWriter = new TabStyleWriter($this->xmlWriter, $tocStyle); + $xmlWriter->startElement('w:tabs'); + $styleWriter = new TabStyleWriter($xmlWriter, $tocStyle); $styleWriter->write(); - $this->xmlWriter->endElement(); + $xmlWriter->endElement(); // Indent if ($indent > 0) { - $this->xmlWriter->startElement('w:ind'); - $this->xmlWriter->writeAttribute('w:left', $indent); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:ind'); + $xmlWriter->writeAttribute('w:left', $indent); + $xmlWriter->endElement(); } - $this->xmlWriter->endElement(); // w:pPr + $xmlWriter->endElement(); // w:pPr } /** @@ -172,26 +177,29 @@ class TOC extends Element */ private function writeFieldMark() { - $minDepth = $this->element->getMinDepth(); - $maxDepth = $this->element->getMaxDepth(); + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:fldChar'); - $this->xmlWriter->writeAttribute('w:fldCharType', 'begin'); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $minDepth = $element->getMinDepth(); + $maxDepth = $element->getMaxDepth(); - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:instrText'); - $this->xmlWriter->writeAttribute('xml:space', 'preserve'); - $this->xmlWriter->writeRaw("TOC \o {$minDepth}-{$maxDepth} \h \z \u"); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'begin'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:fldChar'); - $this->xmlWriter->writeAttribute('w:fldCharType', 'separate'); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:instrText'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->writeRaw("TOC \o {$minDepth}-{$maxDepth} \h \z \u"); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'separate'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); } } diff --git a/src/PhpWord/Writer/Word2007/Element/Table.php b/src/PhpWord/Writer/Word2007/Element/Table.php index 3a29f410..5c82114a 100644 --- a/src/PhpWord/Writer/Word2007/Element/Table.php +++ b/src/PhpWord/Writer/Word2007/Element/Table.php @@ -29,22 +29,21 @@ use PhpOffice\PhpWord\Writer\Word2007\Style\Table as TableStyleWriter; * * @since 0.10.0 */ -class Table extends Element +class Table extends AbstractElement { /** * Write element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Table) { - return; - } + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); - $rows = $this->element->getRows(); + $rows = $element->getRows(); $rowCount = count($rows); if ($rowCount > 0) { - $this->xmlWriter->startElement('w:tbl'); + $xmlWriter->startElement('w:tbl'); // Table grid $cellWidths = array(); @@ -59,37 +58,37 @@ class Table extends Element $cellWidths[] = $cell->getWidth(); } } - $this->xmlWriter->startElement('w:tblGrid'); + $xmlWriter->startElement('w:tblGrid'); foreach ($cellWidths as $width) { - $this->xmlWriter->startElement('w:gridCol'); + $xmlWriter->startElement('w:gridCol'); if (!is_null($width)) { - $this->xmlWriter->writeAttribute('w:w', $width); - $this->xmlWriter->writeAttribute('w:type', 'dxa'); + $xmlWriter->writeAttribute('w:w', $width); + $xmlWriter->writeAttribute('w:type', 'dxa'); } - $this->xmlWriter->endElement(); + $xmlWriter->endElement(); } - $this->xmlWriter->endElement(); // w:tblGrid + $xmlWriter->endElement(); // w:tblGrid // Table style - $tblStyle = $this->element->getStyle(); - $tblWidth = $this->element->getWidth(); + $tblStyle = $element->getStyle(); + $tblWidth = $element->getWidth(); if ($tblStyle instanceof TableStyle) { - $styleWriter = new TableStyleWriter($this->xmlWriter, $tblStyle); + $styleWriter = new TableStyleWriter($xmlWriter, $tblStyle); $styleWriter->setIsFullStyle(false); $styleWriter->write(); } else { if (!empty($tblStyle)) { - $this->xmlWriter->startElement('w:tblPr'); - $this->xmlWriter->startElement('w:tblStyle'); - $this->xmlWriter->writeAttribute('w:val', $tblStyle); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:tblPr'); + $xmlWriter->startElement('w:tblStyle'); + $xmlWriter->writeAttribute('w:val', $tblStyle); + $xmlWriter->endElement(); if (!is_null($tblWidth)) { - $this->xmlWriter->startElement('w:tblW'); - $this->xmlWriter->writeAttribute('w:w', $tblWidth); - $this->xmlWriter->writeAttribute('w:type', 'pct'); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:tblW'); + $xmlWriter->writeAttribute('w:w', $tblWidth); + $xmlWriter->writeAttribute('w:type', 'pct'); + $xmlWriter->endElement(); } - $this->xmlWriter->endElement(); + $xmlWriter->endElement(); } } @@ -97,7 +96,7 @@ class Table extends Element for ($i = 0; $i < $rowCount; $i++) { $this->writeRow($rows[$i]); } - $this->xmlWriter->endElement(); + $xmlWriter->endElement(); } } @@ -106,34 +105,36 @@ class Table extends Element */ private function writeRow(RowElement $row) { + $xmlWriter = $this->getXmlWriter(); + $height = $row->getHeight(); $rowStyle = $row->getStyle(); - $this->xmlWriter->startElement('w:tr'); + $xmlWriter->startElement('w:tr'); if (!is_null($height) || $rowStyle->isTblHeader() || $rowStyle->isCantSplit()) { - $this->xmlWriter->startElement('w:trPr'); + $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(); + $xmlWriter->startElement('w:trHeight'); + $xmlWriter->writeAttribute('w:val', $height); + $xmlWriter->writeAttribute('w:hRule', ($rowStyle->isExactHeight() ? 'exact' : 'atLeast')); + $xmlWriter->endElement(); } if ($rowStyle->isTblHeader()) { - $this->xmlWriter->startElement('w:tblHeader'); - $this->xmlWriter->writeAttribute('w:val', '1'); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:tblHeader'); + $xmlWriter->writeAttribute('w:val', '1'); + $xmlWriter->endElement(); } if ($rowStyle->isCantSplit()) { - $this->xmlWriter->startElement('w:cantSplit'); - $this->xmlWriter->writeAttribute('w:val', '1'); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:cantSplit'); + $xmlWriter->writeAttribute('w:val', '1'); + $xmlWriter->endElement(); } - $this->xmlWriter->endElement(); + $xmlWriter->endElement(); } foreach ($row->getCells() as $cell) { $this->writeCell($cell); } - $this->xmlWriter->endElement(); // w:tr + $xmlWriter->endElement(); // w:tr } /** @@ -141,20 +142,25 @@ class Table extends Element */ private function writeCell(CellElement $cell) { + $xmlWriter = $this->getXmlWriter(); + $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 + $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 if ($cellStyle instanceof CellStyle) { - $styleWriter = new CellStyleWriter($this->xmlWriter, $cellStyle); + $styleWriter = new CellStyleWriter($xmlWriter, $cellStyle); $styleWriter->write(); } - $this->xmlWriter->endElement(); // w:tcPr - $this->parentWriter->writeContainerElements($this->xmlWriter, $cell); - $this->xmlWriter->endElement(); // w:tc + $xmlWriter->endElement(); // w:tcPr + + $containerWriter = new Container($xmlWriter, $cell); + $containerWriter->write(); + + $xmlWriter->endElement(); // w:tc } } diff --git a/src/PhpWord/Writer/Word2007/Element/Text.php b/src/PhpWord/Writer/Word2007/Element/Text.php index c271174f..d6402dbf 100644 --- a/src/PhpWord/Writer/Word2007/Element/Text.php +++ b/src/PhpWord/Writer/Word2007/Element/Text.php @@ -26,41 +26,88 @@ use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter; * * @since 0.10.0 */ -class Text extends Element +class Text extends AbstractElement { /** * Write text element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) { - return; - } + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); - $fontStyle = $this->element->getFontStyle(); - $paragraphStyle = $this->element->getParagraphStyle(); - $text = htmlspecialchars($this->element->getText()); + $text = htmlspecialchars($element->getText()); $text = String::controlCharacterPHP2OOXML($text); - if (!$this->withoutP) { - $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle); - $styleWriter->setIsInline(true); + $this->writeOpeningWP(); - $this->xmlWriter->startElement('w:p'); - $styleWriter->write(); - } - $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle); - $styleWriter->setIsInline(true); + $xmlWriter->startElement('w:r'); + + $this->writeFontStyle(); + + $xmlWriter->startElement('w:t'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->writeRaw($text); + $xmlWriter->endElement(); + $xmlWriter->endElement(); // w:r + + $this->writeEndingWP(); + } + + /** + * Write opening + */ + protected function writeOpeningWP() + { + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); - $this->xmlWriter->startElement('w:r'); - $styleWriter->write(); - $this->xmlWriter->startElement('w:t'); - $this->xmlWriter->writeAttribute('xml:space', 'preserve'); - $this->xmlWriter->writeRaw($text); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); // w:r if (!$this->withoutP) { - $this->xmlWriter->endElement(); // w:p + $xmlWriter->startElement('w:p'); + + if (method_exists($element, 'getParagraphStyle')) { + $this->writeParagraphStyle(); + } } } + + /** + * Write ending + */ + protected function writeEndingWP() + { + $xmlWriter = $this->getXmlWriter(); + + if (!$this->withoutP) { + $xmlWriter->endElement(); // w:p + } + } + + /** + * Write ending + */ + protected function writeParagraphStyle() + { + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); + + $paragraphStyle = $element->getParagraphStyle(); + $styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle); + $styleWriter->setIsInline(true); + $styleWriter->write(); + } + + /** + * Write ending + */ + protected function writeFontStyle() + { + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); + + $fontStyle = $element->getFontStyle(); + $styleWriter = new FontStyleWriter($xmlWriter, $fontStyle); + $styleWriter->setIsInline(true); + $styleWriter->write(); + } } diff --git a/src/PhpWord/Writer/Word2007/Element/TextBreak.php b/src/PhpWord/Writer/Word2007/Element/TextBreak.php index 99dece43..05de2917 100644 --- a/src/PhpWord/Writer/Word2007/Element/TextBreak.php +++ b/src/PhpWord/Writer/Word2007/Element/TextBreak.php @@ -17,50 +17,34 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; -use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter; -use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter; - /** * TextBreak element writer * * @since 0.10.0 */ -class TextBreak extends Element +class TextBreak extends Text { /** * Write text break element */ public function write() { + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); + if (!$this->withoutP) { - $hasStyle = false; - $fontStyle = null; - $paragraphStyle = null; - if (!is_null($this->element)) { - $fontStyle = $this->element->getFontStyle(); - $paragraphStyle = $this->element->getParagraphStyle(); - $hasStyle = !is_null($fontStyle) || !is_null($paragraphStyle); - } + $hasStyle = $element->hasStyle(); if ($hasStyle) { - $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle); - $styleWriter->setIsInline(true); - - $this->xmlWriter->startElement('w:p'); - $styleWriter->write(); - if (!is_null($fontStyle)) { - $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle); - $styleWriter->setIsInline(true); - - $this->xmlWriter->startElement('w:pPr'); - $styleWriter->write(); - $this->xmlWriter->endElement(); // w:pPr - } - $this->xmlWriter->endElement(); // w:p + $this->writeOpeningWP(); + $xmlWriter->startElement('w:pPr'); + $this->writeFontStyle(); + $xmlWriter->endElement(); // w:pPr + $this->writeEndingWP(); } else { - $this->xmlWriter->writeElement('w:p'); + $xmlWriter->writeElement('w:p'); } } else { - $this->xmlWriter->writeElement('w:br'); + $xmlWriter->writeElement('w:br'); } } } diff --git a/src/PhpWord/Writer/Word2007/Element/TextRun.php b/src/PhpWord/Writer/Word2007/Element/TextRun.php index d4b2d291..0343b16c 100644 --- a/src/PhpWord/Writer/Word2007/Element/TextRun.php +++ b/src/PhpWord/Writer/Word2007/Element/TextRun.php @@ -24,24 +24,21 @@ use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter; * * @since 0.10.0 */ -class TextRun extends Element +class TextRun extends Text { /** * Write textrun element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\TextRun) { - return; - } + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); - $paragraphStyle = $this->element->getParagraphStyle(); - $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle); - $styleWriter->setIsInline(true); + $this->writeOpeningWP(); - $this->xmlWriter->startElement('w:p'); - $styleWriter->write(); - $this->parentWriter->writeContainerElements($this->xmlWriter, $this->element); - $this->xmlWriter->endElement(); // w:p + $containerWriter = new Container($xmlWriter, $element); + $containerWriter->write(); + + $this->writeEndingWP(); } } diff --git a/src/PhpWord/Writer/Word2007/Element/Title.php b/src/PhpWord/Writer/Word2007/Element/Title.php index e1c7a0ae..c5eda5fd 100644 --- a/src/PhpWord/Writer/Word2007/Element/Title.php +++ b/src/PhpWord/Writer/Word2007/Element/Title.php @@ -24,54 +24,54 @@ use PhpOffice\PhpWord\Shared\String; * * @since 0.10.0 */ -class Title extends Element +class Title extends AbstractElement { /** * Write title element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Title) { - return; - } + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); - $bookmarkId = $this->element->getBookmarkId(); + $bookmarkId = $element->getBookmarkId(); $anchor = '_Toc' . ($bookmarkId + 252634154); - $style = $this->element->getStyle(); - $text = htmlspecialchars($this->element->getText()); + $style = $element->getStyle(); + + $text = htmlspecialchars($element->getText()); $text = String::controlCharacterPHP2OOXML($text); - $this->xmlWriter->startElement('w:p'); + $xmlWriter->startElement('w:p'); if (!empty($style)) { - $this->xmlWriter->startElement('w:pPr'); - $this->xmlWriter->startElement('w:pStyle'); - $this->xmlWriter->writeAttribute('w:val', $style); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:pPr'); + $xmlWriter->startElement('w:pStyle'); + $xmlWriter->writeAttribute('w:val', $style); + $xmlWriter->endElement(); + $xmlWriter->endElement(); } - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:fldChar'); - $this->xmlWriter->writeAttribute('w:fldCharType', 'end'); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'end'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); - $this->xmlWriter->startElement('w:bookmarkStart'); - $this->xmlWriter->writeAttribute('w:id', $bookmarkId); - $this->xmlWriter->writeAttribute('w:name', $anchor); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:bookmarkStart'); + $xmlWriter->writeAttribute('w:id', $bookmarkId); + $xmlWriter->writeAttribute('w:name', $anchor); + $xmlWriter->endElement(); - $this->xmlWriter->startElement('w:r'); - $this->xmlWriter->startElement('w:t'); - $this->xmlWriter->writeRaw($text); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:t'); + $xmlWriter->writeRaw($text); + $xmlWriter->endElement(); + $xmlWriter->endElement(); - $this->xmlWriter->startElement('w:bookmarkEnd'); - $this->xmlWriter->writeAttribute('w:id', $bookmarkId); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:bookmarkEnd'); + $xmlWriter->writeAttribute('w:id', $bookmarkId); + $xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $xmlWriter->endElement(); } } diff --git a/src/PhpWord/Writer/Word2007/Part/AbstractPart.php b/src/PhpWord/Writer/Word2007/Part/AbstractPart.php index 141dd33a..b0454956 100644 --- a/src/PhpWord/Writer/Word2007/Part/AbstractPart.php +++ b/src/PhpWord/Writer/Word2007/Part/AbstractPart.php @@ -17,11 +17,8 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; -use PhpOffice\PhpWord\Element\AbstractElement; -use PhpOffice\PhpWord\Element\TextBreak; use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Shared\XMLWriter; -use PhpOffice\PhpWord\Writer\Word2007\Element\Element as ElementWriter; use PhpOffice\PhpWord\Writer\WriterInterface; /** @@ -39,11 +36,11 @@ abstract class AbstractPart /** * Set parent writer * - * @param \PhpOffice\PhpWord\Writer\WriterInterface $pWriter + * @param \PhpOffice\PhpWord\Writer\WriterInterface $writer */ - public function setParentWriter(WriterInterface $pWriter = null) + public function setParentWriter(WriterInterface $writer = null) { - $this->parentWriter = $pWriter; + $this->parentWriter = $writer; } /** @@ -57,7 +54,7 @@ abstract class AbstractPart if (!is_null($this->parentWriter)) { return $this->parentWriter; } else { - throw new Exception("No parent WriterInterface assigned."); + throw new Exception('No parent WriterInterface assigned.'); } } @@ -80,48 +77,4 @@ abstract class AbstractPart return new XMLWriter(XMLWriter::STORAGE_MEMORY); } } - - /** - * Write container elements - * - * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param \PhpOffice\PhpWord\Element\AbstractElement $container - */ - public function writeContainerElements(XMLWriter $xmlWriter, AbstractElement $container) - { - // Check allowed elements - $elmCommon = array('Text', 'Link', 'TextBreak', 'Image', 'Object'); - $elmMainCell = array_merge($elmCommon, array('TextRun', 'ListItem', 'CheckBox')); - $allowedElements = array( - 'Section' => array_merge($elmMainCell, array('Table', 'Footnote', 'Title', 'PageBreak', 'TOC')), - 'Header' => array_merge($elmMainCell, array('Table', 'PreserveText')), - 'Footer' => array_merge($elmMainCell, array('Table', 'PreserveText')), - 'Cell' => array_merge($elmMainCell, array('PreserveText', 'Footnote', 'Endnote')), - 'TextRun' => array_merge($elmCommon, array('Footnote', 'Endnote')), - 'Footnote' => $elmCommon, - 'Endnote' => $elmCommon, - ); - $containerName = get_class($container); - $containerName = substr($containerName, strrpos($containerName, '\\') + 1); - if (!array_key_exists($containerName, $allowedElements)) { - throw new Exception('Invalid container.'); - } - - // Loop through elements - $elements = $container->getElements(); - $withoutP = in_array($containerName, array('TextRun', 'Footnote', 'Endnote')) ? true : false; - if (count($elements) > 0) { - foreach ($elements as $element) { - if ($element instanceof AbstractElement) { - $elementWriter = new ElementWriter($xmlWriter, $this, $element, $withoutP); - $elementWriter->write(); - } - } - } else { - if ($containerName == 'Cell') { - $elementWriter = new ElementWriter($xmlWriter, $this, new TextBreak(), $withoutP); - $elementWriter->write(); - } - } - } } diff --git a/src/PhpWord/Writer/Word2007/Part/Document.php b/src/PhpWord/Writer/Word2007/Part/Document.php index ccbd02d0..0f8a16af 100644 --- a/src/PhpWord/Writer/Word2007/Part/Document.php +++ b/src/PhpWord/Writer/Word2007/Part/Document.php @@ -20,6 +20,7 @@ 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; /** @@ -60,7 +61,10 @@ class Document extends AbstractPart if ($sectionCount > 0) { foreach ($sections as $section) { $currentSection++; - $this->writeContainerElements($xmlWriter, $section); + + $containerWriter = new Container($xmlWriter, $section); + $containerWriter->write(); + if ($currentSection == $sectionCount) { $this->writeSectionSettings($xmlWriter, $section); } else { diff --git a/src/PhpWord/Writer/Word2007/Part/Footer.php b/src/PhpWord/Writer/Word2007/Part/Footer.php index 12a7b1aa..db6a2b33 100644 --- a/src/PhpWord/Writer/Word2007/Part/Footer.php +++ b/src/PhpWord/Writer/Word2007/Part/Footer.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; +use PhpOffice\PhpWord\Writer\Word2007\Element\Container; + /** * Word2007 footer part writer: word/footerx.xml */ @@ -58,7 +60,8 @@ class Footer extends AbstractPart $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); $xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml'); - $this->writeContainerElements($xmlWriter, $this->element); + $containerWriter = new Container($xmlWriter, $this->element); + $containerWriter->write(); $xmlWriter->endElement(); // $this->rootElement diff --git a/src/PhpWord/Writer/Word2007/Part/Footnotes.php b/src/PhpWord/Writer/Word2007/Part/Footnotes.php index 7e09446d..1eeabc9c 100644 --- a/src/PhpWord/Writer/Word2007/Part/Footnotes.php +++ b/src/PhpWord/Writer/Word2007/Part/Footnotes.php @@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; use PhpOffice\PhpWord\Element\Footnote; use PhpOffice\PhpWord\Shared\XMLWriter; +use PhpOffice\PhpWord\Writer\Word2007\Element\Container; use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter; /** @@ -165,7 +166,8 @@ class Footnotes extends AbstractPart $xmlWriter->endElement(); // w:t $xmlWriter->endElement(); // w:r - $this->writeContainerElements($xmlWriter, $element); + $containerWriter = new Container($xmlWriter, $element); + $containerWriter->write(); $xmlWriter->endElement(); // w:p $xmlWriter->endElement(); // $this->elementNode diff --git a/src/PhpWord/Writer/Word2007/Part/Rels.php b/src/PhpWord/Writer/Word2007/Part/Rels.php index 95ac32b1..44e62013 100644 --- a/src/PhpWord/Writer/Word2007/Part/Rels.php +++ b/src/PhpWord/Writer/Word2007/Part/Rels.php @@ -27,11 +27,6 @@ use PhpOffice\PhpWord\Shared\XMLWriter; */ class Rels extends AbstractPart { - /** - * Base relationship URL - */ - const RELS_BASE = 'http://schemas.openxmlformats.org/'; - /** * Write part * @@ -62,7 +57,7 @@ class Rels extends AbstractPart { $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); $xmlWriter->startElement('Relationships'); - $xmlWriter->writeAttribute('xmlns', self::RELS_BASE . 'package/2006/relationships'); + $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); // XML files relationships if (is_array($xmlRels)) { @@ -73,17 +68,18 @@ class Rels extends AbstractPart // Media relationships if (is_array($mediaRels)) { - $mapping = array('image' => 'image', 'object' => 'oleObject', 'link' => 'hyperlink'); + $typePrefix = 'officeDocument/2006/relationships/'; + $typeMapping = array('image' => 'image', 'object' => 'oleObject', 'link' => 'hyperlink'); $targetPaths = array('image' => 'media/', 'object' => 'embeddings/'); + foreach ($mediaRels as $mediaRel) { $mediaType = $mediaRel['type']; - $type = array_key_exists($mediaType, $mapping) ? $mapping[$mediaType] : $mediaType; + $type = array_key_exists($mediaType, $typeMapping) ? $typeMapping[$mediaType] : $mediaType; $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); + $this->writeRel($xmlWriter, $relId++, $typePrefix . $type, $target, $targetMode); } } @@ -110,7 +106,7 @@ class Rels extends AbstractPart } $xmlWriter->startElement('Relationship'); $xmlWriter->writeAttribute('Id', $relId); - $xmlWriter->writeAttribute('Type', self::RELS_BASE . $type); + $xmlWriter->writeAttribute('Type', 'http://schemas.openxmlformats.org/' . $type); $xmlWriter->writeAttribute('Target', $target); if ($targetMode != '') { $xmlWriter->writeAttribute('TargetMode', $targetMode); diff --git a/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php b/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php index 41bbed3d..7213e4aa 100644 --- a/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php +++ b/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php @@ -17,6 +17,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Style; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Shared\XMLWriter; @@ -32,10 +33,10 @@ abstract class AbstractStyle * * @var \PhpOffice\PhpWord\Shared\XMLWriter */ - protected $xmlWriter; + private $xmlWriter; /** - * Style + * Style; set protected for a while * * @var string|\PhpOffice\PhpWord\Style\AbstractStyle */ @@ -57,6 +58,33 @@ abstract class AbstractStyle $this->style = $style; } + /** + * Get XML Writer + * + * @return \PhpOffice\PhpWord\Shared\XMLWriter + */ + protected function getXmlWriter() + { + return $this->xmlWriter; + } + + /** + * Get Style + * + * @return \PhpOffice\PhpWord\Style\AbstractStyle + */ + protected function getStyle() + { + if (!is_null($this->style)) { + $styleClass = 'PhpOffice\\PhpWord\\Style\\' . basename(get_class($this->style)); + if (is_object($this->style) && (!$this->style instanceof $styleClass)) { + throw new Exception('No valid style assigned.'); + } + } + + return $this->style; + } + /** * Convert twip value * @@ -73,27 +101,4 @@ 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/Cell.php b/src/PhpWord/Writer/Word2007/Style/Cell.php index 7021a449..5f38f77d 100644 --- a/src/PhpWord/Writer/Word2007/Style/Cell.php +++ b/src/PhpWord/Writer/Word2007/Style/Cell.php @@ -31,64 +31,42 @@ class Cell extends AbstractStyle */ public function write() { - if (!($this->style instanceof \PhpOffice\PhpWord\Style\Cell)) { + if (is_null($style = $this->getStyle())) { return; } - - $brdSz = $this->style->getBorderSize(); - $brdCol = $this->style->getBorderColor(); - $hasBorders = false; - for ($i = 0; $i < 4; $i++) { - if (!is_null($brdSz[$i])) { - $hasBorders = true; - break; - } - } - - // Border - if ($hasBorders) { - $mbWriter = new MarginBorder($this->xmlWriter); - $mbWriter->setSizes($brdSz); - $mbWriter->setColors($brdCol); - $mbWriter->setAttributes(array('defaultColor' => CellStyle::DEFAULT_BORDER_COLOR)); - - $this->xmlWriter->startElement('w:tcBorders'); - $mbWriter->write(); - $this->xmlWriter->endElement(); - } + $xmlWriter = $this->getXmlWriter(); // Text direction - if (!is_null($this->style->getTextDirection())) { - $this->xmlWriter->startElement('w:textDirection'); - $this->xmlWriter->writeAttribute('w:val', $this->style->getTextDirection()); - $this->xmlWriter->endElement(); + $textDir = $style->getTextDirection(); + $xmlWriter->writeElementIf(!is_null($textDir), 'w:textDirection', 'w:val', $textDir); + + // Vertical alignment + $vAlign = $style->getVAlign(); + $xmlWriter->writeElementIf(!is_null($vAlign), 'w:vAlign', 'w:val', $vAlign); + + // Border + if ($style->hasBorders()) { + $xmlWriter->startElement('w:tcBorders'); + + $styleWriter = new MarginBorder($xmlWriter); + $styleWriter->setSizes($style->getBorderSize()); + $styleWriter->setColors($style->getBorderColor()); + $styleWriter->setAttributes(array('defaultColor' => CellStyle::DEFAULT_BORDER_COLOR)); + $styleWriter->write(); + + $xmlWriter->endElement(); } // Shading - if (!is_null($this->style->getShading())) { - $styleWriter = new Shading($this->xmlWriter, $this->style->getShading()); + if (!is_null($style->getShading())) { + $styleWriter = new Shading($xmlWriter, $style->getShading()); $styleWriter->write(); } - // Alignment - if (!is_null($this->style->getVAlign())) { - $this->xmlWriter->startElement('w:vAlign'); - $this->xmlWriter->writeAttribute('w:val', $this->style->getVAlign()); - $this->xmlWriter->endElement(); - } - - // Colspan - if (!is_null($this->style->getGridSpan())) { - $this->xmlWriter->startElement('w:gridSpan'); - $this->xmlWriter->writeAttribute('w:val', $this->style->getGridSpan()); - $this->xmlWriter->endElement(); - } - - // Row span - if (!is_null($this->style->getVMerge())) { - $this->xmlWriter->startElement('w:vMerge'); - $this->xmlWriter->writeAttribute('w:val', $this->style->getVMerge()); - $this->xmlWriter->endElement(); - } + // Colspan & rowspan + $gridSpan = $style->getGridSpan(); + $vMerge = $style->getVMerge(); + $xmlWriter->writeElementIf(!is_null($gridSpan), 'w:gridSpan', 'w:val', $gridSpan); + $xmlWriter->writeElementIf(!is_null($vMerge), 'w:vMerge', 'w:val', $vMerge); } } diff --git a/src/PhpWord/Writer/Word2007/Style/Font.php b/src/PhpWord/Writer/Word2007/Style/Font.php index b127da31..aac53038 100644 --- a/src/PhpWord/Writer/Word2007/Style/Font.php +++ b/src/PhpWord/Writer/Word2007/Style/Font.php @@ -38,13 +38,15 @@ class Font extends AbstractStyle */ public function write() { + $xmlWriter = $this->getXmlWriter(); + $isStyleName = $this->isInline && !is_null($this->style) && is_string($this->style); if ($isStyleName) { - $this->xmlWriter->startElement('w:rPr'); - $this->xmlWriter->startElement('w:rStyle'); - $this->xmlWriter->writeAttribute('w:val', $this->style); - $this->xmlWriter->endElement(); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:rPr'); + $xmlWriter->startElement('w:rStyle'); + $xmlWriter->writeAttribute('w:val', $this->style); + $xmlWriter->endElement(); + $xmlWriter->endElement(); } else { $this->writeStyle(); } @@ -55,67 +57,65 @@ class Font extends AbstractStyle */ private function writeStyle() { - if (!($this->style instanceof \PhpOffice\PhpWord\Style\Font)) { + if (is_null($style = $this->getStyle())) { return; } + $xmlWriter = $this->getXmlWriter(); + $font = $style->getName(); + $color = $style->getColor(); + $size = $style->getSize(); + $underline = $style->getUnderline(); + $fgColor = $style->getFgColor(); + $hint = $style->getHint(); - $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'); + $xmlWriter->startElement('w:rPr'); // Font name/family if ($font != PhpWord::DEFAULT_FONT_NAME) { - $this->xmlWriter->startElement('w:rFonts'); - $this->xmlWriter->writeAttribute('w:ascii', $font); - $this->xmlWriter->writeAttribute('w:hAnsi', $font); - $this->xmlWriter->writeAttribute('w:eastAsia', $font); - $this->xmlWriter->writeAttribute('w:cs', $font); - //Font Content Type - if ($this->style->getHint() != PhpWord::DEFAULT_FONT_CONTENT_TYPE) { - $this->xmlWriter->writeAttribute('w:hint', $this->style->getHint()); - } - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:rFonts'); + $xmlWriter->writeAttribute('w:ascii', $font); + $xmlWriter->writeAttribute('w:hAnsi', $font); + $xmlWriter->writeAttribute('w:eastAsia', $font); + $xmlWriter->writeAttribute('w:cs', $font); + $xmlWriter->writeAttributeIf($hint != PhpWord::DEFAULT_FONT_CONTENT_TYPE, 'w:hint', $hint); + $xmlWriter->endElement(); } // Color - $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); + $xmlWriter->writeElementIf($color != PhpWord::DEFAULT_FONT_COLOR, 'w:color', 'w:val', $color); + $xmlWriter->writeElementIf($size != PhpWord::DEFAULT_FONT_SIZE, 'w:sz', 'w:val', $size * 2); + $xmlWriter->writeElementIf($size != PhpWord::DEFAULT_FONT_SIZE, 'w:szCs', 'w:val', $size * 2); // Bold, italic - $this->writeElementIf($this->style->isBold(), 'w:b'); - $this->writeElementIf($this->style->isItalic(), 'w:i'); - $this->writeElementIf($this->style->isItalic(), 'w:iCs'); + $xmlWriter->writeElementIf($style->isBold(), 'w:b'); + $xmlWriter->writeElementIf($style->isItalic(), 'w:i'); + $xmlWriter->writeElementIf($style->isItalic(), 'w:iCs'); // Strikethrough, double strikethrough - $this->writeElementIf($this->style->isStrikethrough(), 'w:strike'); - $this->writeElementIf($this->style->isDoubleStrikethrough(), 'w:dstrike'); + $xmlWriter->writeElementIf($style->isStrikethrough(), 'w:strike'); + $xmlWriter->writeElementIf($style->isDoubleStrikethrough(), 'w:dstrike'); // Small caps, all caps - $this->writeElementIf($this->style->isSmallCaps(), 'w:smallCaps'); - $this->writeElementIf($this->style->isAllCaps(), 'w:caps'); + $xmlWriter->writeElementIf($style->isSmallCaps(), 'w:smallCaps'); + $xmlWriter->writeElementIf($style->isAllCaps(), 'w:caps'); // Underline - $this->writeElementIf($underline != 'none', 'w:u', 'w:val', $underline); + $xmlWriter->writeElementIf($underline != 'none', 'w:u', 'w:val', $underline); // Foreground-Color - $this->writeElementIf(!is_null($fgColor), 'w:highlight', 'w:val', $fgColor); + $xmlWriter->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'); + $xmlWriter->writeElementIf($style->isSuperScript(), 'w:vertAlign', 'w:val', 'superscript'); + $xmlWriter->writeElementIf($style->isSubScript(), 'w:vertAlign', 'w:val', 'subscript'); // Background-Color - if (!is_null($this->style->getShading())) { - $styleWriter = new Shading($this->xmlWriter, $this->style->getShading()); + if (!is_null($style->getShading())) { + $styleWriter = new Shading($xmlWriter, $style->getShading()); $styleWriter->write(); } - $this->xmlWriter->endElement(); + $xmlWriter->endElement(); } /** diff --git a/src/PhpWord/Writer/Word2007/Style/Image.php b/src/PhpWord/Writer/Word2007/Style/Image.php index b5e23e9a..79b218d6 100644 --- a/src/PhpWord/Writer/Word2007/Style/Image.php +++ b/src/PhpWord/Writer/Word2007/Style/Image.php @@ -38,12 +38,12 @@ class Image extends AbstractStyle */ public function write() { - if (!$this->style instanceof \PhpOffice\PhpWord\Style\Image) { + if (is_null($style = $this->getStyle())) { return; } - - $wrapping = $this->style->getWrappingStyle(); - $positioning = $this->style->getPositioning(); + $xmlWriter = $this->getXmlWriter(); + $wrapping = $style->getWrappingStyle(); + $positioning = $style->getPositioning(); // Default style array $styleArray = array( @@ -60,10 +60,10 @@ class Image extends AbstractStyle $styleArray['mso-position-horizontal-relative'] = 'page'; $styleArray['mso-position-vertical-relative'] = 'page'; } elseif ($positioning == ImageStyle::POSITION_RELATIVE) { - $styleArray['mso-position-horizontal'] = $this->style->getPosHorizontal(); - $styleArray['mso-position-vertical'] = $this->style->getPosVertical(); - $styleArray['mso-position-horizontal-relative'] = $this->style->getPosHorizontalRel(); - $styleArray['mso-position-vertical-relative'] = $this->style->getPosVerticalRel(); + $styleArray['mso-position-horizontal'] = $style->getPosHorizontal(); + $styleArray['mso-position-vertical'] = $style->getPosVertical(); + $styleArray['mso-position-horizontal-relative'] = $style->getPosHorizontalRel(); + $styleArray['mso-position-vertical-relative'] = $style->getPosVerticalRel(); $styleArray['margin-left'] = 0; $styleArray['margin-top'] = 0; } @@ -88,7 +88,7 @@ class Image extends AbstractStyle $imageStyle = $this->assembleStyle($styleArray); - $this->xmlWriter->writeAttribute('style', $imageStyle); + $xmlWriter->writeAttribute('style', $imageStyle); } /** @@ -98,10 +98,12 @@ class Image extends AbstractStyle */ public function writeW10Wrap() { + $xmlWriter = $this->getXmlWriter(); + if (!is_null($this->w10wrap)) { - $this->xmlWriter->startElement('w10:wrap'); - $this->xmlWriter->writeAttribute('type', $this->w10wrap); - $this->xmlWriter->endElement(); // w10:wrap + $xmlWriter->startElement('w10:wrap'); + $xmlWriter->writeAttribute('type', $this->w10wrap); + $xmlWriter->endElement(); // w10:wrap } } diff --git a/src/PhpWord/Writer/Word2007/Style/Indentation.php b/src/PhpWord/Writer/Word2007/Style/Indentation.php index b05e5711..60fbd540 100644 --- a/src/PhpWord/Writer/Word2007/Style/Indentation.php +++ b/src/PhpWord/Writer/Word2007/Style/Indentation.php @@ -29,19 +29,18 @@ class Indentation extends AbstractStyle */ public function write() { - if (!($this->style instanceof \PhpOffice\PhpWord\Style\Indentation)) { + if (is_null($style = $this->getStyle())) { return; } + $xmlWriter = $this->getXmlWriter(); + $firstLine = $style->getFirstLine(); + $hanging = $style->getHanging(); - $this->xmlWriter->startElement('w:ind'); - $this->xmlWriter->writeAttribute('w:left', $this->convertTwip($this->style->getLeft())); - $this->xmlWriter->writeAttribute('w:right', $this->convertTwip($this->style->getRight())); - if (!is_null($this->style->getFirstLine())) { - $this->xmlWriter->writeAttribute('w:firstLine', $this->convertTwip($this->style->getFirstLine())); - } - if (!is_null($this->style->getHanging())) { - $this->xmlWriter->writeAttribute('w:hanging', $this->convertTwip($this->style->getHanging())); - } - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:ind'); + $xmlWriter->writeAttribute('w:left', $this->convertTwip($style->getLeft())); + $xmlWriter->writeAttribute('w:right', $this->convertTwip($style->getRight())); + $xmlWriter->writeAttributeIf(!is_null($firstLine), 'w:firstLine', $this->convertTwip($firstLine)); + $xmlWriter->writeAttributeIf(!is_null($hanging), 'w:hanging', $this->convertTwip($hanging)); + $xmlWriter->endElement(); } } diff --git a/src/PhpWord/Writer/Word2007/Style/LineNumbering.php b/src/PhpWord/Writer/Word2007/Style/LineNumbering.php index 74d4682d..c4c1216b 100644 --- a/src/PhpWord/Writer/Word2007/Style/LineNumbering.php +++ b/src/PhpWord/Writer/Word2007/Style/LineNumbering.php @@ -31,15 +31,16 @@ class LineNumbering extends AbstractStyle */ public function write() { - if (!($this->style instanceof \PhpOffice\PhpWord\Style\LineNumbering)) { + if (is_null($style = $this->getStyle())) { return; } + $xmlWriter = $this->getXmlWriter(); - $this->xmlWriter->startElement('w:lnNumType'); - $this->xmlWriter->writeAttribute('w:start', $this->style->getStart() - 1); - $this->xmlWriter->writeAttribute('w:countBy', $this->style->getIncrement()); - $this->xmlWriter->writeAttribute('w:distance', $this->style->getDistance()); - $this->xmlWriter->writeAttribute('w:restart', $this->style->getRestart()); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:lnNumType'); + $xmlWriter->writeAttribute('w:start', $style->getStart() - 1); + $xmlWriter->writeAttribute('w:countBy', $style->getIncrement()); + $xmlWriter->writeAttribute('w:distance', $style->getDistance()); + $xmlWriter->writeAttribute('w:restart', $style->getRestart()); + $xmlWriter->endElement(); } } diff --git a/src/PhpWord/Writer/Word2007/Style/MarginBorder.php b/src/PhpWord/Writer/Word2007/Style/MarginBorder.php index be2c115d..8f7c1c00 100644 --- a/src/PhpWord/Writer/Word2007/Style/MarginBorder.php +++ b/src/PhpWord/Writer/Word2007/Style/MarginBorder.php @@ -50,31 +50,33 @@ class MarginBorder extends AbstractStyle */ public function write() { + $xmlWriter = $this->getXmlWriter(); + $sides = array('top', 'left', 'right', 'bottom', 'insideH', 'insideV'); $sizeCount = count($this->sizes) - 1; for ($i = 0; $i < $sizeCount; $i++) { if (!is_null($this->sizes[$i])) { - $this->xmlWriter->startElement('w:' . $sides[$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']; } } - $this->xmlWriter->writeAttribute('w:val', 'single'); - $this->xmlWriter->writeAttribute('w:sz', $this->sizes[$i]); - $this->xmlWriter->writeAttribute('w:color', $this->colors[$i]); + $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)) { - $this->xmlWriter->writeAttribute('w:space', $this->attributes['space']); + $xmlWriter->writeAttribute('w:space', $this->attributes['space']); } } } else { - $this->xmlWriter->writeAttribute('w:w', $this->sizes[$i]); - $this->xmlWriter->writeAttribute('w:type', 'dxa'); + $xmlWriter->writeAttribute('w:w', $this->sizes[$i]); + $xmlWriter->writeAttribute('w:type', 'dxa'); } - $this->xmlWriter->endElement(); + $xmlWriter->endElement(); } } } diff --git a/src/PhpWord/Writer/Word2007/Style/Paragraph.php b/src/PhpWord/Writer/Word2007/Style/Paragraph.php index af66abed..c2caea11 100644 --- a/src/PhpWord/Writer/Word2007/Style/Paragraph.php +++ b/src/PhpWord/Writer/Word2007/Style/Paragraph.php @@ -43,16 +43,18 @@ class Paragraph extends AbstractStyle */ public function write() { + $xmlWriter = $this->getXmlWriter(); + $isStyleName = $this->isInline && !is_null($this->style) && is_string($this->style); if ($isStyleName) { if (!$this->withoutPPR) { - $this->xmlWriter->startElement('w:pPr'); + $xmlWriter->startElement('w:pPr'); } - $this->xmlWriter->startElement('w:pStyle'); - $this->xmlWriter->writeAttribute('w:val', $this->style); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:pStyle'); + $xmlWriter->writeAttribute('w:val', $this->style); + $xmlWriter->endElement(); if (!$this->withoutPPR) { - $this->xmlWriter->endElement(); + $xmlWriter->endElement(); } } else { $this->writeStyle(); @@ -64,52 +66,52 @@ class Paragraph extends AbstractStyle */ private function writeStyle() { - if (!($this->style instanceof \PhpOffice\PhpWord\Style\Paragraph)) { + if (is_null($style = $this->getStyle())) { return; } - - $align = $this->style->getAlign(); - $indentation = $this->style->getIndentation(); - $spacing = $this->style->getSpace(); - $tabs = $this->style->getTabs(); + $xmlWriter = $this->getXmlWriter(); + $align = $style->getAlign(); + $indentation = $style->getIndentation(); + $spacing = $style->getSpace(); + $tabs = $style->getTabs(); if (!$this->withoutPPR) { - $this->xmlWriter->startElement('w:pPr'); + $xmlWriter->startElement('w:pPr'); } // Alignment - $this->writeElementIf(!is_null($align), 'w:jc', 'w:val', $align); + $xmlWriter->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'); + $xmlWriter->writeElementIf(!$style->hasWidowControl(), 'w:widowControl', 'w:val', '0'); + $xmlWriter->writeElementIf($style->isKeepNext(), 'w:keepNext', 'w:val', '1'); + $xmlWriter->writeElementIf($style->isKeepLines(), 'w:keepLines', 'w:val', '1'); + $xmlWriter->writeElementIf($style->hasPageBreakBefore(), 'w:pageBreakBefore', 'w:val', '1'); // Indentation if (!is_null($indentation)) { - $styleWriter = new Indentation($this->xmlWriter, $indentation); + $styleWriter = new Indentation($xmlWriter, $indentation); $styleWriter->write(); } // Spacing if (!is_null($spacing)) { - $styleWriter = new Spacing($this->xmlWriter, $spacing); + $styleWriter = new Spacing($xmlWriter, $spacing); $styleWriter->write(); } // Tabs if (!empty($tabs)) { - $this->xmlWriter->startElement("w:tabs"); + $xmlWriter->startElement("w:tabs"); foreach ($tabs as $tab) { - $styleWriter = new Tab($this->xmlWriter, $tab); + $styleWriter = new Tab($xmlWriter, $tab); $styleWriter->write(); } - $this->xmlWriter->endElement(); + $xmlWriter->endElement(); } if (!$this->withoutPPR) { - $this->xmlWriter->endElement(); // w:pPr + $xmlWriter->endElement(); // w:pPr } } diff --git a/src/PhpWord/Writer/Word2007/Style/Section.php b/src/PhpWord/Writer/Word2007/Style/Section.php index 05fc1d08..b773564c 100644 --- a/src/PhpWord/Writer/Word2007/Style/Section.php +++ b/src/PhpWord/Writer/Word2007/Style/Section.php @@ -31,23 +31,21 @@ class Section extends AbstractStyle */ public function write() { - if (!($this->style instanceof \PhpOffice\PhpWord\Style\Section)) { + if (is_null($style = $this->getStyle())) { return; } + $xmlWriter = $this->getXmlWriter(); - // Section break - if (!is_null($this->style->getBreakType())) { - $this->xmlWriter->startElement('w:type'); - $this->xmlWriter->writeAttribute('w:val', $this->style->getBreakType()); - $this->xmlWriter->endElement(); - } + // Break type + $breakType = $style->getBreakType(); + $xmlWriter->writeElementIf(!is_null($breakType), 'w:type', 'w:val', $breakType); // Page size & orientation - $this->xmlWriter->startElement('w:pgSz'); - $this->xmlWriter->writeAttribute('w:orient', $this->style->getOrientation()); - $this->xmlWriter->writeAttribute('w:w', $this->style->getPageSizeW()); - $this->xmlWriter->writeAttribute('w:h', $this->style->getPageSizeH()); - $this->xmlWriter->endElement(); // w:pgSz + $xmlWriter->startElement('w:pgSz'); + $xmlWriter->writeAttribute('w:orient', $style->getOrientation()); + $xmlWriter->writeAttribute('w:w', $style->getPageSizeW()); + $xmlWriter->writeAttribute('w:h', $style->getPageSizeH()); + $xmlWriter->endElement(); // w:pgSz // Margins $margins = array( @@ -59,52 +57,40 @@ class Section extends AbstractStyle 'w:footer' => array('getFooterHeight', SectionStyle::DEFAULT_FOOTER_HEIGHT), 'w:gutter' => array('getGutter', SectionStyle::DEFAULT_GUTTER), ); - $this->xmlWriter->startElement('w:pgMar'); + $xmlWriter->startElement('w:pgMar'); foreach ($margins as $attribute => $value) { list($method, $default) = $value; - $this->xmlWriter->writeAttribute($attribute, $this->convertTwip($this->style->$method(), $default)); + $xmlWriter->writeAttribute($attribute, $this->convertTwip($style->$method(), $default)); } - $this->xmlWriter->endElement(); + $xmlWriter->endElement(); // Borders - $borders = $this->style->getBorderSize(); - $hasBorders = false; - for ($i = 0; $i < 4; $i++) { - if (!is_null($borders[$i])) { - $hasBorders = true; - break; - } - } - if ($hasBorders) { - $styleWriter = new MarginBorder($this->xmlWriter); - $styleWriter->setSizes($borders); - $styleWriter->setColors($this->style->getBorderColor()); + if ($style->hasBorders()) { + $xmlWriter->startElement('w:pgBorders'); + $xmlWriter->writeAttribute('w:offsetFrom', 'page'); + + $styleWriter = new MarginBorder($xmlWriter); + $styleWriter->setSizes($style->getBorderSize()); + $styleWriter->setColors($style->getBorderColor()); $styleWriter->setAttributes(array('space' => '24')); - - $this->xmlWriter->startElement('w:pgBorders'); - $this->xmlWriter->writeAttribute('w:offsetFrom', 'page'); $styleWriter->write(); - $this->xmlWriter->endElement(); - } - // Page numbering - if (!is_null($this->style->getPageNumberingStart())) { - $this->xmlWriter->startElement('w:pgNumType'); - $this->xmlWriter->writeAttribute('w:start', $this->style->getPageNumberingStart()); - $this->xmlWriter->endElement(); + $xmlWriter->endElement(); } // Columns - $this->xmlWriter->startElement('w:cols'); - $this->xmlWriter->writeAttribute('w:num', $this->style->getColsNum()); - $this->xmlWriter->writeAttribute('w:space', $this->convertTwip( - $this->style->getColsSpace(), - SectionStyle::DEFAULT_COLUMN_SPACING - )); - $this->xmlWriter->endElement(); + $colsSpace = $style->getColsSpace(); + $xmlWriter->startElement('w:cols'); + $xmlWriter->writeAttribute('w:num', $style->getColsNum()); + $xmlWriter->writeAttribute('w:space', $this->convertTwip($colsSpace, SectionStyle::DEFAULT_COLUMN_SPACING)); + $xmlWriter->endElement(); + + // Page numbering start + $pageNum = $style->getPageNumberingStart(); + $xmlWriter->writeElementIf(!is_null($pageNum), 'w:pgNumType', 'w:start', $pageNum); // Line numbering - $styleWriter = new LineNumbering($this->xmlWriter, $this->style->getLineNumbering()); + $styleWriter = new LineNumbering($xmlWriter, $style->getLineNumbering()); $styleWriter->write(); } } diff --git a/src/PhpWord/Writer/Word2007/Style/Shading.php b/src/PhpWord/Writer/Word2007/Style/Shading.php index 17c7298a..cc7f9364 100644 --- a/src/PhpWord/Writer/Word2007/Style/Shading.php +++ b/src/PhpWord/Writer/Word2007/Style/Shading.php @@ -29,14 +29,15 @@ class Shading extends AbstractStyle */ public function write() { - if (!($this->style instanceof \PhpOffice\PhpWord\Style\Shading)) { + if (is_null($style = $this->getStyle())) { return; } + $xmlWriter = $this->getXmlWriter(); - $this->xmlWriter->startElement('w:shd'); - $this->xmlWriter->writeAttribute('w:val', $this->style->getPattern()); - $this->xmlWriter->writeAttribute('w:color', $this->style->getColor()); - $this->xmlWriter->writeAttribute('w:fill', $this->style->getFill()); - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:shd'); + $xmlWriter->writeAttribute('w:val', $style->getPattern()); + $xmlWriter->writeAttribute('w:color', $style->getColor()); + $xmlWriter->writeAttribute('w:fill', $style->getFill()); + $xmlWriter->endElement(); } } diff --git a/src/PhpWord/Writer/Word2007/Style/Spacing.php b/src/PhpWord/Writer/Word2007/Style/Spacing.php index 5a059047..faa9b749 100644 --- a/src/PhpWord/Writer/Word2007/Style/Spacing.php +++ b/src/PhpWord/Writer/Word2007/Style/Spacing.php @@ -29,21 +29,21 @@ class Spacing extends AbstractStyle */ public function write() { - if (!($this->style instanceof \PhpOffice\PhpWord\Style\Spacing)) { + if (is_null($style = $this->getStyle())) { return; } + $xmlWriter = $this->getXmlWriter(); + $before = $style->getBefore(); + $after = $style->getAfter(); + $line = $style->getLine(); - $this->xmlWriter->startElement('w:spacing'); - if (!is_null($this->style->getBefore())) { - $this->xmlWriter->writeAttribute('w:before', $this->convertTwip($this->style->getBefore())); - } - if (!is_null($this->style->getAfter())) { - $this->xmlWriter->writeAttribute('w:after', $this->convertTwip($this->style->getAfter())); - } - if (!is_null($this->style->getLine())) { - $this->xmlWriter->writeAttribute('w:line', $this->style->getLine()); - $this->xmlWriter->writeAttribute('w:lineRule', $this->style->getRule()); - } - $this->xmlWriter->endElement(); + $xmlWriter->startElement('w:spacing'); + + $xmlWriter->writeAttributeIf(!is_null($before), 'w:before', $this->convertTwip($before)); + $xmlWriter->writeAttributeIf(!is_null($after), 'w:after', $this->convertTwip($after)); + $xmlWriter->writeAttributeIf(!is_null($line), 'w:line', $line); + $xmlWriter->writeAttributeIf(!is_null($line), 'w:lineRule', $style->getRule()); + + $xmlWriter->endElement(); } } diff --git a/src/PhpWord/Writer/Word2007/Style/Tab.php b/src/PhpWord/Writer/Word2007/Style/Tab.php index 92db7aca..c15d1364 100644 --- a/src/PhpWord/Writer/Word2007/Style/Tab.php +++ b/src/PhpWord/Writer/Word2007/Style/Tab.php @@ -29,14 +29,15 @@ class Tab extends AbstractStyle */ public function write() { - if (!($this->style instanceof \PhpOffice\PhpWord\Style\Tab)) { + if (is_null($style = $this->getStyle())) { return; } + $xmlWriter = $this->getXmlWriter(); - $this->xmlWriter->startElement("w:tab"); - $this->xmlWriter->writeAttribute("w:val", $this->style->getType()); - $this->xmlWriter->writeAttribute("w:leader", $this->style->getLeader()); - $this->xmlWriter->writeAttribute('w:pos', $this->convertTwip($this->style->getPosition())); - $this->xmlWriter->endElement(); + $xmlWriter->startElement("w:tab"); + $xmlWriter->writeAttribute("w:val", $style->getType()); + $xmlWriter->writeAttribute("w:leader", $style->getLeader()); + $xmlWriter->writeAttribute('w:pos', $this->convertTwip($style->getPosition())); + $xmlWriter->endElement(); } } diff --git a/src/PhpWord/Writer/Word2007/Style/Table.php b/src/PhpWord/Writer/Word2007/Style/Table.php index 08100118..6b5569cf 100644 --- a/src/PhpWord/Writer/Word2007/Style/Table.php +++ b/src/PhpWord/Writer/Word2007/Style/Table.php @@ -36,47 +36,47 @@ class Table extends AbstractStyle */ public function write() { - if (!($this->style instanceof \PhpOffice\PhpWord\Style\Table)) { + if (is_null($style = $this->getStyle())) { return; } - - $hasBorders = $this->style->hasBorders(); - $hasMargins = $this->style->hasMargins(); + $xmlWriter = $this->getXmlWriter(); + $hasBorders = $style->hasBorders(); + $hasMargins = $style->hasMargins(); if ($hasMargins || $hasBorders) { - $this->xmlWriter->startElement('w:tblPr'); + $xmlWriter->startElement('w:tblPr'); if ($hasMargins) { - $mbWriter = new MarginBorder($this->xmlWriter); - $mbWriter->setSizes($this->style->getCellMargin()); + $mbWriter = new MarginBorder($xmlWriter); + $mbWriter->setSizes($style->getCellMargin()); - $this->xmlWriter->startElement('w:tblCellMar'); + $xmlWriter->startElement('w:tblCellMar'); $mbWriter->write(); - $this->xmlWriter->endElement(); // w:tblCellMar + $xmlWriter->endElement(); // w:tblCellMar } if ($hasBorders) { - $mbWriter = new MarginBorder($this->xmlWriter); - $mbWriter->setSizes($this->style->getBorderSize()); - $mbWriter->setColors($this->style->getBorderColor()); + $mbWriter = new MarginBorder($xmlWriter); + $mbWriter->setSizes($style->getBorderSize()); + $mbWriter->setColors($style->getBorderColor()); - $this->xmlWriter->startElement('w:tblBorders'); + $xmlWriter->startElement('w:tblBorders'); $mbWriter->write(); - $this->xmlWriter->endElement(); // w:tblBorders + $xmlWriter->endElement(); // w:tblBorders } - $this->xmlWriter->endElement(); // w:tblPr + $xmlWriter->endElement(); // w:tblPr } // Only write background color and first row for full style if ($this->isFullStyle) { // Background color - if (!is_null($this->style->getShading())) { - $this->xmlWriter->startElement('w:tcPr'); - $styleWriter = new Shading($this->xmlWriter, $this->style->getShading()); + if (!is_null($style->getShading())) { + $xmlWriter->startElement('w:tcPr'); + $styleWriter = new Shading($xmlWriter, $style->getShading()); $styleWriter->write(); - $this->xmlWriter->endElement(); + $xmlWriter->endElement(); } // First Row - $firstRow = $this->style->getFirstRow(); + $firstRow = $style->getFirstRow(); if ($firstRow instanceof \PhpOffice\PhpWord\Style\Table) { - $this->writeFirstRow($firstRow, 'firstRow'); + $this->writeFirstRow($firstRow); } } } @@ -96,28 +96,30 @@ class Table extends AbstractStyle * * @param string $type */ - private function writeFirstRow(\PhpOffice\PhpWord\Style\Table $style, $type) + private function writeFirstRow(\PhpOffice\PhpWord\Style\Table $style) { - $this->xmlWriter->startElement('w:tblStylePr'); - $this->xmlWriter->writeAttribute('w:type', $type); - $this->xmlWriter->startElement('w:tcPr'); + $xmlWriter = $this->getXmlWriter(); + + $xmlWriter->startElement('w:tblStylePr'); + $xmlWriter->writeAttribute('w:type', 'firstRow'); + $xmlWriter->startElement('w:tcPr'); if (!is_null($style->getShading())) { - $styleWriter = new Shading($this->xmlWriter, $style->getShading()); + $styleWriter = new Shading($xmlWriter, $style->getShading()); $styleWriter->write(); } // Borders if ($style->hasBorders()) { - $mbWriter = new MarginBorder($this->xmlWriter); + $mbWriter = new MarginBorder($xmlWriter); $mbWriter->setSizes($style->getBorderSize()); $mbWriter->setColors($style->getBorderColor()); - $this->xmlWriter->startElement('w:tcBorders'); + $xmlWriter->startElement('w:tcBorders'); $mbWriter->write(); - $this->xmlWriter->endElement(); // w:tcBorders + $xmlWriter->endElement(); // w:tcBorders } - $this->xmlWriter->endElement(); // w:tcPr - $this->xmlWriter->endElement(); // w:tblStylePr + $xmlWriter->endElement(); // w:tcPr + $xmlWriter->endElement(); // w:tblStylePr } } diff --git a/tests/PhpWord/Tests/Style/FontTest.php b/tests/PhpWord/Tests/Style/FontTest.php index cfa51892..2f5c49c6 100644 --- a/tests/PhpWord/Tests/Style/FontTest.php +++ b/tests/PhpWord/Tests/Style/FontTest.php @@ -97,8 +97,9 @@ class FontTest extends \PHPUnit_Framework_TestCase 'fgColor' => Font::FGCOLOR_YELLOW, 'bgColor' => 'FFFF00', 'hint' => 'eastAsia', + 'line-height' => 2, ); - $object->setArrayStyle($attributes); + $object->setStyleByArray($attributes); foreach ($attributes as $key => $value) { $get = "get{$key}"; $this->assertEquals($value, $object->$get());