Merge branch 'refactor' into develop

This commit is contained in:
Ivan Lanin 2014-05-08 12:17:14 +07:00
commit e12daacc0b
68 changed files with 1569 additions and 1590 deletions

View File

@ -9,7 +9,7 @@ tools:
enabled: true enabled: true
timeout: 900 timeout: 900
php_sim: php_sim:
min_mass: 25 min_mass: 30
php_pdepend: true php_pdepend: true
php_analyzer: true php_analyzer: true
sensiolabs_security_checker: true sensiolabs_security_checker: true

View File

@ -39,8 +39,7 @@ abstract class AbstractContainer extends AbstractElement
*/ */
protected function addElement(AbstractElement $element) protected function addElement(AbstractElement $element)
{ {
// $type = get_class($element); // $type = basename(get_class($element));
// $type = str_replace('PhpOffice\\PhpWord\\Element\\', '', $type);
$element->setElementIndex($this->countElements() + 1); $element->setElementIndex($this->countElements() + 1);
$element->setElementId(); $element->setElementId();
$element->setPhpWord($this->phpWord); $element->setPhpWord($this->phpWord);
@ -79,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 = 'PhpOffice\\PhpWord\\Element\\' . $elementName; $elementClass = dirname(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'))) {
@ -249,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 = 'PhpOffice\\PhpWord\\Element\\' . $elementName; $elementClass = dirname(get_class($this)) . '\\' . $elementName;
$docPart = strtolower($elementName); $docPart = strtolower($elementName);
$addMethod = "add{$elementName}"; $addMethod = "add{$elementName}";

View File

@ -223,8 +223,7 @@ class Section extends AbstractContainer
private function addHeaderFooter($type = Header::AUTO, $header = true) private function addHeaderFooter($type = Header::AUTO, $header = true)
{ {
$collectionArray = $header ? 'headers' : 'footers'; $collectionArray = $header ? 'headers' : 'footers';
$containerClass = 'PhpOffice\\PhpWord\\Element\\'; $containerClass = dirname(get_class($this)) . '\\' . ($header ? 'Header' : 'Footer');
$containerClass .= ($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

@ -75,7 +75,7 @@ class Text extends AbstractElement
$this->setParagraphStyle($paragraphStyle); $this->setParagraphStyle($paragraphStyle);
} elseif (is_array($style)) { } elseif (is_array($style)) {
$this->fontStyle = new Font('text', $paragraphStyle); $this->fontStyle = new Font('text', $paragraphStyle);
$this->fontStyle->setArrayStyle($style); $this->fontStyle->setStyleByArray($style);
} elseif (null === $style) { } elseif (null === $style) {
$this->fontStyle = new Font('text', $paragraphStyle); $this->fontStyle = new Font('text', $paragraphStyle);
} else { } else {
@ -106,7 +106,7 @@ class Text extends AbstractElement
{ {
if (is_array($style)) { if (is_array($style)) {
$this->paragraphStyle = new Paragraph; $this->paragraphStyle = new Paragraph;
$this->paragraphStyle->setArrayStyle($style); $this->paragraphStyle->setStyleByArray($style);
} elseif ($style instanceof Paragraph) { } elseif ($style instanceof Paragraph) {
$this->paragraphStyle = $style; $this->paragraphStyle = $style;
} elseif (null === $style) { } elseif (null === $style) {

View File

@ -69,7 +69,7 @@ class TextBreak extends AbstractElement
$this->setParagraphStyle($paragraphStyle); $this->setParagraphStyle($paragraphStyle);
} elseif (is_array($style)) { } elseif (is_array($style)) {
$this->fontStyle = new Font('text', $paragraphStyle); $this->fontStyle = new Font('text', $paragraphStyle);
$this->fontStyle->setArrayStyle($style); $this->fontStyle->setStyleByArray($style);
} else { } else {
$this->fontStyle = $style; $this->fontStyle = $style;
$this->setParagraphStyle($paragraphStyle); $this->setParagraphStyle($paragraphStyle);
@ -97,7 +97,7 @@ class TextBreak extends AbstractElement
{ {
if (is_array($style)) { if (is_array($style)) {
$this->paragraphStyle = new Paragraph; $this->paragraphStyle = new Paragraph;
$this->paragraphStyle->setArrayStyle($style); $this->paragraphStyle->setStyleByArray($style);
} elseif ($style instanceof Paragraph) { } elseif ($style instanceof Paragraph) {
$this->paragraphStyle = $style; $this->paragraphStyle = $style;
} else { } else {
@ -115,4 +115,14 @@ class TextBreak extends AbstractElement
{ {
return $this->paragraphStyle; return $this->paragraphStyle;
} }
/**
* Has font/paragraph style defined
*
* @return bool
*/
public function hasStyle()
{
return !is_null($this->fontStyle) || !is_null($this->paragraphStyle);
}
} }

View File

@ -149,4 +149,39 @@ class XMLWriter
return $this->text($text); return $this->text($text);
} }
/**
* Write element if ...
*
* @param bool $condition
* @param string $element
* @param string $attribute
* @param string $value
*/
public function writeElementIf($condition, $element, $attribute = null, $value = null)
{
if ($condition) {
if (is_null($attribute)) {
$this->xmlWriter->writeElement($element, $value);
} else {
$this->xmlWriter->startElement($element);
$this->xmlWriter->writeAttribute($attribute, $value);
$this->xmlWriter->endElement();
}
}
}
/**
* Write attribute if ...
*
* @param bool $condition
* @param string $attribute
* @param string $value
*/
public function writeAttributeIf($condition, $attribute, $value)
{
if ($condition) {
$this->xmlWriter->writeAttribute($attribute, $value);
}
}
} }

View File

@ -38,10 +38,17 @@ abstract class AbstractStyle
* *
* This number starts from one and defined in Style::setStyleValues() * This number starts from one and defined in Style::setStyleValues()
* *
* @var integer|null * @var int|null
*/ */
protected $index; protected $index;
/**
* Aliases
*
* @var array
*/
protected $aliases = array();
/** /**
* Get style name * Get style name
* *
@ -68,7 +75,7 @@ abstract class AbstractStyle
/** /**
* Get index number * Get index number
* *
* @return integer|null * @return int|null
*/ */
public function getIndex() public function getIndex()
{ {
@ -78,7 +85,7 @@ abstract class AbstractStyle
/** /**
* Set index number * Set index number
* *
* @param integer|null $value * @param int|null $value
* @return self * @return self
*/ */
public function setIndex($value = null) public function setIndex($value = null)
@ -102,6 +109,9 @@ abstract class AbstractStyle
*/ */
public function setStyleValue($key, $value) public function setStyleValue($key, $value)
{ {
if (isset($this->aliases[$key])) {
$key = $this->aliases[$key];
}
$method = 'set' . String::removeUnderscorePrefix($key); $method = 'set' . String::removeUnderscorePrefix($key);
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
$this->$method($value); $this->$method($value);
@ -150,6 +160,9 @@ 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;
} }
@ -161,8 +174,8 @@ abstract class AbstractStyle
* Set numeric value * Set numeric value
* *
* @param mixed $value * @param mixed $value
* @param integer|float|null $default * @param int|float|null $default
* @return integer|float|null * @return int|float|null
*/ */
protected function setNumericVal($value, $default = null) protected function setNumericVal($value, $default = null)
{ {
@ -177,11 +190,14 @@ abstract class AbstractStyle
* Set integer value * Set integer value
* *
* @param mixed $value * @param mixed $value
* @param integer|null $default * @param int|null $default
* @return integer|null * @return int|null
*/ */
protected function setIntVal($value, $default = null) protected function setIntVal($value, $default = null)
{ {
if (is_string($value)) {
$value = intval($value);
}
if (!is_int($value)) { if (!is_int($value)) {
$value = $default; $value = $default;
} }
@ -198,6 +214,9 @@ abstract class AbstractStyle
*/ */
protected function setFloatVal($value, $default = null) protected function setFloatVal($value, $default = null)
{ {
if (is_string($value)) {
$value = floatval($value);
}
if (!is_float($value)) { if (!is_float($value)) {
$value = $default; $value = $default;
} }
@ -220,4 +239,38 @@ abstract class AbstractStyle
return $value; return $value;
} }
/**
* Set object value
*
* @param mixed $value
* @param string $styleName
* @param mixed $style
*/
protected function setObjectVal($value, $styleName, &$style)
{
$styleClass = dirname(get_class($this)) . '\\' . $styleName;
if (is_array($value)) {
if (!$style instanceof $styleClass) {
$style = new $styleClass();
}
$style->setStyleByArray($value);
} else {
$style = $value;
}
return $style;
}
/**
* Set style using associative array
*
* @param array $style
* @deprecated 0.11.0
* @codeCoverageIgnore
*/
public function setArrayStyle(array $style = array())
{
return $this->setStyleByArray($style);
}
} }

View File

@ -323,4 +323,22 @@ class Border extends AbstractStyle
{ {
return $this->borderBottomColor; return $this->borderBottomColor;
} }
/**
* Has borders?
*
* @return bool
*/
public function hasBorders()
{
$hasBorders = false;
$borders = $this->getBorderSize();
for ($i = 0; $i < count($borders); $i++) {
if (!is_null($borders[$i])) {
$hasBorders = true;
}
}
return $hasBorders;
}
} }

View File

@ -42,7 +42,7 @@ class Cell extends Border
* *
* @var string * @var string
*/ */
private $valign; private $vAlign;
/** /**
* Text Direction * Text Direction
@ -80,7 +80,7 @@ class Cell extends Border
*/ */
public function getVAlign() public function getVAlign()
{ {
return $this->valign; return $this->vAlign;
} }
/** /**
@ -90,7 +90,7 @@ class Cell extends Border
*/ */
public function setVAlign($value = null) public function setVAlign($value = null)
{ {
$this->valign = $value; $this->vAlign = $value;
} }
/** /**
@ -183,19 +183,12 @@ class Cell extends Border
/** /**
* Set shading * Set shading
* *
* @param array $value * @param mixed $value
* @return self * @return self
*/ */
public function setShading($value = null) public function setShading($value = null)
{ {
if (is_array($value)) { $this->setObjectVal($value, 'Shading', $this->shading);
if (!$this->shading instanceof Shading) {
$this->shading = new Shading();
}
$this->shading->setStyleByArray($value);
} else {
$this->shading = null;
}
return $this; return $this;
} }

View File

@ -70,6 +70,13 @@ class Font extends AbstractStyle
const FGCOLOR_LIGHTGRAY = 'lightGray'; const FGCOLOR_LIGHTGRAY = 'lightGray';
const FGCOLOR_BLACK = 'black'; const FGCOLOR_BLACK = 'black';
/**
* Aliases
*
* @var array
*/
protected $aliases = array('line-height' => 'lineHeight');
/** /**
* Font style type * Font style type
* *
@ -77,13 +84,6 @@ class Font extends AbstractStyle
*/ */
private $type; private $type;
/**
* Paragraph style
*
* @var \PhpOffice\PhpWord\Style\Paragraph
*/
private $paragraphStyle;
/** /**
* Font name * Font name
* *
@ -91,6 +91,13 @@ class Font extends AbstractStyle
*/ */
private $name = PhpWord::DEFAULT_FONT_NAME; private $name = PhpWord::DEFAULT_FONT_NAME;
/**
* Font Content Type
*
* @var string
*/
private $hint = PhpWord::DEFAULT_FONT_CONTENT_TYPE;
/** /**
* Font size * Font size
* *
@ -98,6 +105,13 @@ class Font extends AbstractStyle
*/ */
private $size = PhpWord::DEFAULT_FONT_SIZE; private $size = PhpWord::DEFAULT_FONT_SIZE;
/**
* Font color
*
* @var string
*/
private $color = PhpWord::DEFAULT_FONT_COLOR;
/** /**
* Bold * Bold
* *
@ -112,6 +126,13 @@ class Font extends AbstractStyle
*/ */
private $italic = false; private $italic = false;
/**
* Undeline
*
* @var string
*/
private $underline = self::UNDERLINE_NONE;
/** /**
* Superscript * Superscript
* *
@ -126,13 +147,6 @@ class Font extends AbstractStyle
*/ */
private $subScript = false; private $subScript = false;
/**
* Undeline
*
* @var string
*/
private $underline = self::UNDERLINE_NONE;
/** /**
* Strikethrough * Strikethrough
* *
@ -147,40 +161,6 @@ class Font extends AbstractStyle
*/ */
private $doubleStrikethrough = false; private $doubleStrikethrough = false;
/**
* Font color
*
* @var string
*/
private $color = PhpWord::DEFAULT_FONT_COLOR;
/**
* Foreground/highlight
*
* @var string
*/
private $fgColor = null;
/**
* Text line height
*
* @var int
*/
/**
* Text line height
*
* @var int
*/
private $lineHeight = 1.0;
/**
* Font Content Type
*
* @var string
*/
private $hint = PhpWord::DEFAULT_FONT_CONTENT_TYPE;
/** /**
* Small caps * Small caps
* *
@ -197,6 +177,26 @@ class Font extends AbstractStyle
*/ */
private $allCaps = false; private $allCaps = false;
/**
* Foreground/highlight
*
* @var string
*/
private $fgColor;
/**
* Text line height
*
* @var int
*/
/**
* Paragraph style
*
* @var \PhpOffice\PhpWord\Style\Paragraph
*/
private $paragraph;
/** /**
* Shading * Shading
* *
@ -208,39 +208,12 @@ class Font extends AbstractStyle
* Create new font style * Create new font style
* *
* @param string $type Type of font * @param string $type Type of font
* @param array $paragraphStyle Paragraph styles definition * @param array $paragraph Paragraph styles definition
*/ */
public function __construct($type = 'text', $paragraphStyle = null) public function __construct($type = 'text', $paragraph = null)
{ {
$this->type = $type; $this->type = $type;
$this->setParagraph($paragraph);
if ($paragraphStyle instanceof Paragraph) {
$this->paragraphStyle = $paragraphStyle;
} elseif (is_array($paragraphStyle)) {
$this->paragraphStyle = new Paragraph;
$this->paragraphStyle->setArrayStyle($paragraphStyle);
} else {
$this->paragraphStyle = $paragraphStyle;
}
}
/**
* Set style using associative array
*
* @param array $style
* @return $this
*/
public function setArrayStyle(array $style = array())
{
foreach ($style as $key => $value) {
if ($key === 'line-height') {
$this->setLineHeight($value);
null;
}
$this->setStyleValue($key, $value);
}
return $this;
} }
/** /**
@ -266,6 +239,28 @@ class Font extends AbstractStyle
return $this; return $this;
} }
/**
* Get Font Content Type
*
* @return string
*/
public function getHint()
{
return $this->hint;
}
/**
* Set Font Content Type
*
* @param string $value
* @return self
*/
public function setHint($value = PhpWord::DEFAULT_FONT_CONTENT_TYPE)
{
$this->hint = $this->setNonEmptyVal($value, PhpWord::DEFAULT_FONT_CONTENT_TYPE);
return $this;
}
/** /**
* Get font size * Get font size
@ -290,6 +285,29 @@ class Font extends AbstractStyle
return $this; return $this;
} }
/**
* Get font color
*
* @return string
*/
public function getColor()
{
return $this->color;
}
/**
* Set font color
*
* @param string $value
* @return self
*/
public function setColor($value = PhpWord::DEFAULT_FONT_COLOR)
{
$this->color = $this->setNonEmptyVal($value, PhpWord::DEFAULT_FONT_COLOR);
return $this;
}
/** /**
* Get bold * Get bold
* *
@ -336,58 +354,6 @@ class Font extends AbstractStyle
return $this; return $this;
} }
/**
* Get superscript
*
* @return bool
*/
public function isSuperScript()
{
return $this->superScript;
}
/**
* Set superscript
*
* @param bool $value
* @return self
*/
public function setSuperScript($value = false)
{
$this->superScript = $this->setBoolVal($value, $this->superScript);
if ($this->superScript) {
$this->subScript = false;
}
return $this;
}
/**
* Get subscript
*
* @return bool
*/
public function isSubScript()
{
return $this->subScript;
}
/**
* Set subscript
*
* @param bool $value
* @return self
*/
public function setSubScript($value = false)
{
$this->subScript = $this->setBoolVal($value, $this->subScript);
if ($this->subScript) {
$this->superScript = false;
}
return $this;
}
/** /**
* Get underline * Get underline
* *
@ -411,6 +377,57 @@ class Font extends AbstractStyle
return $this; return $this;
} }
/**
* Get superscript
*
* @return bool
*/
public function isSuperScript()
{
return $this->superScript;
}
/**
* Set superscript
*
* @param bool $value
* @return self
*/
public function setSuperScript($value = false)
{
$this->superScript = $this->setBoolVal($value, $this->superScript);
$this->toggleFalse($this->subScript, $this->superScript);
return $this;
}
/**
* Get subscript
*
* @return bool
*/
public function isSubScript()
{
return $this->subScript;
}
/**
* Set subscript
*
* @param bool $value
* @return self
*/
public function setSubScript($value = false)
{
$this->subScript = $this->setBoolVal($value, $this->subScript);
$this->toggleFalse($this->subScript, $this->superScript);
if ($this->subScript) {
$this->superScript = false;
}
return $this;
}
/** /**
* Get strikethrough * Get strikethrough
* *
@ -430,9 +447,7 @@ class Font extends AbstractStyle
public function setStrikethrough($value = false) public function setStrikethrough($value = false)
{ {
$this->strikethrough = $this->setBoolVal($value, $this->strikethrough); $this->strikethrough = $this->setBoolVal($value, $this->strikethrough);
if ($this->strikethrough) { $this->toggleFalse($this->doubleStrikethrough, $this->strikethrough);
$this->doubleStrikethrough = false;
}
return $this; return $this;
} }
@ -456,32 +471,55 @@ class Font extends AbstractStyle
public function setDoubleStrikethrough($value = false) public function setDoubleStrikethrough($value = false)
{ {
$this->doubleStrikethrough = $this->setBoolVal($value, $this->doubleStrikethrough); $this->doubleStrikethrough = $this->setBoolVal($value, $this->doubleStrikethrough);
if ($this->doubleStrikethrough) { $this->toggleFalse($this->strikethrough, $this->doubleStrikethrough);
$this->strikethrough = false;
}
return $this; return $this;
} }
/** /**
* Get font color * Get small caps
* *
* @return string * @return bool
*/ */
public function getColor() public function isSmallCaps()
{ {
return $this->color; return $this->smallCaps;
} }
/** /**
* Set font color * Set small caps
* *
* @param string $value * @param bool $value
* @return self * @return self
*/ */
public function setColor($value = PhpWord::DEFAULT_FONT_COLOR) public function setSmallCaps($value = false)
{ {
$this->color = $this->setNonEmptyVal($value, PhpWord::DEFAULT_FONT_COLOR); $this->smallCaps = $this->setBoolVal($value, $this->smallCaps);
$this->toggleFalse($this->allCaps, $this->smallCaps);
return $this;
}
/**
* Get all caps
*
* @return bool
*/
public function isAllCaps()
{
return $this->allCaps;
}
/**
* Set all caps
*
* @param bool $value
* @return self
*/
public function setAllCaps($value = false)
{
$this->allCaps = $this->setBoolVal($value, $this->allCaps);
$this->toggleFalse($this->smallCaps, $this->allCaps);
return $this; return $this;
} }
@ -542,38 +580,6 @@ class Font extends AbstractStyle
return $this->type; return $this->type;
} }
/**
* Get paragraph style
*
* @return \PhpOffice\PhpWord\Style\Paragraph
*/
public function getParagraphStyle()
{
return $this->paragraphStyle;
}
/**
* Set lineheight
*
* @param int|float|string $lineHeight
* @return $this
* @throws \PhpOffice\PhpWord\Exception\InvalidStyleException
*/
public function setLineHeight($lineHeight)
{
if (is_string($lineHeight)) {
$lineHeight = floatval(preg_replace('/[^0-9\.\,]/', '', $lineHeight));
}
if ((!is_integer($lineHeight) && !is_float($lineHeight)) || !$lineHeight) {
throw new InvalidStyleException('Line height must be a valid number');
}
$this->lineHeight = $lineHeight;
$this->getParagraphStyle()->setLineHeight($lineHeight);
return $this;
}
/** /**
* Get line height * Get line height
* *
@ -581,80 +587,41 @@ class Font extends AbstractStyle
*/ */
public function getLineHeight() public function getLineHeight()
{ {
return $this->lineHeight; return $this->getParagraph()->getLineHeight();
} }
/** /**
* Get Font Content Type * Set lineheight
* *
* @return string * @param int|float|string $value
*/
public function getHint()
{
return $this->hint;
}
/**
* Set Font Content Type
*
* @param string $value
* @return self * @return self
*/ */
public function setHint($value = PhpWord::DEFAULT_FONT_CONTENT_TYPE) public function setLineHeight($value)
{ {
$this->hint = $this->setNonEmptyVal($value, PhpWord::DEFAULT_FONT_CONTENT_TYPE); $this->setParagraph(array('lineHeight' => $value));
return $this; return $this;
} }
/** /**
* Get small caps * Get paragraph style
* *
* @return bool * @return \PhpOffice\PhpWord\Style\Paragraph
*/ */
public function isSmallCaps() public function getParagraph()
{ {
return $this->smallCaps; return $this->paragraph;
} }
/** /**
* Set small caps * Set shading
* *
* @param bool $value * @param mixed $value
* @return self * @return self
*/ */
public function setSmallCaps($value = false) public function setParagraph($value = null)
{ {
$this->smallCaps = $this->setBoolVal($value, $this->smallCaps); $this->setObjectVal($value, 'Paragraph', $this->paragraph);
if ($this->smallCaps) {
$this->allCaps = false;
}
return $this;
}
/**
* Get all caps
*
* @return bool
*/
public function isAllCaps()
{
return $this->allCaps;
}
/**
* Set all caps
*
* @param bool $value
* @return self
*/
public function setAllCaps($value = false)
{
$this->allCaps = $this->setBoolVal($value, $this->allCaps);
if ($this->allCaps) {
$this->smallCaps = false;
}
return $this; return $this;
} }
@ -672,23 +639,29 @@ class Font extends AbstractStyle
/** /**
* Set shading * Set shading
* *
* @param array $value * @param mixed $value
* @return self * @return self
*/ */
public function setShading($value = null) public function setShading($value = null)
{ {
if (is_array($value)) { $this->setObjectVal($value, 'Shading', $this->shading);
if (!$this->shading instanceof Shading) {
$this->shading = new Shading();
}
$this->shading->setStyleByArray($value);
} else {
$this->shading = null;
}
return $this; return $this;
} }
/**
* Toggle $target value to false when $source true
*
* @param \PhpOffice\PhpWord\Style\AbstractStyle $target
* @param bool $sourceValue
*/
private function toggleFalse(&$target, $sourceValue)
{
if ($sourceValue == true) {
$target = false;
}
}
/** /**
* Get bold * Get bold
* *
@ -743,4 +716,15 @@ class Font extends AbstractStyle
{ {
return $this->isStrikethrough(); return $this->isStrikethrough();
} }
/**
* Get paragraph style
*
* @deprecated 0.11.0
* @codeCoverageIgnore
*/
public function getParagraphStyle()
{
return $this->getParagraph();
}
} }

View File

@ -27,6 +27,13 @@ class Paragraph extends AbstractStyle
{ {
const LINE_HEIGHT = 240; const LINE_HEIGHT = 240;
/**
* Aliases
*
* @var array
*/
protected $aliases = array('line-height' => 'lineHeight');
/** /**
* Paragraph alignment * Paragraph alignment
* *
@ -104,24 +111,6 @@ class Paragraph extends AbstractStyle
*/ */
private $spacing; private $spacing;
/**
* Set style by array
*
* @param array $style
* @return $this
*/
public function setArrayStyle(array $style = array())
{
foreach ($style as $key => $value) {
if ($key === 'line-height') {
null;
}
$this->setStyleValue($key, $value);
}
return $this;
}
/** /**
* Set Style value * Set Style value
* *
@ -135,9 +124,6 @@ class Paragraph extends AbstractStyle
$value = $value * 720; $value = $value * 720;
} 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
} elseif ($key === 'line-height') {
$this->setLineHeight($value);
return;
} }
$method = 'set' . $key; $method = 'set' . $key;
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
@ -168,6 +154,7 @@ class Paragraph extends AbstractStyle
$value = 'both'; $value = 'both';
} }
$this->align = $value; $this->align = $value;
return $this; return $this;
} }
@ -362,6 +349,7 @@ class Paragraph extends AbstractStyle
public function setBasedOn($value = 'Normal') public function setBasedOn($value = 'Normal')
{ {
$this->basedOn = $value; $this->basedOn = $value;
return $this; return $this;
} }
@ -384,6 +372,7 @@ class Paragraph extends AbstractStyle
public function setNext($value = null) public function setNext($value = null)
{ {
$this->next = $value; $this->next = $value;
return $this; return $this;
} }
@ -405,10 +394,8 @@ class Paragraph extends AbstractStyle
*/ */
public function setWidowControl($value = true) public function setWidowControl($value = true)
{ {
if (!is_bool($value)) { $this->widowControl = $this->setBoolVal($value, $this->widowControl);
$value = true;
}
$this->widowControl = $value;
return $this; return $this;
} }
@ -430,10 +417,8 @@ class Paragraph extends AbstractStyle
*/ */
public function setKeepNext($value = false) public function setKeepNext($value = false)
{ {
if (!is_bool($value)) { $this->keepNext = $this->setBoolVal($value, $this->keepNext);
$value = false;
}
$this->keepNext = $value;
return $this; return $this;
} }
@ -455,10 +440,8 @@ class Paragraph extends AbstractStyle
*/ */
public function setKeepLines($value = false) public function setKeepLines($value = false)
{ {
if (!is_bool($value)) { $this->keepLines = $this->setBoolVal($value, $this->keepLines);
$value = false;
}
$this->keepLines = $value;
return $this; return $this;
} }
@ -480,10 +463,8 @@ class Paragraph extends AbstractStyle
*/ */
public function setPageBreakBefore($value = false) public function setPageBreakBefore($value = false)
{ {
if (!is_bool($value)) { $this->pageBreakBefore = $this->setBoolVal($value, $this->pageBreakBefore);
$value = false;
}
$this->pageBreakBefore = $value;
return $this; return $this;
} }
@ -500,19 +481,12 @@ class Paragraph extends AbstractStyle
/** /**
* Set shading * Set shading
* *
* @param array $value * @param mixed $value
* @return self * @return self
*/ */
public function setIndentation($value = null) public function setIndentation($value = null)
{ {
if (is_array($value)) { $this->setObjectVal($value, 'Indentation', $this->indentation);
if (!$this->indentation instanceof Indentation) {
$this->indentation = new Indentation();
}
$this->indentation->setStyleByArray($value);
} else {
$this->indentation = null;
}
return $this; return $this;
} }
@ -531,20 +505,13 @@ class Paragraph extends AbstractStyle
/** /**
* Set shading * Set shading
* *
* @param array $value * @param mixed $value
* @return self * @return self
* @todo Rename to setSpacing in 1.0 * @todo Rename to setSpacing in 1.0
*/ */
public function setSpace($value = null) public function setSpace($value = null)
{ {
if (is_array($value)) { $this->setObjectVal($value, 'Spacing', $this->spacing);
if (!$this->spacing instanceof Spacing) {
$this->spacing = new Spacing();
}
$this->spacing->setStyleByArray($value);
} else {
$this->spacing = null;
}
return $this; return $this;
} }

View File

@ -509,19 +509,12 @@ class Section extends Border
/** /**
* Set line numbering * Set line numbering
* *
* @param array $value * @param mixed $value
* @return self * @return self
*/ */
public function setLineNumbering($value = null) public function setLineNumbering($value = null)
{ {
if (is_array($value)) { $this->setObjectVal($value, 'LineNumbering', $this->lineNumbering);
if (!$this->lineNumbering instanceof LineNumbering) {
$this->lineNumbering = new LineNumbering($value);
}
$this->lineNumbering->setStyleByArray($value);
} else {
$this->lineNumbering = null;
}
return $this; return $this;
} }

View File

@ -425,41 +425,16 @@ class Table extends Border
/** /**
* Set shading * Set shading
* *
* @param array $value * @param mixed $value
* @return self * @return self
*/ */
public function setShading($value = null) public function setShading($value = null)
{ {
if (is_array($value)) { $this->setObjectVal($value, 'Shading', $this->shading);
if (!$this->shading instanceof Shading) {
$this->shading = new Shading();
}
$this->shading->setStyleByArray($value);
} else {
$this->shading = null;
}
return $this; return $this;
} }
/**
* Has borders?
*
* @return bool
*/
public function hasBorders()
{
$hasBorders = false;
$borders = $this->getBorderSize();
for ($i = 0; $i < 6; $i++) {
if (!is_null($borders[$i])) {
$hasBorders = true;
}
}
return $hasBorders;
}
/** /**
* Has margins? * Has margins?
* *
@ -469,7 +444,7 @@ class Table extends Border
{ {
$hasMargins = false; $hasMargins = false;
$margins = $this->getCellMargin(); $margins = $this->getCellMargin();
for ($i = 0; $i < 4; $i++) { for ($i = 0; $i < count($margins); $i++) {
if (!is_null($margins[$i])) { if (!is_null($margins[$i])) {
$hasMargins = true; $hasMargins = true;
} }

View File

@ -67,7 +67,10 @@ class HTML extends AbstractWriter implements WriterInterface
*/ */
public function save($filename = null) public function save($filename = null)
{ {
if (!is_null($this->getPhpWord())) { if (is_null($this->phpWord)) {
throw new Exception('PhpWord object unassigned.');
}
$this->setTempDir(sys_get_temp_dir() . '/PHPWordWriter/'); $this->setTempDir(sys_get_temp_dir() . '/PHPWordWriter/');
$hFile = fopen($filename, 'w'); $hFile = fopen($filename, 'w');
if ($hFile !== false) { if ($hFile !== false) {
@ -77,9 +80,6 @@ class HTML extends AbstractWriter implements WriterInterface
throw new Exception("Can't open file"); throw new Exception("Can't open file");
} }
$this->clearTempDir(); $this->clearTempDir();
} else {
throw new Exception("No PHPWord assigned.");
}
} }
/** /**

View File

@ -72,14 +72,13 @@ class Element
*/ */
public function write() public function write()
{ {
$html = ''; $content = '';
$elmName = str_replace('PhpOffice\\PhpWord\\Element\\', '', get_class($this->element)); $writerClass = dirname(get_class($this)) . '\\' . basename(get_class($this->element));
$elmWriterClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Element\\' . $elmName; if (class_exists($writerClass)) {
if (class_exists($elmWriterClass) === true) { $writer = new $writerClass($this->parentWriter, $this->element, $this->withoutP);
$elmWriter = new $elmWriterClass($this->parentWriter, $this->element, $this->withoutP); $content = $writer->write();
$html = $elmWriter->write();
} }
return $html; return $content;
} }
} }

View File

@ -47,7 +47,7 @@ class ODText extends AbstractWriter implements WriterInterface
'Manifest' => 'META-INF/manifest.xml', 'Manifest' => 'META-INF/manifest.xml',
); );
foreach (array_keys($this->parts) as $partName) { foreach (array_keys($this->parts) as $partName) {
$partClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Part\\' . $partName; $partClass = get_class($this) . '\\Part\\' . $partName;
if (class_exists($partClass)) { if (class_exists($partClass)) {
$partObject = new $partClass(); $partObject = new $partClass();
$partObject->setParentWriter($this); $partObject->setParentWriter($this);
@ -67,7 +67,10 @@ class ODText extends AbstractWriter implements WriterInterface
*/ */
public function save($filename = null) public function save($filename = null)
{ {
if (!is_null($this->phpWord)) { if (is_null($this->phpWord)) {
throw new Exception('PhpWord object unassigned.');
}
$filename = $this->getTempFile($filename); $filename = $this->getTempFile($filename);
$objZip = $this->getZipArchive($filename); $objZip = $this->getZipArchive($filename);
@ -90,8 +93,5 @@ class ODText extends AbstractWriter implements WriterInterface
} }
$this->cleanupTempFile(); $this->cleanupTempFile();
} else {
throw new Exception("PhpWord object unassigned.");
}
} }
} }

View File

@ -0,0 +1,27 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\ODText\Element;
/**
* Abstract element writer
*
* @since 0.11.0
*/
abstract class AbstractElement extends \PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement
{
}

View File

@ -0,0 +1,27 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\ODText\Element;
/**
* Container element writer (section, textrun, header, footnote, cell, etc.)
*
* @since 0.11.0
*/
class Container extends \PhpOffice\PhpWord\Writer\Word2007\Element\Container
{
}

View File

@ -1,87 +0,0 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\ODText\Element;
use PhpOffice\PhpWord\Element\AbstractElement;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Writer\ODText\Part\AbstractPart;
/**
* Generic element writer
*
* @since 0.10.0
*/
class Element
{
/**
* XML writer
*
* @var \PhpOffice\PhpWord\Shared\XMLWriter
*/
protected $xmlWriter;
/**
* Parent writer
*
* @var \PhpOffice\PhpWord\Writer\ODText\AbstractPart
*/
protected $parentWriter;
/**
* Element
*
* @var \PhpOffice\PhpWord\Element\AbstractElement
*/
protected $element;
/**
* Without paragraph
*
* @var bool
*/
protected $withoutP = false;
/**
* Create new instance
*
* @param \PhpOffice\PhpWord\Element\AbstractElement $element
* @param bool $withoutP
*/
public function __construct(XMLWriter $xmlWriter, AbstractPart $parentWriter, $element, $withoutP = false)
{
$this->xmlWriter = $xmlWriter;
$this->parentWriter = $parentWriter;
$this->element = $element;
$this->withoutP = $withoutP;
}
/**
* Write element
*
* @return string
*/
public function write()
{
$elmName = str_replace('PhpOffice\\PhpWord\\Element\\', '', get_class($this->element));
$elmWriterClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Element\\' . $elmName;
if (class_exists($elmWriterClass) === true) {
$elmWriter = new $elmWriterClass($this->xmlWriter, $this->parentWriter, $this->element, $this->withoutP);
$elmWriter->write();
}
}
}

View File

@ -24,43 +24,42 @@ use PhpOffice\PhpWord\Shared\Drawing;
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class Image extends Element class Image extends AbstractElement
{ {
/** /**
* Write element * Write element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Image) { $xmlWriter = $this->getXmlWriter();
return; $element = $this->getElement();
}
$mediaIndex = $this->element->getMediaIndex(); $mediaIndex = $element->getMediaIndex();
$target = 'Pictures/' . $this->element->getTarget(); $target = 'Pictures/' . $element->getTarget();
$style = $this->element->getStyle(); $style = $element->getStyle();
$width = Drawing::pixelsToCentimeters($style->getWidth()); $width = Drawing::pixelsToCentimeters($style->getWidth());
$height = Drawing::pixelsToCentimeters($style->getHeight()); $height = Drawing::pixelsToCentimeters($style->getHeight());
$this->xmlWriter->startElement('text:p'); $xmlWriter->startElement('text:p');
$this->xmlWriter->writeAttribute('text:style-name', 'Standard'); $xmlWriter->writeAttribute('text:style-name', 'Standard');
$this->xmlWriter->startElement('draw:frame'); $xmlWriter->startElement('draw:frame');
$this->xmlWriter->writeAttribute('draw:style-name', 'fr' . $mediaIndex); $xmlWriter->writeAttribute('draw:style-name', 'fr' . $mediaIndex);
$this->xmlWriter->writeAttribute('draw:name', $this->element->getElementId()); $xmlWriter->writeAttribute('draw:name', $element->getElementId());
$this->xmlWriter->writeAttribute('text:anchor-type', 'as-char'); $xmlWriter->writeAttribute('text:anchor-type', 'as-char');
$this->xmlWriter->writeAttribute('svg:width', $width . 'cm'); $xmlWriter->writeAttribute('svg:width', $width . 'cm');
$this->xmlWriter->writeAttribute('svg:height', $height . 'cm'); $xmlWriter->writeAttribute('svg:height', $height . 'cm');
$this->xmlWriter->writeAttribute('draw:z-index', $mediaIndex); $xmlWriter->writeAttribute('draw:z-index', $mediaIndex);
$this->xmlWriter->startElement('draw:image'); $xmlWriter->startElement('draw:image');
$this->xmlWriter->writeAttribute('xlink:href', $target); $xmlWriter->writeAttribute('xlink:href', $target);
$this->xmlWriter->writeAttribute('xlink:type', 'simple'); $xmlWriter->writeAttribute('xlink:type', 'simple');
$this->xmlWriter->writeAttribute('xlink:show', 'embed'); $xmlWriter->writeAttribute('xlink:show', 'embed');
$this->xmlWriter->writeAttribute('xlink:actuate', 'onLoad'); $xmlWriter->writeAttribute('xlink:actuate', 'onLoad');
$this->xmlWriter->endElement(); // draw:image $xmlWriter->endElement(); // draw:image
$this->xmlWriter->endElement(); // draw:frame $xmlWriter->endElement(); // draw:frame
$this->xmlWriter->endElement(); // text:p $xmlWriter->endElement(); // text:p
} }
} }

View File

@ -22,29 +22,28 @@ namespace PhpOffice\PhpWord\Writer\ODText\Element;
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class Link extends Element class Link extends AbstractElement
{ {
/** /**
* Write element * Write element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Link) { $xmlWriter = $this->getXmlWriter();
return; $element = $this->getElement();
}
if (!$this->withoutP) { if (!$this->withoutP) {
$this->xmlWriter->startElement('text:p'); // text:p $xmlWriter->startElement('text:p'); // text:p
} }
$this->xmlWriter->startElement('text:a'); $xmlWriter->startElement('text:a');
$this->xmlWriter->writeAttribute('xlink:type', 'simple'); $xmlWriter->writeAttribute('xlink:type', 'simple');
$this->xmlWriter->writeAttribute('xlink:href', $this->element->getTarget()); $xmlWriter->writeAttribute('xlink:href', $element->getTarget());
$this->xmlWriter->writeRaw($this->element->getText()); $xmlWriter->writeRaw($element->getText());
$this->xmlWriter->endElement(); // text:a $xmlWriter->endElement(); // text:a
if (!$this->withoutP) { if (!$this->withoutP) {
$this->xmlWriter->endElement(); // text:p $xmlWriter->endElement(); // text:p
} }
} }
} }

View File

@ -25,50 +25,42 @@ use PhpOffice\PhpWord\Writer\ODText\Element\Element as ElementWriter;
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class Table extends Element class Table extends AbstractElement
{ {
/** /**
* Write element * Write element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Table) { $xmlWriter = $this->getXmlWriter();
return; $element = $this->getElement();
} $rows = $element->getRows();
$rows = $this->element->getRows();
$rowCount = count($rows); $rowCount = count($rows);
$colCount = $this->element->countColumns(); $colCount = $element->countColumns();
if ($rowCount > 0) {
$this->xmlWriter->startElement('table:table');
$this->xmlWriter->writeAttribute('table:name', $this->element->getElementId());
$this->xmlWriter->writeAttribute('table:style', $this->element->getElementId());
$this->xmlWriter->startElement('table:table-column'); if ($rowCount > 0) {
$this->xmlWriter->writeAttribute('table:number-columns-repeated', $colCount); $xmlWriter->startElement('table:table');
$this->xmlWriter->endElement(); // table:table-column $xmlWriter->writeAttribute('table:name', $element->getElementId());
$xmlWriter->writeAttribute('table:style', $element->getElementId());
$xmlWriter->startElement('table:table-column');
$xmlWriter->writeAttribute('table:number-columns-repeated', $colCount);
$xmlWriter->endElement(); // table:table-column
foreach ($rows as $row) { foreach ($rows as $row) {
$this->xmlWriter->startElement('table:table-row'); $xmlWriter->startElement('table:table-row');
foreach ($row->getCells() as $cell) { foreach ($row->getCells() as $cell) {
$this->xmlWriter->startElement('table:table-cell'); $xmlWriter->startElement('table:table-cell');
$this->xmlWriter->writeAttribute('office:value-type', 'string'); $xmlWriter->writeAttribute('office:value-type', 'string');
$elements = $cell->getElements();
if (count($elements) > 0) { $containerWriter = new Container($xmlWriter, $cell);
foreach ($elements as $element) { $containerWriter->write();
$elementWriter = new ElementWriter($this->xmlWriter, $this->parentWriter, $element);
$elementWriter->write(); $xmlWriter->endElement(); // table:table-cell
} }
} else { $xmlWriter->endElement(); // table:table-row
$element = new TextBreakElement();
$elementWriter = new ElementWriter($this->xmlWriter, $this->parentWriter, $element);
$elementWriter->write();
} }
$this->xmlWriter->endElement(); // table:table-cell $xmlWriter->endElement(); // table:table
}
$this->xmlWriter->endElement(); // table:table-row
}
$this->xmlWriter->endElement(); // table:table
} }
} }
} }

View File

@ -22,19 +22,17 @@ namespace PhpOffice\PhpWord\Writer\ODText\Element;
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class Text extends Element class Text extends AbstractElement
{ {
/** /**
* Write element * Write element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) { $xmlWriter = $this->getXmlWriter();
return; $element = $this->getElement();
} $fontStyle = $element->getFontStyle();
$paragraphStyle = $element->getParagraphStyle();
$fontStyle = $this->element->getFontStyle();
$paragraphStyle = $this->element->getParagraphStyle();
// @todo Commented for TextRun. Should really checkout this value // @todo Commented for TextRun. Should really checkout this value
// $fStyleIsObject = ($fontStyle instanceof Font) ? true : false; // $fStyleIsObject = ($fontStyle instanceof Font) ? true : false;
@ -45,31 +43,31 @@ class Text extends Element
throw new Exception('PhpWord : $fStyleIsObject wouldn\'t be an object'); throw new Exception('PhpWord : $fStyleIsObject wouldn\'t be an object');
} else { } else {
if (!$this->withoutP) { if (!$this->withoutP) {
$this->xmlWriter->startElement('text:p'); // text:p $xmlWriter->startElement('text:p'); // text:p
} }
if (empty($fontStyle)) { if (empty($fontStyle)) {
if (empty($paragraphStyle)) { if (empty($paragraphStyle)) {
$this->xmlWriter->writeAttribute('text:style-name', 'P1'); $xmlWriter->writeAttribute('text:style-name', 'P1');
} elseif (is_string($paragraphStyle)) { } elseif (is_string($paragraphStyle)) {
$this->xmlWriter->writeAttribute('text:style-name', $paragraphStyle); $xmlWriter->writeAttribute('text:style-name', $paragraphStyle);
} }
$this->xmlWriter->writeRaw($this->element->getText()); $xmlWriter->writeRaw($element->getText());
} else { } else {
if (empty($paragraphStyle)) { if (empty($paragraphStyle)) {
$this->xmlWriter->writeAttribute('text:style-name', 'Standard'); $xmlWriter->writeAttribute('text:style-name', 'Standard');
} elseif (is_string($paragraphStyle)) { } elseif (is_string($paragraphStyle)) {
$this->xmlWriter->writeAttribute('text:style-name', $paragraphStyle); $xmlWriter->writeAttribute('text:style-name', $paragraphStyle);
} }
// text:span // text:span
$this->xmlWriter->startElement('text:span'); $xmlWriter->startElement('text:span');
if (is_string($fontStyle)) { if (is_string($fontStyle)) {
$this->xmlWriter->writeAttribute('text:style-name', $fontStyle); $xmlWriter->writeAttribute('text:style-name', $fontStyle);
} }
$this->xmlWriter->writeRaw($this->element->getText()); $xmlWriter->writeRaw($element->getText());
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
if (!$this->withoutP) { if (!$this->withoutP) {
$this->xmlWriter->endElement(); // text:p $xmlWriter->endElement(); // text:p
} }
} }
} }

View File

@ -22,15 +22,17 @@ namespace PhpOffice\PhpWord\Writer\ODText\Element;
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class TextBreak extends Element class TextBreak extends AbstractElement
{ {
/** /**
* Write element * Write element
*/ */
public function write() public function write()
{ {
$this->xmlWriter->startElement('text:p'); $xmlWriter = $this->getXmlWriter();
$this->xmlWriter->writeAttribute('text:style-name', 'Standard');
$this->xmlWriter->endElement(); $xmlWriter->startElement('text:p');
$xmlWriter->writeAttribute('text:style-name', 'Standard');
$xmlWriter->endElement();
} }
} }

View File

@ -17,38 +17,26 @@
namespace PhpOffice\PhpWord\Writer\ODText\Element; namespace PhpOffice\PhpWord\Writer\ODText\Element;
use PhpOffice\PhpWord\Element\Link as LinkElement;
use PhpOffice\PhpWord\Element\Text as TextElement;
/** /**
* TextRun element writer * TextRun element writer
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class TextRun extends Element class TextRun extends AbstractElement
{ {
/** /**
* Write element * Write element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\TextRun) { $xmlWriter = $this->getXmlWriter();
return; $element = $this->getElement();
}
$elements = $this->element->getElements(); $xmlWriter->startElement('text:p');
$this->xmlWriter->startElement('text:p');
if (count($elements) > 0) { $containerWriter = new Container($xmlWriter, $element);
foreach ($elements as $element) { $containerWriter->write();
if ($element instanceof TextElement) {
$elementWriter = new Text($this->xmlWriter, $this->parentWriter, $element, true); $xmlWriter->endElement();
$elementWriter->write();
} elseif ($element instanceof LinkElement) {
$elementWriter = new Link($this->xmlWriter, $this->parentWriter, $element, true);
$elementWriter->write();
}
}
}
$this->xmlWriter->endElement();
} }
} }

View File

@ -25,7 +25,7 @@ use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Writer\ODText\Element\Element as ElementWriter; use PhpOffice\PhpWord\Writer\ODText\Element\Container;
/** /**
* ODText content part writer: content.xml * ODText content part writer: content.xml
@ -73,12 +73,9 @@ class Content extends AbstractPart
$sectionCount = count($sections); $sectionCount = count($sections);
if ($sectionCount > 0) { if ($sectionCount > 0) {
foreach ($sections as $section) { foreach ($sections as $section) {
$elements = $section->getElements();
// $xmlWriter->startElement('text:section'); // $xmlWriter->startElement('text:section');
foreach ($elements as $element) { $containerWriter = new Container($xmlWriter, $section);
$elementWriter = new ElementWriter($xmlWriter, $this, $element, false); $containerWriter->write();
$elementWriter->write();
}
// $xmlWriter->endElement(); // text:section // $xmlWriter->endElement(); // text:section
} }
} }
@ -104,7 +101,7 @@ class Content extends AbstractPart
if (preg_match('#^T[0-9]+$#', $styleName) != 0 if (preg_match('#^T[0-9]+$#', $styleName) != 0
|| preg_match('#^P[0-9]+$#', $styleName) != 0 || preg_match('#^P[0-9]+$#', $styleName) != 0
) { ) {
$styleClass = str_replace('Style', 'Writer\\ODText\\Style', get_class($style)); $styleClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Style\\' . basename(get_class($style));
if (class_exists($styleClass)) { if (class_exists($styleClass)) {
$styleWriter = new $styleClass($xmlWriter, $style); $styleWriter = new $styleClass($xmlWriter, $style);
$styleWriter->setIsAuto(true); $styleWriter->setIsAuto(true);

View File

@ -91,7 +91,7 @@ class Styles extends AbstractPart
if (preg_match('#^T[0-9]+$#', $styleName) == 0 if (preg_match('#^T[0-9]+$#', $styleName) == 0
&& preg_match('#^P[0-9]+$#', $styleName) == 0 && preg_match('#^P[0-9]+$#', $styleName) == 0
) { ) {
$styleClass = str_replace('Style', 'Writer\\ODText\\Style', get_class($style)); $styleClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Style\\' . basename(get_class($style));
if (class_exists($styleClass)) { if (class_exists($styleClass)) {
$styleWriter = new $styleClass($xmlWriter, $style); $styleWriter = new $styleClass($xmlWriter, $style);
$styleWriter->write(); $styleWriter->write();

View File

@ -34,37 +34,40 @@ class Font extends AbstractStyle
*/ */
public function write() public function write()
{ {
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Font)) { if (is_null($style = $this->getStyle())) {
return; return;
} }
$xmlWriter = $this->getXmlWriter();
$this->xmlWriter->startElement('style:style'); $xmlWriter->startElement('style:style');
$this->xmlWriter->writeAttribute('style:name', $this->style->getStyleName()); $xmlWriter->writeAttribute('style:name', $style->getStyleName());
$this->xmlWriter->writeAttribute('style:family', 'text'); $xmlWriter->writeAttribute('style:family', 'text');
$this->xmlWriter->startElement('style:text-properties'); $xmlWriter->startElement('style:text-properties');
if ($this->style->getName()) {
$this->xmlWriter->writeAttribute('style:font-name', $this->style->getName()); // Name
$this->xmlWriter->writeAttribute('style:font-name-complex', $this->style->getName()); $font = $style->getName();
} $xmlWriter->writeAttributeIf($font, 'style:font-name', $font);
if ($this->style->getSize()) { $xmlWriter->writeAttributeIf($font, 'style:font-name-complex', $font);
$this->xmlWriter->writeAttribute('fo:font-size', ($this->style->getSize()) . 'pt'); $size = $style->getSize();
$this->xmlWriter->writeAttribute('style:font-size-asian', ($this->style->getSize()) . 'pt');
$this->xmlWriter->writeAttribute('style:font-size-complex', ($this->style->getSize()) . 'pt'); // Size
} $xmlWriter->writeAttributeIf($size, 'fo:font-size', $size . 'pt');
if ($this->style->getColor()) { $xmlWriter->writeAttributeIf($size, 'style:font-size-asian', $size . 'pt');
$this->xmlWriter->writeAttribute('fo:color', '#' . $this->style->getColor()); $xmlWriter->writeAttributeIf($size, 'style:font-size-complex', $size . 'pt');
}
if ($this->style->isItalic()) { // Color
$this->xmlWriter->writeAttribute('fo:font-style', 'italic'); $color = $style->getColor();
$this->xmlWriter->writeAttribute('style:font-style-asian', 'italic'); $xmlWriter->writeAttributeIf($color, 'fo:color', '#' . $color);
$this->xmlWriter->writeAttribute('style:font-style-complex', 'italic');
} // Bold & italic
if ($this->style->isBold()) { $xmlWriter->writeAttributeIf($style->isBold(), 'fo:font-weight', 'bold');
$this->xmlWriter->writeAttribute('fo:font-weight', 'bold'); $xmlWriter->writeAttributeIf($style->isBold(), 'style:font-weight-asian', 'bold');
$this->xmlWriter->writeAttribute('style:font-weight-asian', 'bold'); $xmlWriter->writeAttributeIf($style->isItalic(), 'fo:font-style', 'italic');
} $xmlWriter->writeAttributeIf($style->isItalic(), 'style:font-style-asian', 'italic');
$this->xmlWriter->endElement(); // style:text-properties $xmlWriter->writeAttributeIf($style->isItalic(), 'style:font-style-complex', 'italic');
$this->xmlWriter->endElement(); // style:style
$xmlWriter->endElement(); // style:text-properties
$xmlWriter->endElement(); // style:style
} }
/** /**

View File

@ -34,32 +34,33 @@ class Paragraph extends AbstractStyle
*/ */
public function write() public function write()
{ {
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Paragraph)) { if (is_null($style = $this->getStyle())) {
return; return;
} }
$xmlWriter = $this->getXmlWriter();
$marginTop = is_null($this->style->getSpaceBefore()) ? '0' : round(17.6 / $this->style->getSpaceBefore(), 2); $marginTop = is_null($style->getSpaceBefore()) ? '0' : round(17.6 / $style->getSpaceBefore(), 2);
$marginBottom = is_null($this->style->getSpaceAfter()) ? '0' : round(17.6 / $this->style->getSpaceAfter(), 2); $marginBottom = is_null($style->getSpaceAfter()) ? '0' : round(17.6 / $style->getSpaceAfter(), 2);
$this->xmlWriter->startElement('style:style'); $xmlWriter->startElement('style:style');
$this->xmlWriter->writeAttribute('style:name', $this->style->getStyleName()); $xmlWriter->writeAttribute('style:name', $style->getStyleName());
$this->xmlWriter->writeAttribute('style:family', 'paragraph'); $xmlWriter->writeAttribute('style:family', 'paragraph');
if ($this->isAuto) { if ($this->isAuto) {
$this->xmlWriter->writeAttribute('style:parent-style-name', 'Standard'); $xmlWriter->writeAttribute('style:parent-style-name', 'Standard');
$this->xmlWriter->writeAttribute('style:master-page-name', 'Standard'); $xmlWriter->writeAttribute('style:master-page-name', 'Standard');
} }
$this->xmlWriter->startElement('style:paragraph-properties'); $xmlWriter->startElement('style:paragraph-properties');
if ($this->isAuto) { if ($this->isAuto) {
$this->xmlWriter->writeAttribute('style:page-number', 'auto'); $xmlWriter->writeAttribute('style:page-number', 'auto');
} else { } else {
$this->xmlWriter->writeAttribute('fo:margin-top', $marginTop . 'cm'); $xmlWriter->writeAttribute('fo:margin-top', $marginTop . 'cm');
$this->xmlWriter->writeAttribute('fo:margin-bottom', $marginBottom . 'cm'); $xmlWriter->writeAttribute('fo:margin-bottom', $marginBottom . 'cm');
$this->xmlWriter->writeAttribute('fo:text-align', $this->style->getAlign()); $xmlWriter->writeAttribute('fo:text-align', $style->getAlign());
} }
$this->xmlWriter->endElement(); //style:paragraph-properties $xmlWriter->endElement(); //style:paragraph-properties
$this->xmlWriter->endElement(); //style:style $xmlWriter->endElement(); //style:style
} }
/** /**

View File

@ -52,7 +52,7 @@ class PDF
set_include_path(get_include_path() . PATH_SEPARATOR . $pdfLibraryPath); set_include_path(get_include_path() . PATH_SEPARATOR . $pdfLibraryPath);
} }
$rendererName = 'PhpOffice\\PhpWord\\Writer\\PDF\\' . $pdfLibraryName; $rendererName = get_class($this) . '\\' . $pdfLibraryName;
$this->renderer = new $rendererName($phpWord); $this->renderer = new $rendererName($phpWord);
} }

View File

@ -66,15 +66,17 @@ class RTF extends AbstractWriter implements WriterInterface
/** /**
* Save PhpWord to file * Save PhpWord to file
* *
* @param string $pFilename * @param string $filename
* @throws \PhpOffice\PhpWord\Exception\Exception * @throws \PhpOffice\PhpWord\Exception\Exception
*/ */
public function save($pFilename = null) public function save($filename = null)
{ {
if (!is_null($this->phpWord)) { if (is_null($this->phpWord)) {
$pFilename = $this->getTempFile($pFilename); throw new Exception('PhpWord object unassigned.');
}
$hFile = fopen($pFilename, 'w'); $filename = $this->getTempFile($filename);
$hFile = fopen($filename, 'w');
if ($hFile !== false) { if ($hFile !== false) {
fwrite($hFile, $this->writeDocument()); fwrite($hFile, $this->writeDocument());
fclose($hFile); fclose($hFile);
@ -82,9 +84,6 @@ class RTF extends AbstractWriter implements WriterInterface
throw new Exception("Can't open file"); throw new Exception("Can't open file");
} }
$this->cleanupTempFile(); $this->cleanupTempFile();
} else {
throw new Exception("PhpWord object unassigned.");
}
} }
/** /**

View File

@ -67,14 +67,13 @@ class Element
*/ */
public function write() public function write()
{ {
$rtfText = ''; $content = '';
$elmName = str_replace('PhpOffice\\PhpWord\\Element\\', '', get_class($this->element)); $writerClass = dirname(get_class($this)) . '\\' . basename(get_class($this->element));
$elmWriterClass = 'PhpOffice\\PhpWord\\Writer\\RTF\\Element\\' . $elmName; if (class_exists($writerClass)) {
if (class_exists($elmWriterClass) === true) { $writer = new $writerClass($this->parentWriter, $this->element, $this->withoutP);
$elmWriter = new $elmWriterClass($this->parentWriter, $this->element, $this->withoutP); $content = $writer->write();
$rtfText = $elmWriter->write();
} }
return $rtfText; return $content;
} }
} }

