Refactor writers and styles

This commit is contained in:
Ivan Lanin 2014-05-08 19:25:29 +07:00
parent ea41b08a9a
commit f7dd9dd07c
33 changed files with 450 additions and 430 deletions

View File

@ -78,7 +78,7 @@ abstract class AbstractContainer extends AbstractElement
public function addText($text, $fontStyle = null, $paragraphStyle = null, $elementName = 'Text') public function addText($text, $fontStyle = null, $paragraphStyle = null, $elementName = 'Text')
{ {
$this->checkValidity($elementName); $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 // Reset paragraph style for footnote and textrun. They have their own
if (in_array($this->container, array('textrun', 'footnote', 'endnote'))) { 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') public function addFootnote($paragraphStyle = null, $elementName = 'Footnote')
{ {
$this->checkValidity($elementName); $this->checkValidity($elementName);
$elementClass = __NAMESPACE__ . '\\' . $elementName; $elementClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . $elementName;
$docPart = strtolower($elementName); $docPart = strtolower($elementName);
$addMethod = "add{$elementName}"; $addMethod = "add{$elementName}";

View File

@ -231,9 +231,7 @@ abstract class AbstractElement
protected function setStyle($styleObject, $styleValue = null, $returnObject = false) protected function setStyle($styleObject, $styleValue = null, $returnObject = false)
{ {
if (!is_null($styleValue) && is_array($styleValue)) { if (!is_null($styleValue) && is_array($styleValue)) {
foreach ($styleValue as $key => $value) { $styleObject->setStyleByArray($styleValue);
$styleObject->setStyleValue($key, $value);
}
$style = $styleObject; $style = $styleObject;
} else { } else {
$style = $returnObject ? $styleObject : $styleValue; $style = $returnObject ? $styleObject : $styleValue;

View File

@ -222,8 +222,9 @@ class Section extends AbstractContainer
*/ */
private function addHeaderFooter($type = Header::AUTO, $header = true) 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'; $collectionArray = $header ? 'headers' : 'footers';
$containerClass = __NAMESPACE__ . '\\' . ($header ? 'Header' : 'Footer');
$collection = &$this->$collectionArray; $collection = &$this->$collectionArray;
if (in_array($type, array(Header::AUTO, Header::FIRST, Header::EVEN))) { if (in_array($type, array(Header::AUTO, Header::FIRST, Header::EVEN))) {

View File

@ -68,20 +68,14 @@ class TOC extends AbstractElement
$this->TOCStyle = new TOCStyle(); $this->TOCStyle = new TOCStyle();
if (!is_null($tocStyle) && is_array($tocStyle)) { if (!is_null($tocStyle) && is_array($tocStyle)) {
foreach ($tocStyle as $key => $value) { $this->TOCStyle->setStyleByArray($tocStyle);
$this->TOCStyle->setStyleValue($key, $value);
}
} }
if (!is_null($fontStyle)) { if (!is_null($fontStyle) && is_array($fontStyle)) {
if (is_array($fontStyle)) { $this->fontStyle = new Font();
$this->fontStyle = new Font(); $this->fontStyle->setStyleByArray($fontStyle);
foreach ($fontStyle as $key => $value) { } else {
$this->fontStyle->setStyleValue($key, $value); $this->fontStyle = $fontStyle;
}
} else {
$this->fontStyle = $fontStyle;
}
} }
$this->minDepth = $minDepth; $this->minDepth = $minDepth;

View File

@ -138,60 +138,59 @@ abstract class AbstractPart
/** /**
* Read w:pPr * Read w:pPr
* *
* @return string|array|null * @return array|null
*/ */
protected function readParagraphStyle(XMLReader $xmlReader, \DOMElement $domNode) protected function readParagraphStyle(XMLReader $xmlReader, \DOMElement $domNode)
{ {
$style = null; if (!$xmlReader->elementExists('w:pPr', $domNode)) {
if ($xmlReader->elementExists('w:pPr', $domNode)) { return;
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',
);
$nodes = $xmlReader->getElements('w:pPr/*', $domNode); $style = array();
foreach ($nodes as $node) { $mapping = array(
if (!array_key_exists($node->nodeName, $mapping)) { 'w:pStyle' => 'styleName',
continue; 'w:ind' => 'indent', 'w:spacing' => 'spacing',
} 'w:jc' => 'align', 'w:basedOn' => 'basedOn', 'w:next' => 'next',
$property = $mapping[$node->nodeName]; 'w:widowControl' => 'widowControl', 'w:keepNext' => 'keepNext',
switch ($node->nodeName) { 'w:keepLines' => 'keepLines', 'w:pageBreakBefore' => 'pageBreakBefore',
);
case 'w:ind': $nodes = $xmlReader->getElements('w:pPr/*', $domNode);
$style['indent'] = $xmlReader->getAttribute('w:left', $node); foreach ($nodes as $node) {
$style['hanging'] = $xmlReader->getAttribute('w:hanging', $node); if (!array_key_exists($node->nodeName, $mapping)) {
break; continue;
}
$property = $mapping[$node->nodeName];
switch ($node->nodeName) {
case 'w:spacing': case 'w:ind':
$style['spaceAfter'] = $xmlReader->getAttribute('w:after', $node); $style['indent'] = $xmlReader->getAttribute('w:left', $node);
$style['spaceBefore'] = $xmlReader->getAttribute('w:before', $node); $style['hanging'] = $xmlReader->getAttribute('w:hanging', $node);
// Commented. Need to adjust the number when return value is null break;
// $style['spacing'] = $xmlReader->getAttribute('w:line', $node);
break;
case 'w:keepNext': case 'w:spacing':
case 'w:keepLines': $style['spaceAfter'] = $xmlReader->getAttribute('w:after', $node);
case 'w:pageBreakBefore': $style['spaceBefore'] = $xmlReader->getAttribute('w:before', $node);
$style[$property] = true; // Commented. Need to adjust the number when return value is null
break; // $style['spacing'] = $xmlReader->getAttribute('w:line', $node);
break;
case 'w:widowControl': case 'w:keepNext':
$style[$property] = false; case 'w:keepLines':
break; case 'w:pageBreakBefore':
$style[$property] = true;
break;
case 'w:jc': case 'w:widowControl':
case 'w:basedOn': $style[$property] = false;
case 'w:next': break;
$style[$property] = $xmlReader->getAttribute('w:val', $node);
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 * Read w:rPr
* *
* @return string|array|null * @return array|null
*/ */
protected function readFontStyle(XMLReader $xmlReader, \DOMElement $domNode) protected function readFontStyle(XMLReader $xmlReader, \DOMElement $domNode)
{ {
$style = null; if (is_null($domNode)) {
return;
}
// Hyperlink has an extra w:r child // Hyperlink has an extra w:r child
if ($domNode->nodeName == 'w:hyperlink') { if ($domNode->nodeName == 'w:hyperlink') {
$domNode = $xmlReader->getElement('w:r', $domNode); $domNode = $xmlReader->getElement('w:r', $domNode);
} }
if (is_null($domNode)) { if (!$xmlReader->elementExists('w:rPr', $domNode)) {
return $style; 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); $style = array();
foreach ($nodes as $node) { $mapping = array(
if (!array_key_exists($node->nodeName, $mapping)) { 'w:rStyle' => 'styleName',
continue; '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]; break;
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;
}
}
} }
} }

View File

@ -160,9 +160,6 @@ abstract class AbstractStyle
*/ */
protected function setBoolVal($value, $default = null) protected function setBoolVal($value, $default = null)
{ {
if (is_string($value)) {
$value = (bool)$value;
}
if (!is_bool($value)) { if (!is_bool($value)) {
$value = $default; $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 mixed $value
* @param int|null $default * @param int|null $default
@ -195,7 +192,7 @@ abstract class AbstractStyle
*/ */
protected function setIntVal($value, $default = null) protected function setIntVal($value, $default = null)
{ {
if (is_string($value)) { if (is_string($value) && (preg_match('/[^\d]/', $value) == 0)) {
$value = intval($value); $value = intval($value);
} }
if (!is_int($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 mixed $value
* @param float|null $default * @param float|null $default
@ -214,7 +211,7 @@ abstract class AbstractStyle
*/ */
protected function setFloatVal($value, $default = null) protected function setFloatVal($value, $default = null)
{ {
if (is_string($value)) { if (is_string($value) && (preg_match('/[^\d\.\,]/', $value) == 0)) {
$value = floatval($value); $value = floatval($value);
} }
if (!is_float($value)) { if (!is_float($value)) {
@ -231,9 +228,11 @@ abstract class AbstractStyle
* @param array $enum * @param array $enum
* @param mixed $default * @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; $value = $default;
} }
@ -249,7 +248,7 @@ abstract class AbstractStyle
*/ */
protected function setObjectVal($value, $styleName, &$style) 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 (is_array($value)) {
if (!$style instanceof $styleClass) { if (!$style instanceof $styleClass) {
$style = new $styleClass(); $style = new $styleClass();

View File

@ -22,6 +22,16 @@ namespace PhpOffice\PhpWord\Style;
*/ */
class Cell extends Border 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 * Text direction constants
* *
@ -30,6 +40,14 @@ class Cell extends Border
const TEXT_DIR_BTLR = 'btLr'; const TEXT_DIR_BTLR = 'btLr';
const TEXT_DIR_TBRL = 'tbRl'; const TEXT_DIR_TBRL = 'tbRl';
/**
* Vertical merge (rowspan) constants
*
* @const string
*/
const VMERGE_RESTART = 'restart';
const VMERGE_CONTINUE = 'continue';
/** /**
* Default border color * Default border color
* *
@ -56,7 +74,7 @@ class Cell extends Border
* *
* @var integer * @var integer
*/ */
private $gridSpan = null; private $gridSpan;
/** /**
* rowspan (restart, continue) * rowspan (restart, continue)
@ -66,7 +84,7 @@ class Cell extends Border
* *
* @var string * @var string
*/ */
private $vMerge = null; private $vMerge;
/** /**
* Shading * Shading
@ -87,10 +105,14 @@ class Cell extends Border
* Set vertical align * Set vertical align
* *
* @param string $value * @param string $value
* @return self
*/ */
public function setVAlign($value = null) 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 * Set text direction
* *
* @param string $value * @param string $value
* @return self
*/ */
public function setTextDirection($value = null) 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)); 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) * 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; 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 * Get shading
* *

View File

@ -216,6 +216,16 @@ class Font extends AbstractStyle
$this->setParagraph($paragraph); $this->setParagraph($paragraph);
} }
/**
* Get style type
*
* @return string
*/
public function getStyleType()
{
return $this->type;
}
/** /**
* Get font name * Get font name
* *
@ -570,16 +580,6 @@ class Font extends AbstractStyle
$this->setShading(array('fill' => $value)); $this->setShading(array('fill' => $value));
} }
/**
* Get style type
*
* @return string
*/
public function getStyleType()
{
return $this->type;
}
/** /**
* Get line height * 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 * @param bool $sourceValue
*/ */
private function toggleFalse(&$target, $sourceValue) private function toggleFalse(&$target, $sourceValue)

View File

@ -117,7 +117,7 @@ class Image extends AbstractStyle
* *
* @var string * @var string
*/ */
private $wrappingStyle; private $wrappingStyle = self::WRAPPING_STYLE_INLINE;
/** /**
* Positioning type (relative or absolute) * Positioning type (relative or absolute)
@ -131,40 +131,28 @@ class Image extends AbstractStyle
* *
* @var string * @var string
*/ */
private $posHorizontal; private $posHorizontal = self::POSITION_HORIZONTAL_LEFT;
/** /**
* Horizontal Relation * Horizontal Relation
* *
* @var string * @var string
*/ */
private $posHorizontalRel; private $posHorizontalRel = self::POSITION_RELATIVE_TO_CHAR;
/** /**
* Vertical alignment * Vertical alignment
* *
* @var string * @var string
*/ */
private $posVertical; private $posVertical = self::POSITION_VERTICAL_TOP;
/** /**
* Vertical Relation * Vertical Relation
* *
* @var string * @var string
*/ */
private $posVerticalRel; private $posVerticalRel = self::POSITION_RELATIVE_TO_LINE;
/**
* 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);
}
/** /**
* Get width * Get width
@ -283,14 +271,12 @@ class Image extends AbstractStyle
*/ */
public function setWrappingStyle($wrappingStyle) public function setWrappingStyle($wrappingStyle)
{ {
$enum = array(self::WRAPPING_STYLE_INLINE, self::WRAPPING_STYLE_INFRONT, self::WRAPPING_STYLE_BEHIND, $enum = array(
self::WRAPPING_STYLE_SQUARE, self::WRAPPING_STYLE_TIGHT); self::WRAPPING_STYLE_INLINE,
self::WRAPPING_STYLE_INFRONT, self::WRAPPING_STYLE_BEHIND,
if (in_array($wrappingStyle, $enum)) { self::WRAPPING_STYLE_SQUARE, self::WRAPPING_STYLE_TIGHT,
$this->wrappingStyle = $wrappingStyle; );
} else { $this->wrappingStyle = $this->setEnumVal($wrappingStyle, $enum, $this->wrappingStyle);
throw new \InvalidArgumentException('Invalid wrapping style.');
}
return $this; return $this;
} }
@ -315,12 +301,7 @@ class Image extends AbstractStyle
public function setPositioning($positioning) public function setPositioning($positioning)
{ {
$enum = array(self::POSITION_RELATIVE, self::POSITION_ABSOLUTE); $enum = array(self::POSITION_RELATIVE, self::POSITION_ABSOLUTE);
$this->positioning = $this->setEnumVal($positioning, $enum, $this->positioning);
if (in_array($positioning, $enum)) {
$this->positioning = $positioning;
} else {
throw new \InvalidArgumentException('Invalid positioning.');
}
return $this; return $this;
} }
@ -344,14 +325,11 @@ class Image extends AbstractStyle
*/ */
public function setPosHorizontal($alignment) public function setPosHorizontal($alignment)
{ {
$enum = array(self::POSITION_HORIZONTAL_LEFT, self::POSITION_HORIZONTAL_CENTER, $enum = array(
self::POSITION_HORIZONTAL_RIGHT); self::POSITION_HORIZONTAL_LEFT, self::POSITION_HORIZONTAL_CENTER,
self::POSITION_HORIZONTAL_RIGHT,
if (in_array($alignment, $enum)) { );
$this->posHorizontal = $alignment; $this->posHorizontal = $this->setEnumVal($alignment, $enum, $this->posHorizontal);
} else {
throw new \InvalidArgumentException('Invalid horizontal alignment.');
}
return $this; return $this;
} }
@ -375,14 +353,12 @@ class Image extends AbstractStyle
*/ */
public function setPosVertical($alignment) public function setPosVertical($alignment)
{ {
$enum = array(self::POSITION_VERTICAL_TOP, self::POSITION_VERTICAL_CENTER, $enum = array(
self::POSITION_VERTICAL_BOTTOM, self::POSITION_VERTICAL_INSIDE, self::POSITION_VERTICAL_OUTSIDE); self::POSITION_VERTICAL_TOP, self::POSITION_VERTICAL_CENTER,
self::POSITION_VERTICAL_BOTTOM, self::POSITION_VERTICAL_INSIDE,
if (in_array($alignment, $enum)) { self::POSITION_VERTICAL_OUTSIDE,
$this->posVertical = $alignment; );
} else { $this->posVertical = $this->setEnumVal($alignment, $enum, $this->posVertical);
throw new \InvalidArgumentException('Invalid vertical alignment.');
}
return $this; return $this;
} }
@ -406,16 +382,13 @@ class Image extends AbstractStyle
*/ */
public function setPosHorizontalRel($relto) 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_COLUMN, self::POSITION_RELATIVE_TO_CHAR,
self::POSITION_RELATIVE_TO_LMARGIN, self::POSITION_RELATIVE_TO_RMARGIN, self::POSITION_RELATIVE_TO_LMARGIN, self::POSITION_RELATIVE_TO_RMARGIN,
self::POSITION_RELATIVE_TO_IMARGIN, self::POSITION_RELATIVE_TO_OMARGIN); self::POSITION_RELATIVE_TO_IMARGIN, self::POSITION_RELATIVE_TO_OMARGIN,
);
if (in_array($relto, $enum)) { $this->posHorizontalRel = $this->setEnumVal($relto, $enum, $this->posHorizontalRel);
$this->posHorizontalRel = $relto;
} else {
throw new \InvalidArgumentException('Invalid relative horizontal alignment.');
}
return $this; return $this;
} }
@ -439,16 +412,13 @@ class Image extends AbstractStyle
*/ */
public function setPosVerticalRel($relto) 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_LINE,
self::POSITION_RELATIVE_TO_TMARGIN, self::POSITION_RELATIVE_TO_BMARGIN, self::POSITION_RELATIVE_TO_TMARGIN, self::POSITION_RELATIVE_TO_BMARGIN,
self::POSITION_RELATIVE_TO_IMARGIN, self::POSITION_RELATIVE_TO_OMARGIN); self::POSITION_RELATIVE_TO_IMARGIN, self::POSITION_RELATIVE_TO_OMARGIN,
);
if (in_array($relto, $enum)) { $this->posVerticalRel = $this->setEnumVal($relto, $enum, $this->posVerticalRel);
$this->posVerticalRel = $relto;
} else {
throw new \InvalidArgumentException('Invalid relative vertical alignment.');
}
return $this; return $this;
} }

View File

@ -86,14 +86,19 @@ class ListItem extends AbstractStyle
* Set legacy list type for version < 0.10.0 * Set legacy list type for version < 0.10.0
* *
* @param integer $value * @param integer $value
* @return self
*/ */
public function setListType($value = self::TYPE_BULLET_FILLED) 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_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->listType = $this->setEnumVal($value, $enum, $this->listType);
$this->getListTypeStyle(); $this->getListTypeStyle();
return $this;
} }
/** /**
@ -110,6 +115,7 @@ class ListItem extends AbstractStyle
* Set numbering style name * Set numbering style name
* *
* @param string $value * @param string $value
* @return self
*/ */
public function setNumStyle($value) public function setNumStyle($value)
{ {
@ -119,6 +125,8 @@ class ListItem extends AbstractStyle
$this->numId = $numStyleObject->getIndex(); $this->numId = $numStyleObject->getIndex();
$numStyleObject->setNumId($this->numId); $numStyleObject->setNumId($this->numId);
} }
return $this;
} }
/** /**

View File

@ -69,6 +69,7 @@ class Numbering extends AbstractStyle
public function setNumId($value) public function setNumId($value)
{ {
$this->numId = $this->setIntVal($value, $this->numId); $this->numId = $this->setIntVal($value, $this->numId);
return $this; return $this;
} }
@ -92,6 +93,7 @@ class Numbering extends AbstractStyle
{ {
$enum = array('singleLevel', 'multilevel', 'hybridMultilevel'); $enum = array('singleLevel', 'multilevel', 'hybridMultilevel');
$this->type = $this->setEnumVal($value, $enum, $this->type); $this->type = $this->setEnumVal($value, $enum, $this->type);
return $this; return $this;
} }

View File

@ -125,10 +125,8 @@ class Paragraph extends AbstractStyle
} elseif ($key == 'spacing') { } elseif ($key == 'spacing') {
$value += 240; // because line height of 1 matches 240 twips $value += 240; // because line height of 1 matches 240 twips
} }
$method = 'set' . $key;
if (method_exists($this, $method)) { return parent::setStyleValue($key, $value);
$this->$method($value);
}
} }
/** /**

View File

@ -71,6 +71,8 @@ class Row extends AbstractStyle
public function setTblHeader($value = false) public function setTblHeader($value = false)
{ {
$this->tblHeader = $this->setBoolVal($value, $this->tblHeader); $this->tblHeader = $this->setBoolVal($value, $this->tblHeader);
return $this;
} }
/** /**
@ -92,6 +94,8 @@ class Row extends AbstractStyle
public function setCantSplit($value = false) public function setCantSplit($value = false)
{ {
$this->cantSplit = $this->setBoolVal($value, $this->cantSplit); $this->cantSplit = $this->setBoolVal($value, $this->cantSplit);
return $this;
} }
/** /**
@ -113,6 +117,7 @@ class Row extends AbstractStyle
public function setExactHeight($value = false) public function setExactHeight($value = false)
{ {
$this->exactHeight = $this->setBoolVal($value, $this->exactHeight); $this->exactHeight = $this->setBoolVal($value, $this->exactHeight);
return $this; return $this;
} }

View File

@ -261,7 +261,7 @@ class Section extends Border
* @param int|float $value * @param int|float $value
* @return self * @return self
*/ */
public function setMarginTop($value = '') public function setMarginTop($value = null)
{ {
$this->marginTop = $this->setNumericVal($value, self::DEFAULT_MARGIN); $this->marginTop = $this->setNumericVal($value, self::DEFAULT_MARGIN);
@ -284,7 +284,7 @@ class Section extends Border
* @param int|float $value * @param int|float $value
* @return self * @return self
*/ */
public function setMarginLeft($value = '') public function setMarginLeft($value = null)
{ {
$this->marginLeft = $this->setNumericVal($value, self::DEFAULT_MARGIN); $this->marginLeft = $this->setNumericVal($value, self::DEFAULT_MARGIN);
@ -307,7 +307,7 @@ class Section extends Border
* @param int|float $value * @param int|float $value
* @return self * @return self
*/ */
public function setMarginRight($value = '') public function setMarginRight($value = null)
{ {
$this->marginRight = $this->setNumericVal($value, self::DEFAULT_MARGIN); $this->marginRight = $this->setNumericVal($value, self::DEFAULT_MARGIN);
@ -330,7 +330,7 @@ class Section extends Border
* @param int|float $value * @param int|float $value
* @return self * @return self
*/ */
public function setMarginBottom($value = '') public function setMarginBottom($value = null)
{ {
$this->marginBottom = $this->setNumericVal($value, self::DEFAULT_MARGIN); $this->marginBottom = $this->setNumericVal($value, self::DEFAULT_MARGIN);
@ -353,7 +353,7 @@ class Section extends Border
* @param int|float $value * @param int|float $value
* @return self * @return self
*/ */
public function setGutter($value = '') public function setGutter($value = null)
{ {
$this->gutter = $this->setNumericVal($value, self::DEFAULT_GUTTER); $this->gutter = $this->setNumericVal($value, self::DEFAULT_GUTTER);
@ -376,7 +376,7 @@ class Section extends Border
* @param int|float $value * @param int|float $value
* @return self * @return self
*/ */
public function setHeaderHeight($value = '') public function setHeaderHeight($value = null)
{ {
$this->headerHeight = $this->setNumericVal($value, self::DEFAULT_HEADER_HEIGHT); $this->headerHeight = $this->setNumericVal($value, self::DEFAULT_HEADER_HEIGHT);
@ -399,7 +399,7 @@ class Section extends Border
* @param int|float $value * @param int|float $value
* @return self * @return self
*/ */
public function setFooterHeight($value = '') public function setFooterHeight($value = null)
{ {
$this->footerHeight = $this->setNumericVal($value, self::DEFAULT_FOOTER_HEIGHT); $this->footerHeight = $this->setNumericVal($value, self::DEFAULT_FOOTER_HEIGHT);
@ -444,7 +444,7 @@ class Section extends Border
* @param int $value * @param int $value
* @return self * @return self
*/ */
public function setColsNum($value = '') public function setColsNum($value = null)
{ {
$this->colsNum = $this->setIntVal($value, self::DEFAULT_COLUMN_COUNT); $this->colsNum = $this->setIntVal($value, self::DEFAULT_COLUMN_COUNT);
@ -467,7 +467,7 @@ class Section extends Border
* @param int|float $value * @param int|float $value
* @return self * @return self
*/ */
public function setColsSpace($value = '') public function setColsSpace($value = null)
{ {
$this->colsSpace = $this->setNumericVal($value, self::DEFAULT_COLUMN_SPACING); $this->colsSpace = $this->setNumericVal($value, self::DEFAULT_COLUMN_SPACING);

View File

@ -89,9 +89,10 @@ class Shading extends AbstractStyle
*/ */
public function setPattern($value = null) public function setPattern($value = null)
{ {
$enum = array(self::PATTERN_CLEAR, self::PATTERN_SOLID, self::PATTERN_HSTRIPE, $enum = array(
self::PATTERN_VSTRIPE, self::PATTERN_DSTRIPE, self::PATTERN_HCROSS, self::PATTERN_DCROSS); 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); $this->pattern = $this->setEnumVal($value, $enum, $this->pattern);
return $this; return $this;

View File

@ -36,7 +36,7 @@ class TOC extends Tab
/** /**
* Indent * Indent
* *
* @var int * @var int|float (twip)
*/ */
private $indent = 200; private $indent = 200;
@ -51,7 +51,7 @@ class TOC extends Tab
/** /**
* Get Tab Position * Get Tab Position
* *
* @return int * @return int|float
*/ */
public function getTabPos() public function getTabPos()
{ {
@ -61,11 +61,12 @@ class TOC extends Tab
/** /**
* Set Tab Position * Set Tab Position
* *
* @param int $value * @param int|float $value
* @return self
*/ */
public function setTabPos($value) public function setTabPos($value)
{ {
$this->setPosition($value); return $this->setPosition($value);
} }
/** /**
@ -82,16 +83,17 @@ class TOC extends Tab
* Set Tab Leader * Set Tab Leader
* *
* @param string $value * @param string $value
* @return self
*/ */
public function setTabLeader($value = self::TAB_LEADER_DOT) public function setTabLeader($value = self::TAB_LEADER_DOT)
{ {
$this->setLeader($value); return $this->setLeader($value);
} }
/** /**
* Get Indent * Get Indent
* *
* @return int * @return int|float
*/ */
public function getIndent() public function getIndent()
{ {
@ -101,10 +103,13 @@ class TOC extends Tab
/** /**
* Set Indent * Set Indent
* *
* @param string $value * @param int|float $value
* @return self
*/ */
public function setIndent($value) public function setIndent($value)
{ {
$this->indent = $value; $this->indent = $this->setNumericVal($value, $this->indent);
return $this;
} }
} }

View File

@ -107,12 +107,18 @@ class Tab extends AbstractStyle
* Set stop type * Set stop type
* *
* @param string $value * @param string $value
* @return self
*/ */
public function setType($value) public function setType($value)
{ {
$enum = array(self::TAB_STOP_CLEAR, self::TAB_STOP_LEFT, self::TAB_STOP_CENTER, $enum = array(
self::TAB_STOP_RIGHT, self::TAB_STOP_DECIMAL, self::TAB_STOP_BAR, self::TAB_STOP_NUM); 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); $this->type = $this->setEnumVal($value, $enum, $this->type);
return $this;
} }
/** /**
@ -129,12 +135,17 @@ class Tab extends AbstractStyle
* Set leader * Set leader
* *
* @param string $value * @param string $value
* @return self
*/ */
public function setLeader($value) public function setLeader($value)
{ {
$enum = array(self::TAB_LEADER_NONE, self::TAB_LEADER_DOT, self::TAB_LEADER_HYPHEN, $enum = array(
self::TAB_LEADER_UNDERSCORE, self::TAB_LEADER_HEAVY, self::TAB_LEADER_MIDDLEDOT); 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); $this->leader = $this->setEnumVal($value, $enum, $this->leader);
return $this;
} }
/** /**
@ -151,9 +162,12 @@ class Tab extends AbstractStyle
* Set position * Set position
* *
* @param int|float $value * @param int|float $value
* @return self
*/ */
public function setPosition($value) public function setPosition($value)
{ {
$this->position = $this->setNumericVal($value, $this->position); $this->position = $this->setNumericVal($value, $this->position);
return $this;
} }
} }

View File

@ -112,15 +112,11 @@ class Table extends Border
unset($this->firstRow->borderInsideVSize); unset($this->firstRow->borderInsideVSize);
unset($this->firstRow->borderInsideHColor); unset($this->firstRow->borderInsideHColor);
unset($this->firstRow->borderInsideHSize); unset($this->firstRow->borderInsideHSize);
foreach ($styleFirstRow as $key => $value) { $this->firstRow->setStyleByArray($styleFirstRow);
$this->firstRow->setStyleValue($key, $value);
}
} }
if (!is_null($styleTable) && is_array($styleTable)) { if (!is_null($styleTable) && is_array($styleTable)) {
foreach ($styleTable as $key => $value) { $this->setStyleByArray($styleTable);
$this->setStyleValue($key, $value);
}
} }
} }

View File

@ -73,7 +73,8 @@ class Element
public function write() public function write()
{ {
$content = ''; $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)) { if (class_exists($writerClass)) {
$writer = new $writerClass($this->parentWriter, $this->element, $this->withoutP); $writer = new $writerClass($this->parentWriter, $this->element, $this->withoutP);
$content = $writer->write(); $content = $writer->write();

View File

@ -68,7 +68,8 @@ class Element
public function write() public function write()
{ {
$content = ''; $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)) { if (class_exists($writerClass)) {
$writer = new $writerClass($this->parentWriter, $this->element, $this->withoutP); $writer = new $writerClass($this->parentWriter, $this->element, $this->withoutP);
$content = $writer->write(); $content = $writer->write();

View File

@ -32,15 +32,16 @@ class Container extends AbstractElement
public function write() public function write()
{ {
$xmlWriter = $this->getXmlWriter(); $xmlWriter = $this->getXmlWriter();
$element = $this->getElement(); $container = $this->getElement();
// Loop through subelements // Loop through subelements
$containerClass = basename(get_class($element)); $containerClass = basename(get_class($container));
$subelements = $element->getElements(); $subelements = $container->getElements();
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote', 'TextBox')) ? true : false; $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote', 'TextBox')) ? true : false;
if (count($subelements) > 0) { if (count($subelements) > 0) {
foreach ($subelements as $subelement) { 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)) { if (class_exists($writerClass)) {
$writer = new $writerClass($xmlWriter, $subelement, $withoutP); $writer = new $writerClass($xmlWriter, $subelement, $withoutP);
$writer->write(); $writer->write();
@ -49,7 +50,7 @@ class Container extends AbstractElement
} else { } else {
// Special case for Cell: They have to contain a TextBreak at least // Special case for Cell: They have to contain a TextBreak at least
if ($containerClass == 'Cell') { 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 = new $writerClass($xmlWriter, new TextBreakElement(), $withoutP);
$writer->write(); $writer->write();
} }

View File

@ -85,18 +85,20 @@ class Styles extends AbstractPart
$xmlWriter->startElement('w:name'); $xmlWriter->startElement('w:name');
$xmlWriter->writeAttribute('w:val', $styleName); $xmlWriter->writeAttribute('w:val', $styleName);
$xmlWriter->endElement(); $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 = new ParagraphStyleWriter($xmlWriter, $paragraphStyle);
$styleWriter->write(); $styleWriter->write();
} }
// w:rPr
$styleWriter = new FontStyleWriter($xmlWriter, $style); $styleWriter = new FontStyleWriter($xmlWriter, $style);
$styleWriter->write(); $styleWriter->write();
$xmlWriter->endElement(); $xmlWriter->endElement();
// Paragraph style // Paragraph style
@ -108,23 +110,19 @@ class Styles extends AbstractPart
$xmlWriter->startElement('w:name'); $xmlWriter->startElement('w:name');
$xmlWriter->writeAttribute('w:val', $styleName); $xmlWriter->writeAttribute('w:val', $styleName);
$xmlWriter->endElement(); $xmlWriter->endElement();
// Parent style // Parent style
$basedOn = $style->getBasedOn(); $basedOn = $style->getBasedOn();
if (!is_null($basedOn)) { $xmlWriter->writeElementIf(!is_null($basedOn), 'w:basedOn', 'w:val', $basedOn);
$xmlWriter->startElement('w:basedOn');
$xmlWriter->writeAttribute('w:val', $basedOn);
$xmlWriter->endElement();
}
// Next paragraph style // Next paragraph style
$next = $style->getNext(); $next = $style->getNext();
if (!is_null($next)) { $xmlWriter->writeElementIf(!is_null($next), 'w:next', 'w:val', $next);
$xmlWriter->startElement('w:next');
$xmlWriter->writeAttribute('w:val', $next);
$xmlWriter->endElement();
}
// w:pPr
$styleWriter = new ParagraphStyleWriter($xmlWriter, $style); $styleWriter = new ParagraphStyleWriter($xmlWriter, $style);
$styleWriter->write(); $styleWriter->write();
$xmlWriter->endElement(); $xmlWriter->endElement();
// Table style // Table style

View File

@ -58,8 +58,9 @@ class Cell extends AbstractStyle
} }
// Shading // Shading
if (!is_null($style->getShading())) { $shading = $style->getShading();
$styleWriter = new Shading($xmlWriter, $style->getShading()); if (!is_null($shading)) {
$styleWriter = new Shading($xmlWriter, $shading);
$styleWriter->write(); $styleWriter->write();
} }

View File

@ -61,16 +61,16 @@ class Font extends AbstractStyle
return; return;
} }
$xmlWriter = $this->getXmlWriter(); $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'); $xmlWriter->startElement('w:rPr');
// Style name
$styleName = $style->getStyleName();
$xmlWriter->writeElementIf(!is_null($styleName), 'w:rStyle', 'w:val', $styleName);
// Font name/family // Font name/family
$font = $style->getName();
$hint = $style->getHint();
if ($font != PhpWord::DEFAULT_FONT_NAME) { if ($font != PhpWord::DEFAULT_FONT_NAME) {
$xmlWriter->startElement('w:rFonts'); $xmlWriter->startElement('w:rFonts');
$xmlWriter->writeAttribute('w:ascii', $font); $xmlWriter->writeAttribute('w:ascii', $font);
@ -82,7 +82,11 @@ class Font extends AbstractStyle
} }
// Color // Color
$color = $style->getColor();
$xmlWriter->writeElementIf($color != PhpWord::DEFAULT_FONT_COLOR, 'w:color', 'w:val', $color); $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:sz', 'w:val', $size * 2);
$xmlWriter->writeElementIf($size != PhpWord::DEFAULT_FONT_SIZE, 'w:szCs', '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'); $xmlWriter->writeElementIf($style->isAllCaps(), 'w:caps');
// Underline // Underline
$underline = $style->getUnderline();
$xmlWriter->writeElementIf($underline != 'none', 'w:u', 'w:val', $underline); $xmlWriter->writeElementIf($underline != 'none', 'w:u', 'w:val', $underline);
// Foreground-Color // Foreground-Color
$fgColor = $style->getFgColor();
$xmlWriter->writeElementIf(!is_null($fgColor), 'w:highlight', 'w:val', $fgColor); $xmlWriter->writeElementIf(!is_null($fgColor), 'w:highlight', 'w:val', $fgColor);
// Superscript/subscript // Superscript/subscript
@ -110,8 +116,9 @@ class Font extends AbstractStyle
$xmlWriter->writeElementIf($style->isSubScript(), 'w:vertAlign', 'w:val', 'subscript'); $xmlWriter->writeElementIf($style->isSubScript(), 'w:vertAlign', 'w:val', 'subscript');
// Background-Color // Background-Color
if (!is_null($style->getShading())) { $shading = $style->getShading();
$styleWriter = new Shading($xmlWriter, $style->getShading()); if (!is_null($shading)) {
$styleWriter = new Shading($xmlWriter, $shading);
$styleWriter->write(); $styleWriter->write();
} }

View File

@ -38,12 +38,35 @@ class Image extends AbstractStyle
*/ */
public function write() 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; return;
} }
$xmlWriter = $this->getXmlWriter(); $xmlWriter = $this->getXmlWriter();
$wrapping = $style->getWrappingStyle(); $xmlWriter->startElement('w10:wrap');
$positioning = $style->getPositioning(); $xmlWriter->writeAttribute('type', $this->w10wrap);
$xmlWriter->endElement(); // w10:wrap
}
/**
* Write style attribute
*/
protected function writeStyle()
{
$xmlWriter = $this->getXmlWriter();
$style = $this->getStyle();
// Default style array // Default style array
$styleArray = array( $styleArray = array(
@ -52,9 +75,10 @@ class Image extends AbstractStyle
'mso-width-relative' => 'margin', 'mso-width-relative' => 'margin',
'mso-height-relative' => 'margin', 'mso-height-relative' => 'margin',
); );
$styleArray = array_merge($styleArray, $this->getElementStyle($style)); $styleArray = array_merge($styleArray, $this->getElementStyle());
// Absolute/relative positioning // Absolute/relative positioning
$positioning = $style->getPositioning();
$styleArray['position'] = $positioning; $styleArray['position'] = $positioning;
if ($positioning == ImageStyle::POSITION_ABSOLUTE) { if ($positioning == ImageStyle::POSITION_ABSOLUTE) {
$styleArray['mso-position-horizontal-relative'] = 'page'; $styleArray['mso-position-horizontal-relative'] = 'page';
@ -69,6 +93,7 @@ class Image extends AbstractStyle
} }
// Wrapping style // Wrapping style
$wrapping = $style->getWrappingStyle();
if ($wrapping == ImageStyle::WRAPPING_STYLE_INLINE) { if ($wrapping == ImageStyle::WRAPPING_STYLE_INLINE) {
// Nothing to do when inline // Nothing to do when inline
} elseif ($wrapping == ImageStyle::WRAPPING_STYLE_BEHIND) { } elseif ($wrapping == ImageStyle::WRAPPING_STYLE_BEHIND) {
@ -91,29 +116,14 @@ class Image extends AbstractStyle
$xmlWriter->writeAttribute('style', $imageStyle); $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 * Get element style
* *
* @return array * @return array
*/ */
protected function getElementStyle(ImageStyle $style) private function getElementStyle()
{ {
$style = $this->getStyle();
$styles = array(); $styles = array();
$styleValues = array( $styleValues = array(
'width' => $style->getWidth(), 'width' => $style->getWidth(),

View File

@ -33,14 +33,18 @@ class Indentation extends AbstractStyle
return; return;
} }
$xmlWriter = $this->getXmlWriter(); $xmlWriter = $this->getXmlWriter();
$firstLine = $style->getFirstLine();
$hanging = $style->getHanging();
$xmlWriter->startElement('w:ind'); $xmlWriter->startElement('w:ind');
$xmlWriter->writeAttribute('w:left', $this->convertTwip($style->getLeft())); $xmlWriter->writeAttribute('w:left', $this->convertTwip($style->getLeft()));
$xmlWriter->writeAttribute('w:right', $this->convertTwip($style->getRight())); $xmlWriter->writeAttribute('w:right', $this->convertTwip($style->getRight()));
$firstLine = $style->getFirstLine();
$xmlWriter->writeAttributeIf(!is_null($firstLine), 'w:firstLine', $this->convertTwip($firstLine)); $xmlWriter->writeAttributeIf(!is_null($firstLine), 'w:firstLine', $this->convertTwip($firstLine));
$hanging = $style->getHanging();
$xmlWriter->writeAttributeIf(!is_null($hanging), 'w:hanging', $this->convertTwip($hanging)); $xmlWriter->writeAttributeIf(!is_null($hanging), 'w:hanging', $this->convertTwip($hanging));
$xmlWriter->endElement(); $xmlWriter->endElement();
} }
} }

View File

@ -70,16 +70,17 @@ class Paragraph extends AbstractStyle
return; return;
} }
$xmlWriter = $this->getXmlWriter(); $xmlWriter = $this->getXmlWriter();
$align = $style->getAlign();
$indentation = $style->getIndentation();
$spacing = $style->getSpace();
$tabs = $style->getTabs();
if (!$this->withoutPPR) { if (!$this->withoutPPR) {
$xmlWriter->startElement('w:pPr'); $xmlWriter->startElement('w:pPr');
} }
// Style name
$styleName = $style->getStyleName();
$xmlWriter->writeElementIf(!is_null($styleName), 'w:pStyle', 'w:val', $styleName);
// Alignment // Alignment
$align = $style->getAlign();
$xmlWriter->writeElementIf(!is_null($align), 'w:jc', 'w:val', $align); $xmlWriter->writeElementIf(!is_null($align), 'w:jc', 'w:val', $align);
// Pagination // Pagination
@ -89,18 +90,21 @@ class Paragraph extends AbstractStyle
$xmlWriter->writeElementIf($style->hasPageBreakBefore(), 'w:pageBreakBefore', 'w:val', '1'); $xmlWriter->writeElementIf($style->hasPageBreakBefore(), 'w:pageBreakBefore', 'w:val', '1');
// Indentation // Indentation
$indentation = $style->getIndentation();
if (!is_null($indentation)) { if (!is_null($indentation)) {
$styleWriter = new Indentation($xmlWriter, $indentation); $styleWriter = new Indentation($xmlWriter, $indentation);
$styleWriter->write(); $styleWriter->write();
} }
// Spacing // Spacing
$spacing = $style->getSpace();
if (!is_null($spacing)) { if (!is_null($spacing)) {
$styleWriter = new Spacing($xmlWriter, $spacing); $styleWriter = new Spacing($xmlWriter, $spacing);
$styleWriter->write(); $styleWriter->write();
} }
// Tabs // Tabs
$tabs = $style->getTabs();
if (!empty($tabs)) { if (!empty($tabs)) {
$xmlWriter->startElement("w:tabs"); $xmlWriter->startElement("w:tabs");
foreach ($tabs as $tab) { foreach ($tabs as $tab) {

View File

@ -33,15 +33,18 @@ class Spacing extends AbstractStyle
return; return;
} }
$xmlWriter = $this->getXmlWriter(); $xmlWriter = $this->getXmlWriter();
$before = $style->getBefore();
$after = $style->getAfter();
$line = $style->getLine();
$xmlWriter->startElement('w:spacing'); $xmlWriter->startElement('w:spacing');
$before = $style->getBefore();
$xmlWriter->writeAttributeIf(!is_null($before), 'w:before', $this->convertTwip($before)); $xmlWriter->writeAttributeIf(!is_null($before), 'w:before', $this->convertTwip($before));
$after = $style->getAfter();
$xmlWriter->writeAttributeIf(!is_null($after), 'w:after', $this->convertTwip($after)); $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:line', $line);
$xmlWriter->writeAttributeIf(!is_null($line), 'w:lineRule', $style->getRule()); $xmlWriter->writeAttributeIf(!is_null($line), 'w:lineRule', $style->getRule());
$xmlWriter->endElement(); $xmlWriter->endElement();

View File

@ -40,9 +40,9 @@ class Table extends AbstractStyle
return; return;
} }
$xmlWriter = $this->getXmlWriter(); $xmlWriter = $this->getXmlWriter();
$hasBorders = $style->hasBorders(); $hasBorders = $style->hasBorders();
$hasMargins = $style->hasMargins(); $hasMargins = $style->hasMargins();
if ($hasMargins || $hasBorders) { if ($hasMargins || $hasBorders) {
$xmlWriter->startElement('w:tblPr'); $xmlWriter->startElement('w:tblPr');
if ($hasMargins) { if ($hasMargins) {
@ -64,6 +64,7 @@ class Table extends AbstractStyle
} }
$xmlWriter->endElement(); // w:tblPr $xmlWriter->endElement(); // w:tblPr
} }
// Only write background color and first row for full style // Only write background color and first row for full style
if ($this->isFullStyle) { if ($this->isFullStyle) {
// Background color // Background color

View File

@ -17,6 +17,7 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Style; namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style\TextBox as TextBoxStyle; use PhpOffice\PhpWord\Style\TextBox as TextBoxStyle;
/** /**
@ -31,71 +32,10 @@ class TextBox extends Image
*/ */
public function write() public function write()
{ {
if (is_null($style = $this->getStyle())) { if (!is_null($this->getStyle())) {
return; $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 <v:stroke dashstyle="dashDot" linestyle="thickBetweenThin"/>
} }
/** /**
@ -105,49 +45,62 @@ class TextBox extends Image
*/ */
public function writeW10Wrap() public function writeW10Wrap()
{ {
$xmlWriter = $this->getXmlWriter(); if (is_null($this->w10wrap)) {
return;
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
} }
$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 <v:stroke dashstyle="dashDot" linestyle="thickBetweenThin"/>
} }
} }

View File

@ -58,6 +58,18 @@ class AbstractStyleTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(false, self::callProtectedMethod($stub, 'setBoolVal', array('a', false))); $this->assertEquals(false, self::callProtectedMethod($stub, 'setBoolVal', array('a', false)));
$this->assertEquals(200, self::callProtectedMethod($stub, 'setIntVal', array('foo', 200))); $this->assertEquals(200, self::callProtectedMethod($stub, 'setIntVal', array('foo', 200)));
$this->assertEquals(2.1, self::callProtectedMethod($stub, 'setFloatVal', array('foo', 2.1))); $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'))); $this->assertEquals('b', self::callProtectedMethod($stub, 'setEnumVal', array('z', array('a', 'b'), 'b')));
} }

View File

@ -97,7 +97,7 @@ class FontTest extends \PHPUnit_Framework_TestCase
'fgColor' => Font::FGCOLOR_YELLOW, 'fgColor' => Font::FGCOLOR_YELLOW,
'bgColor' => 'FFFF00', 'bgColor' => 'FFFF00',
'hint' => 'eastAsia', 'hint' => 'eastAsia',
'line-height' => 2, 'lineHeight' => 2,
); );
$object->setStyleByArray($attributes); $object->setStyleByArray($attributes);
foreach ($attributes as $key => $value) { foreach ($attributes as $key => $value) {

View File

@ -249,12 +249,14 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
// Default // Default
$this->assertEquals(1, $oSettings->getColsNum()); $this->assertEquals(1, $oSettings->getColsNum());
// Null value
$oSettings->setColsNum();
$this->assertEquals(1, $oSettings->getColsNum());
// Random value
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$oSettings->setColsNum($iVal); $oSettings->setColsNum($iVal);
$this->assertEquals($iVal, $oSettings->getColsNum()); $this->assertEquals($iVal, $oSettings->getColsNum());
$oSettings->setColsNum();
$this->assertEquals(1, $oSettings->getColsNum());
} }
/** /**