diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index c575f80e..b2e2aad4 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -78,7 +78,7 @@ abstract class AbstractContainer extends AbstractElement public function addText($text, $fontStyle = null, $paragraphStyle = null, $elementName = 'Text') { $this->checkValidity($elementName); - $elementClass = __NAMESPACE__ . '\\' . $elementName; + $elementClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . $elementName; // Reset paragraph style for footnote and textrun. They have their own if (in_array($this->container, array('textrun', 'footnote', 'endnote'))) { @@ -248,7 +248,7 @@ abstract class AbstractContainer extends AbstractElement public function addFootnote($paragraphStyle = null, $elementName = 'Footnote') { $this->checkValidity($elementName); - $elementClass = __NAMESPACE__ . '\\' . $elementName; + $elementClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . $elementName; $docPart = strtolower($elementName); $addMethod = "add{$elementName}"; diff --git a/src/PhpWord/Element/AbstractElement.php b/src/PhpWord/Element/AbstractElement.php index a3987b58..7e3151e2 100644 --- a/src/PhpWord/Element/AbstractElement.php +++ b/src/PhpWord/Element/AbstractElement.php @@ -231,9 +231,7 @@ abstract class AbstractElement protected function setStyle($styleObject, $styleValue = null, $returnObject = false) { if (!is_null($styleValue) && is_array($styleValue)) { - foreach ($styleValue as $key => $value) { - $styleObject->setStyleValue($key, $value); - } + $styleObject->setStyleByArray($styleValue); $style = $styleObject; } else { $style = $returnObject ? $styleObject : $styleValue; diff --git a/src/PhpWord/Element/Section.php b/src/PhpWord/Element/Section.php index dc216fce..9067c73c 100644 --- a/src/PhpWord/Element/Section.php +++ b/src/PhpWord/Element/Section.php @@ -222,8 +222,9 @@ class Section extends AbstractContainer */ private function addHeaderFooter($type = Header::AUTO, $header = true) { + $containerClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . + ($header ? 'Header' : 'Footer'); $collectionArray = $header ? 'headers' : 'footers'; - $containerClass = __NAMESPACE__ . '\\' . ($header ? 'Header' : 'Footer'); $collection = &$this->$collectionArray; if (in_array($type, array(Header::AUTO, Header::FIRST, Header::EVEN))) { diff --git a/src/PhpWord/Element/TOC.php b/src/PhpWord/Element/TOC.php index 2ecb091d..548f4f8a 100644 --- a/src/PhpWord/Element/TOC.php +++ b/src/PhpWord/Element/TOC.php @@ -68,20 +68,14 @@ class TOC extends AbstractElement $this->TOCStyle = new TOCStyle(); if (!is_null($tocStyle) && is_array($tocStyle)) { - foreach ($tocStyle as $key => $value) { - $this->TOCStyle->setStyleValue($key, $value); - } + $this->TOCStyle->setStyleByArray($tocStyle); } - if (!is_null($fontStyle)) { - if (is_array($fontStyle)) { - $this->fontStyle = new Font(); - foreach ($fontStyle as $key => $value) { - $this->fontStyle->setStyleValue($key, $value); - } - } else { - $this->fontStyle = $fontStyle; - } + if (!is_null($fontStyle) && is_array($fontStyle)) { + $this->fontStyle = new Font(); + $this->fontStyle->setStyleByArray($fontStyle); + } else { + $this->fontStyle = $fontStyle; } $this->minDepth = $minDepth; diff --git a/src/PhpWord/Reader/Word2007/AbstractPart.php b/src/PhpWord/Reader/Word2007/AbstractPart.php index 4547b67c..a6efb329 100644 --- a/src/PhpWord/Reader/Word2007/AbstractPart.php +++ b/src/PhpWord/Reader/Word2007/AbstractPart.php @@ -138,60 +138,59 @@ abstract class AbstractPart /** * Read w:pPr * - * @return string|array|null + * @return array|null */ protected function readParagraphStyle(XMLReader $xmlReader, \DOMElement $domNode) { - $style = null; - if ($xmlReader->elementExists('w:pPr', $domNode)) { - if ($xmlReader->elementExists('w:pPr/w:pStyle', $domNode)) { - $style = $xmlReader->getAttribute('w:val', $domNode, 'w:pPr/w:pStyle'); - } else { - $style = array(); - $mapping = array( - 'w:ind' => 'indent', 'w:spacing' => 'spacing', - 'w:jc' => 'align', 'w:basedOn' => 'basedOn', 'w:next' => 'next', - 'w:widowControl' => 'widowControl', 'w:keepNext' => 'keepNext', - 'w:keepLines' => 'keepLines', 'w:pageBreakBefore' => 'pageBreakBefore', - ); + if (!$xmlReader->elementExists('w:pPr', $domNode)) { + return; + } - $nodes = $xmlReader->getElements('w:pPr/*', $domNode); - foreach ($nodes as $node) { - if (!array_key_exists($node->nodeName, $mapping)) { - continue; - } - $property = $mapping[$node->nodeName]; - switch ($node->nodeName) { + $style = array(); + $mapping = array( + 'w:pStyle' => 'styleName', + 'w:ind' => 'indent', 'w:spacing' => 'spacing', + 'w:jc' => 'align', 'w:basedOn' => 'basedOn', 'w:next' => 'next', + 'w:widowControl' => 'widowControl', 'w:keepNext' => 'keepNext', + 'w:keepLines' => 'keepLines', 'w:pageBreakBefore' => 'pageBreakBefore', + ); - case 'w:ind': - $style['indent'] = $xmlReader->getAttribute('w:left', $node); - $style['hanging'] = $xmlReader->getAttribute('w:hanging', $node); - break; + $nodes = $xmlReader->getElements('w:pPr/*', $domNode); + foreach ($nodes as $node) { + if (!array_key_exists($node->nodeName, $mapping)) { + continue; + } + $property = $mapping[$node->nodeName]; + switch ($node->nodeName) { - case 'w:spacing': - $style['spaceAfter'] = $xmlReader->getAttribute('w:after', $node); - $style['spaceBefore'] = $xmlReader->getAttribute('w:before', $node); - // Commented. Need to adjust the number when return value is null - // $style['spacing'] = $xmlReader->getAttribute('w:line', $node); - break; + case 'w:ind': + $style['indent'] = $xmlReader->getAttribute('w:left', $node); + $style['hanging'] = $xmlReader->getAttribute('w:hanging', $node); + break; - case 'w:keepNext': - case 'w:keepLines': - case 'w:pageBreakBefore': - $style[$property] = true; - break; + case 'w:spacing': + $style['spaceAfter'] = $xmlReader->getAttribute('w:after', $node); + $style['spaceBefore'] = $xmlReader->getAttribute('w:before', $node); + // Commented. Need to adjust the number when return value is null + // $style['spacing'] = $xmlReader->getAttribute('w:line', $node); + break; - case 'w:widowControl': - $style[$property] = false; - break; + case 'w:keepNext': + case 'w:keepLines': + case 'w:pageBreakBefore': + $style[$property] = true; + break; - case 'w:jc': - case 'w:basedOn': - case 'w:next': - $style[$property] = $xmlReader->getAttribute('w:val', $node); - break; - } - } + case 'w:widowControl': + $style[$property] = false; + break; + + case 'w:pStyle': + case 'w:jc': + case 'w:basedOn': + case 'w:next': + $style[$property] = $xmlReader->getAttribute('w:val', $node); + break; } } @@ -201,70 +200,69 @@ abstract class AbstractPart /** * Read w:rPr * - * @return string|array|null + * @return array|null */ protected function readFontStyle(XMLReader $xmlReader, \DOMElement $domNode) { - $style = null; + if (is_null($domNode)) { + return; + } // Hyperlink has an extra w:r child if ($domNode->nodeName == 'w:hyperlink') { $domNode = $xmlReader->getElement('w:r', $domNode); } - if (is_null($domNode)) { - return $style; + if (!$xmlReader->elementExists('w:rPr', $domNode)) { + return; } - if ($xmlReader->elementExists('w:rPr', $domNode)) { - if ($xmlReader->elementExists('w:rPr/w:rStyle', $domNode)) { - $style = $xmlReader->getAttribute('w:val', $domNode, 'w:rPr/w:rStyle'); - } else { - $style = array(); - $mapping = array( - 'w:b' => 'bold', 'w:i' => 'italic', 'w:color' => 'color', - 'w:strike' => 'strikethrough', 'w:u' => 'underline', - 'w:highlight' => 'fgColor', 'w:sz' => 'size', - 'w:rFonts' => 'name', 'w:vertAlign' => 'superScript', - ); - $nodes = $xmlReader->getElements('w:rPr/*', $domNode); - foreach ($nodes as $node) { - if (!array_key_exists($node->nodeName, $mapping)) { - continue; + $style = array(); + $mapping = array( + 'w:rStyle' => 'styleName', + 'w:b' => 'bold', 'w:i' => 'italic', 'w:color' => 'color', + 'w:strike' => 'strikethrough', 'w:u' => 'underline', + 'w:highlight' => 'fgColor', 'w:sz' => 'size', + 'w:rFonts' => 'name', 'w:vertAlign' => 'superScript', + ); + + $nodes = $xmlReader->getElements('w:rPr/*', $domNode); + foreach ($nodes as $node) { + if (!array_key_exists($node->nodeName, $mapping)) { + continue; + } + $property = $mapping[$node->nodeName]; + switch ($node->nodeName) { + + case 'w:rFonts': + $style['name'] = $xmlReader->getAttribute('w:ascii', $node); + $style['hint'] = $xmlReader->getAttribute('w:hint', $node); + break; + + case 'w:b': + case 'w:i': + case 'w:strike': + $style[$property] = true; + break; + + case 'w:rStyle': + case 'w:u': + case 'w:highlight': + case 'w:color': + $style[$property] = $xmlReader->getAttribute('w:val', $node); + break; + + case 'w:sz': + $style[$property] = $xmlReader->getAttribute('w:val', $node) / 2; + break; + + case 'w:vertAlign': + $style[$property] = $xmlReader->getAttribute('w:val', $node); + if ($style[$property] == 'superscript') { + $style['superScript'] = true; + } else { + $style['superScript'] = false; + $style['subScript'] = true; } - $property = $mapping[$node->nodeName]; - switch ($node->nodeName) { - - case 'w:rFonts': - $style['name'] = $xmlReader->getAttribute('w:ascii', $node); - $style['hint'] = $xmlReader->getAttribute('w:hint', $node); - break; - - case 'w:b': - case 'w:i': - case 'w:strike': - $style[$property] = true; - break; - - case 'w:u': - case 'w:highlight': - case 'w:color': - $style[$property] = $xmlReader->getAttribute('w:val', $node); - break; - - case 'w:sz': - $style[$property] = $xmlReader->getAttribute('w:val', $node) / 2; - break; - - case 'w:vertAlign': - $style[$property] = $xmlReader->getAttribute('w:val', $node); - if ($style[$property] == 'superscript') { - $style['superScript'] = true; - } else { - $style['superScript'] = false; - $style['subScript'] = true; - } - break; - } - } + break; } } diff --git a/src/PhpWord/Style/AbstractStyle.php b/src/PhpWord/Style/AbstractStyle.php index 082596af..e4fbb9a5 100644 --- a/src/PhpWord/Style/AbstractStyle.php +++ b/src/PhpWord/Style/AbstractStyle.php @@ -160,9 +160,6 @@ abstract class AbstractStyle */ protected function setBoolVal($value, $default = null) { - if (is_string($value)) { - $value = (bool)$value; - } if (!is_bool($value)) { $value = $default; } @@ -187,7 +184,7 @@ abstract class AbstractStyle } /** - * Set integer value + * Set float value: Convert string that contains only numeric into integer * * @param mixed $value * @param int|null $default @@ -195,7 +192,7 @@ abstract class AbstractStyle */ protected function setIntVal($value, $default = null) { - if (is_string($value)) { + if (is_string($value) && (preg_match('/[^\d]/', $value) == 0)) { $value = intval($value); } if (!is_int($value)) { @@ -206,7 +203,7 @@ abstract class AbstractStyle } /** - * Set float value + * Set float value: Convert string that contains only numeric into float * * @param mixed $value * @param float|null $default @@ -214,7 +211,7 @@ abstract class AbstractStyle */ protected function setFloatVal($value, $default = null) { - if (is_string($value)) { + if (is_string($value) && (preg_match('/[^\d\.\,]/', $value) == 0)) { $value = floatval($value); } if (!is_float($value)) { @@ -231,9 +228,11 @@ abstract class AbstractStyle * @param array $enum * @param mixed $default */ - protected function setEnumVal($value, $enum, $default = null) + protected function setEnumVal($value = null, $enum = array(), $default = null) { - if (!in_array($value, $enum)) { + if (!is_null($value) && !empty($enum) && !in_array($value, $enum)) { + throw new \InvalidArgumentException('Invalid style value.'); + } elseif (is_null($value)) { $value = $default; } @@ -249,7 +248,7 @@ abstract class AbstractStyle */ protected function setObjectVal($value, $styleName, &$style) { - $styleClass = __NAMESPACE__ . '\\' . $styleName; + $styleClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . $styleName; if (is_array($value)) { if (!$style instanceof $styleClass) { $style = new $styleClass(); diff --git a/src/PhpWord/Style/Cell.php b/src/PhpWord/Style/Cell.php index 31b7e307..e44ffa38 100644 --- a/src/PhpWord/Style/Cell.php +++ b/src/PhpWord/Style/Cell.php @@ -22,6 +22,16 @@ namespace PhpOffice\PhpWord\Style; */ class Cell extends Border { + /** + * Vertical alignment constants + * + * @const string + */ + const VALIGN_TOP = 'top'; + const VALIGN_CENTER = 'center'; + const VALIGN_BOTTOM = 'bottom'; + const VALIGN_BOTH = 'both'; + /** * Text direction constants * @@ -30,6 +40,14 @@ class Cell extends Border const TEXT_DIR_BTLR = 'btLr'; const TEXT_DIR_TBRL = 'tbRl'; + /** + * Vertical merge (rowspan) constants + * + * @const string + */ + const VMERGE_RESTART = 'restart'; + const VMERGE_CONTINUE = 'continue'; + /** * Default border color * @@ -56,7 +74,7 @@ class Cell extends Border * * @var integer */ - private $gridSpan = null; + private $gridSpan; /** * rowspan (restart, continue) @@ -66,7 +84,7 @@ class Cell extends Border * * @var string */ - private $vMerge = null; + private $vMerge; /** * Shading @@ -87,10 +105,14 @@ class Cell extends Border * Set vertical align * * @param string $value + * @return self */ public function setVAlign($value = null) { - $this->vAlign = $value; + $enum = array(self::VALIGN_TOP, self::VALIGN_CENTER, self::VALIGN_BOTTOM, self::VALIGN_BOTH); + $this->vAlign = $this->setEnumVal($value, $enum, $this->vAlign); + + return $this; } /** @@ -105,10 +127,14 @@ class Cell extends Border * Set text direction * * @param string $value + * @return self */ public function setTextDirection($value = null) { - $this->textDirection = $value; + $enum = array(self::TEXT_DIR_BTLR, self::TEXT_DIR_TBRL); + $this->textDirection = $this->setEnumVal($value, $enum, $this->textDirection); + + return $this; } /** @@ -134,16 +160,6 @@ class Cell extends Border return $this->setShading(array('fill' => $value)); } - /** - * Set grid span (colspan) - * - * @param int $value - */ - public function setGridSpan($value = null) - { - $this->gridSpan = $value; - } - /** * Get grid span (colspan) */ @@ -153,13 +169,16 @@ class Cell extends Border } /** - * Set vertical merge (rowspan) + * Set grid span (colspan) * - * @param string $value + * @param int $value + * @return self */ - public function setVMerge($value = null) + public function setGridSpan($value = null) { - $this->vMerge = $value; + $this->gridSpan = $this->setIntVal($value, $this->gridSpan); + + return $this; } /** @@ -170,6 +189,20 @@ class Cell extends Border return $this->vMerge; } + /** + * Set vertical merge (rowspan) + * + * @param string $value + * @return self + */ + public function setVMerge($value = null) + { + $enum = array(self::VMERGE_RESTART, self::VMERGE_CONTINUE); + $this->vMerge = $this->setEnumVal($value, $enum, $this->vMerge); + + return $this; + } + /** * Get shading * diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index f0ee893e..57d0770a 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -216,6 +216,16 @@ class Font extends AbstractStyle $this->setParagraph($paragraph); } + /** + * Get style type + * + * @return string + */ + public function getStyleType() + { + return $this->type; + } + /** * Get font name * @@ -570,16 +580,6 @@ class Font extends AbstractStyle $this->setShading(array('fill' => $value)); } - /** - * Get style type - * - * @return string - */ - public function getStyleType() - { - return $this->type; - } - /** * Get line height * @@ -650,9 +650,9 @@ class Font extends AbstractStyle } /** - * Toggle $target value to false when $source true + * Toggle $target property to false when $source true * - * @param \PhpOffice\PhpWord\Style\AbstractStyle $target + * @param mixed $target Target property * @param bool $sourceValue */ private function toggleFalse(&$target, $sourceValue) diff --git a/src/PhpWord/Style/Image.php b/src/PhpWord/Style/Image.php index 540d3d4f..5dc82d0b 100644 --- a/src/PhpWord/Style/Image.php +++ b/src/PhpWord/Style/Image.php @@ -117,7 +117,7 @@ class Image extends AbstractStyle * * @var string */ - private $wrappingStyle; + private $wrappingStyle = self::WRAPPING_STYLE_INLINE; /** * Positioning type (relative or absolute) @@ -131,40 +131,28 @@ class Image extends AbstractStyle * * @var string */ - private $posHorizontal; + private $posHorizontal = self::POSITION_HORIZONTAL_LEFT; /** * Horizontal Relation * * @var string */ - private $posHorizontalRel; + private $posHorizontalRel = self::POSITION_RELATIVE_TO_CHAR; /** * Vertical alignment * * @var string */ - private $posVertical; + private $posVertical = self::POSITION_VERTICAL_TOP; /** * Vertical Relation * * @var string */ - private $posVerticalRel; - - /** - * Create new image style - */ - public function __construct() - { - $this->setWrappingStyle(self::WRAPPING_STYLE_INLINE); - $this->setPosHorizontal(self::POSITION_HORIZONTAL_LEFT); - $this->setPosHorizontalRel(self::POSITION_RELATIVE_TO_CHAR); - $this->setPosVertical(self::POSITION_VERTICAL_TOP); - $this->setPosVerticalRel(self::POSITION_RELATIVE_TO_LINE); - } + private $posVerticalRel = self::POSITION_RELATIVE_TO_LINE; /** * Get width @@ -283,14 +271,12 @@ class Image extends AbstractStyle */ public function setWrappingStyle($wrappingStyle) { - $enum = array(self::WRAPPING_STYLE_INLINE, self::WRAPPING_STYLE_INFRONT, self::WRAPPING_STYLE_BEHIND, - self::WRAPPING_STYLE_SQUARE, self::WRAPPING_STYLE_TIGHT); - - if (in_array($wrappingStyle, $enum)) { - $this->wrappingStyle = $wrappingStyle; - } else { - throw new \InvalidArgumentException('Invalid wrapping style.'); - } + $enum = array( + self::WRAPPING_STYLE_INLINE, + self::WRAPPING_STYLE_INFRONT, self::WRAPPING_STYLE_BEHIND, + self::WRAPPING_STYLE_SQUARE, self::WRAPPING_STYLE_TIGHT, + ); + $this->wrappingStyle = $this->setEnumVal($wrappingStyle, $enum, $this->wrappingStyle); return $this; } @@ -315,12 +301,7 @@ class Image extends AbstractStyle public function setPositioning($positioning) { $enum = array(self::POSITION_RELATIVE, self::POSITION_ABSOLUTE); - - if (in_array($positioning, $enum)) { - $this->positioning = $positioning; - } else { - throw new \InvalidArgumentException('Invalid positioning.'); - } + $this->positioning = $this->setEnumVal($positioning, $enum, $this->positioning); return $this; } @@ -344,14 +325,11 @@ class Image extends AbstractStyle */ public function setPosHorizontal($alignment) { - $enum = array(self::POSITION_HORIZONTAL_LEFT, self::POSITION_HORIZONTAL_CENTER, - self::POSITION_HORIZONTAL_RIGHT); - - if (in_array($alignment, $enum)) { - $this->posHorizontal = $alignment; - } else { - throw new \InvalidArgumentException('Invalid horizontal alignment.'); - } + $enum = array( + self::POSITION_HORIZONTAL_LEFT, self::POSITION_HORIZONTAL_CENTER, + self::POSITION_HORIZONTAL_RIGHT, + ); + $this->posHorizontal = $this->setEnumVal($alignment, $enum, $this->posHorizontal); return $this; } @@ -375,14 +353,12 @@ class Image extends AbstractStyle */ public function setPosVertical($alignment) { - $enum = array(self::POSITION_VERTICAL_TOP, self::POSITION_VERTICAL_CENTER, - self::POSITION_VERTICAL_BOTTOM, self::POSITION_VERTICAL_INSIDE, self::POSITION_VERTICAL_OUTSIDE); - - if (in_array($alignment, $enum)) { - $this->posVertical = $alignment; - } else { - throw new \InvalidArgumentException('Invalid vertical alignment.'); - } + $enum = array( + self::POSITION_VERTICAL_TOP, self::POSITION_VERTICAL_CENTER, + self::POSITION_VERTICAL_BOTTOM, self::POSITION_VERTICAL_INSIDE, + self::POSITION_VERTICAL_OUTSIDE, + ); + $this->posVertical = $this->setEnumVal($alignment, $enum, $this->posVertical); return $this; } @@ -406,16 +382,13 @@ class Image extends AbstractStyle */ public function setPosHorizontalRel($relto) { - $enum = array(self::POSITION_RELATIVE_TO_MARGIN, self::POSITION_RELATIVE_TO_PAGE, + $enum = array( + self::POSITION_RELATIVE_TO_MARGIN, self::POSITION_RELATIVE_TO_PAGE, self::POSITION_RELATIVE_TO_COLUMN, self::POSITION_RELATIVE_TO_CHAR, self::POSITION_RELATIVE_TO_LMARGIN, self::POSITION_RELATIVE_TO_RMARGIN, - self::POSITION_RELATIVE_TO_IMARGIN, self::POSITION_RELATIVE_TO_OMARGIN); - - if (in_array($relto, $enum)) { - $this->posHorizontalRel = $relto; - } else { - throw new \InvalidArgumentException('Invalid relative horizontal alignment.'); - } + self::POSITION_RELATIVE_TO_IMARGIN, self::POSITION_RELATIVE_TO_OMARGIN, + ); + $this->posHorizontalRel = $this->setEnumVal($relto, $enum, $this->posHorizontalRel); return $this; } @@ -439,16 +412,13 @@ class Image extends AbstractStyle */ public function setPosVerticalRel($relto) { - $enum = array(self::POSITION_RELATIVE_TO_MARGIN, self::POSITION_RELATIVE_TO_PAGE, + $enum = array( + self::POSITION_RELATIVE_TO_MARGIN, self::POSITION_RELATIVE_TO_PAGE, self::POSITION_RELATIVE_TO_LINE, self::POSITION_RELATIVE_TO_TMARGIN, self::POSITION_RELATIVE_TO_BMARGIN, - self::POSITION_RELATIVE_TO_IMARGIN, self::POSITION_RELATIVE_TO_OMARGIN); - - if (in_array($relto, $enum)) { - $this->posVerticalRel = $relto; - } else { - throw new \InvalidArgumentException('Invalid relative vertical alignment.'); - } + self::POSITION_RELATIVE_TO_IMARGIN, self::POSITION_RELATIVE_TO_OMARGIN, + ); + $this->posVerticalRel = $this->setEnumVal($relto, $enum, $this->posVerticalRel); return $this; } diff --git a/src/PhpWord/Style/ListItem.php b/src/PhpWord/Style/ListItem.php index 6b5366cd..554d75b0 100644 --- a/src/PhpWord/Style/ListItem.php +++ b/src/PhpWord/Style/ListItem.php @@ -86,14 +86,19 @@ class ListItem extends AbstractStyle * Set legacy list type for version < 0.10.0 * * @param integer $value + * @return self */ public function setListType($value = self::TYPE_BULLET_FILLED) { - $enum = array(self::TYPE_SQUARE_FILLED, self::TYPE_BULLET_FILLED, + $enum = array( + self::TYPE_SQUARE_FILLED, self::TYPE_BULLET_FILLED, self::TYPE_BULLET_EMPTY, self::TYPE_NUMBER, - self::TYPE_NUMBER_NESTED, self::TYPE_ALPHANUM); + self::TYPE_NUMBER_NESTED, self::TYPE_ALPHANUM + ); $this->listType = $this->setEnumVal($value, $enum, $this->listType); $this->getListTypeStyle(); + + return $this; } /** @@ -110,6 +115,7 @@ class ListItem extends AbstractStyle * Set numbering style name * * @param string $value + * @return self */ public function setNumStyle($value) { @@ -119,6 +125,8 @@ class ListItem extends AbstractStyle $this->numId = $numStyleObject->getIndex(); $numStyleObject->setNumId($this->numId); } + + return $this; } /** diff --git a/src/PhpWord/Style/Numbering.php b/src/PhpWord/Style/Numbering.php index d92f6f3f..726af2be 100644 --- a/src/PhpWord/Style/Numbering.php +++ b/src/PhpWord/Style/Numbering.php @@ -69,6 +69,7 @@ class Numbering extends AbstractStyle public function setNumId($value) { $this->numId = $this->setIntVal($value, $this->numId); + return $this; } @@ -92,6 +93,7 @@ class Numbering extends AbstractStyle { $enum = array('singleLevel', 'multilevel', 'hybridMultilevel'); $this->type = $this->setEnumVal($value, $enum, $this->type); + return $this; } diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index b04646ea..2765aca4 100644 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -125,10 +125,8 @@ class Paragraph extends AbstractStyle } elseif ($key == 'spacing') { $value += 240; // because line height of 1 matches 240 twips } - $method = 'set' . $key; - if (method_exists($this, $method)) { - $this->$method($value); - } + + return parent::setStyleValue($key, $value); } /** diff --git a/src/PhpWord/Style/Row.php b/src/PhpWord/Style/Row.php index c3f2722a..310564d8 100644 --- a/src/PhpWord/Style/Row.php +++ b/src/PhpWord/Style/Row.php @@ -71,6 +71,8 @@ class Row extends AbstractStyle public function setTblHeader($value = false) { $this->tblHeader = $this->setBoolVal($value, $this->tblHeader); + + return $this; } /** @@ -92,6 +94,8 @@ class Row extends AbstractStyle public function setCantSplit($value = false) { $this->cantSplit = $this->setBoolVal($value, $this->cantSplit); + + return $this; } /** @@ -113,6 +117,7 @@ class Row extends AbstractStyle public function setExactHeight($value = false) { $this->exactHeight = $this->setBoolVal($value, $this->exactHeight); + return $this; } diff --git a/src/PhpWord/Style/Section.php b/src/PhpWord/Style/Section.php index 7306c6a6..8a0c071d 100644 --- a/src/PhpWord/Style/Section.php +++ b/src/PhpWord/Style/Section.php @@ -261,7 +261,7 @@ class Section extends Border * @param int|float $value * @return self */ - public function setMarginTop($value = '') + public function setMarginTop($value = null) { $this->marginTop = $this->setNumericVal($value, self::DEFAULT_MARGIN); @@ -284,7 +284,7 @@ class Section extends Border * @param int|float $value * @return self */ - public function setMarginLeft($value = '') + public function setMarginLeft($value = null) { $this->marginLeft = $this->setNumericVal($value, self::DEFAULT_MARGIN); @@ -307,7 +307,7 @@ class Section extends Border * @param int|float $value * @return self */ - public function setMarginRight($value = '') + public function setMarginRight($value = null) { $this->marginRight = $this->setNumericVal($value, self::DEFAULT_MARGIN); @@ -330,7 +330,7 @@ class Section extends Border * @param int|float $value * @return self */ - public function setMarginBottom($value = '') + public function setMarginBottom($value = null) { $this->marginBottom = $this->setNumericVal($value, self::DEFAULT_MARGIN); @@ -353,7 +353,7 @@ class Section extends Border * @param int|float $value * @return self */ - public function setGutter($value = '') + public function setGutter($value = null) { $this->gutter = $this->setNumericVal($value, self::DEFAULT_GUTTER); @@ -376,7 +376,7 @@ class Section extends Border * @param int|float $value * @return self */ - public function setHeaderHeight($value = '') + public function setHeaderHeight($value = null) { $this->headerHeight = $this->setNumericVal($value, self::DEFAULT_HEADER_HEIGHT); @@ -399,7 +399,7 @@ class Section extends Border * @param int|float $value * @return self */ - public function setFooterHeight($value = '') + public function setFooterHeight($value = null) { $this->footerHeight = $this->setNumericVal($value, self::DEFAULT_FOOTER_HEIGHT); @@ -444,7 +444,7 @@ class Section extends Border * @param int $value * @return self */ - public function setColsNum($value = '') + public function setColsNum($value = null) { $this->colsNum = $this->setIntVal($value, self::DEFAULT_COLUMN_COUNT); @@ -467,7 +467,7 @@ class Section extends Border * @param int|float $value * @return self */ - public function setColsSpace($value = '') + public function setColsSpace($value = null) { $this->colsSpace = $this->setNumericVal($value, self::DEFAULT_COLUMN_SPACING); diff --git a/src/PhpWord/Style/Shading.php b/src/PhpWord/Style/Shading.php index 3e970e6e..5c9742c9 100644 --- a/src/PhpWord/Style/Shading.php +++ b/src/PhpWord/Style/Shading.php @@ -89,9 +89,10 @@ class Shading extends AbstractStyle */ public function setPattern($value = null) { - $enum = array(self::PATTERN_CLEAR, self::PATTERN_SOLID, self::PATTERN_HSTRIPE, - self::PATTERN_VSTRIPE, self::PATTERN_DSTRIPE, self::PATTERN_HCROSS, self::PATTERN_DCROSS); - + $enum = array( + self::PATTERN_CLEAR, self::PATTERN_SOLID, self::PATTERN_HSTRIPE, + self::PATTERN_VSTRIPE, self::PATTERN_DSTRIPE, self::PATTERN_HCROSS, self::PATTERN_DCROSS + ); $this->pattern = $this->setEnumVal($value, $enum, $this->pattern); return $this; diff --git a/src/PhpWord/Style/TOC.php b/src/PhpWord/Style/TOC.php index 6ec4318f..52855652 100644 --- a/src/PhpWord/Style/TOC.php +++ b/src/PhpWord/Style/TOC.php @@ -36,7 +36,7 @@ class TOC extends Tab /** * Indent * - * @var int + * @var int|float (twip) */ private $indent = 200; @@ -51,7 +51,7 @@ class TOC extends Tab /** * Get Tab Position * - * @return int + * @return int|float */ public function getTabPos() { @@ -61,11 +61,12 @@ class TOC extends Tab /** * Set Tab Position * - * @param int $value + * @param int|float $value + * @return self */ public function setTabPos($value) { - $this->setPosition($value); + return $this->setPosition($value); } /** @@ -82,16 +83,17 @@ class TOC extends Tab * Set Tab Leader * * @param string $value + * @return self */ public function setTabLeader($value = self::TAB_LEADER_DOT) { - $this->setLeader($value); + return $this->setLeader($value); } /** * Get Indent * - * @return int + * @return int|float */ public function getIndent() { @@ -101,10 +103,13 @@ class TOC extends Tab /** * Set Indent * - * @param string $value + * @param int|float $value + * @return self */ public function setIndent($value) { - $this->indent = $value; + $this->indent = $this->setNumericVal($value, $this->indent); + + return $this; } } diff --git a/src/PhpWord/Style/Tab.php b/src/PhpWord/Style/Tab.php index 4bbba164..900e1fbd 100644 --- a/src/PhpWord/Style/Tab.php +++ b/src/PhpWord/Style/Tab.php @@ -107,12 +107,18 @@ class Tab extends AbstractStyle * Set stop type * * @param string $value + * @return self */ public function setType($value) { - $enum = array(self::TAB_STOP_CLEAR, self::TAB_STOP_LEFT, self::TAB_STOP_CENTER, - self::TAB_STOP_RIGHT, self::TAB_STOP_DECIMAL, self::TAB_STOP_BAR, self::TAB_STOP_NUM); + $enum = array( + self::TAB_STOP_CLEAR, self::TAB_STOP_LEFT, self::TAB_STOP_CENTER, + self::TAB_STOP_RIGHT, self::TAB_STOP_DECIMAL, self::TAB_STOP_BAR, + self::TAB_STOP_NUM, + ); $this->type = $this->setEnumVal($value, $enum, $this->type); + + return $this; } /** @@ -129,12 +135,17 @@ class Tab extends AbstractStyle * Set leader * * @param string $value + * @return self */ public function setLeader($value) { - $enum = array(self::TAB_LEADER_NONE, self::TAB_LEADER_DOT, self::TAB_LEADER_HYPHEN, - self::TAB_LEADER_UNDERSCORE, self::TAB_LEADER_HEAVY, self::TAB_LEADER_MIDDLEDOT); + $enum = array( + self::TAB_LEADER_NONE, self::TAB_LEADER_DOT, self::TAB_LEADER_HYPHEN, + self::TAB_LEADER_UNDERSCORE, self::TAB_LEADER_HEAVY, self::TAB_LEADER_MIDDLEDOT, + ); $this->leader = $this->setEnumVal($value, $enum, $this->leader); + + return $this; } /** @@ -151,9 +162,12 @@ class Tab extends AbstractStyle * Set position * * @param int|float $value + * @return self */ public function setPosition($value) { $this->position = $this->setNumericVal($value, $this->position); + + return $this; } } diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index 9173d0d7..c9850e73 100644 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -112,15 +112,11 @@ class Table extends Border unset($this->firstRow->borderInsideVSize); unset($this->firstRow->borderInsideHColor); unset($this->firstRow->borderInsideHSize); - foreach ($styleFirstRow as $key => $value) { - $this->firstRow->setStyleValue($key, $value); - } + $this->firstRow->setStyleByArray($styleFirstRow); } if (!is_null($styleTable) && is_array($styleTable)) { - foreach ($styleTable as $key => $value) { - $this->setStyleValue($key, $value); - } + $this->setStyleByArray($styleTable); } } diff --git a/src/PhpWord/Writer/HTML/Element/Element.php b/src/PhpWord/Writer/HTML/Element/Element.php index 7ad8a00d..85525129 100644 --- a/src/PhpWord/Writer/HTML/Element/Element.php +++ b/src/PhpWord/Writer/HTML/Element/Element.php @@ -73,7 +73,8 @@ class Element public function write() { $content = ''; - $writerClass = __NAMESPACE__ . '\\' . basename(get_class($this->element)); + $writerClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . + basename(get_class($this->element)); if (class_exists($writerClass)) { $writer = new $writerClass($this->parentWriter, $this->element, $this->withoutP); $content = $writer->write(); diff --git a/src/PhpWord/Writer/RTF/Element/Element.php b/src/PhpWord/Writer/RTF/Element/Element.php index d460deac..7a2126e4 100644 --- a/src/PhpWord/Writer/RTF/Element/Element.php +++ b/src/PhpWord/Writer/RTF/Element/Element.php @@ -68,7 +68,8 @@ class Element public function write() { $content = ''; - $writerClass = __NAMESPACE__ . '\\' . basename(get_class($this->element)); + $writerClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . + basename(get_class($this->element)); if (class_exists($writerClass)) { $writer = new $writerClass($this->parentWriter, $this->element, $this->withoutP); $content = $writer->write(); diff --git a/src/PhpWord/Writer/Word2007/Element/Container.php b/src/PhpWord/Writer/Word2007/Element/Container.php index 377241ee..4b3c8109 100644 --- a/src/PhpWord/Writer/Word2007/Element/Container.php +++ b/src/PhpWord/Writer/Word2007/Element/Container.php @@ -32,15 +32,16 @@ class Container extends AbstractElement public function write() { $xmlWriter = $this->getXmlWriter(); - $element = $this->getElement(); + $container = $this->getElement(); // Loop through subelements - $containerClass = basename(get_class($element)); - $subelements = $element->getElements(); + $containerClass = basename(get_class($container)); + $subelements = $container->getElements(); $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote', 'TextBox')) ? true : false; if (count($subelements) > 0) { foreach ($subelements as $subelement) { - $writerClass = __NAMESPACE__ . '\\' . basename(get_class($subelement)); + $writerClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . + basename(get_class($subelement)); if (class_exists($writerClass)) { $writer = new $writerClass($xmlWriter, $subelement, $withoutP); $writer->write(); @@ -49,7 +50,7 @@ class Container extends AbstractElement } else { // Special case for Cell: They have to contain a TextBreak at least if ($containerClass == 'Cell') { - $writerClass = __NAMESPACE__ . '\\TextBreak'; + $writerClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\TextBreak'; $writer = new $writerClass($xmlWriter, new TextBreakElement(), $withoutP); $writer->write(); } diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index 02af5b4e..108e7a0e 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -85,18 +85,20 @@ class Styles extends AbstractPart $xmlWriter->startElement('w:name'); $xmlWriter->writeAttribute('w:val', $styleName); $xmlWriter->endElement(); - if (!is_null($paragraphStyle)) { - // Point parent style to Normal - $xmlWriter->startElement('w:basedOn'); - $xmlWriter->writeAttribute('w:val', 'Normal'); - $xmlWriter->endElement(); + // Parent style + $xmlWriter->writeElementIf(!is_null($paragraphStyle), 'w:basedOn', 'w:val', 'Normal'); + + // w:pPr + if (!is_null($paragraphStyle)) { $styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle); $styleWriter->write(); } + // w:rPr $styleWriter = new FontStyleWriter($xmlWriter, $style); $styleWriter->write(); + $xmlWriter->endElement(); // Paragraph style @@ -108,23 +110,19 @@ class Styles extends AbstractPart $xmlWriter->startElement('w:name'); $xmlWriter->writeAttribute('w:val', $styleName); $xmlWriter->endElement(); + // Parent style $basedOn = $style->getBasedOn(); - if (!is_null($basedOn)) { - $xmlWriter->startElement('w:basedOn'); - $xmlWriter->writeAttribute('w:val', $basedOn); - $xmlWriter->endElement(); - } + $xmlWriter->writeElementIf(!is_null($basedOn), 'w:basedOn', 'w:val', $basedOn); + // Next paragraph style $next = $style->getNext(); - if (!is_null($next)) { - $xmlWriter->startElement('w:next'); - $xmlWriter->writeAttribute('w:val', $next); - $xmlWriter->endElement(); - } + $xmlWriter->writeElementIf(!is_null($next), 'w:next', 'w:val', $next); + // w:pPr $styleWriter = new ParagraphStyleWriter($xmlWriter, $style); $styleWriter->write(); + $xmlWriter->endElement(); // Table style diff --git a/src/PhpWord/Writer/Word2007/Style/Cell.php b/src/PhpWord/Writer/Word2007/Style/Cell.php index 5f38f77d..76c347f0 100644 --- a/src/PhpWord/Writer/Word2007/Style/Cell.php +++ b/src/PhpWord/Writer/Word2007/Style/Cell.php @@ -58,8 +58,9 @@ class Cell extends AbstractStyle } // Shading - if (!is_null($style->getShading())) { - $styleWriter = new Shading($xmlWriter, $style->getShading()); + $shading = $style->getShading(); + if (!is_null($shading)) { + $styleWriter = new Shading($xmlWriter, $shading); $styleWriter->write(); } diff --git a/src/PhpWord/Writer/Word2007/Style/Font.php b/src/PhpWord/Writer/Word2007/Style/Font.php index aac53038..5965d5a4 100644 --- a/src/PhpWord/Writer/Word2007/Style/Font.php +++ b/src/PhpWord/Writer/Word2007/Style/Font.php @@ -61,16 +61,16 @@ class Font extends AbstractStyle return; } $xmlWriter = $this->getXmlWriter(); - $font = $style->getName(); - $color = $style->getColor(); - $size = $style->getSize(); - $underline = $style->getUnderline(); - $fgColor = $style->getFgColor(); - $hint = $style->getHint(); $xmlWriter->startElement('w:rPr'); + // Style name + $styleName = $style->getStyleName(); + $xmlWriter->writeElementIf(!is_null($styleName), 'w:rStyle', 'w:val', $styleName); + // Font name/family + $font = $style->getName(); + $hint = $style->getHint(); if ($font != PhpWord::DEFAULT_FONT_NAME) { $xmlWriter->startElement('w:rFonts'); $xmlWriter->writeAttribute('w:ascii', $font); @@ -82,7 +82,11 @@ class Font extends AbstractStyle } // Color + $color = $style->getColor(); $xmlWriter->writeElementIf($color != PhpWord::DEFAULT_FONT_COLOR, 'w:color', 'w:val', $color); + + // Size + $size = $style->getSize(); $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); @@ -100,9 +104,11 @@ class Font extends AbstractStyle $xmlWriter->writeElementIf($style->isAllCaps(), 'w:caps'); // Underline + $underline = $style->getUnderline(); $xmlWriter->writeElementIf($underline != 'none', 'w:u', 'w:val', $underline); // Foreground-Color + $fgColor = $style->getFgColor(); $xmlWriter->writeElementIf(!is_null($fgColor), 'w:highlight', 'w:val', $fgColor); // Superscript/subscript @@ -110,8 +116,9 @@ class Font extends AbstractStyle $xmlWriter->writeElementIf($style->isSubScript(), 'w:vertAlign', 'w:val', 'subscript'); // Background-Color - if (!is_null($style->getShading())) { - $styleWriter = new Shading($xmlWriter, $style->getShading()); + $shading = $style->getShading(); + if (!is_null($shading)) { + $styleWriter = new Shading($xmlWriter, $shading); $styleWriter->write(); } diff --git a/src/PhpWord/Writer/Word2007/Style/Image.php b/src/PhpWord/Writer/Word2007/Style/Image.php index 2dd3398c..f9338759 100644 --- a/src/PhpWord/Writer/Word2007/Style/Image.php +++ b/src/PhpWord/Writer/Word2007/Style/Image.php @@ -38,12 +38,35 @@ class Image extends AbstractStyle */ public function write() { - if (is_null($style = $this->getStyle())) { + if (!is_null($this->getStyle())) { + $this->writeStyle(); + } + } + + /** + * Write w10 wrapping + * + * @return array + */ + public function writeW10Wrap() + { + if (is_null($this->w10wrap)) { return; } + $xmlWriter = $this->getXmlWriter(); - $wrapping = $style->getWrappingStyle(); - $positioning = $style->getPositioning(); + $xmlWriter->startElement('w10:wrap'); + $xmlWriter->writeAttribute('type', $this->w10wrap); + $xmlWriter->endElement(); // w10:wrap + } + + /** + * Write style attribute + */ + protected function writeStyle() + { + $xmlWriter = $this->getXmlWriter(); + $style = $this->getStyle(); // Default style array $styleArray = array( @@ -52,9 +75,10 @@ class Image extends AbstractStyle 'mso-width-relative' => 'margin', 'mso-height-relative' => 'margin', ); - $styleArray = array_merge($styleArray, $this->getElementStyle($style)); + $styleArray = array_merge($styleArray, $this->getElementStyle()); // Absolute/relative positioning + $positioning = $style->getPositioning(); $styleArray['position'] = $positioning; if ($positioning == ImageStyle::POSITION_ABSOLUTE) { $styleArray['mso-position-horizontal-relative'] = 'page'; @@ -69,6 +93,7 @@ class Image extends AbstractStyle } // Wrapping style + $wrapping = $style->getWrappingStyle(); if ($wrapping == ImageStyle::WRAPPING_STYLE_INLINE) { // Nothing to do when inline } elseif ($wrapping == ImageStyle::WRAPPING_STYLE_BEHIND) { @@ -91,29 +116,14 @@ class Image extends AbstractStyle $xmlWriter->writeAttribute('style', $imageStyle); } - /** - * Write w10 wrapping - * - * @return array - */ - public function writeW10Wrap() - { - $xmlWriter = $this->getXmlWriter(); - - if (!is_null($this->w10wrap)) { - $xmlWriter->startElement('w10:wrap'); - $xmlWriter->writeAttribute('type', $this->w10wrap); - $xmlWriter->endElement(); // w10:wrap - } - } - /** * Get element style * * @return array */ - protected function getElementStyle(ImageStyle $style) + private function getElementStyle() { + $style = $this->getStyle(); $styles = array(); $styleValues = array( 'width' => $style->getWidth(), diff --git a/src/PhpWord/Writer/Word2007/Style/Indentation.php b/src/PhpWord/Writer/Word2007/Style/Indentation.php index 60fbd540..797179b7 100644 --- a/src/PhpWord/Writer/Word2007/Style/Indentation.php +++ b/src/PhpWord/Writer/Word2007/Style/Indentation.php @@ -33,14 +33,18 @@ class Indentation extends AbstractStyle return; } $xmlWriter = $this->getXmlWriter(); - $firstLine = $style->getFirstLine(); - $hanging = $style->getHanging(); $xmlWriter->startElement('w:ind'); + $xmlWriter->writeAttribute('w:left', $this->convertTwip($style->getLeft())); $xmlWriter->writeAttribute('w:right', $this->convertTwip($style->getRight())); + + $firstLine = $style->getFirstLine(); $xmlWriter->writeAttributeIf(!is_null($firstLine), 'w:firstLine', $this->convertTwip($firstLine)); + + $hanging = $style->getHanging(); $xmlWriter->writeAttributeIf(!is_null($hanging), 'w:hanging', $this->convertTwip($hanging)); + $xmlWriter->endElement(); } } diff --git a/src/PhpWord/Writer/Word2007/Style/Paragraph.php b/src/PhpWord/Writer/Word2007/Style/Paragraph.php index c2caea11..7056028a 100644 --- a/src/PhpWord/Writer/Word2007/Style/Paragraph.php +++ b/src/PhpWord/Writer/Word2007/Style/Paragraph.php @@ -70,16 +70,17 @@ class Paragraph extends AbstractStyle return; } $xmlWriter = $this->getXmlWriter(); - $align = $style->getAlign(); - $indentation = $style->getIndentation(); - $spacing = $style->getSpace(); - $tabs = $style->getTabs(); if (!$this->withoutPPR) { $xmlWriter->startElement('w:pPr'); } + // Style name + $styleName = $style->getStyleName(); + $xmlWriter->writeElementIf(!is_null($styleName), 'w:pStyle', 'w:val', $styleName); + // Alignment + $align = $style->getAlign(); $xmlWriter->writeElementIf(!is_null($align), 'w:jc', 'w:val', $align); // Pagination @@ -89,18 +90,21 @@ class Paragraph extends AbstractStyle $xmlWriter->writeElementIf($style->hasPageBreakBefore(), 'w:pageBreakBefore', 'w:val', '1'); // Indentation + $indentation = $style->getIndentation(); if (!is_null($indentation)) { $styleWriter = new Indentation($xmlWriter, $indentation); $styleWriter->write(); } // Spacing + $spacing = $style->getSpace(); if (!is_null($spacing)) { $styleWriter = new Spacing($xmlWriter, $spacing); $styleWriter->write(); } // Tabs + $tabs = $style->getTabs(); if (!empty($tabs)) { $xmlWriter->startElement("w:tabs"); foreach ($tabs as $tab) { diff --git a/src/PhpWord/Writer/Word2007/Style/Spacing.php b/src/PhpWord/Writer/Word2007/Style/Spacing.php index faa9b749..b594d9c0 100644 --- a/src/PhpWord/Writer/Word2007/Style/Spacing.php +++ b/src/PhpWord/Writer/Word2007/Style/Spacing.php @@ -33,15 +33,18 @@ class Spacing extends AbstractStyle return; } $xmlWriter = $this->getXmlWriter(); - $before = $style->getBefore(); - $after = $style->getAfter(); - $line = $style->getLine(); $xmlWriter->startElement('w:spacing'); + $before = $style->getBefore(); $xmlWriter->writeAttributeIf(!is_null($before), 'w:before', $this->convertTwip($before)); + + $after = $style->getAfter(); $xmlWriter->writeAttributeIf(!is_null($after), 'w:after', $this->convertTwip($after)); + + $line = $style->getLine(); $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/Table.php b/src/PhpWord/Writer/Word2007/Style/Table.php index 6b5569cf..7be6e6ae 100644 --- a/src/PhpWord/Writer/Word2007/Style/Table.php +++ b/src/PhpWord/Writer/Word2007/Style/Table.php @@ -40,9 +40,9 @@ class Table extends AbstractStyle return; } $xmlWriter = $this->getXmlWriter(); + $hasBorders = $style->hasBorders(); $hasMargins = $style->hasMargins(); - if ($hasMargins || $hasBorders) { $xmlWriter->startElement('w:tblPr'); if ($hasMargins) { @@ -64,6 +64,7 @@ class Table extends AbstractStyle } $xmlWriter->endElement(); // w:tblPr } + // Only write background color and first row for full style if ($this->isFullStyle) { // Background color diff --git a/src/PhpWord/Writer/Word2007/Style/TextBox.php b/src/PhpWord/Writer/Word2007/Style/TextBox.php index f526f888..ee9c74cc 100644 --- a/src/PhpWord/Writer/Word2007/Style/TextBox.php +++ b/src/PhpWord/Writer/Word2007/Style/TextBox.php @@ -17,6 +17,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Style; +use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Style\TextBox as TextBoxStyle; /** @@ -31,71 +32,10 @@ class TextBox extends Image */ public function write() { - if (is_null($style = $this->getStyle())) { - return; + if (!is_null($this->getStyle())) { + $this->writeStyle(); + $this->writeBorder(); } - $xmlWriter = $this->getXmlWriter(); - $wrapping = $style->getWrappingStyle(); - $positioning = $style->getPositioning(); - - // Default style array - $styleArray = array( - 'mso-width-percent' => '0', - 'mso-height-percent' => '0', - 'mso-width-relative' => 'margin', - 'mso-height-relative' => 'margin', - ); - $styleArray = array_merge($styleArray, $this->getElementStyle($style)); - - // Absolute/relative positioning - $styleArray['position'] = $positioning; - if ($positioning == TextBoxStyle::POSITION_ABSOLUTE) { - $styleArray['mso-position-horizontal-relative'] = 'page'; - $styleArray['mso-position-vertical-relative'] = 'page'; - } elseif ($positioning == TextBoxStyle::POSITION_RELATIVE) { - $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; - } - - // Wrapping style - if ($wrapping == TextBoxStyle::WRAPPING_STYLE_INLINE) { - // Nothing to do when inline - } elseif ($wrapping == TextBoxStyle::WRAPPING_STYLE_BEHIND) { - $styleArray['z-index'] = -251658752; - } else { - $styleArray['z-index'] = 251659264; - $styleArray['mso-position-horizontal'] = 'absolute'; - $styleArray['mso-position-vertical'] = 'absolute'; - } - - // w10 wrapping - if ($wrapping == TextBoxStyle::WRAPPING_STYLE_SQUARE) { - $this->w10wrap = 'square'; - } elseif ($wrapping == TextBoxStyle::WRAPPING_STYLE_TIGHT) { - $this->w10wrap = 'tight'; - } - - $textboxStyle = $this->assembleStyle($styleArray); - - $xmlWriter->writeAttribute('style', $textboxStyle); - - $borderSize = $style->getBorderSize(); - if ($borderSize !== null) { - $xmlWriter->writeAttribute('strokeweight', $style->getBorderSize().'pt'); - } - - $borderColor = $style->getBorderColor(); - if (empty($borderColor)) { - $xmlWriter->writeAttribute('stroked', 'f'); - } else { - $xmlWriter->writeAttribute('strokecolor', $borderColor); - } - //@todo - } /** @@ -105,49 +45,62 @@ class TextBox extends Image */ public function writeW10Wrap() { - $xmlWriter = $this->getXmlWriter(); - - if (!is_null($this->w10wrap)) { - $xmlWriter->startElement('w10:wrap'); - $xmlWriter->writeAttribute('type', $this->w10wrap); - - switch ($style->getPositioning()) { - case TextBoxStyle::POSITION_ABSOLUTE: - $xmlWriter->writeAttribute('anchorx', "page"); - $xmlWriter->writeAttribute('anchory', "page"); - break; - case TextBoxStyle::POSITION_RELATIVE: - switch ($style->getPosVerticalRel()) { - case TextBoxStyle::POSITION_RELATIVE_TO_MARGIN: - $xmlWriter->writeAttribute('anchory', "margin"); - break; - case TextBoxStyle::POSITION_RELATIVE_TO_PAGE: - $xmlWriter->writeAttribute('anchory', "page"); - break; - case TextBoxStyle::POSITION_RELATIVE_TO_TMARGIN: - $xmlWriter->writeAttribute('anchory', "margin"); - break; - case TextBoxStyle::POSITION_RELATIVE_TO_BMARGIN: - $xmlWriter->writeAttribute('anchory', "page"); - break; - } - switch ($style->getPosHorizontalRel()) { - case TextBoxStyle::POSITION_RELATIVE_TO_MARGIN: - $xmlWriter->writeAttribute('anchorx', "margin"); - break; - case TextBoxStyle::POSITION_RELATIVE_TO_PAGE: - $xmlWriter->writeAttribute('anchorx', "page"); - break; - case TextBoxStyle::POSITION_RELATIVE_TO_LMARGIN: - $xmlWriter->writeAttribute('anchorx', "margin"); - break; - case TextBoxStyle::POSITION_RELATIVE_TO_RMARGIN: - $xmlWriter->writeAttribute('anchorx', "page"); - break; - } - } - - $xmlWriter->endElement(); // w10:wrap + if (is_null($this->w10wrap)) { + return; } + + $relativePositions = array( + TextBoxStyle::POSITION_RELATIVE_TO_MARGIN => 'margin', + TextBoxStyle::POSITION_RELATIVE_TO_PAGE => 'page', + TextBoxStyle::POSITION_RELATIVE_TO_TMARGIN => 'margin', + TextBoxStyle::POSITION_RELATIVE_TO_BMARGIN => 'page', + TextBoxStyle::POSITION_RELATIVE_TO_LMARGIN => 'margin', + TextBoxStyle::POSITION_RELATIVE_TO_RMARGIN => 'page', + ); + $pos = $style->getPositioning(); + $vPos = $style->getPosVerticalRel(); + $hPos = $style->getPosHorizontalRel(); + + $xmlWriter = $this->getXmlWriter(); + $xmlWriter->startElement('w10:wrap'); + $xmlWriter->writeAttribute('type', $this->w10wrap); + + if ($pos == TextBoxStyle::POSITION_ABSOLUTE) { + $xmlWriter->writeAttribute('anchorx', "page"); + $xmlWriter->writeAttribute('anchory', "page"); + } elseif ($pos == TextBoxStyle::POSITION_RELATIVE) { + if (array_key_exists($vPos, $relativePositions)) { + $xmlWriter->writeAttribute('anchory', $relativePositions[$vPos]); + } + if (array_key_exists($hPos, $relativePositions)) { + $xmlWriter->writeAttribute('anchorx', $relativePositions[$hPos]); + } + } + + $xmlWriter->endElement(); // w10:wrap + } + + /** + * Writer border + */ + private function writeBorder() + { + $xmlWriter = $this->getXmlWriter(); + $style = $this->getStyle(); + + // Border size + $borderSize = $style->getBorderSize(); + if ($borderSize !== null) { + $xmlWriter->writeAttribute('strokeweight', $borderSize . 'pt'); + } + + // Border color + $borderColor = $style->getBorderColor(); + if (empty($borderColor)) { + $xmlWriter->writeAttribute('stroked', 'f'); + } else { + $xmlWriter->writeAttribute('strokecolor', $borderColor); + } + //@todo } } diff --git a/tests/PhpWord/Tests/Style/AbstractStyleTest.php b/tests/PhpWord/Tests/Style/AbstractStyleTest.php index 38222520..15ff8fac 100644 --- a/tests/PhpWord/Tests/Style/AbstractStyleTest.php +++ b/tests/PhpWord/Tests/Style/AbstractStyleTest.php @@ -58,6 +58,18 @@ class AbstractStyleTest extends \PHPUnit_Framework_TestCase $this->assertEquals(false, self::callProtectedMethod($stub, 'setBoolVal', array('a', false))); $this->assertEquals(200, self::callProtectedMethod($stub, 'setIntVal', array('foo', 200))); $this->assertEquals(2.1, self::callProtectedMethod($stub, 'setFloatVal', array('foo', 2.1))); + $this->assertEquals('b', self::callProtectedMethod($stub, 'setEnumVal', array(null, array('a', 'b'), 'b'))); + } + + /** + * Test setEnumVal exception + * + * @expectedException \InvalidArgumentException + */ + public function testSetValEnumException() + { + $stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle'); + $this->assertEquals('b', self::callProtectedMethod($stub, 'setEnumVal', array('z', array('a', 'b'), 'b'))); } diff --git a/tests/PhpWord/Tests/Style/FontTest.php b/tests/PhpWord/Tests/Style/FontTest.php index 2f5c49c6..74aa5e2e 100644 --- a/tests/PhpWord/Tests/Style/FontTest.php +++ b/tests/PhpWord/Tests/Style/FontTest.php @@ -97,7 +97,7 @@ class FontTest extends \PHPUnit_Framework_TestCase 'fgColor' => Font::FGCOLOR_YELLOW, 'bgColor' => 'FFFF00', 'hint' => 'eastAsia', - 'line-height' => 2, + 'lineHeight' => 2, ); $object->setStyleByArray($attributes); foreach ($attributes as $key => $value) { diff --git a/tests/PhpWord/Tests/Style/SectionTest.php b/tests/PhpWord/Tests/Style/SectionTest.php index a6b386d9..dd7273cb 100644 --- a/tests/PhpWord/Tests/Style/SectionTest.php +++ b/tests/PhpWord/Tests/Style/SectionTest.php @@ -249,12 +249,14 @@ class SettingsTest extends \PHPUnit_Framework_TestCase // Default $this->assertEquals(1, $oSettings->getColsNum()); + // Null value + $oSettings->setColsNum(); + $this->assertEquals(1, $oSettings->getColsNum()); + + // Random value $iVal = rand(1, 1000); $oSettings->setColsNum($iVal); $this->assertEquals($iVal, $oSettings->getColsNum()); - - $oSettings->setColsNum(); - $this->assertEquals(1, $oSettings->getColsNum()); } /**