View File

@ -72,7 +72,7 @@ class Word2007 extends AbstractWriter implements WriterInterface
'Endnotes' => '', 'Endnotes' => '',
); );
foreach (array_keys($this->parts) as $partName) { foreach (array_keys($this->parts) as $partName) {
$partClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Part\\' . $partName; $partClass = get_class($this) . '\\Part\\' . $partName;
if (class_exists($partClass)) { if (class_exists($partClass)) {
$partObject = new $partClass(); $partObject = new $partClass();
$partObject->setParentWriter($this); $partObject->setParentWriter($this);
@ -91,7 +91,10 @@ class Word2007 extends AbstractWriter implements WriterInterface
*/ */
public function save($filename = null) public function save($filename = null)
{ {
if (!is_null($this->phpWord)) { if (is_null($this->phpWord)) {
throw new Exception('PhpWord object unassigned.');
}
$filename = $this->getTempFile($filename); $filename = $this->getTempFile($filename);
$objZip = $this->getZipArchive($filename); $objZip = $this->getZipArchive($filename);
@ -139,9 +142,6 @@ class Word2007 extends AbstractWriter implements WriterInterface
} }
$this->cleanupTempFile(); $this->cleanupTempFile();
} else {
throw new Exception("PhpWord object unassigned.");
}
} }
/** /**

View File

@ -17,37 +17,30 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element; namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Element\AbstractElement; use PhpOffice\PhpWord\Element\AbstractElement as Element;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Writer\Word2007\Part\AbstractPart;
/** /**
* Generic element writer * Abstract element writer
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class Element abstract class AbstractElement
{ {
/** /**
* XML writer * XML writer
* *
* @var \PhpOffice\PhpWord\Shared\XMLWriter * @var \PhpOffice\PhpWord\Shared\XMLWriter
*/ */
protected $xmlWriter; private $xmlWriter;
/**
* Parent writer
*
* @var \PhpOffice\PhpWord\Writer\Word2007\Part\AbstractPart
*/
protected $parentWriter;
/** /**
* Element * Element
* *
* @var \PhpOffice\PhpWord\Element\AbstractElement * @var \PhpOffice\PhpWord\Element\AbstractElement
*/ */
protected $element; private $element;
/** /**
* Without paragraph * Without paragraph
@ -56,32 +49,49 @@ class Element
*/ */
protected $withoutP = false; protected $withoutP = false;
/**
* Write element
*/
abstract public function write();
/** /**
* Create new instance * Create new instance
* *
* @param \PhpOffice\PhpWord\Element\AbstractElement $element
* @param bool $withoutP * @param bool $withoutP
*/ */
public function __construct(XMLWriter $xmlWriter, AbstractPart $parentWriter, $element, $withoutP = false) public function __construct(XMLWriter $xmlWriter, Element $element, $withoutP = false)
{ {
$this->xmlWriter = $xmlWriter; $this->xmlWriter = $xmlWriter;
$this->parentWriter = $parentWriter;
$this->element = $element; $this->element = $element;
$this->withoutP = $withoutP; $this->withoutP = $withoutP;
} }
/** /**
* Write element * Get XML Writer
* *
* @return string * @return \PhpOffice\PhpWord\Shared\XMLWriter
*/ */
public function write() protected function getXmlWriter()
{ {
$elmName = str_replace('PhpOffice\\PhpWord\\Element\\', '', get_class($this->element)); return $this->xmlWriter;
$elmWriterClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $elmName; }
if (class_exists($elmWriterClass) === true) {
$elmWriter = new $elmWriterClass($this->xmlWriter, $this->parentWriter, $this->element, $this->withoutP); /**
$elmWriter->write(); * Get Element
*
* @return \PhpOffice\PhpWord\Element\AbstractElement
*/
protected function getElement()
{
if (!is_null($this->element)) {
$elementClass = 'PhpOffice\\PhpWord\\Element\\' . basename(get_class($this->element));
if ($this->element instanceof $elementClass) {
return $this->element;
} else {
throw new Exception('No valid element assigned.');
}
} else {
throw new Exception('No element assigned.');
} }
} }
} }

