From faba46cc059e0ba4e8df297051f0d9e746fecb89 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Mon, 31 Mar 2014 23:52:42 +0700 Subject: [PATCH] Extends `Container` abstract class into `Footnote`, `TextRun`, and `Cell` element class. --- src/PhpWord/Container/Container.php | 153 ++++++--- src/PhpWord/Element/Footnote.php | 102 +----- src/PhpWord/Element/Table/Cell.php | 313 ++---------------- src/PhpWord/Element/TextRun.php | 140 +------- .../PhpWord/Tests/Element/Table/CellTest.php | 4 +- 5 files changed, 166 insertions(+), 546 deletions(-) diff --git a/src/PhpWord/Container/Container.php b/src/PhpWord/Container/Container.php index 7ca3ebb1..379f7b14 100644 --- a/src/PhpWord/Container/Container.php +++ b/src/PhpWord/Container/Container.php @@ -57,6 +57,20 @@ abstract class Container */ protected $elements = array(); + /** + * Parent container type: section|header|footer + * + * @var string + */ + protected $parentContainer = null; + + /** + * Parent container Id + * + * @var int + */ + protected $parentContainerId; + /** * Relation Id * @@ -68,19 +82,22 @@ abstract class Container * Add text element * * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph + * @param mixed $fontStyle + * @param mixed $paragraphStyle * @return Text */ - public function addText($text, $styleFont = null, $styleParagraph = null) + public function addText($text, $fontStyle = null, $paragraphStyle = null) { + if (in_array($this->containerType, array('footnote', 'textrun'))) { + $paragraphStyle = null; + } if (!String::isUTF8($text)) { $text = utf8_encode($text); } - $text = new Text($text, $styleFont, $styleParagraph); - $this->elements[] = $text; + $element = new Text($text, $fontStyle, $paragraphStyle); + $this->elements[] = $element; - return $text; + return $element; } /** @@ -100,12 +117,16 @@ abstract class Container /** * Add textrun element * - * @param mixed $styleParagraph + * @param mixed $paragraphStyle * @return TextRun */ - public function addTextRun($styleParagraph = null) + public function addTextRun($paragraphStyle = null) { - $textRun = new TextRun($styleParagraph); + if (!in_array($this->containerType, array('section', 'header', 'footer', 'cell'))) { + throw new \BadMethodCallException(); + } + + $textRun = new TextRun($paragraphStyle); $this->elements[] = $textRun; return $textRun; @@ -116,14 +137,17 @@ abstract class Container * * @param string $linkSrc * @param string $linkName - * @param mixed $styleFont - * @param mixed $styleParagraph + * @param mixed $fontStyle + * @param mixed $paragraphStyle * @return Link * @todo Enable link element in header and footer */ - public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null) + public function addLink($linkSrc, $linkName = null, $fontStyle = null, $paragraphStyle = null) { - if ($this->containerType != 'section') { + if (!in_array($this->containerType, array('section', 'footnote', 'textrun', 'cell'))) { + throw new \BadMethodCallException(); + } + if ($this->containerType == 'cell' && $this->parentContainer != 'section') { throw new \BadMethodCallException(); } @@ -135,10 +159,15 @@ abstract class Container $linkName = utf8_encode($linkName); } } - $link = new Link($linkSrc, $linkName, $styleFont, $styleParagraph); - $rID = Media::addSectionLinkElement($linkSrc); + $link = new Link($linkSrc, $linkName, $fontStyle, $paragraphStyle); + if ($this->containerType == 'footnote') { + $rID = Footnote::addFootnoteLinkElement($linkSrc); + } else { + $rID = Media::addSectionLinkElement($linkSrc); + } $link->setRelationId($rID); $this->elements[] = $link; + return $link; } @@ -148,7 +177,7 @@ abstract class Container * @param string $text * @param int $depth * @return Title - * @todo Enable title element in header and footer + * @todo Enable title element in header, footer, footnote, textrun */ public function addTitle($text, $depth = 1) { @@ -172,6 +201,7 @@ abstract class Container $title->setAnchor($anchor); $title->setBookmarkId($bookmarkId); $this->elements[] = $title; + return $title; } @@ -179,20 +209,23 @@ abstract class Container * Add preserve text element * * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph + * @param mixed $fontStyle + * @param mixed $paragraphStyle * @return PreserveText */ - public function addPreserveText($text, $styleFont = null, $styleParagraph = null) + public function addPreserveText($text, $fontStyle = null, $paragraphStyle = null) { - if ($this->containerType == 'section') { + if (!in_array($this->containerType, array('header', 'footer', 'cell'))) { + throw new \BadMethodCallException(); + } + if ($this->containerType == 'cell' && $this->parentContainer == 'section') { throw new \BadMethodCallException(); } if (!String::isUTF8($text)) { $text = utf8_encode($text); } - $ptext = new PreserveText($text, $styleFont, $styleParagraph); + $ptext = new PreserveText($text, $fontStyle, $paragraphStyle); $this->elements[] = $ptext; return $ptext; @@ -203,23 +236,27 @@ abstract class Container * * @param string $text * @param int $depth - * @param mixed $styleFont + * @param mixed $fontStyle * @param mixed $styleList - * @param mixed $styleParagraph + * @param mixed $paragraphStyle * @return ListItem * @todo Enable list item element in header and footer */ - public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null) + public function addListItem($text, $depth = 0, $fontStyle = null, $styleList = null, $paragraphStyle = null) { - if ($this->containerType != 'section') { + if (!in_array($this->containerType, array('section', 'cell'))) { + throw new \BadMethodCallException(); + } + if ($this->containerType == 'cell' && $this->parentContainer != 'section') { throw new \BadMethodCallException(); } if (!String::isUTF8($text)) { $text = utf8_encode($text); } - $listItem = new ListItem($text, $depth, $styleFont, $styleList, $styleParagraph); + $listItem = new ListItem($text, $depth, $fontStyle, $styleList, $paragraphStyle); $this->elements[] = $listItem; + return $listItem; } @@ -231,6 +268,10 @@ abstract class Container */ public function addTable($style = null) { + if (!in_array($this->containerType, array('section', 'header', 'footer'))) { + throw new \BadMethodCallException(); + } + $table = new Table($this->containerType, $this->sectionId, $style); $this->elements[] = $table; @@ -247,17 +288,29 @@ abstract class Container */ public function addImage($src, $style = null, $isWatermark = false) { + if ($this->containerType == 'footnote') { + throw new \BadMethodCallException(); + } + if (!is_null($this->parentContainer)) { + $imageContainerType = $this->parentContainer; + $imageContainerId = $this->parentContainerId; + } else { + $imageContainerType = $this->containerType; + $imageContainerId = $this->sectionId; + } + $image = new Image($src, $style, $isWatermark); if (!is_null($image->getSource())) { - switch ($this->containerType) { + switch ($imageContainerType) { + case 'textrun': case 'section': $rID = Media::addSectionMediaElement($src, 'image', $image); break; case 'header': - $rID = Media::addHeaderMediaElement($this->sectionId, $src, $image); + $rID = Media::addHeaderMediaElement($imageContainerId, $src, $image); break; case 'footer': - $rID = Media::addFooterMediaElement($this->sectionId, $src, $image); + $rID = Media::addFooterMediaElement($imageContainerId, $src, $image); break; } $image->setRelationId($rID); @@ -280,7 +333,10 @@ abstract class Container */ public function addObject($src, $style = null) { - if ($this->containerType != 'section') { + if (!in_array($this->containerType, array('section', 'cell'))) { + throw new \BadMethodCallException(); + } + if ($this->containerType == 'cell' && $this->parentContainer != 'section') { throw new \BadMethodCallException(); } @@ -309,17 +365,17 @@ abstract class Container /** * Add footnote element * - * @param mixed $styleParagraph + * @param mixed $paragraphStyle * @return FootnoteElement * @todo Enable footnote element in header and footer */ - public function addFootnote($styleParagraph = null) + public function addFootnote($paragraphStyle = null) { - if ($this->containerType != 'section') { + if (!in_array($this->containerType, array('section', 'textrun'))) { throw new \BadMethodCallException(); } - $footnote = new FootnoteElement($styleParagraph); + $footnote = new FootnoteElement($paragraphStyle); $refID = Footnote::addFootnoteElement($footnote); $footnote->setReferenceId($refID); $this->elements[] = $footnote; @@ -331,14 +387,17 @@ abstract class Container * * @param string $name * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph + * @param mixed $fontStyle + * @param mixed $paragraphStyle * @return CheckBox * @todo Enable checkbox element in header and footer */ - public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null) + public function addCheckBox($name, $text, $fontStyle = null, $paragraphStyle = null) { - if ($this->containerType != 'section') { + if (!in_array($this->containerType, array('section', 'cell'))) { + throw new \BadMethodCallException(); + } + if ($this->containerType == 'cell' && $this->parentContainer != 'section') { throw new \BadMethodCallException(); } @@ -348,7 +407,7 @@ abstract class Container if (!String::isUTF8($text)) { $text = utf8_encode($text); } - $element = new CheckBox($name, $text, $styleFont, $styleParagraph); + $element = new CheckBox($name, $text, $fontStyle, $paragraphStyle); $this->elements[] = $element; return $element; @@ -380,7 +439,7 @@ abstract class Container */ public function getRelationId() { - if ($this->containerType == 'section') { + if (!in_array($this->containerType, array('header', 'footer'))) { throw new \BadMethodCallException(); } @@ -394,7 +453,7 @@ abstract class Container */ public function setRelationId($rId) { - if ($this->containerType == 'section') { + if (!in_array($this->containerType, array('header', 'footer'))) { throw new \BadMethodCallException(); } @@ -416,22 +475,22 @@ abstract class Container /** * Create textrun element * - * @param mixed $styleParagraph + * @param mixed $paragraphStyle * @deprecated 0.9.2 */ - public function createTextRun($styleParagraph = null) + public function createTextRun($paragraphStyle = null) { - return $this->addTextRun($styleParagraph); + return $this->addTextRun($paragraphStyle); } /** * Create footnote element * - * @param mixed $styleParagraph + * @param mixed $paragraphStyle * @deprecated 0.9.2 */ - public function createFootnote($styleParagraph = null) + public function createFootnote($paragraphStyle = null) { - return $this->addFootnote($styleParagraph); + return $this->addFootnote($paragraphStyle); } } diff --git a/src/PhpWord/Element/Footnote.php b/src/PhpWord/Element/Footnote.php index d1cb61cf..a683b3b0 100644 --- a/src/PhpWord/Element/Footnote.php +++ b/src/PhpWord/Element/Footnote.php @@ -9,125 +9,59 @@ namespace PhpOffice\PhpWord\Element; +use PhpOffice\PhpWord\Container\Container; use PhpOffice\PhpWord\Style\Paragraph; /** * Footnote element */ -class Footnote +class Footnote extends Container { /** * Paragraph style * - * @var \PhpOffice\PhpWord\Style\Paragraph + * @var string|Paragraph */ - private $_styleParagraph; + private $paragraphStyle; /** * Footnote Reference ID * * @var string */ - private $_refId; + private $referenceId; /** - * Text collection + * Create new instance * - * @var array + * @param string|array|Paragraph $paragraphStyle */ - private $_elementCollection; - - /** - * Create a new Footnote Element - * - * @param mixed $styleParagraph - */ - public function __construct($styleParagraph = null) + public function __construct($paragraphStyle = null) { - $this->_elementCollection = array(); + $this->containerType = 'footnote'; // Set paragraph style - if (is_array($styleParagraph)) { - $this->_styleParagraph = new Paragraph(); - - foreach ($styleParagraph as $key => $value) { + if (is_array($paragraphStyle)) { + $this->paragraphStyle = new Paragraph(); + foreach ($paragraphStyle as $key => $value) { if (substr($key, 0, 1) != '_') { $key = '_' . $key; } - $this->_styleParagraph->setStyleValue($key, $value); + $this->paragraphStyle->setStyleValue($key, $value); } } else { - $this->_styleParagraph = $styleParagraph; + $this->paragraphStyle = $paragraphStyle; } } - - /** - * Add a Text Element - * - * @param string $text - * @param mixed $styleFont - * @return \PhpOffice\PhpWord\Element\Text - */ - public function addText($text = null, $styleFont = null) - { - $givenText = $text; - $text = new Text($givenText, $styleFont); - $this->_elementCollection[] = $text; - return $text; - } - - /** - * Add TextBreak - * - * @param int $count - * @param mixed $fontStyle - * @param mixed $paragraphStyle - */ - public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null) - { - for ($i = 1; $i <= $count; $i++) { - $this->_elementCollection[] = new TextBreak($fontStyle, $paragraphStyle); - } - } - - /** - * Add a Link Element - * - * @param string $linkSrc - * @param string $linkName - * @param mixed $styleFont - * @return \PhpOffice\PhpWord\Element\Link - */ - public function addLink($linkSrc, $linkName = null, $styleFont = null) - { - - $link = new Link($linkSrc, $linkName, $styleFont); - $rID = \PhpOffice\PhpWord\Footnote::addFootnoteLinkElement($linkSrc); - $link->setRelationId($rID); - - $this->_elementCollection[] = $link; - return $link; - } - - /** - * Get Footnote content - * - * @return array - */ - public function getElements() - { - return $this->_elementCollection; - } - /** * Get paragraph style * - * @return \PhpOffice\PhpWord\Style\Paragraph + * @return string|Paragraph */ public function getParagraphStyle() { - return $this->_styleParagraph; + return $this->paragraphStyle; } /** @@ -137,7 +71,7 @@ class Footnote */ public function getReferenceId() { - return $this->_refId; + return $this->referenceId; } /** @@ -147,6 +81,6 @@ class Footnote */ public function setReferenceId($refId) { - $this->_refId = $refId; + $this->referenceId = $refId; } } diff --git a/src/PhpWord/Element/Table/Cell.php b/src/PhpWord/Element/Table/Cell.php index 0e0d688e..0a14400c 100755 --- a/src/PhpWord/Element/Table/Cell.php +++ b/src/PhpWord/Element/Table/Cell.php @@ -9,76 +9,44 @@ namespace PhpOffice\PhpWord\Element\Table; -use PhpOffice\PhpWord\Exception\Exception; -use PhpOffice\PhpWord\Exception\InvalidObjectException; -use PhpOffice\PhpWord\Exception\InvalidImageException; -use PhpOffice\PhpWord\Media; -use PhpOffice\PhpWord\Element\PreserveText; -use PhpOffice\PhpWord\Element\Image; -use PhpOffice\PhpWord\Element\Link; -use PhpOffice\PhpWord\Element\ListItem; -use PhpOffice\PhpWord\Element\Object; -use PhpOffice\PhpWord\Element\Text; -use PhpOffice\PhpWord\Element\TextBreak; -use PhpOffice\PhpWord\Element\TextRun; -use PhpOffice\PhpWord\Element\CheckBox; -use PhpOffice\PhpWord\Shared\String; +use PhpOffice\PhpWord\Container\Container; +use PhpOffice\PhpWord\Style\Cell as CellStyle; /** * Table cell element */ -class Cell +class Cell extends Container { /** - * Cell Width + * Cell width * * @var int */ - private $_width = null; + private $width = null; /** - * Cell Style + * Cell style * - * @var \PhpOffice\PhpWord\Style\Cell + * @var CellStyle */ - private $_style; + private $cellStyle; /** - * Cell Element Collection + * Create new instance * - * @var array - */ - private $_elementCollection = array(); - - /** - * Table holder - * - * @var string - */ - private $_insideOf; - - /** - * Section/Header/Footer count - * - * @var int - */ - private $_pCount; - - - /** - * Create a new Table Cell - * - * @param string $insideOf - * @param int $pCount + * @param string $parentType section|header|footer + * @param int $parentId * @param int $width - * @param mixed $style + * @param array|CellStyle $style */ - public function __construct($insideOf, $pCount, $width = null, $style = null) + public function __construct($parentType, $parentId, $width = null, $style = null) { - $this->_insideOf = $insideOf; - $this->_pCount = $pCount; - $this->_width = $width; - $this->_style = new \PhpOffice\PhpWord\Style\Cell(); + $this->containerType = 'cell'; + + $this->parentContainer = $parentType; + $this->parentContainerId = $parentId; + $this->width = $width; + $this->cellStyle = new CellStyle(); if (!is_null($style)) { if (is_array($style)) { @@ -86,260 +54,31 @@ class Cell if (substr($key, 0, 1) != '_') { $key = '_' . $key; } - $this->_style->setStyleValue($key, $value); + $this->cellStyle->setStyleValue($key, $value); } } else { - $this->_style = $style; + $this->cellStyle = $style; } } } /** - * Add a Text Element + * Get cell style * - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\Text - */ - public function addText($text, $styleFont = null, $styleParagraph = null) - { - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $text = new Text($text, $styleFont, $styleParagraph); - $this->_elementCollection[] = $text; - return $text; - } - - /** - * Add a Link Element - * - * @param string $linkSrc - * @param string $linkName - * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Link - */ - public function addLink($linkSrc, $linkName = null, $style = null) - { - if ($this->_insideOf == 'section') { - if (!String::isUTF8($linkSrc)) { - $linkSrc = utf8_encode($linkSrc); - } - if (!is_null($linkName)) { - if (!String::isUTF8($linkName)) { - $linkName = utf8_encode($linkName); - } - } - - $link = new Link($linkSrc, $linkName, $style); - $rID = Media::addSectionLinkElement($linkSrc); - $link->setRelationId($rID); - - $this->_elementCollection[] = $link; - return $link; - } else { - throw new Exception('Unsupported Link header / footer reference'); - return false; - } - } - - /** - * Add TextBreak - * - * @param int $count - * @param null|string|array|\PhpOffice\PhpWord\Style\Font $fontStyle - * @param null|string|array|\PhpOffice\PhpWord\Style\Paragraph $paragraphStyle - */ - public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null) - { - for ($i = 1; $i <= $count; $i++) { - $this->_elementCollection[] = new TextBreak($fontStyle, $paragraphStyle); - } - } - - /** - * Add a ListItem Element - * - * @param string $text - * @param int $depth - * @param mixed $styleText - * @param mixed $styleList - * @return \PhpOffice\PhpWord\Element\ListItem - */ - public function addListItem($text, $depth = 0, $styleText = null, $styleList = null) - { - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $listItem = new ListItem($text, $depth, $styleText, $styleList); - $this->_elementCollection[] = $listItem; - return $listItem; - } - - /** - * Add a Image Element - * - * @param string $src - * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Image - */ - public function addImage($src, $style = null) - { - $image = new Image($src, $style); - if (!is_null($image->getSource())) { - if ($this->_insideOf == 'section') { - $rID = Media::addSectionMediaElement($src, 'image', $image); - } elseif ($this->_insideOf == 'header') { - $rID = Media::addHeaderMediaElement($this->_pCount, $src, $image); - } elseif ($this->_insideOf == 'footer') { - $rID = Media::addFooterMediaElement($this->_pCount, $src, $image); - } - $image->setRelationId($rID); - $this->_elementCollection[] = $image; - return $image; - } else { - throw new InvalidImageException; - } - } - - /** - * Add a by PHP created Image Element - * - * @param string $src - * @param mixed $style - * @deprecated - */ - public function addMemoryImage($src, $style = null) - { - return $this->addImage($src, $style); - } - - /** - * Add a OLE-Object Element - * - * @param string $src - * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Object - */ - public function addObject($src, $style = null) - { - $object = new Object($src, $style); - - if (!is_null($object->getSource())) { - $inf = pathinfo($src); - $ext = $inf['extension']; - if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') { - $ext = substr($ext, 0, -1); - } - - $iconSrc = __DIR__ . '/../../_staticDocParts/'; - if (!\file_exists($iconSrc . '_' . $ext . '.png')) { - $iconSrc = $iconSrc . '_default.png'; - } else { - $iconSrc .= '_' . $ext . '.png'; - } - - $rIDimg = Media::addSectionMediaElement($iconSrc, 'image', new Image($iconSrc)); - $data = Media::addSectionMediaElement($src, 'oleObject'); - $rID = $data[0]; - $objectId = $data[1]; - - $object->setRelationId($rID); - $object->setObjectId($objectId); - $object->setImageRelationId($rIDimg); - - $this->_elementCollection[] = $object; - return $object; - } else { - throw new InvalidObjectException; - } - } - - /** - * Add a PreserveText Element - * - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\PreserveText - */ - public function addPreserveText($text, $styleFont = null, $styleParagraph = null) - { - if ($this->_insideOf == 'footer' || $this->_insideOf == 'header') { - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $ptext = new PreserveText($text, $styleFont, $styleParagraph); - $this->_elementCollection[] = $ptext; - return $ptext; - } else { - throw new Exception('addPreserveText only supported in footer/header.'); - } - } - - /** - * Create a new TextRun - * - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\TextRun - */ - public function createTextRun($styleParagraph = null) - { - $textRun = new TextRun($styleParagraph); - $this->_elementCollection[] = $textRun; - return $textRun; - } - - /** - * Add a CheckBox Element - * - * @param string $name - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\CheckBox - */ - public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null) - { - if (!String::isUTF8($name)) { - $name = utf8_encode($name); - } - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $text = new CheckBox($name, $text, $styleFont, $styleParagraph); - $this->_elementCollection[] = $text; - return $text; - } - - /** - * Get all Elements - * - * @return array - */ - public function getElements() - { - return $this->_elementCollection; - } - - /** - * Get Cell Style - * - * @return \PhpOffice\PhpWord\Style\Cell + * @return CellStyle */ public function getStyle() { - return $this->_style; + return $this->cellStyle; } /** - * Get Cell width + * Get cell width * * @return int */ public function getWidth() { - return $this->_width; + return $this->width; } } diff --git a/src/PhpWord/Element/TextRun.php b/src/PhpWord/Element/TextRun.php index a890bb04..63401d33 100755 --- a/src/PhpWord/Element/TextRun.php +++ b/src/PhpWord/Element/TextRun.php @@ -9,156 +9,44 @@ namespace PhpOffice\PhpWord\Element; -use PhpOffice\PhpWord\Exception\InvalidImageException; -use PhpOffice\PhpWord\Media; -use PhpOffice\PhpWord\Shared\String; -use PhpOffice\PhpWord\Style\Font; +use PhpOffice\PhpWord\Container\Container; use PhpOffice\PhpWord\Style\Paragraph; /** * Textrun/paragraph element */ -class TextRun +class TextRun extends Container { /** * Paragraph style * - * @var Paragraph + * @var string|Paragraph */ - private $_styleParagraph; + private $paragraphStyle; /** - * Text collection + * Create new instance * - * @var array + * @param string|array|Paragraph $paragraphStyle */ - private $_elementCollection; - - - /** - * Create a new TextRun Element - * - * @param mixed $styleParagraph - */ - public function __construct($styleParagraph = null) + public function __construct($paragraphStyle = null) { - $this->_elementCollection = array(); + $this->containerType = 'textrun'; // Set paragraph style - if (is_array($styleParagraph)) { - $this->_styleParagraph = new Paragraph(); - - foreach ($styleParagraph as $key => $value) { + if (is_array($paragraphStyle)) { + $this->paragraphStyle = new Paragraph(); + foreach ($paragraphStyle as $key => $value) { if (substr($key, 0, 1) != '_') { $key = '_' . $key; } - $this->_styleParagraph->setStyleValue($key, $value); + $this->paragraphStyle->setStyleValue($key, $value); } } else { - $this->_styleParagraph = $styleParagraph; + $this->paragraphStyle = $paragraphStyle; } } - - /** - * Add a Text Element - * - * @param string $text - * @param mixed $styleFont - * @return \PhpOffice\PhpWord\Element\Text - */ - public function addText($text = null, $styleFont = null) - { - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $text = new Text($text, $styleFont); - $this->_elementCollection[] = $text; - return $text; - } - - /** - * Add a Link Element - * - * @param string $linkSrc - * @param string $linkName - * @param mixed $styleFont - * @return \PhpOffice\PhpWord\Element\Link - */ - public function addLink($linkSrc, $linkName = null, $styleFont = null) - { - $linkSrc = utf8_encode($linkSrc); - if (!is_null($linkName)) { - $linkName = utf8_encode($linkName); - } - - $link = new Link($linkSrc, $linkName, $styleFont); - $rID = Media::addSectionLinkElement($linkSrc); - $link->setRelationId($rID); - - $this->_elementCollection[] = $link; - return $link; - } - - /** - * Add a Image Element - * - * @param string $imageSrc - * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Image - */ - public function addImage($imageSrc, $style = null) - { - $image = new Image($imageSrc, $style); - if (!is_null($image->getSource())) { - $rID = Media::addSectionMediaElement($imageSrc, 'image', $image); - $image->setRelationId($rID); - $this->_elementCollection[] = $image; - return $image; - } else { - throw new InvalidImageException; - } - } - - /** - * Add TextBreak - * - * @param int $count - * @param mixed $fontStyle - * @param mixed $paragraphStyle - */ - public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null) - { - for ($i = 1; $i <= $count; $i++) { - $this->_elementCollection[] = new TextBreak($fontStyle, $paragraphStyle); - } - } - - /** - * Create a new Footnote Element - * - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\Footnote - */ - public function createFootnote($styleParagraph = null) - { - $footnote = new \PhpOffice\PhpWord\Element\Footnote($styleParagraph); - $refID = \PhpOffice\PhpWord\Footnote::addFootnoteElement($footnote); - $footnote->setReferenceId($refID); - $this->_elementCollection[] = $footnote; - return $footnote; - } - - /** - * Get TextRun content - * - * @return string - */ - public function getElements() - { - return $this->_elementCollection; - } - /** * Get Paragraph style * @@ -166,6 +54,6 @@ class TextRun */ public function getParagraphStyle() { - return $this->_styleParagraph; + return $this->paragraphStyle; } } diff --git a/tests/PhpWord/Tests/Element/Table/CellTest.php b/tests/PhpWord/Tests/Element/Table/CellTest.php index 661120d4..b8daca61 100644 --- a/tests/PhpWord/Tests/Element/Table/CellTest.php +++ b/tests/PhpWord/Tests/Element/Table/CellTest.php @@ -92,7 +92,7 @@ class CellTest extends \PHPUnit_Framework_TestCase /** * Add link exception - * @expectedException \PhpOffice\PhpWord\Exception\Exception + * @expectedException \BadMethodCallException */ public function testAddLinkException() { @@ -272,7 +272,7 @@ class CellTest extends \PHPUnit_Framework_TestCase /** * Add preserve text exception * - * @expectedException \PhpOffice\PhpWord\Exception\Exception + * @expectedException \BadMethodCallException */ public function testAddPreserveTextException() {