Extends Container abstract class into Footnote, TextRun, and Cell element class.

This commit is contained in:
Ivan Lanin 2014-03-31 23:52:42 +07:00
parent 2bf0bbb094
commit faba46cc05
5 changed files with 166 additions and 546 deletions

View File

@ -57,6 +57,20 @@ abstract class Container
*/ */
protected $elements = array(); protected $elements = array();
/**
* Parent container type: section|header|footer
*
* @var string
*/
protected $parentContainer = null;
/**
* Parent container Id
*
* @var int
*/
protected $parentContainerId;
/** /**
* Relation Id * Relation Id
* *
@ -68,19 +82,22 @@ abstract class Container
* Add text element * Add text element
* *
* @param string $text * @param string $text
* @param mixed $styleFont * @param mixed $fontStyle
* @param mixed $styleParagraph * @param mixed $paragraphStyle
* @return Text * @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)) { if (!String::isUTF8($text)) {
$text = utf8_encode($text); $text = utf8_encode($text);
} }
$text = new Text($text, $styleFont, $styleParagraph); $element = new Text($text, $fontStyle, $paragraphStyle);
$this->elements[] = $text; $this->elements[] = $element;
return $text; return $element;
} }
/** /**
@ -100,12 +117,16 @@ abstract class Container
/** /**
* Add textrun element * Add textrun element
* *
* @param mixed $styleParagraph * @param mixed $paragraphStyle
* @return TextRun * @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; $this->elements[] = $textRun;
return $textRun; return $textRun;
@ -116,14 +137,17 @@ abstract class Container
* *
* @param string $linkSrc * @param string $linkSrc
* @param string $linkName * @param string $linkName
* @param mixed $styleFont * @param mixed $fontStyle
* @param mixed $styleParagraph * @param mixed $paragraphStyle
* @return Link * @return Link
* @todo Enable link element in header and footer * @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(); throw new \BadMethodCallException();
} }
@ -135,10 +159,15 @@ abstract class Container
$linkName = utf8_encode($linkName); $linkName = utf8_encode($linkName);
} }
} }
$link = new Link($linkSrc, $linkName, $styleFont, $styleParagraph); $link = new Link($linkSrc, $linkName, $fontStyle, $paragraphStyle);
$rID = Media::addSectionLinkElement($linkSrc); if ($this->containerType == 'footnote') {
$rID = Footnote::addFootnoteLinkElement($linkSrc);
} else {
$rID = Media::addSectionLinkElement($linkSrc);
}
$link->setRelationId($rID); $link->setRelationId($rID);
$this->elements[] = $link; $this->elements[] = $link;
return $link; return $link;
} }
@ -148,7 +177,7 @@ abstract class Container
* @param string $text * @param string $text
* @param int $depth * @param int $depth
* @return Title * @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) public function addTitle($text, $depth = 1)
{ {
@ -172,6 +201,7 @@ abstract class Container
$title->setAnchor($anchor); $title->setAnchor($anchor);
$title->setBookmarkId($bookmarkId); $title->setBookmarkId($bookmarkId);
$this->elements[] = $title; $this->elements[] = $title;
return $title; return $title;
} }
@ -179,20 +209,23 @@ abstract class Container
* Add preserve text element * Add preserve text element
* *
* @param string $text * @param string $text
* @param mixed $styleFont * @param mixed $fontStyle
* @param mixed $styleParagraph * @param mixed $paragraphStyle
* @return PreserveText * @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(); throw new \BadMethodCallException();
} }
if (!String::isUTF8($text)) { if (!String::isUTF8($text)) {
$text = utf8_encode($text); $text = utf8_encode($text);
} }
$ptext = new PreserveText($text, $styleFont, $styleParagraph); $ptext = new PreserveText($text, $fontStyle, $paragraphStyle);
$this->elements[] = $ptext; $this->elements[] = $ptext;
return $ptext; return $ptext;
@ -203,23 +236,27 @@ abstract class Container
* *
* @param string $text * @param string $text
* @param int $depth * @param int $depth
* @param mixed $styleFont * @param mixed $fontStyle
* @param mixed $styleList * @param mixed $styleList
* @param mixed $styleParagraph * @param mixed $paragraphStyle
* @return ListItem * @return ListItem
* @todo Enable list item element in header and footer * @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(); throw new \BadMethodCallException();
} }
if (!String::isUTF8($text)) { if (!String::isUTF8($text)) {
$text = utf8_encode($text); $text = utf8_encode($text);
} }
$listItem = new ListItem($text, $depth, $styleFont, $styleList, $styleParagraph); $listItem = new ListItem($text, $depth, $fontStyle, $styleList, $paragraphStyle);
$this->elements[] = $listItem; $this->elements[] = $listItem;
return $listItem; return $listItem;
} }
@ -231,6 +268,10 @@ abstract class Container
*/ */
public function addTable($style = null) 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); $table = new Table($this->containerType, $this->sectionId, $style);
$this->elements[] = $table; $this->elements[] = $table;
@ -247,17 +288,29 @@ abstract class Container
*/ */
public function addImage($src, $style = null, $isWatermark = false) 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); $image = new Image($src, $style, $isWatermark);
if (!is_null($image->getSource())) { if (!is_null($image->getSource())) {
switch ($this->containerType) { switch ($imageContainerType) {
case 'textrun':
case 'section': case 'section':
$rID = Media::addSectionMediaElement($src, 'image', $image); $rID = Media::addSectionMediaElement($src, 'image', $image);
break; break;
case 'header': case 'header':
$rID = Media::addHeaderMediaElement($this->sectionId, $src, $image); $rID = Media::addHeaderMediaElement($imageContainerId, $src, $image);
break; break;
case 'footer': case 'footer':
$rID = Media::addFooterMediaElement($this->sectionId, $src, $image); $rID = Media::addFooterMediaElement($imageContainerId, $src, $image);
break; break;
} }
$image->setRelationId($rID); $image->setRelationId($rID);
@ -280,7 +333,10 @@ abstract class Container
*/ */
public function addObject($src, $style = null) 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(); throw new \BadMethodCallException();
} }
@ -309,17 +365,17 @@ abstract class Container
/** /**
* Add footnote element * Add footnote element
* *
* @param mixed $styleParagraph * @param mixed $paragraphStyle
* @return FootnoteElement * @return FootnoteElement
* @todo Enable footnote element in header and footer * @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(); throw new \BadMethodCallException();
} }
$footnote = new FootnoteElement($styleParagraph); $footnote = new FootnoteElement($paragraphStyle);
$refID = Footnote::addFootnoteElement($footnote); $refID = Footnote::addFootnoteElement($footnote);
$footnote->setReferenceId($refID); $footnote->setReferenceId($refID);
$this->elements[] = $footnote; $this->elements[] = $footnote;
@ -331,14 +387,17 @@ abstract class Container
* *
* @param string $name * @param string $name
* @param string $text * @param string $text
* @param mixed $styleFont * @param mixed $fontStyle
* @param mixed $styleParagraph * @param mixed $paragraphStyle
* @return CheckBox * @return CheckBox
* @todo Enable checkbox element in header and footer * @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(); throw new \BadMethodCallException();
} }
@ -348,7 +407,7 @@ abstract class Container
if (!String::isUTF8($text)) { if (!String::isUTF8($text)) {
$text = utf8_encode($text); $text = utf8_encode($text);
} }
$element = new CheckBox($name, $text, $styleFont, $styleParagraph); $element = new CheckBox($name, $text, $fontStyle, $paragraphStyle);
$this->elements[] = $element; $this->elements[] = $element;
return $element; return $element;
@ -380,7 +439,7 @@ abstract class Container
*/ */
public function getRelationId() public function getRelationId()
{ {
if ($this->containerType == 'section') { if (!in_array($this->containerType, array('header', 'footer'))) {
throw new \BadMethodCallException(); throw new \BadMethodCallException();
} }
@ -394,7 +453,7 @@ abstract class Container
*/ */
public function setRelationId($rId) public function setRelationId($rId)
{ {
if ($this->containerType == 'section') { if (!in_array($this->containerType, array('header', 'footer'))) {
throw new \BadMethodCallException(); throw new \BadMethodCallException();
} }
@ -416,22 +475,22 @@ abstract class Container
/** /**
* Create textrun element * Create textrun element
* *
* @param mixed $styleParagraph * @param mixed $paragraphStyle
* @deprecated 0.9.2 * @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 * Create footnote element
* *
* @param mixed $styleParagraph * @param mixed $paragraphStyle
* @deprecated 0.9.2 * @deprecated 0.9.2
*/ */
public function createFootnote($styleParagraph = null) public function createFootnote($paragraphStyle = null)
{ {
return $this->addFootnote($styleParagraph); return $this->addFootnote($paragraphStyle);
} }
} }

View File

@ -9,125 +9,59 @@
namespace PhpOffice\PhpWord\Element; namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Container\Container;
use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Style\Paragraph;
/** /**
* Footnote element * Footnote element
*/ */
class Footnote class Footnote extends Container
{ {
/** /**
* Paragraph style * Paragraph style
* *
* @var \PhpOffice\PhpWord\Style\Paragraph * @var string|Paragraph
*/ */
private $_styleParagraph; private $paragraphStyle;
/** /**
* Footnote Reference ID * Footnote Reference ID
* *
* @var string * @var string
*/ */
private $_refId; private $referenceId;
/** /**
* Text collection * Create new instance
* *
* @var array * @param string|array|Paragraph $paragraphStyle
*/ */
private $_elementCollection; public function __construct($paragraphStyle = null)
/**
* Create a new Footnote Element
*
* @param mixed $styleParagraph
*/
public function __construct($styleParagraph = null)
{ {
$this->_elementCollection = array(); $this->containerType = 'footnote';
// Set paragraph style // Set paragraph style
if (is_array($styleParagraph)) { if (is_array($paragraphStyle)) {
$this->_styleParagraph = new Paragraph(); $this->paragraphStyle = new Paragraph();
foreach ($paragraphStyle as $key => $value) {
foreach ($styleParagraph as $key => $value) {
if (substr($key, 0, 1) != '_') { if (substr($key, 0, 1) != '_') {
$key = '_' . $key; $key = '_' . $key;
} }
$this->_styleParagraph->setStyleValue($key, $value); $this->paragraphStyle->setStyleValue($key, $value);
} }
} else { } 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 * Get paragraph style
* *
* @return \PhpOffice\PhpWord\Style\Paragraph * @return string|Paragraph
*/ */
public function getParagraphStyle() public function getParagraphStyle()
{ {
return $this->_styleParagraph; return $this->paragraphStyle;
} }
/** /**
@ -137,7 +71,7 @@ class Footnote
*/ */
public function getReferenceId() public function getReferenceId()
{ {
return $this->_refId; return $this->referenceId;
} }
/** /**
@ -147,6 +81,6 @@ class Footnote
*/ */
public function setReferenceId($refId) public function setReferenceId($refId)
{ {
$this->_refId = $refId; $this->referenceId = $refId;
} }
} }

View File

@ -9,76 +9,44 @@
namespace PhpOffice\PhpWord\Element\Table; namespace PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Container\Container;
use PhpOffice\PhpWord\Exception\InvalidObjectException; use PhpOffice\PhpWord\Style\Cell as CellStyle;
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;
/** /**
* Table cell element * Table cell element
*/ */
class Cell class Cell extends Container
{ {
/** /**
* Cell Width * Cell width
* *
* @var int * @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 * @param string $parentType section|header|footer
*/ * @param int $parentId
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 int $width * @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->containerType = 'cell';
$this->_pCount = $pCount;
$this->_width = $width; $this->parentContainer = $parentType;
$this->_style = new \PhpOffice\PhpWord\Style\Cell(); $this->parentContainerId = $parentId;
$this->width = $width;
$this->cellStyle = new CellStyle();
if (!is_null($style)) { if (!is_null($style)) {
if (is_array($style)) { if (is_array($style)) {
@ -86,260 +54,31 @@ class Cell
if (substr($key, 0, 1) != '_') { if (substr($key, 0, 1) != '_') {
$key = '_' . $key; $key = '_' . $key;
} }
$this->_style->setStyleValue($key, $value); $this->cellStyle->setStyleValue($key, $value);
} }
} else { } else {
$this->_style = $style; $this->cellStyle = $style;
} }
} }
} }
/** /**
* Add a Text Element * Get cell style
* *
* @param string $text * @return CellStyle
* @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
*/ */
public function getStyle() public function getStyle()
{ {
return $this->_style; return $this->cellStyle;
} }
/** /**
* Get Cell width * Get cell width
* *
* @return int * @return int
*/ */
public function getWidth() public function getWidth()
{ {
return $this->_width; return $this->width;
} }
} }

View File

@ -9,156 +9,44 @@
namespace PhpOffice\PhpWord\Element; namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Exception\InvalidImageException; use PhpOffice\PhpWord\Container\Container;
use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Style\Paragraph;
/** /**
* Textrun/paragraph element * Textrun/paragraph element
*/ */
class TextRun class TextRun extends Container
{ {
/** /**
* Paragraph style * 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; public function __construct($paragraphStyle = null)
/**
* Create a new TextRun Element
*
* @param mixed $styleParagraph
*/
public function __construct($styleParagraph = null)
{ {
$this->_elementCollection = array(); $this->containerType = 'textrun';
// Set paragraph style // Set paragraph style
if (is_array($styleParagraph)) { if (is_array($paragraphStyle)) {
$this->_styleParagraph = new Paragraph(); $this->paragraphStyle = new Paragraph();
foreach ($paragraphStyle as $key => $value) {
foreach ($styleParagraph as $key => $value) {
if (substr($key, 0, 1) != '_') { if (substr($key, 0, 1) != '_') {
$key = '_' . $key; $key = '_' . $key;
} }
$this->_styleParagraph->setStyleValue($key, $value); $this->paragraphStyle->setStyleValue($key, $value);
} }
} else { } 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 * Get Paragraph style
* *
@ -166,6 +54,6 @@ class TextRun
*/ */
public function getParagraphStyle() public function getParagraphStyle()
{ {
return $this->_styleParagraph; return $this->paragraphStyle;
} }
} }

View File

@ -92,7 +92,7 @@ class CellTest extends \PHPUnit_Framework_TestCase
/** /**
* Add link exception * Add link exception
* @expectedException \PhpOffice\PhpWord\Exception\Exception * @expectedException \BadMethodCallException
*/ */
public function testAddLinkException() public function testAddLinkException()
{ {
@ -272,7 +272,7 @@ class CellTest extends \PHPUnit_Framework_TestCase
/** /**
* Add preserve text exception * Add preserve text exception
* *
* @expectedException \PhpOffice\PhpWord\Exception\Exception * @expectedException \BadMethodCallException
*/ */
public function testAddPreserveTextException() public function testAddPreserveTextException()
{ {