View File

@ -18,91 +18,77 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element; namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Shared\String; use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
/** /**
* CheckBox element writer * CheckBox element writer
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class CheckBox extends Element class CheckBox extends Text
{ {
/** /**
* Write element * Write element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\CheckBox) { $xmlWriter = $this->getXmlWriter();
return; $element = $this->getElement();
}
$name = htmlspecialchars($this->element->getName()); $name = htmlspecialchars($element->getName());
$name = String::controlCharacterPHP2OOXML($name); $name = String::controlCharacterPHP2OOXML($name);
$text = htmlspecialchars($this->element->getText()); $text = htmlspecialchars($element->getText());
$text = String::controlCharacterPHP2OOXML($text); $text = String::controlCharacterPHP2OOXML($text);
$fontStyle = $this->element->getFontStyle();
$paragraphStyle = $this->element->getParagraphStyle();
if (!$this->withoutP) { $this->writeOpeningWP();
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:r');
$styleWriter->write(); $xmlWriter->startElement('w:fldChar');
} $xmlWriter->writeAttribute('w:fldCharType', 'begin');
$xmlWriter->startElement('w:ffData');
$xmlWriter->startElement('w:name');
$xmlWriter->writeAttribute('w:val', $name);
$xmlWriter->endElement(); //w:name
$xmlWriter->writeAttribute('w:enabled', '');
$xmlWriter->startElement('w:calcOnExit');
$xmlWriter->writeAttribute('w:val', '0');
$xmlWriter->endElement(); //w:calcOnExit
$xmlWriter->startElement('w:checkBox');
$xmlWriter->writeAttribute('w:sizeAuto', '');
$xmlWriter->startElement('w:default');
$xmlWriter->writeAttribute('w:val', 0);
$xmlWriter->endElement(); //w:default
$xmlWriter->endElement(); //w:checkBox
$xmlWriter->endElement(); // w:ffData
$xmlWriter->endElement(); // w:fldChar
$xmlWriter->endElement(); // w:r
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:fldChar'); $xmlWriter->startElement('w:instrText');
$this->xmlWriter->writeAttribute('w:fldCharType', 'begin'); $xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->startElement('w:ffData'); $xmlWriter->writeRaw(' FORMCHECKBOX ');
$this->xmlWriter->startElement('w:name'); $xmlWriter->endElement();// w:instrText
$this->xmlWriter->writeAttribute('w:val', $name); $xmlWriter->endElement(); // w:r
$this->xmlWriter->endElement(); //w:name $xmlWriter->startElement('w:r');
$this->xmlWriter->writeAttribute('w:enabled', ''); $xmlWriter->startElement('w:fldChar');
$this->xmlWriter->startElement('w:calcOnExit'); $xmlWriter->writeAttribute('w:fldCharType', 'seperate');
$this->xmlWriter->writeAttribute('w:val', '0'); $xmlWriter->endElement();// w:fldChar
$this->xmlWriter->endElement(); //w:calcOnExit $xmlWriter->endElement(); // w:r
$this->xmlWriter->startElement('w:checkBox'); $xmlWriter->startElement('w:r');
$this->xmlWriter->writeAttribute('w:sizeAuto', ''); $xmlWriter->startElement('w:fldChar');
$this->xmlWriter->startElement('w:default'); $xmlWriter->writeAttribute('w:fldCharType', 'end');
$this->xmlWriter->writeAttribute('w:val', 0); $xmlWriter->endElement();// w:fldChar
$this->xmlWriter->endElement(); //w:default $xmlWriter->endElement(); // w:r
$this->xmlWriter->endElement(); //w:checkBox
$this->xmlWriter->endElement(); // w:ffData
$this->xmlWriter->endElement(); // w:fldChar
$this->xmlWriter->endElement(); // w:r
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:instrText');
$this->xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->writeRaw(' FORMCHECKBOX ');
$this->xmlWriter->endElement();// w:instrText
$this->xmlWriter->endElement(); // w:r
$this->xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:fldChar');
$this->xmlWriter->writeAttribute('w:fldCharType', 'seperate');
$this->xmlWriter->endElement();// w:fldChar
$this->xmlWriter->endElement(); // w:r
$this->xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:fldChar');
$this->xmlWriter->writeAttribute('w:fldCharType', 'end');
$this->xmlWriter->endElement();// w:fldChar
$this->xmlWriter->endElement(); // w:r
$styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle); $this->writeFontStyle();
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:t');
$styleWriter->write(); $xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->startElement('w:t'); $xmlWriter->writeRaw($text);
$this->xmlWriter->writeAttribute('xml:space', 'preserve'); $xmlWriter->endElement(); // w:t
$this->xmlWriter->writeRaw($text); $xmlWriter->endElement(); // w:r
$this->xmlWriter->endElement(); // w:t
$this->xmlWriter->endElement(); // w:r
if (!$this->withoutP) { $this->writeEndingWP();
$this->xmlWriter->endElement(); // w:p
}
} }
} }

View File

@ -0,0 +1,58 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Element\TextBreak as TextBreakElement;
/**
* Container element writer (section, textrun, header, footnote, cell, etc.)
*
* @since 0.11.0
*/
class Container extends AbstractElement
{
/**
* Write element
*/
public function write()
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
// Loop through subelements
$containerClass = basename(get_class($element));
$subelements = $element->getElements();
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false;
if (count($subelements) > 0) {
foreach ($subelements as $subelement) {
$writerClass = dirname(get_class($this)) . '\\' . basename(get_class($subelement));
if (class_exists($writerClass)) {
$writer = new $writerClass($xmlWriter, $subelement, $withoutP);
$writer->write();
}
}
} else {
// Special case for Cell: They have to contain a TextBreak at least
if ($containerClass == 'Cell') {
$writerClass = dirname(get_class($this)) . '\\TextBreak';
$writer = new $writerClass($xmlWriter, new TextBreakElement(), $withoutP);
$writer->write();
}
}
}
}

View File

@ -22,7 +22,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element;
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class Footnote extends Element class Footnote extends Text
{ {
/** /**
* Reference type footnoteReference|endnoteReference * Reference type footnoteReference|endnoteReference
@ -36,25 +36,22 @@ class Footnote extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Footnote) { $xmlWriter = $this->getXmlWriter();
return; $element = $this->getElement();
}
if (!$this->withoutP) { $this->writeOpeningWP();
$this->xmlWriter->startElement('w:p');
} $xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:rPr');
$this->xmlWriter->startElement('w:rPr'); $xmlWriter->startElement('w:rStyle');
$this->xmlWriter->startElement('w:rStyle'); $xmlWriter->writeAttribute('w:val', ucfirst($this->referenceType));
$this->xmlWriter->writeAttribute('w:val', ucfirst($this->referenceType)); $xmlWriter->endElement(); // w:rStyle
$this->xmlWriter->endElement(); // w:rStyle $xmlWriter->endElement(); // w:rPr
$this->xmlWriter->endElement(); // w:rPr $xmlWriter->startElement("w:{$this->referenceType}");
$this->xmlWriter->startElement("w:{$this->referenceType}"); $xmlWriter->writeAttribute('w:id', $element->getRelationId());
$this->xmlWriter->writeAttribute('w:id', $this->element->getRelationId()); $xmlWriter->endElement(); // w:$referenceType
$this->xmlWriter->endElement(); // w:$referenceType $xmlWriter->endElement(); // w:r
$this->xmlWriter->endElement(); // w:r
if (!$this->withoutP) { $this->writeEndingWP();
$this->xmlWriter->endElement(); // w:p
}
} }
} }

View File

@ -24,14 +24,16 @@ use PhpOffice\PhpWord\Writer\Word2007\Style\Image as ImageStyleWriter;
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class Image extends Element class Image extends AbstractElement
{ {
/** /**
* Write element * Write element
*/ */
public function write() public function write()
{ {
if ($this->element->isWatermark()) { $element = $this->getElement();
if ($element->isWatermark()) {
$this->writeWatermark(); $this->writeWatermark();
} else { } else {
$this->writeImage(); $this->writeImage();
@ -43,41 +45,40 @@ class Image extends Element
*/ */
private function writeImage() private function writeImage()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Image) { $xmlWriter = $this->getXmlWriter();
return; $element = $this->getElement();
}
$rId = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0); $rId = $element->getRelationId() + ($element->isInSection() ? 6 : 0);
$style = $this->element->getStyle(); $style = $element->getStyle();
$styleWriter = new ImageStyleWriter($this->xmlWriter, $style); $styleWriter = new ImageStyleWriter($xmlWriter, $style);
if (!$this->withoutP) { if (!$this->withoutP) {
$this->xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:p');
if (!is_null($style->getAlign())) { if (!is_null($style->getAlign())) {
$this->xmlWriter->startElement('w:pPr'); $xmlWriter->startElement('w:pPr');
$this->xmlWriter->startElement('w:jc'); $xmlWriter->startElement('w:jc');
$this->xmlWriter->writeAttribute('w:val', $style->getAlign()); $xmlWriter->writeAttribute('w:val', $style->getAlign());
$this->xmlWriter->endElement(); // w:jc $xmlWriter->endElement(); // w:jc
$this->xmlWriter->endElement(); // w:pPr $xmlWriter->endElement(); // w:pPr
} }
} }
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:pict'); $xmlWriter->startElement('w:pict');
$this->xmlWriter->startElement('v:shape'); $xmlWriter->startElement('v:shape');
$this->xmlWriter->writeAttribute('type', '#_x0000_t75'); $xmlWriter->writeAttribute('type', '#_x0000_t75');
$styleWriter->write(); $styleWriter->write();
$this->xmlWriter->startElement('v:imagedata'); $xmlWriter->startElement('v:imagedata');
$this->xmlWriter->writeAttribute('r:id', 'rId' . $rId); $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
$this->xmlWriter->writeAttribute('o:title', ''); $xmlWriter->writeAttribute('o:title', '');
$this->xmlWriter->endElement(); // v:imagedata $xmlWriter->endElement(); // v:imagedata
$styleWriter->writeW10Wrap(); $styleWriter->writeW10Wrap();
$this->xmlWriter->endElement(); // v:shape $xmlWriter->endElement(); // v:shape
$this->xmlWriter->endElement(); // w:pict $xmlWriter->endElement(); // w:pict
$this->xmlWriter->endElement(); // w:r $xmlWriter->endElement(); // w:r
if (!$this->withoutP) { if (!$this->withoutP) {
$this->xmlWriter->endElement(); // w:p $xmlWriter->endElement(); // w:p
} }
} }
/** /**
@ -85,24 +86,27 @@ class Image extends Element
*/ */
private function writeWatermark() private function writeWatermark()
{ {
$rId = $this->element->getRelationId(); $xmlWriter = $this->getXmlWriter();
$style = $this->element->getStyle(); $element = $this->getElement();
$style->setPositioning('absolute');
$styleWriter = new ImageStyleWriter($this->xmlWriter, $style);
$this->xmlWriter->startElement('w:p'); $rId = $element->getRelationId();
$this->xmlWriter->startElement('w:r'); $style = $element->getStyle();
$this->xmlWriter->startElement('w:pict'); $style->setPositioning('absolute');
$this->xmlWriter->startElement('v:shape'); $styleWriter = new ImageStyleWriter($xmlWriter, $style);
$this->xmlWriter->writeAttribute('type', '#_x0000_t75');
$xmlWriter->startElement('w:p');
$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:pict');
$xmlWriter->startElement('v:shape');
$xmlWriter->writeAttribute('type', '#_x0000_t75');
$styleWriter->write(); $styleWriter->write();
$this->xmlWriter->startElement('v:imagedata'); $xmlWriter->startElement('v:imagedata');
$this->xmlWriter->writeAttribute('r:id', 'rId' . $rId); $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
$this->xmlWriter->writeAttribute('o:title', ''); $xmlWriter->writeAttribute('o:title', '');
$this->xmlWriter->endElement(); // v:imagedata $xmlWriter->endElement(); // v:imagedata
$this->xmlWriter->endElement(); // v:shape $xmlWriter->endElement(); // v:shape
$this->xmlWriter->endElement(); // w:pict $xmlWriter->endElement(); // w:pict
$this->xmlWriter->endElement(); // w:r $xmlWriter->endElement(); // w:r
$this->xmlWriter->endElement(); // w:p $xmlWriter->endElement(); // w:p
} }
} }

View File

@ -17,53 +17,39 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element; namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
/** /**
* Link element writer * Link element writer
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class Link extends Element class Link extends Text
{ {
/** /**
* Write link element * Write link element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Link) { $xmlWriter = $this->getXmlWriter();
return; $element = $this->getElement();
}
$rId = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0); $rId = $element->getRelationId() + ($element->isInSection() ? 6 : 0);
$fontStyle = $this->element->getFontStyle();
$paragraphStyle = $this->element->getParagraphStyle();
if (!$this->withoutP) { $this->writeOpeningWP();
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:hyperlink');
$styleWriter->write(); $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
} $xmlWriter->writeAttribute('w:history', '1');
$xmlWriter->startElement('w:r');
$styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle); $this->writeFontStyle();
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:hyperlink'); $xmlWriter->startElement('w:t');
$this->xmlWriter->writeAttribute('r:id', 'rId' . $rId); $xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->writeAttribute('w:history', '1'); $xmlWriter->writeRaw($element->getText());
$this->xmlWriter->startElement('w:r'); $xmlWriter->endElement(); // w:t
$styleWriter->write(); $xmlWriter->endElement(); // w:r
$this->xmlWriter->startElement('w:t'); $xmlWriter->endElement(); // w:hyperlink
$this->xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->writeRaw($this->element->getText()); $this->writeEndingWP();
$this->xmlWriter->endElement(); // w:t
$this->xmlWriter->endElement(); // w:r
$this->xmlWriter->endElement(); // w:hyperlink
if (!$this->withoutP) {
$this->xmlWriter->endElement(); // w:p
}
} }
} }

View File

@ -17,7 +17,6 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element; namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Writer\Word2007\Element\Element as ElementWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter; use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
/** /**
@ -25,42 +24,41 @@ use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class ListItem extends Element class ListItem extends AbstractElement
{ {
/** /**
* Write list item element * Write list item element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\ListItem) { $xmlWriter = $this->getXmlWriter();
return; $element = $this->getElement();
}
$textObject = $this->element->getTextObject(); $textObject = $element->getTextObject();
$depth = $this->element->getDepth();
$numId = $this->element->getStyle()->getNumId(); $styleWriter = new ParagraphStyleWriter($xmlWriter, $textObject->getParagraphStyle());
$paragraphStyle = $textObject->getParagraphStyle();
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
$styleWriter->setWithoutPPR(true); $styleWriter->setWithoutPPR(true);
$styleWriter->setIsInline(true); $styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:p');
$this->xmlWriter->startElement('w:pPr'); $xmlWriter->startElement('w:pPr');
$styleWriter->write(); $styleWriter->write();
$this->xmlWriter->startElement('w:numPr');
$this->xmlWriter->startElement('w:ilvl');
$this->xmlWriter->writeAttribute('w:val', $depth);
$this->xmlWriter->endElement(); // w:ilvl
$this->xmlWriter->startElement('w:numId');
$this->xmlWriter->writeAttribute('w:val', $numId);
$this->xmlWriter->endElement(); // w:numId
$this->xmlWriter->endElement(); // w:numPr
$this->xmlWriter->endElement(); // w:pPr
$elementWriter = new ElementWriter($this->xmlWriter, $this->parentWriter, $textObject, true); $xmlWriter->startElement('w:numPr');
$xmlWriter->startElement('w:ilvl');
$xmlWriter->writeAttribute('w:val', $element->getDepth());
$xmlWriter->endElement(); // w:ilvl
$xmlWriter->startElement('w:numId');
$xmlWriter->writeAttribute('w:val', $element->getStyle()->getNumId());
$xmlWriter->endElement(); // w:numId
$xmlWriter->endElement(); // w:numPr
$xmlWriter->endElement(); // w:pPr
$elementWriter = new Text($xmlWriter, $textObject, true);
$elementWriter->write(); $elementWriter->write();
$this->xmlWriter->endElement(); // w:p $xmlWriter->endElement(); // w:p
} }
} }

View File

@ -22,60 +22,59 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element;
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class Object extends Element class Object extends AbstractElement
{ {
/** /**
* Write object element * Write object element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Object) { $xmlWriter = $this->getXmlWriter();
return; $element = $this->getElement();
}
$rIdObject = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0); $rIdObject = $element->getRelationId() + ($element->isInSection() ? 6 : 0);
$rIdImage = $this->element->getImageRelationId() + ($this->element->isInSection() ? 6 : 0); $rIdImage = $element->getImageRelationId() + ($element->isInSection() ? 6 : 0);
$shapeId = md5($rIdObject . '_' . $rIdImage); $shapeId = md5($rIdObject . '_' . $rIdImage);
$objectId = $this->element->getRelationId() + 1325353440; $objectId = $element->getRelationId() + 1325353440;
$style = $this->element->getStyle(); $style = $element->getStyle();
$align = $style->getAlign(); $align = $style->getAlign();
if (!$this->withoutP) { if (!$this->withoutP) {
$this->xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:p');
} }
if (!is_null($align)) { if (!is_null($align)) {
$this->xmlWriter->startElement('w:pPr'); $xmlWriter->startElement('w:pPr');
$this->xmlWriter->startElement('w:jc'); $xmlWriter->startElement('w:jc');
$this->xmlWriter->writeAttribute('w:val', $align); $xmlWriter->writeAttribute('w:val', $align);
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:object'); $xmlWriter->startElement('w:object');
$this->xmlWriter->writeAttribute('w:dxaOrig', '249'); $xmlWriter->writeAttribute('w:dxaOrig', '249');
$this->xmlWriter->writeAttribute('w:dyaOrig', '160'); $xmlWriter->writeAttribute('w:dyaOrig', '160');
$this->xmlWriter->startElement('v:shape'); $xmlWriter->startElement('v:shape');
$this->xmlWriter->writeAttribute('id', $shapeId); $xmlWriter->writeAttribute('id', $shapeId);
$this->xmlWriter->writeAttribute('type', '#_x0000_t75'); $xmlWriter->writeAttribute('type', '#_x0000_t75');
$this->xmlWriter->writeAttribute('style', 'width:104px;height:67px'); $xmlWriter->writeAttribute('style', 'width:104px;height:67px');
$this->xmlWriter->writeAttribute('o:ole', ''); $xmlWriter->writeAttribute('o:ole', '');
$this->xmlWriter->startElement('v:imagedata'); $xmlWriter->startElement('v:imagedata');
$this->xmlWriter->writeAttribute('r:id', 'rId' . $rIdImage); $xmlWriter->writeAttribute('r:id', 'rId' . $rIdImage);
$this->xmlWriter->writeAttribute('o:title', ''); $xmlWriter->writeAttribute('o:title', '');
$this->xmlWriter->endElement(); // v:imagedata $xmlWriter->endElement(); // v:imagedata
$this->xmlWriter->endElement(); // v:shape $xmlWriter->endElement(); // v:shape
$this->xmlWriter->startElement('o:OLEObject'); $xmlWriter->startElement('o:OLEObject');
$this->xmlWriter->writeAttribute('Type', 'Embed'); $xmlWriter->writeAttribute('Type', 'Embed');
$this->xmlWriter->writeAttribute('ProgID', 'Package'); $xmlWriter->writeAttribute('ProgID', 'Package');
$this->xmlWriter->writeAttribute('ShapeID', $shapeId); $xmlWriter->writeAttribute('ShapeID', $shapeId);
$this->xmlWriter->writeAttribute('DrawAspect', 'Icon'); $xmlWriter->writeAttribute('DrawAspect', 'Icon');
$this->xmlWriter->writeAttribute('ObjectID', '_' . $objectId); $xmlWriter->writeAttribute('ObjectID', '_' . $objectId);
$this->xmlWriter->writeAttribute('r:id', 'rId' . $rIdObject); $xmlWriter->writeAttribute('r:id', 'rId' . $rIdObject);
$this->xmlWriter->endElement(); // o:OLEObject $xmlWriter->endElement(); // o:OLEObject
$this->xmlWriter->endElement(); // w:object $xmlWriter->endElement(); // w:object
$this->xmlWriter->endElement(); // w:r $xmlWriter->endElement(); // w:r
if (!$this->withoutP) { if (!$this->withoutP) {
$this->xmlWriter->endElement(); // w:p $xmlWriter->endElement(); // w:p
} }
} }
} }

View File

@ -22,19 +22,21 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element;
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class PageBreak extends Element class PageBreak extends AbstractElement
{ {
/** /**
* Write element * Write element
*/ */
public function write() public function write()
{ {
$this->xmlWriter->startElement('w:p'); $xmlWriter = $this->getXmlWriter();
$this->xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:br'); $xmlWriter->startElement('w:p');
$this->xmlWriter->writeAttribute('w:type', 'page'); $xmlWriter->startElement('w:r');
$this->xmlWriter->endElement(); $xmlWriter->startElement('w:br');
$this->xmlWriter->endElement(); $xmlWriter->writeAttribute('w:type', 'page');
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$xmlWriter->endElement();
$xmlWriter->endElement();
} }
} }

View File

@ -18,85 +18,76 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element; namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Shared\String; use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
/** /**
* PreserveText element writer * PreserveText element writer
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class PreserveText extends Element class PreserveText extends Text
{ {
/** /**
* Write preserve text element * Write preserve text element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\PreserveText) { $xmlWriter = $this->getXmlWriter();
return; $element = $this->getElement();
}
$fontStyle = $this->element->getFontStyle(); $texts = $element->getText();
$paragraphStyle = $this->element->getParagraphStyle();
$texts = $this->element->getText();
if (!is_array($texts)) { if (!is_array($texts)) {
$texts = array($texts); $texts = array($texts);
} }
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle); $this->writeOpeningWP();
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p');
$styleWriter->write();
foreach ($texts as $text) { foreach ($texts as $text) {
if (substr($text, 0, 1) == '{') { if (substr($text, 0, 1) == '{') {
$text = substr($text, 1, -1); $text = substr($text, 1, -1);
$styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:fldChar'); $xmlWriter->startElement('w:fldChar');
$this->xmlWriter->writeAttribute('w:fldCharType', 'begin'); $xmlWriter->writeAttribute('w:fldCharType', 'begin');
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$styleWriter->write();
$this->xmlWriter->startElement('w:instrText');
$this->xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->writeRaw($text);
$this->xmlWriter->endElement();
$this->xmlWriter->endElement();
$this->xmlWriter->startElement('w:r'); $this->writeFontStyle();
$this->xmlWriter->startElement('w:fldChar');
$this->xmlWriter->writeAttribute('w:fldCharType', 'separate');
$this->xmlWriter->endElement();
$this->xmlWriter->endElement();
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:instrText');
$this->xmlWriter->startElement('w:fldChar'); $xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->writeAttribute('w:fldCharType', 'end'); $xmlWriter->writeRaw($text);
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:fldChar');
$xmlWriter->writeAttribute('w:fldCharType', 'separate');
$xmlWriter->endElement();
$xmlWriter->endElement();
$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:fldChar');
$xmlWriter->writeAttribute('w:fldCharType', 'end');
$xmlWriter->endElement();
$xmlWriter->endElement();
} else { } else {
$text = htmlspecialchars($text); $text = htmlspecialchars($text);
$text = String::controlCharacterPHP2OOXML($text); $text = String::controlCharacterPHP2OOXML($text);
$styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$styleWriter->write();
$this->xmlWriter->startElement('w:t'); $this->writeFontStyle();
$this->xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->writeRaw($text); $xmlWriter->startElement('w:t');
$this->xmlWriter->endElement(); $xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->endElement(); $xmlWriter->writeRaw($text);
$xmlWriter->endElement();
$xmlWriter->endElement();
} }
} }
$this->xmlWriter->endElement(); // p $this->writeEndingWP();
} }
} }

View File

@ -27,18 +27,17 @@ use PhpOffice\PhpWord\Writer\Word2007\Style\Tab as TabStyleWriter;
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class TOC extends Element class TOC extends AbstractElement
{ {
/** /**
* Write element * Write element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\TOC) { $xmlWriter = $this->getXmlWriter();
return; $element = $this->getElement();
}
$titles = $this->element->getTitles(); $titles = $element->getTitles();
$writeFieldMark = true; $writeFieldMark = true;
foreach ($titles as $title) { foreach ($titles as $title) {
@ -48,13 +47,13 @@ class TOC extends Element
} }
} }
$this->xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:p');
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:fldChar'); $xmlWriter->startElement('w:fldChar');
$this->xmlWriter->writeAttribute('w:fldCharType', 'end'); $xmlWriter->writeAttribute('w:fldCharType', 'end');
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
/** /**
@ -65,13 +64,16 @@ class TOC extends Element
*/ */
private function writeTitle($title, $writeFieldMark) private function writeTitle($title, $writeFieldMark)
{ {
$tocStyle = $this->element->getStyleTOC(); $xmlWriter = $this->getXmlWriter();
$fontStyle = $this->element->getStyleFont(); $element = $this->getElement();
$tocStyle = $element->getStyleTOC();
$fontStyle = $element->getStyleFont();
$isObject = ($fontStyle instanceof Font) ? true : false; $isObject = ($fontStyle instanceof Font) ? true : false;
$anchor = '_Toc' . ($title->getBookmarkId() + 252634154); $anchor = '_Toc' . ($title->getBookmarkId() + 252634154);
$indent = ($title->getDepth() - 1) * $tocStyle->getIndent(); $indent = ($title->getDepth() - 1) * $tocStyle->getIndent();
$this->xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:p');
// Write style and field mark // Write style and field mark
$this->writeStyle($indent); $this->writeStyle($indent);
@ -80,47 +82,47 @@ class TOC extends Element
} }
// Hyperlink // Hyperlink
$this->xmlWriter->startElement('w:hyperlink'); $xmlWriter->startElement('w:hyperlink');
$this->xmlWriter->writeAttribute('w:anchor', $anchor); $xmlWriter->writeAttribute('w:anchor', $anchor);
$this->xmlWriter->writeAttribute('w:history', '1'); $xmlWriter->writeAttribute('w:history', '1');
// Title text // Title text
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
if ($isObject) { if ($isObject) {
$styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle); $styleWriter = new FontStyleWriter($xmlWriter, $fontStyle);
$styleWriter->write(); $styleWriter->write();
} }
$this->xmlWriter->startElement('w:t'); $xmlWriter->startElement('w:t');
$this->xmlWriter->writeRaw($title->getText()); $xmlWriter->writeRaw($title->getText());
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->endElement(); // w:r $xmlWriter->endElement(); // w:r
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$this->xmlWriter->writeElement('w:tab', null); $xmlWriter->writeElement('w:tab', null);
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:fldChar'); $xmlWriter->startElement('w:fldChar');
$this->xmlWriter->writeAttribute('w:fldCharType', 'begin'); $xmlWriter->writeAttribute('w:fldCharType', 'begin');
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:instrText'); $xmlWriter->startElement('w:instrText');
$this->xmlWriter->writeAttribute('xml:space', 'preserve'); $xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->writeRaw('PAGEREF ' . $anchor . ' \h'); $xmlWriter->writeRaw('PAGEREF ' . $anchor . ' \h');
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:fldChar'); $xmlWriter->startElement('w:fldChar');
$this->xmlWriter->writeAttribute('w:fldCharType', 'end'); $xmlWriter->writeAttribute('w:fldCharType', 'end');
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->endElement(); // w:hyperlink $xmlWriter->endElement(); // w:hyperlink
$this->xmlWriter->endElement(); // w:p $xmlWriter->endElement(); // w:p
} }
/** /**
@ -130,41 +132,44 @@ class TOC extends Element
*/ */
private function writeStyle($indent) private function writeStyle($indent)
{ {
$tocStyle = $this->element->getStyleTOC(); $xmlWriter = $this->getXmlWriter();
$fontStyle = $this->element->getStyleFont(); $element = $this->getElement();
$tocStyle = $element->getStyleTOC();
$fontStyle = $element->getStyleFont();
$isObject = ($fontStyle instanceof Font) ? true : false; $isObject = ($fontStyle instanceof Font) ? true : false;
$this->xmlWriter->startElement('w:pPr'); $xmlWriter->startElement('w:pPr');
// Paragraph // Paragraph
if ($isObject && !is_null($fontStyle->getParagraphStyle())) { if ($isObject && !is_null($fontStyle->getParagraphStyle())) {
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $fontStyle->getParagraphStyle()); $styleWriter = new ParagraphStyleWriter($xmlWriter, $fontStyle->getParagraphStyle());
$styleWriter->write(); $styleWriter->write();
} }
// Font // Font
if (!empty($fontStyle) && !$isObject) { if (!empty($fontStyle) && !$isObject) {
$this->xmlWriter->startElement('w:rPr'); $xmlWriter->startElement('w:rPr');
$this->xmlWriter->startElement('w:rStyle'); $xmlWriter->startElement('w:rStyle');
$this->xmlWriter->writeAttribute('w:val', $fontStyle); $xmlWriter->writeAttribute('w:val', $fontStyle);
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->endElement(); // w:rPr $xmlWriter->endElement(); // w:rPr
} }
// Tab // Tab
$this->xmlWriter->startElement('w:tabs'); $xmlWriter->startElement('w:tabs');
$styleWriter = new TabStyleWriter($this->xmlWriter, $tocStyle); $styleWriter = new TabStyleWriter($xmlWriter, $tocStyle);
$styleWriter->write(); $styleWriter->write();
$this->xmlWriter->endElement(); $xmlWriter->endElement();
// Indent // Indent
if ($indent > 0) { if ($indent > 0) {
$this->xmlWriter->startElement('w:ind'); $xmlWriter->startElement('w:ind');
$this->xmlWriter->writeAttribute('w:left', $indent); $xmlWriter->writeAttribute('w:left', $indent);
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
$this->xmlWriter->endElement(); // w:pPr $xmlWriter->endElement(); // w:pPr
} }
/** /**
@ -172,26 +177,29 @@ class TOC extends Element
*/ */
private function writeFieldMark() private function writeFieldMark()
{ {
$minDepth = $this->element->getMinDepth(); $xmlWriter = $this->getXmlWriter();
$maxDepth = $this->element->getMaxDepth(); $element = $this->getElement();
$this->xmlWriter->startElement('w:r'); $minDepth = $element->getMinDepth();
$this->xmlWriter->startElement('w:fldChar'); $maxDepth = $element->getMaxDepth();
$this->xmlWriter->writeAttribute('w:fldCharType', 'begin');
$this->xmlWriter->endElement();
$this->xmlWriter->endElement();
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:instrText'); $xmlWriter->startElement('w:fldChar');
$this->xmlWriter->writeAttribute('xml:space', 'preserve'); $xmlWriter->writeAttribute('w:fldCharType', 'begin');
$this->xmlWriter->writeRaw("TOC \o {$minDepth}-{$maxDepth} \h \z \u"); $xmlWriter->endElement();
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->endElement();
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:fldChar'); $xmlWriter->startElement('w:instrText');
$this->xmlWriter->writeAttribute('w:fldCharType', 'separate'); $xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->endElement(); $xmlWriter->writeRaw("TOC \o {$minDepth}-{$maxDepth} \h \z \u");
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$xmlWriter->endElement();
$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:fldChar');
$xmlWriter->writeAttribute('w:fldCharType', 'separate');
$xmlWriter->endElement();
$xmlWriter->endElement();
} }
} }

View File

@ -29,22 +29,21 @@ use PhpOffice\PhpWord\Writer\Word2007\Style\Table as TableStyleWriter;
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class Table extends Element class Table extends AbstractElement
{ {
/** /**
* Write element * Write element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Table) { $xmlWriter = $this->getXmlWriter();
return; $element = $this->getElement();
}
$rows = $this->element->getRows(); $rows = $element->getRows();
$rowCount = count($rows); $rowCount = count($rows);
if ($rowCount > 0) { if ($rowCount > 0) {
$this->xmlWriter->startElement('w:tbl'); $xmlWriter->startElement('w:tbl');
// Table grid // Table grid
$cellWidths = array(); $cellWidths = array();
@ -59,37 +58,37 @@ class Table extends Element
$cellWidths[] = $cell->getWidth(); $cellWidths[] = $cell->getWidth();
} }
} }
$this->xmlWriter->startElement('w:tblGrid'); $xmlWriter->startElement('w:tblGrid');
foreach ($cellWidths as $width) { foreach ($cellWidths as $width) {
$this->xmlWriter->startElement('w:gridCol'); $xmlWriter->startElement('w:gridCol');
if (!is_null($width)) { if (!is_null($width)) {
$this->xmlWriter->writeAttribute('w:w', $width); $xmlWriter->writeAttribute('w:w', $width);
$this->xmlWriter->writeAttribute('w:type', 'dxa'); $xmlWriter->writeAttribute('w:type', 'dxa');
} }
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
$this->xmlWriter->endElement(); // w:tblGrid $xmlWriter->endElement(); // w:tblGrid
// Table style // Table style
$tblStyle = $this->element->getStyle(); $tblStyle = $element->getStyle();
$tblWidth = $this->element->getWidth(); $tblWidth = $element->getWidth();
if ($tblStyle instanceof TableStyle) { if ($tblStyle instanceof TableStyle) {
$styleWriter = new TableStyleWriter($this->xmlWriter, $tblStyle); $styleWriter = new TableStyleWriter($xmlWriter, $tblStyle);
$styleWriter->setIsFullStyle(false); $styleWriter->setIsFullStyle(false);
$styleWriter->write(); $styleWriter->write();
} else { } else {
if (!empty($tblStyle)) { if (!empty($tblStyle)) {
$this->xmlWriter->startElement('w:tblPr'); $xmlWriter->startElement('w:tblPr');
$this->xmlWriter->startElement('w:tblStyle'); $xmlWriter->startElement('w:tblStyle');
$this->xmlWriter->writeAttribute('w:val', $tblStyle); $xmlWriter->writeAttribute('w:val', $tblStyle);
$this->xmlWriter->endElement(); $xmlWriter->endElement();
if (!is_null($tblWidth)) { if (!is_null($tblWidth)) {
$this->xmlWriter->startElement('w:tblW'); $xmlWriter->startElement('w:tblW');
$this->xmlWriter->writeAttribute('w:w', $tblWidth); $xmlWriter->writeAttribute('w:w', $tblWidth);
$this->xmlWriter->writeAttribute('w:type', 'pct'); $xmlWriter->writeAttribute('w:type', 'pct');
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
} }
@ -97,7 +96,7 @@ class Table extends Element
for ($i = 0; $i < $rowCount; $i++) { for ($i = 0; $i < $rowCount; $i++) {
$this->writeRow($rows[$i]); $this->writeRow($rows[$i]);
} }
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
} }
@ -106,34 +105,36 @@ class Table extends Element
*/ */
private function writeRow(RowElement $row) private function writeRow(RowElement $row)
{ {
$xmlWriter = $this->getXmlWriter();
$height = $row->getHeight(); $height = $row->getHeight();
$rowStyle = $row->getStyle(); $rowStyle = $row->getStyle();
$this->xmlWriter->startElement('w:tr'); $xmlWriter->startElement('w:tr');
if (!is_null($height) || $rowStyle->isTblHeader() || $rowStyle->isCantSplit()) { if (!is_null($height) || $rowStyle->isTblHeader() || $rowStyle->isCantSplit()) {
$this->xmlWriter->startElement('w:trPr'); $xmlWriter->startElement('w:trPr');
if (!is_null($height)) { if (!is_null($height)) {
$this->xmlWriter->startElement('w:trHeight'); $xmlWriter->startElement('w:trHeight');
$this->xmlWriter->writeAttribute('w:val', $height); $xmlWriter->writeAttribute('w:val', $height);
$this->xmlWriter->writeAttribute('w:hRule', ($rowStyle->isExactHeight() ? 'exact' : 'atLeast')); $xmlWriter->writeAttribute('w:hRule', ($rowStyle->isExactHeight() ? 'exact' : 'atLeast'));
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
if ($rowStyle->isTblHeader()) { if ($rowStyle->isTblHeader()) {
$this->xmlWriter->startElement('w:tblHeader'); $xmlWriter->startElement('w:tblHeader');
$this->xmlWriter->writeAttribute('w:val', '1'); $xmlWriter->writeAttribute('w:val', '1');
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
if ($rowStyle->isCantSplit()) { if ($rowStyle->isCantSplit()) {
$this->xmlWriter->startElement('w:cantSplit'); $xmlWriter->startElement('w:cantSplit');
$this->xmlWriter->writeAttribute('w:val', '1'); $xmlWriter->writeAttribute('w:val', '1');
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
foreach ($row->getCells() as $cell) { foreach ($row->getCells() as $cell) {
$this->writeCell($cell); $this->writeCell($cell);
} }
$this->xmlWriter->endElement(); // w:tr $xmlWriter->endElement(); // w:tr
} }
/** /**
@ -141,20 +142,25 @@ class Table extends Element
*/ */
private function writeCell(CellElement $cell) private function writeCell(CellElement $cell)
{ {
$xmlWriter = $this->getXmlWriter();
$cellStyle = $cell->getStyle(); $cellStyle = $cell->getStyle();
$this->xmlWriter->startElement('w:tc'); $xmlWriter->startElement('w:tc');
$this->xmlWriter->startElement('w:tcPr'); $xmlWriter->startElement('w:tcPr');
$this->xmlWriter->startElement('w:tcW'); $xmlWriter->startElement('w:tcW');
$this->xmlWriter->writeAttribute('w:w', $cell->getWidth()); $xmlWriter->writeAttribute('w:w', $cell->getWidth());
$this->xmlWriter->writeAttribute('w:type', 'dxa'); $xmlWriter->writeAttribute('w:type', 'dxa');
$this->xmlWriter->endElement(); // w:tcW $xmlWriter->endElement(); // w:tcW
if ($cellStyle instanceof CellStyle) { if ($cellStyle instanceof CellStyle) {
$styleWriter = new CellStyleWriter($this->xmlWriter, $cellStyle); $styleWriter = new CellStyleWriter($xmlWriter, $cellStyle);
$styleWriter->write(); $styleWriter->write();
} }
$this->xmlWriter->endElement(); // w:tcPr $xmlWriter->endElement(); // w:tcPr
$this->parentWriter->writeContainerElements($this->xmlWriter, $cell);
$this->xmlWriter->endElement(); // w:tc $containerWriter = new Container($xmlWriter, $cell);
$containerWriter->write();
$xmlWriter->endElement(); // w:tc
} }
} }

View File

@ -26,41 +26,88 @@ use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class Text extends Element class Text extends AbstractElement
{ {
/** /**
* Write text element * Write text element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) { $xmlWriter = $this->getXmlWriter();
return; $element = $this->getElement();
}
$fontStyle = $this->element->getFontStyle(); $text = htmlspecialchars($element->getText());
$paragraphStyle = $this->element->getParagraphStyle();
$text = htmlspecialchars($this->element->getText());
$text = String::controlCharacterPHP2OOXML($text); $text = String::controlCharacterPHP2OOXML($text);
if (!$this->withoutP) { $this->writeOpeningWP();
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:r');
$this->writeFontStyle();
$xmlWriter->startElement('w:t');
$xmlWriter->writeAttribute('xml:space', 'preserve');
$xmlWriter->writeRaw($text);
$xmlWriter->endElement();
$xmlWriter->endElement(); // w:r
$this->writeEndingWP();
}
/**
* Write opening
*/
protected function writeOpeningWP()
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$this->withoutP) {
$xmlWriter->startElement('w:p');
if (method_exists($element, 'getParagraphStyle')) {
$this->writeParagraphStyle();
}
}
}
/**
* Write ending
*/
protected function writeEndingWP()
{
$xmlWriter = $this->getXmlWriter();
if (!$this->withoutP) {
$xmlWriter->endElement(); // w:p
}
}
/**
* Write ending
*/
protected function writeParagraphStyle()
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
$paragraphStyle = $element->getParagraphStyle();
$styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle);
$styleWriter->setIsInline(true);
$styleWriter->write(); $styleWriter->write();
} }
$styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:r'); /**
* Write ending
*/
protected function writeFontStyle()
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
$fontStyle = $element->getFontStyle();
$styleWriter = new FontStyleWriter($xmlWriter, $fontStyle);
$styleWriter->setIsInline(true);
$styleWriter->write(); $styleWriter->write();
$this->xmlWriter->startElement('w:t');
$this->xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->writeRaw($text);
$this->xmlWriter->endElement();
$this->xmlWriter->endElement(); // w:r
if (!$this->withoutP) {
$this->xmlWriter->endElement(); // w:p
}
} }
} }

View File

@ -17,50 +17,34 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element; namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
/** /**
* TextBreak element writer * TextBreak element writer
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class TextBreak extends Element class TextBreak extends Text
{ {
/** /**
* Write text break element * Write text break element
*/ */
public function write() public function write()
{ {
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$this->withoutP) { if (!$this->withoutP) {
$hasStyle = false; $hasStyle = $element->hasStyle();
$fontStyle = null;
$paragraphStyle = null;
if (!is_null($this->element)) {
$fontStyle = $this->element->getFontStyle();
$paragraphStyle = $this->element->getParagraphStyle();
$hasStyle = !is_null($fontStyle) || !is_null($paragraphStyle);
}
if ($hasStyle) { if ($hasStyle) {
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle); $this->writeOpeningWP();
$styleWriter->setIsInline(true); $xmlWriter->startElement('w:pPr');
$this->writeFontStyle();
$this->xmlWriter->startElement('w:p'); $xmlWriter->endElement(); // w:pPr
$styleWriter->write(); $this->writeEndingWP();
if (!is_null($fontStyle)) {
$styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:pPr');
$styleWriter->write();
$this->xmlWriter->endElement(); // w:pPr
}
$this->xmlWriter->endElement(); // w:p
} else { } else {
$this->xmlWriter->writeElement('w:p'); $xmlWriter->writeElement('w:p');
} }
} else { } else {
$this->xmlWriter->writeElement('w:br'); $xmlWriter->writeElement('w:br');
} }
} }
} }

View File

@ -24,24 +24,21 @@ use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class TextRun extends Element class TextRun extends Text
{ {
/** /**
* Write textrun element * Write textrun element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\TextRun) { $xmlWriter = $this->getXmlWriter();
return; $element = $this->getElement();
}
$paragraphStyle = $this->element->getParagraphStyle(); $this->writeOpeningWP();
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p'); $containerWriter = new Container($xmlWriter, $element);
$styleWriter->write(); $containerWriter->write();
$this->parentWriter->writeContainerElements($this->xmlWriter, $this->element);
$this->xmlWriter->endElement(); // w:p $this->writeEndingWP();
} }
} }

View File

@ -24,54 +24,54 @@ use PhpOffice\PhpWord\Shared\String;
* *
* @since 0.10.0 * @since 0.10.0
*/ */
class Title extends Element class Title extends AbstractElement
{ {
/** /**
* Write title element * Write title element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Title) { $xmlWriter = $this->getXmlWriter();
return; $element = $this->getElement();
}
$bookmarkId = $this->element->getBookmarkId(); $bookmarkId = $element->getBookmarkId();
$anchor = '_Toc' . ($bookmarkId + 252634154); $anchor = '_Toc' . ($bookmarkId + 252634154);
$style = $this->element->getStyle(); $style = $element->getStyle();
$text = htmlspecialchars($this->element->getText());
$text = htmlspecialchars($element->getText());
$text = String::controlCharacterPHP2OOXML($text); $text = String::controlCharacterPHP2OOXML($text);
$this->xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:p');
if (!empty($style)) { if (!empty($style)) {
$this->xmlWriter->startElement('w:pPr'); $xmlWriter->startElement('w:pPr');
$this->xmlWriter->startElement('w:pStyle'); $xmlWriter->startElement('w:pStyle');
$this->xmlWriter->writeAttribute('w:val', $style); $xmlWriter->writeAttribute('w:val', $style);
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:fldChar'); $xmlWriter->startElement('w:fldChar');
$this->xmlWriter->writeAttribute('w:fldCharType', 'end'); $xmlWriter->writeAttribute('w:fldCharType', 'end');
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->startElement('w:bookmarkStart'); $xmlWriter->startElement('w:bookmarkStart');
$this->xmlWriter->writeAttribute('w:id', $bookmarkId); $xmlWriter->writeAttribute('w:id', $bookmarkId);
$this->xmlWriter->writeAttribute('w:name', $anchor); $xmlWriter->writeAttribute('w:name', $anchor);
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:t'); $xmlWriter->startElement('w:t');
$this->xmlWriter->writeRaw($text); $xmlWriter->writeRaw($text);
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->startElement('w:bookmarkEnd'); $xmlWriter->startElement('w:bookmarkEnd');
$this->xmlWriter->writeAttribute('w:id', $bookmarkId); $xmlWriter->writeAttribute('w:id', $bookmarkId);
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
} }

View File

@ -17,11 +17,8 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Part; namespace PhpOffice\PhpWord\Writer\Word2007\Part;
use PhpOffice\PhpWord\Element\AbstractElement;
use PhpOffice\PhpWord\Element\TextBreak;
use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Writer\Word2007\Element\Element as ElementWriter;
use PhpOffice\PhpWord\Writer\WriterInterface; use PhpOffice\PhpWord\Writer\WriterInterface;
/** /**
@ -39,11 +36,11 @@ abstract class AbstractPart
/** /**
* Set parent writer * Set parent writer
* *
* @param \PhpOffice\PhpWord\Writer\WriterInterface $pWriter * @param \PhpOffice\PhpWord\Writer\WriterInterface $writer
*/ */
public function setParentWriter(WriterInterface $pWriter = null) public function setParentWriter(WriterInterface $writer = null)
{ {
$this->parentWriter = $pWriter; $this->parentWriter = $writer;
} }
/** /**
@ -57,7 +54,7 @@ abstract class AbstractPart
if (!is_null($this->parentWriter)) { if (!is_null($this->parentWriter)) {
return $this->parentWriter; return $this->parentWriter;
} else { } else {
throw new Exception("No parent WriterInterface assigned."); throw new Exception('No parent WriterInterface assigned.');
} }
} }
@ -80,49 +77,4 @@ abstract class AbstractPart
return new XMLWriter(XMLWriter::STORAGE_MEMORY); return new XMLWriter(XMLWriter::STORAGE_MEMORY);
} }
} }
/**
* Write container elements
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param \PhpOffice\PhpWord\Element\AbstractElement $container
*/
public function writeContainerElements(XMLWriter $xmlWriter, AbstractElement $container)
{
// Check allowed elements
$elmCommon = array('Text', 'Link', 'TextBreak', 'Image', 'Object');
$elmMainCell = array_merge($elmCommon, array('TextRun', 'ListItem', 'CheckBox'));
$allowedElements = array(
'Section' => array_merge($elmMainCell, array('Table', 'Footnote', 'Title', 'PageBreak', 'TOC', 'TextBox')),
'Header' => array_merge($elmMainCell, array('Table', 'PreserveText', 'TextBox')),
'Footer' => array_merge($elmMainCell, array('Table', 'PreserveText', 'TextBox')),
'Cell' => array_merge($elmMainCell, array('PreserveText', 'Footnote', 'Endnote')),
'TextBox' => array_merge($elmMainCell, array('PreserveText', 'Footnote', 'Endnote')),
'TextRun' => array_merge($elmCommon, array('Footnote', 'Endnote')),
'Footnote' => $elmCommon,
'Endnote' => $elmCommon,
);
$containerName = get_class($container);
$containerName = substr($containerName, strrpos($containerName, '\\') + 1);
if (!array_key_exists($containerName, $allowedElements)) {
throw new Exception('Invalid container.'.$containerName. print_r($allowedElements, true));
}
// Loop through elements
$elements = $container->getElements();
$withoutP = in_array($containerName, array('TextRun', 'Footnote', 'Endnote', 'TextBox')) ? true : false;
if (count($elements) > 0) {
foreach ($elements as $element) {
if ($element instanceof AbstractElement) {
$elementWriter = new ElementWriter($xmlWriter, $this, $element, $withoutP);
$elementWriter->write();
}
}
} else {
if ($containerName == 'Cell') {
$elementWriter = new ElementWriter($xmlWriter, $this, new TextBreak(), $withoutP);
$elementWriter->write();
}
}
}
} }

View File

@ -20,6 +20,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part;
use PhpOffice\PhpWord\Element\Section; use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Writer\Word2007\Element\Container;
use PhpOffice\PhpWord\Writer\Word2007\Style\Section as SectionStyleWriter; use PhpOffice\PhpWord\Writer\Word2007\Style\Section as SectionStyleWriter;
/** /**
@ -60,7 +61,10 @@ class Document extends AbstractPart
if ($sectionCount > 0) { if ($sectionCount > 0) {
foreach ($sections as $section) { foreach ($sections as $section) {
$currentSection++; $currentSection++;
$this->writeContainerElements($xmlWriter, $section);
$containerWriter = new Container($xmlWriter, $section);
$containerWriter->write();
if ($currentSection == $sectionCount) { if ($currentSection == $sectionCount) {
$this->writeSectionSettings($xmlWriter, $section); $this->writeSectionSettings($xmlWriter, $section);
} else { } else {

View File

@ -17,6 +17,8 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Part; namespace PhpOffice\PhpWord\Writer\Word2007\Part;
use PhpOffice\PhpWord\Writer\Word2007\Element\Container;
/** /**
* Word2007 footer part writer: word/footerx.xml * Word2007 footer part writer: word/footerx.xml
*/ */
@ -58,7 +60,8 @@ class Footer extends AbstractPart
$xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
$xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml'); $xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml');
$this->writeContainerElements($xmlWriter, $this->element); $containerWriter = new Container($xmlWriter, $this->element);
$containerWriter->write();
$xmlWriter->endElement(); // $this->rootElement $xmlWriter->endElement(); // $this->rootElement

View File

@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part;
use PhpOffice\PhpWord\Element\Footnote; use PhpOffice\PhpWord\Element\Footnote;
use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Writer\Word2007\Element\Container;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter; use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
/** /**
@ -165,7 +166,8 @@ class Footnotes extends AbstractPart
$xmlWriter->endElement(); // w:t $xmlWriter->endElement(); // w:t
$xmlWriter->endElement(); // w:r $xmlWriter->endElement(); // w:r
$this->writeContainerElements($xmlWriter, $element); $containerWriter = new Container($xmlWriter, $element);
$containerWriter->write();
$xmlWriter->endElement(); // w:p $xmlWriter->endElement(); // w:p
$xmlWriter->endElement(); // $this->elementNode $xmlWriter->endElement(); // $this->elementNode

View File

@ -27,11 +27,6 @@ use PhpOffice\PhpWord\Shared\XMLWriter;
*/ */
class Rels extends AbstractPart class Rels extends AbstractPart
{ {
/**
* Base relationship URL
*/
const RELS_BASE = 'http://schemas.openxmlformats.org/';
/** /**
* Write part * Write part
* *
@ -62,7 +57,7 @@ class Rels extends AbstractPart
{ {
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); $xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
$xmlWriter->startElement('Relationships'); $xmlWriter->startElement('Relationships');
$xmlWriter->writeAttribute('xmlns', self::RELS_BASE . 'package/2006/relationships'); $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
// XML files relationships // XML files relationships
if (is_array($xmlRels)) { if (is_array($xmlRels)) {
@ -73,17 +68,18 @@ class Rels extends AbstractPart
// Media relationships // Media relationships
if (is_array($mediaRels)) { if (is_array($mediaRels)) {
$mapping = array('image' => 'image', 'object' => 'oleObject', 'link' => 'hyperlink'); $typePrefix = 'officeDocument/2006/relationships/';
$typeMapping = array('image' => 'image', 'object' => 'oleObject', 'link' => 'hyperlink');
$targetPaths = array('image' => 'media/', 'object' => 'embeddings/'); $targetPaths = array('image' => 'media/', 'object' => 'embeddings/');
foreach ($mediaRels as $mediaRel) { foreach ($mediaRels as $mediaRel) {
$mediaType = $mediaRel['type']; $mediaType = $mediaRel['type'];
$type = array_key_exists($mediaType, $mapping) ? $mapping[$mediaType] : $mediaType; $type = array_key_exists($mediaType, $typeMapping) ? $typeMapping[$mediaType] : $mediaType;
$target = array_key_exists($mediaType, $targetPaths) ? $targetPaths[$mediaType] : ''; $target = array_key_exists($mediaType, $targetPaths) ? $targetPaths[$mediaType] : '';
$target .= $mediaRel['target']; $target .= $mediaRel['target'];
$targetMode = ($type == 'hyperlink') ? 'External' : ''; $targetMode = ($type == 'hyperlink') ? 'External' : '';
$type = "officeDocument/2006/relationships/{$type}";
$this->writeRel($xmlWriter, $relId++, $type, $target, $targetMode); $this->writeRel($xmlWriter, $relId++, $typePrefix . $type, $target, $targetMode);
} }
} }
@ -110,7 +106,7 @@ class Rels extends AbstractPart
} }
$xmlWriter->startElement('Relationship'); $xmlWriter->startElement('Relationship');
$xmlWriter->writeAttribute('Id', $relId); $xmlWriter->writeAttribute('Id', $relId);
$xmlWriter->writeAttribute('Type', self::RELS_BASE . $type); $xmlWriter->writeAttribute('Type', 'http://schemas.openxmlformats.org/' . $type);
$xmlWriter->writeAttribute('Target', $target); $xmlWriter->writeAttribute('Target', $target);
if ($targetMode != '') { if ($targetMode != '') {
$xmlWriter->writeAttribute('TargetMode', $targetMode); $xmlWriter->writeAttribute('TargetMode', $targetMode);

View File

@ -17,6 +17,7 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Style; namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Shared\XMLWriter;
@ -32,10 +33,10 @@ abstract class AbstractStyle
* *
* @var \PhpOffice\PhpWord\Shared\XMLWriter * @var \PhpOffice\PhpWord\Shared\XMLWriter
*/ */
protected $xmlWriter; private $xmlWriter;
/** /**
* Style * Style; set protected for a while
* *
* @var string|\PhpOffice\PhpWord\Style\AbstractStyle * @var string|\PhpOffice\PhpWord\Style\AbstractStyle
*/ */
@ -57,6 +58,33 @@ abstract class AbstractStyle
$this->style = $style; $this->style = $style;
} }
/**
* Get XML Writer
*
* @return \PhpOffice\PhpWord\Shared\XMLWriter
*/
protected function getXmlWriter()
{
return $this->xmlWriter;
}
/**
* Get Style
*
* @return \PhpOffice\PhpWord\Style\AbstractStyle
*/
protected function getStyle()
{
if (!is_null($this->style)) {
$styleClass = 'PhpOffice\\PhpWord\\Style\\' . basename(get_class($this->style));
if (is_object($this->style) && (!$this->style instanceof $styleClass)) {
throw new Exception('No valid style assigned.');
}
}
return $this->style;
}
/** /**
* Convert twip value * Convert twip value
* *
@ -73,27 +101,4 @@ abstract class AbstractStyle
return $value * $unit; return $value * $unit;
} }
} }
/**
* Write element when ...
*
* @param bool $condition
* @param string $element
* @param string $attribute
* @param string $value
*/
protected function writeElementIf($condition, $element, $attribute = null, $value = null)
{
if (!$condition) {
return;
}
if (is_null($attribute)) {
$this->xmlWriter->writeElement($element, $value);
} else {
$this->xmlWriter->startElement($element);
$this->xmlWriter->writeAttribute($attribute, $value);
$this->xmlWriter->endElement();
}
}
} }

View File

@ -31,64 +31,42 @@ class Cell extends AbstractStyle
*/ */
public function write() public function write()
{ {
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Cell)) { if (is_null($style = $this->getStyle())) {
return; return;
} }
$xmlWriter = $this->getXmlWriter();
$brdSz = $this->style->getBorderSize();
$brdCol = $this->style->getBorderColor();
$hasBorders = false;
for ($i = 0; $i < 4; $i++) {
if (!is_null($brdSz[$i])) {
$hasBorders = true;
break;
}
}
// Border
if ($hasBorders) {
$mbWriter = new MarginBorder($this->xmlWriter);
$mbWriter->setSizes($brdSz);
$mbWriter->setColors($brdCol);
$mbWriter->setAttributes(array('defaultColor' => CellStyle::DEFAULT_BORDER_COLOR));
$this->xmlWriter->startElement('w:tcBorders');
$mbWriter->write();
$this->xmlWriter->endElement();
}
// Text direction // Text direction
if (!is_null($this->style->getTextDirection())) { $textDir = $style->getTextDirection();
$this->xmlWriter->startElement('w:textDirection'); $xmlWriter->writeElementIf(!is_null($textDir), 'w:textDirection', 'w:val', $textDir);
$this->xmlWriter->writeAttribute('w:val', $this->style->getTextDirection());
$this->xmlWriter->endElement(); // Vertical alignment
$vAlign = $style->getVAlign();
$xmlWriter->writeElementIf(!is_null($vAlign), 'w:vAlign', 'w:val', $vAlign);
// Border
if ($style->hasBorders()) {
$xmlWriter->startElement('w:tcBorders');
$styleWriter = new MarginBorder($xmlWriter);
$styleWriter->setSizes($style->getBorderSize());
$styleWriter->setColors($style->getBorderColor());
$styleWriter->setAttributes(array('defaultColor' => CellStyle::DEFAULT_BORDER_COLOR));
$styleWriter->write();
$xmlWriter->endElement();
} }
// Shading // Shading
if (!is_null($this->style->getShading())) { if (!is_null($style->getShading())) {
$styleWriter = new Shading($this->xmlWriter, $this->style->getShading()); $styleWriter = new Shading($xmlWriter, $style->getShading());
$styleWriter->write(); $styleWriter->write();
} }
// Alignment // Colspan & rowspan
if (!is_null($this->style->getVAlign())) { $gridSpan = $style->getGridSpan();
$this->xmlWriter->startElement('w:vAlign'); $vMerge = $style->getVMerge();
$this->xmlWriter->writeAttribute('w:val', $this->style->getVAlign()); $xmlWriter->writeElementIf(!is_null($gridSpan), 'w:gridSpan', 'w:val', $gridSpan);
$this->xmlWriter->endElement(); $xmlWriter->writeElementIf(!is_null($vMerge), 'w:vMerge', 'w:val', $vMerge);
}
// Colspan
if (!is_null($this->style->getGridSpan())) {
$this->xmlWriter->startElement('w:gridSpan');
$this->xmlWriter->writeAttribute('w:val', $this->style->getGridSpan());
$this->xmlWriter->endElement();
}
// Row span
if (!is_null($this->style->getVMerge())) {
$this->xmlWriter->startElement('w:vMerge');
$this->xmlWriter->writeAttribute('w:val', $this->style->getVMerge());
$this->xmlWriter->endElement();
}
} }
} }

View File

@ -38,13 +38,15 @@ class Font extends AbstractStyle
*/ */
public function write() public function write()
{ {
$xmlWriter = $this->getXmlWriter();
$isStyleName = $this->isInline && !is_null($this->style) && is_string($this->style); $isStyleName = $this->isInline && !is_null($this->style) && is_string($this->style);
if ($isStyleName) { if ($isStyleName) {
$this->xmlWriter->startElement('w:rPr'); $xmlWriter->startElement('w:rPr');
$this->xmlWriter->startElement('w:rStyle'); $xmlWriter->startElement('w:rStyle');
$this->xmlWriter->writeAttribute('w:val', $this->style); $xmlWriter->writeAttribute('w:val', $this->style);
$this->xmlWriter->endElement(); $xmlWriter->endElement();
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} else { } else {
$this->writeStyle(); $this->writeStyle();
} }
@ -55,67 +57,65 @@ class Font extends AbstractStyle
*/ */
private function writeStyle() private function writeStyle()
{ {
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Font)) { if (is_null($style = $this->getStyle())) {
return; return;
} }
$xmlWriter = $this->getXmlWriter();
$font = $style->getName();
$color = $style->getColor();
$size = $style->getSize();
$underline = $style->getUnderline();
$fgColor = $style->getFgColor();
$hint = $style->getHint();
$font = $this->style->getName(); $xmlWriter->startElement('w:rPr');
$color = $this->style->getColor();
$size = $this->style->getSize();
$underline = $this->style->getUnderline();
$fgColor = $this->style->getFgColor();
$this->xmlWriter->startElement('w:rPr');
// Font name/family // Font name/family
if ($font != PhpWord::DEFAULT_FONT_NAME) { if ($font != PhpWord::DEFAULT_FONT_NAME) {
$this->xmlWriter->startElement('w:rFonts'); $xmlWriter->startElement('w:rFonts');
$this->xmlWriter->writeAttribute('w:ascii', $font); $xmlWriter->writeAttribute('w:ascii', $font);
$this->xmlWriter->writeAttribute('w:hAnsi', $font); $xmlWriter->writeAttribute('w:hAnsi', $font);
$this->xmlWriter->writeAttribute('w:eastAsia', $font); $xmlWriter->writeAttribute('w:eastAsia', $font);
$this->xmlWriter->writeAttribute('w:cs', $font); $xmlWriter->writeAttribute('w:cs', $font);
//Font Content Type $xmlWriter->writeAttributeIf($hint != PhpWord::DEFAULT_FONT_CONTENT_TYPE, 'w:hint', $hint);
if ($this->style->getHint() != PhpWord::DEFAULT_FONT_CONTENT_TYPE) { $xmlWriter->endElement();
$this->xmlWriter->writeAttribute('w:hint', $this->style->getHint());
}
$this->xmlWriter->endElement();
} }
// Color // Color
$this->writeElementIf($color != PhpWord::DEFAULT_FONT_COLOR, 'w:color', 'w:val', $color); $xmlWriter->writeElementIf($color != PhpWord::DEFAULT_FONT_COLOR, 'w:color', 'w:val', $color);
$this->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);
$this->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);
// Bold, italic // Bold, italic
$this->writeElementIf($this->style->isBold(), 'w:b'); $xmlWriter->writeElementIf($style->isBold(), 'w:b');
$this->writeElementIf($this->style->isItalic(), 'w:i'); $xmlWriter->writeElementIf($style->isItalic(), 'w:i');
$this->writeElementIf($this->style->isItalic(), 'w:iCs'); $xmlWriter->writeElementIf($style->isItalic(), 'w:iCs');
// Strikethrough, double strikethrough // Strikethrough, double strikethrough
$this->writeElementIf($this->style->isStrikethrough(), 'w:strike'); $xmlWriter->writeElementIf($style->isStrikethrough(), 'w:strike');
$this->writeElementIf($this->style->isDoubleStrikethrough(), 'w:dstrike'); $xmlWriter->writeElementIf($style->isDoubleStrikethrough(), 'w:dstrike');
// Small caps, all caps // Small caps, all caps
$this->writeElementIf($this->style->isSmallCaps(), 'w:smallCaps'); $xmlWriter->writeElementIf($style->isSmallCaps(), 'w:smallCaps');
$this->writeElementIf($this->style->isAllCaps(), 'w:caps'); $xmlWriter->writeElementIf($style->isAllCaps(), 'w:caps');
// Underline // Underline
$this->writeElementIf($underline != 'none', 'w:u', 'w:val', $underline); $xmlWriter->writeElementIf($underline != 'none', 'w:u', 'w:val', $underline);
// Foreground-Color // Foreground-Color
$this->writeElementIf(!is_null($fgColor), 'w:highlight', 'w:val', $fgColor); $xmlWriter->writeElementIf(!is_null($fgColor), 'w:highlight', 'w:val', $fgColor);
// Superscript/subscript // Superscript/subscript
$this->writeElementIf($this->style->isSuperScript(), 'w:vertAlign', 'w:val', 'superscript'); $xmlWriter->writeElementIf($style->isSuperScript(), 'w:vertAlign', 'w:val', 'superscript');
$this->writeElementIf($this->style->isSubScript(), 'w:vertAlign', 'w:val', 'subscript'); $xmlWriter->writeElementIf($style->isSubScript(), 'w:vertAlign', 'w:val', 'subscript');
// Background-Color // Background-Color
if (!is_null($this->style->getShading())) { if (!is_null($style->getShading())) {
$styleWriter = new Shading($this->xmlWriter, $this->style->getShading()); $styleWriter = new Shading($xmlWriter, $style->getShading());
$styleWriter->write(); $styleWriter->write();
} }
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
/** /**

View File

@ -38,12 +38,12 @@ class Image extends AbstractStyle
*/ */
public function write() public function write()
{ {
if (!$this->style instanceof \PhpOffice\PhpWord\Style\Image) { if (is_null($style = $this->getStyle())) {
return; return;
} }
$xmlWriter = $this->getXmlWriter();
$wrapping = $this->style->getWrappingStyle(); $wrapping = $style->getWrappingStyle();
$positioning = $this->style->getPositioning(); $positioning = $style->getPositioning();
// Default style array // Default style array
$styleArray = array( $styleArray = array(
@ -60,10 +60,10 @@ class Image extends AbstractStyle
$styleArray['mso-position-horizontal-relative'] = 'page'; $styleArray['mso-position-horizontal-relative'] = 'page';
$styleArray['mso-position-vertical-relative'] = 'page'; $styleArray['mso-position-vertical-relative'] = 'page';
} elseif ($positioning == ImageStyle::POSITION_RELATIVE) { } elseif ($positioning == ImageStyle::POSITION_RELATIVE) {
$styleArray['mso-position-horizontal'] = $this->style->getPosHorizontal(); $styleArray['mso-position-horizontal'] = $style->getPosHorizontal();
$styleArray['mso-position-vertical'] = $this->style->getPosVertical(); $styleArray['mso-position-vertical'] = $style->getPosVertical();
$styleArray['mso-position-horizontal-relative'] = $this->style->getPosHorizontalRel(); $styleArray['mso-position-horizontal-relative'] = $style->getPosHorizontalRel();
$styleArray['mso-position-vertical-relative'] = $this->style->getPosVerticalRel(); $styleArray['mso-position-vertical-relative'] = $style->getPosVerticalRel();
$styleArray['margin-left'] = 0; $styleArray['margin-left'] = 0;
$styleArray['margin-top'] = 0; $styleArray['margin-top'] = 0;
} }
@ -88,7 +88,7 @@ class Image extends AbstractStyle
$imageStyle = $this->assembleStyle($styleArray); $imageStyle = $this->assembleStyle($styleArray);
$this->xmlWriter->writeAttribute('style', $imageStyle); $xmlWriter->writeAttribute('style', $imageStyle);
} }
/** /**
@ -98,10 +98,12 @@ class Image extends AbstractStyle
*/ */
public function writeW10Wrap() public function writeW10Wrap()
{ {
$xmlWriter = $this->getXmlWriter();
if (!is_null($this->w10wrap)) { if (!is_null($this->w10wrap)) {
$this->xmlWriter->startElement('w10:wrap'); $xmlWriter->startElement('w10:wrap');
$this->xmlWriter->writeAttribute('type', $this->w10wrap); $xmlWriter->writeAttribute('type', $this->w10wrap);
$this->xmlWriter->endElement(); // w10:wrap $xmlWriter->endElement(); // w10:wrap
} }
} }

View File

@ -29,19 +29,18 @@ class Indentation extends AbstractStyle
*/ */
public function write() public function write()
{ {
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Indentation)) { if (is_null($style = $this->getStyle())) {
return; return;
} }
$xmlWriter = $this->getXmlWriter();
$firstLine = $style->getFirstLine();
$hanging = $style->getHanging();
$this->xmlWriter->startElement('w:ind'); $xmlWriter->startElement('w:ind');
$this->xmlWriter->writeAttribute('w:left', $this->convertTwip($this->style->getLeft())); $xmlWriter->writeAttribute('w:left', $this->convertTwip($style->getLeft()));
$this->xmlWriter->writeAttribute('w:right', $this->convertTwip($this->style->getRight())); $xmlWriter->writeAttribute('w:right', $this->convertTwip($style->getRight()));
if (!is_null($this->style->getFirstLine())) { $xmlWriter->writeAttributeIf(!is_null($firstLine), 'w:firstLine', $this->convertTwip($firstLine));
$this->xmlWriter->writeAttribute('w:firstLine', $this->convertTwip($this->style->getFirstLine())); $xmlWriter->writeAttributeIf(!is_null($hanging), 'w:hanging', $this->convertTwip($hanging));
} $xmlWriter->endElement();
if (!is_null($this->style->getHanging())) {
$this->xmlWriter->writeAttribute('w:hanging', $this->convertTwip($this->style->getHanging()));
}
$this->xmlWriter->endElement();
} }
} }

View File

@ -31,15 +31,16 @@ class LineNumbering extends AbstractStyle
*/ */
public function write() public function write()
{ {
if (!($this->style instanceof \PhpOffice\PhpWord\Style\LineNumbering)) { if (is_null($style = $this->getStyle())) {
return; return;
} }
$xmlWriter = $this->getXmlWriter();
$this->xmlWriter->startElement('w:lnNumType'); $xmlWriter->startElement('w:lnNumType');
$this->xmlWriter->writeAttribute('w:start', $this->style->getStart() - 1); $xmlWriter->writeAttribute('w:start', $style->getStart() - 1);
$this->xmlWriter->writeAttribute('w:countBy', $this->style->getIncrement()); $xmlWriter->writeAttribute('w:countBy', $style->getIncrement());
$this->xmlWriter->writeAttribute('w:distance', $this->style->getDistance()); $xmlWriter->writeAttribute('w:distance', $style->getDistance());
$this->xmlWriter->writeAttribute('w:restart', $this->style->getRestart()); $xmlWriter->writeAttribute('w:restart', $style->getRestart());
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
} }

View File

@ -50,31 +50,33 @@ class MarginBorder extends AbstractStyle
*/ */
public function write() public function write()
{ {
$xmlWriter = $this->getXmlWriter();
$sides = array('top', 'left', 'right', 'bottom', 'insideH', 'insideV'); $sides = array('top', 'left', 'right', 'bottom', 'insideH', 'insideV');
$sizeCount = count($this->sizes) - 1; $sizeCount = count($this->sizes) - 1;
for ($i = 0; $i < $sizeCount; $i++) { for ($i = 0; $i < $sizeCount; $i++) {
if (!is_null($this->sizes[$i])) { if (!is_null($this->sizes[$i])) {
$this->xmlWriter->startElement('w:' . $sides[$i]); $xmlWriter->startElement('w:' . $sides[$i]);
if (!empty($this->colors)) { if (!empty($this->colors)) {
if (is_null($this->colors[$i]) && !empty($this->attributes)) { if (is_null($this->colors[$i]) && !empty($this->attributes)) {
if (array_key_exists('defaultColor', $this->attributes)) { if (array_key_exists('defaultColor', $this->attributes)) {
$this->colors[$i] = $this->attributes['defaultColor']; $this->colors[$i] = $this->attributes['defaultColor'];
} }
} }
$this->xmlWriter->writeAttribute('w:val', 'single'); $xmlWriter->writeAttribute('w:val', 'single');
$this->xmlWriter->writeAttribute('w:sz', $this->sizes[$i]); $xmlWriter->writeAttribute('w:sz', $this->sizes[$i]);
$this->xmlWriter->writeAttribute('w:color', $this->colors[$i]); $xmlWriter->writeAttribute('w:color', $this->colors[$i]);
if (!empty($this->attributes)) { if (!empty($this->attributes)) {
if (array_key_exists('space', $this->attributes)) { if (array_key_exists('space', $this->attributes)) {
$this->xmlWriter->writeAttribute('w:space', $this->attributes['space']); $xmlWriter->writeAttribute('w:space', $this->attributes['space']);
} }
} }
} else { } else {
$this->xmlWriter->writeAttribute('w:w', $this->sizes[$i]); $xmlWriter->writeAttribute('w:w', $this->sizes[$i]);
$this->xmlWriter->writeAttribute('w:type', 'dxa'); $xmlWriter->writeAttribute('w:type', 'dxa');
} }
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
} }
} }

View File

@ -43,16 +43,18 @@ class Paragraph extends AbstractStyle
*/ */
public function write() public function write()
{ {
$xmlWriter = $this->getXmlWriter();
$isStyleName = $this->isInline && !is_null($this->style) && is_string($this->style); $isStyleName = $this->isInline && !is_null($this->style) && is_string($this->style);
if ($isStyleName) { if ($isStyleName) {
if (!$this->withoutPPR) { if (!$this->withoutPPR) {
$this->xmlWriter->startElement('w:pPr'); $xmlWriter->startElement('w:pPr');
} }
$this->xmlWriter->startElement('w:pStyle'); $xmlWriter->startElement('w:pStyle');
$this->xmlWriter->writeAttribute('w:val', $this->style); $xmlWriter->writeAttribute('w:val', $this->style);
$this->xmlWriter->endElement(); $xmlWriter->endElement();
if (!$this->withoutPPR) { if (!$this->withoutPPR) {
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
} else { } else {
$this->writeStyle(); $this->writeStyle();
@ -64,52 +66,52 @@ class Paragraph extends AbstractStyle
*/ */
private function writeStyle() private function writeStyle()
{ {
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Paragraph)) { if (is_null($style = $this->getStyle())) {
return; return;
} }
$xmlWriter = $this->getXmlWriter();
$align = $this->style->getAlign(); $align = $style->getAlign();
$indentation = $this->style->getIndentation(); $indentation = $style->getIndentation();
$spacing = $this->style->getSpace(); $spacing = $style->getSpace();
$tabs = $this->style->getTabs(); $tabs = $style->getTabs();
if (!$this->withoutPPR) { if (!$this->withoutPPR) {
$this->xmlWriter->startElement('w:pPr'); $xmlWriter->startElement('w:pPr');
} }
// Alignment // Alignment
$this->writeElementIf(!is_null($align), 'w:jc', 'w:val', $align); $xmlWriter->writeElementIf(!is_null($align), 'w:jc', 'w:val', $align);
// Pagination // Pagination
$this->writeElementIf(!$this->style->hasWidowControl(), 'w:widowControl', 'w:val', '0'); $xmlWriter->writeElementIf(!$style->hasWidowControl(), 'w:widowControl', 'w:val', '0');
$this->writeElementIf($this->style->isKeepNext(), 'w:keepNext', 'w:val', '1'); $xmlWriter->writeElementIf($style->isKeepNext(), 'w:keepNext', 'w:val', '1');
$this->writeElementIf($this->style->isKeepLines(), 'w:keepLines', 'w:val', '1'); $xmlWriter->writeElementIf($style->isKeepLines(), 'w:keepLines', 'w:val', '1');
$this->writeElementIf($this->style->hasPageBreakBefore(), 'w:pageBreakBefore', 'w:val', '1'); $xmlWriter->writeElementIf($style->hasPageBreakBefore(), 'w:pageBreakBefore', 'w:val', '1');
// Indentation // Indentation
if (!is_null($indentation)) { if (!is_null($indentation)) {
$styleWriter = new Indentation($this->xmlWriter, $indentation); $styleWriter = new Indentation($xmlWriter, $indentation);
$styleWriter->write(); $styleWriter->write();
} }
// Spacing // Spacing
if (!is_null($spacing)) { if (!is_null($spacing)) {
$styleWriter = new Spacing($this->xmlWriter, $spacing); $styleWriter = new Spacing($xmlWriter, $spacing);
$styleWriter->write(); $styleWriter->write();
} }
// Tabs // Tabs
if (!empty($tabs)) { if (!empty($tabs)) {
$this->xmlWriter->startElement("w:tabs"); $xmlWriter->startElement("w:tabs");
foreach ($tabs as $tab) { foreach ($tabs as $tab) {
$styleWriter = new Tab($this->xmlWriter, $tab); $styleWriter = new Tab($xmlWriter, $tab);
$styleWriter->write(); $styleWriter->write();
} }
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
if (!$this->withoutPPR) { if (!$this->withoutPPR) {
$this->xmlWriter->endElement(); // w:pPr $xmlWriter->endElement(); // w:pPr
} }
} }

View File

@ -31,23 +31,21 @@ class Section extends AbstractStyle
*/ */
public function write() public function write()
{ {
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Section)) { if (is_null($style = $this->getStyle())) {
return; return;
} }
$xmlWriter = $this->getXmlWriter();
// Section break // Break type
if (!is_null($this->style->getBreakType())) { $breakType = $style->getBreakType();
$this->xmlWriter->startElement('w:type'); $xmlWriter->writeElementIf(!is_null($breakType), 'w:type', 'w:val', $breakType);
$this->xmlWriter->writeAttribute('w:val', $this->style->getBreakType());
$this->xmlWriter->endElement();
}
// Page size & orientation // Page size & orientation
$this->xmlWriter->startElement('w:pgSz'); $xmlWriter->startElement('w:pgSz');
$this->xmlWriter->writeAttribute('w:orient', $this->style->getOrientation()); $xmlWriter->writeAttribute('w:orient', $style->getOrientation());
$this->xmlWriter->writeAttribute('w:w', $this->style->getPageSizeW()); $xmlWriter->writeAttribute('w:w', $style->getPageSizeW());
$this->xmlWriter->writeAttribute('w:h', $this->style->getPageSizeH()); $xmlWriter->writeAttribute('w:h', $style->getPageSizeH());
$this->xmlWriter->endElement(); // w:pgSz $xmlWriter->endElement(); // w:pgSz
// Margins // Margins
$margins = array( $margins = array(
@ -59,52 +57,40 @@ class Section extends AbstractStyle
'w:footer' => array('getFooterHeight', SectionStyle::DEFAULT_FOOTER_HEIGHT), 'w:footer' => array('getFooterHeight', SectionStyle::DEFAULT_FOOTER_HEIGHT),
'w:gutter' => array('getGutter', SectionStyle::DEFAULT_GUTTER), 'w:gutter' => array('getGutter', SectionStyle::DEFAULT_GUTTER),
); );
$this->xmlWriter->startElement('w:pgMar'); $xmlWriter->startElement('w:pgMar');
foreach ($margins as $attribute => $value) { foreach ($margins as $attribute => $value) {
list($method, $default) = $value; list($method, $default) = $value;
$this->xmlWriter->writeAttribute($attribute, $this->convertTwip($this->style->$method(), $default)); $xmlWriter->writeAttribute($attribute, $this->convertTwip($style->$method(), $default));
} }
$this->xmlWriter->endElement(); $xmlWriter->endElement();
// Borders // Borders
$borders = $this->style->getBorderSize(); if ($style->hasBorders()) {
$hasBorders = false; $xmlWriter->startElement('w:pgBorders');
for ($i = 0; $i < 4; $i++) { $xmlWriter->writeAttribute('w:offsetFrom', 'page');
if (!is_null($borders[$i])) {
$hasBorders = true; $styleWriter = new MarginBorder($xmlWriter);
break; $styleWriter->setSizes($style->getBorderSize());
} $styleWriter->setColors($style->getBorderColor());
}
if ($hasBorders) {
$styleWriter = new MarginBorder($this->xmlWriter);
$styleWriter->setSizes($borders);
$styleWriter->setColors($this->style->getBorderColor());
$styleWriter->setAttributes(array('space' => '24')); $styleWriter->setAttributes(array('space' => '24'));
$this->xmlWriter->startElement('w:pgBorders');
$this->xmlWriter->writeAttribute('w:offsetFrom', 'page');
$styleWriter->write(); $styleWriter->write();
$this->xmlWriter->endElement();
}
// Page numbering $xmlWriter->endElement();
if (!is_null($this->style->getPageNumberingStart())) {
$this->xmlWriter->startElement('w:pgNumType');
$this->xmlWriter->writeAttribute('w:start', $this->style->getPageNumberingStart());
$this->xmlWriter->endElement();
} }
// Columns // Columns
$this->xmlWriter->startElement('w:cols'); $colsSpace = $style->getColsSpace();
$this->xmlWriter->writeAttribute('w:num', $this->style->getColsNum()); $xmlWriter->startElement('w:cols');
$this->xmlWriter->writeAttribute('w:space', $this->convertTwip( $xmlWriter->writeAttribute('w:num', $style->getColsNum());
$this->style->getColsSpace(), $xmlWriter->writeAttribute('w:space', $this->convertTwip($colsSpace, SectionStyle::DEFAULT_COLUMN_SPACING));
SectionStyle::DEFAULT_COLUMN_SPACING $xmlWriter->endElement();
));
$this->xmlWriter->endElement(); // Page numbering start
$pageNum = $style->getPageNumberingStart();
$xmlWriter->writeElementIf(!is_null($pageNum), 'w:pgNumType', 'w:start', $pageNum);
// Line numbering // Line numbering
$styleWriter = new LineNumbering($this->xmlWriter, $this->style->getLineNumbering()); $styleWriter = new LineNumbering($xmlWriter, $style->getLineNumbering());
$styleWriter->write(); $styleWriter->write();
} }
} }

View File

@ -29,14 +29,15 @@ class Shading extends AbstractStyle
*/ */
public function write() public function write()
{ {
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Shading)) { if (is_null($style = $this->getStyle())) {
return; return;
} }
$xmlWriter = $this->getXmlWriter();
$this->xmlWriter->startElement('w:shd'); $xmlWriter->startElement('w:shd');
$this->xmlWriter->writeAttribute('w:val', $this->style->getPattern()); $xmlWriter->writeAttribute('w:val', $style->getPattern());
$this->xmlWriter->writeAttribute('w:color', $this->style->getColor()); $xmlWriter->writeAttribute('w:color', $style->getColor());
$this->xmlWriter->writeAttribute('w:fill', $this->style->getFill()); $xmlWriter->writeAttribute('w:fill', $style->getFill());
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
} }

View File

@ -29,21 +29,21 @@ class Spacing extends AbstractStyle
*/ */
public function write() public function write()
{ {
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Spacing)) { if (is_null($style = $this->getStyle())) {
return; return;
} }
$xmlWriter = $this->getXmlWriter();
$before = $style->getBefore();
$after = $style->getAfter();
$line = $style->getLine();
$this->xmlWriter->startElement('w:spacing'); $xmlWriter->startElement('w:spacing');
if (!is_null($this->style->getBefore())) {
$this->xmlWriter->writeAttribute('w:before', $this->convertTwip($this->style->getBefore())); $xmlWriter->writeAttributeIf(!is_null($before), 'w:before', $this->convertTwip($before));
} $xmlWriter->writeAttributeIf(!is_null($after), 'w:after', $this->convertTwip($after));
if (!is_null($this->style->getAfter())) { $xmlWriter->writeAttributeIf(!is_null($line), 'w:line', $line);
$this->xmlWriter->writeAttribute('w:after', $this->convertTwip($this->style->getAfter())); $xmlWriter->writeAttributeIf(!is_null($line), 'w:lineRule', $style->getRule());
}
if (!is_null($this->style->getLine())) { $xmlWriter->endElement();
$this->xmlWriter->writeAttribute('w:line', $this->style->getLine());
$this->xmlWriter->writeAttribute('w:lineRule', $this->style->getRule());
}
$this->xmlWriter->endElement();
} }
} }

View File

@ -29,14 +29,15 @@ class Tab extends AbstractStyle
*/ */
public function write() public function write()
{ {
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Tab)) { if (is_null($style = $this->getStyle())) {
return; return;
} }
$xmlWriter = $this->getXmlWriter();
$this->xmlWriter->startElement("w:tab"); $xmlWriter->startElement("w:tab");
$this->xmlWriter->writeAttribute("w:val", $this->style->getType()); $xmlWriter->writeAttribute("w:val", $style->getType());
$this->xmlWriter->writeAttribute("w:leader", $this->style->getLeader()); $xmlWriter->writeAttribute("w:leader", $style->getLeader());
$this->xmlWriter->writeAttribute('w:pos', $this->convertTwip($this->style->getPosition())); $xmlWriter->writeAttribute('w:pos', $this->convertTwip($style->getPosition()));
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
} }

View File

@ -36,47 +36,47 @@ class Table extends AbstractStyle
*/ */
public function write() public function write()
{ {
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Table)) { if (is_null($style = $this->getStyle())) {
return; return;
} }
$xmlWriter = $this->getXmlWriter();
$hasBorders = $this->style->hasBorders(); $hasBorders = $style->hasBorders();
$hasMargins = $this->style->hasMargins(); $hasMargins = $style->hasMargins();
if ($hasMargins || $hasBorders) { if ($hasMargins || $hasBorders) {
$this->xmlWriter->startElement('w:tblPr'); $xmlWriter->startElement('w:tblPr');
if ($hasMargins) { if ($hasMargins) {
$mbWriter = new MarginBorder($this->xmlWriter); $mbWriter = new MarginBorder($xmlWriter);
$mbWriter->setSizes($this->style->getCellMargin()); $mbWriter->setSizes($style->getCellMargin());
$this->xmlWriter->startElement('w:tblCellMar'); $xmlWriter->startElement('w:tblCellMar');
$mbWriter->write(); $mbWriter->write();
$this->xmlWriter->endElement(); // w:tblCellMar $xmlWriter->endElement(); // w:tblCellMar
} }
if ($hasBorders) { if ($hasBorders) {
$mbWriter = new MarginBorder($this->xmlWriter); $mbWriter = new MarginBorder($xmlWriter);
$mbWriter->setSizes($this->style->getBorderSize()); $mbWriter->setSizes($style->getBorderSize());
$mbWriter->setColors($this->style->getBorderColor()); $mbWriter->setColors($style->getBorderColor());
$this->xmlWriter->startElement('w:tblBorders'); $xmlWriter->startElement('w:tblBorders');
$mbWriter->write(); $mbWriter->write();
$this->xmlWriter->endElement(); // w:tblBorders $xmlWriter->endElement(); // w:tblBorders
} }
$this->xmlWriter->endElement(); // w:tblPr $xmlWriter->endElement(); // w:tblPr
} }
// Only write background color and first row for full style // Only write background color and first row for full style
if ($this->isFullStyle) { if ($this->isFullStyle) {
// Background color // Background color
if (!is_null($this->style->getShading())) { if (!is_null($style->getShading())) {
$this->xmlWriter->startElement('w:tcPr'); $xmlWriter->startElement('w:tcPr');
$styleWriter = new Shading($this->xmlWriter, $this->style->getShading()); $styleWriter = new Shading($xmlWriter, $style->getShading());
$styleWriter->write(); $styleWriter->write();
$this->xmlWriter->endElement(); $xmlWriter->endElement();
} }
// First Row // First Row
$firstRow = $this->style->getFirstRow(); $firstRow = $style->getFirstRow();
if ($firstRow instanceof \PhpOffice\PhpWord\Style\Table) { if ($firstRow instanceof \PhpOffice\PhpWord\Style\Table) {
$this->writeFirstRow($firstRow, 'firstRow'); $this->writeFirstRow($firstRow);
} }
} }
} }
@ -96,28 +96,30 @@ class Table extends AbstractStyle
* *
* @param string $type * @param string $type
*/ */
private function writeFirstRow(\PhpOffice\PhpWord\Style\Table $style, $type) private function writeFirstRow(\PhpOffice\PhpWord\Style\Table $style)
{ {
$this->xmlWriter->startElement('w:tblStylePr'); $xmlWriter = $this->getXmlWriter();
$this->xmlWriter->writeAttribute('w:type', $type);
$this->xmlWriter->startElement('w:tcPr'); $xmlWriter->startElement('w:tblStylePr');
$xmlWriter->writeAttribute('w:type', 'firstRow');
$xmlWriter->startElement('w:tcPr');
if (!is_null($style->getShading())) { if (!is_null($style->getShading())) {
$styleWriter = new Shading($this->xmlWriter, $style->getShading()); $styleWriter = new Shading($xmlWriter, $style->getShading());
$styleWriter->write(); $styleWriter->write();
} }
// Borders // Borders
if ($style->hasBorders()) { if ($style->hasBorders()) {
$mbWriter = new MarginBorder($this->xmlWriter); $mbWriter = new MarginBorder($xmlWriter);
$mbWriter->setSizes($style->getBorderSize()); $mbWriter->setSizes($style->getBorderSize());
$mbWriter->setColors($style->getBorderColor()); $mbWriter->setColors($style->getBorderColor());
$this->xmlWriter->startElement('w:tcBorders'); $xmlWriter->startElement('w:tcBorders');
$mbWriter->write(); $mbWriter->write();
$this->xmlWriter->endElement(); // w:tcBorders $xmlWriter->endElement(); // w:tcBorders
} }
$this->xmlWriter->endElement(); // w:tcPr $xmlWriter->endElement(); // w:tcPr
$this->xmlWriter->endElement(); // w:tblStylePr $xmlWriter->endElement(); // w:tblStylePr
} }
} }

View File

@ -97,8 +97,9 @@ 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,
); );
$object->setArrayStyle($attributes); $object->setStyleByArray($attributes);
foreach ($attributes as $key => $value) { foreach ($attributes as $key => $value) {
$get = "get{$key}"; $get = "get{$key}";
$this->assertEquals($value, $object->$get()); $this->assertEquals($value, $object->$get());