Container abstract class

This commit is contained in:
Ivan Lanin 2014-03-31 23:10:51 +07:00
parent 13e5ca0a84
commit 2bf0bbb094
9 changed files with 537 additions and 720 deletions

View File

@ -25,6 +25,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers
- Writer: Refactor writer classes and make a new Writer abstract class - @ivanlanin GH-160
- Reader: Rename AbstractReader > Reader - @ivanlanin
- General: Refactor folders: Element, Container, and Exception - @ivanlanin
- Container: Create new Container abstract class - @ivanlanin
## 0.9.1 - 27 Mar 2014

View File

@ -0,0 +1,437 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Container;
use PhpOffice\PhpWord\Exception\InvalidImageException;
use PhpOffice\PhpWord\Exception\InvalidObjectException;
use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\TOC;
use PhpOffice\PhpWord\Footnote;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Element\Link;
use PhpOffice\PhpWord\Element\Title;
use PhpOffice\PhpWord\Element\PreserveText;
use PhpOffice\PhpWord\Element\TextBreak;
use PhpOffice\PhpWord\Element\ListItem;
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\Element\Image;
use PhpOffice\PhpWord\Element\Object;
use PhpOffice\PhpWord\Element\Footnote as FootnoteElement;
use PhpOffice\PhpWord\Element\CheckBox;
/**
* Container abstract class
*
* @since 0.9.2
*/
abstract class Container
{
/**
* Container type section|header|footer
*
* @var string
*/
protected $containerType;
/**
* Section Id
*
* @var int
*/
protected $sectionId;
/**
* Footer Element Collection
*
* @var int
*/
protected $elements = array();
/**
* Relation Id
*
* @var int
*/
private $relationId;
/**
* Add text element
*
* @param string $text
* @param mixed $styleFont
* @param mixed $styleParagraph
* @return Text
*/
public function addText($text, $styleFont = null, $styleParagraph = null)
{
if (!String::isUTF8($text)) {
$text = utf8_encode($text);
}
$text = new Text($text, $styleFont, $styleParagraph);
$this->elements[] = $text;
return $text;
}
/**
* Add text break element
*
* @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->elements[] = new TextBreak($fontStyle, $paragraphStyle);
}
}
/**
* Add textrun element
*
* @param mixed $styleParagraph
* @return TextRun
*/
public function addTextRun($styleParagraph = null)
{
$textRun = new TextRun($styleParagraph);
$this->elements[] = $textRun;
return $textRun;
}
/**
* Add link element
*
* @param string $linkSrc
* @param string $linkName
* @param mixed $styleFont
* @param mixed $styleParagraph
* @return Link
* @todo Enable link element in header and footer
*/
public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null)
{
if ($this->containerType != 'section') {
throw new \BadMethodCallException();
}
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, $styleFont, $styleParagraph);
$rID = Media::addSectionLinkElement($linkSrc);
$link->setRelationId($rID);
$this->elements[] = $link;
return $link;
}
/**
* Add a Title Element
*
* @param string $text
* @param int $depth
* @return Title
* @todo Enable title element in header and footer
*/
public function addTitle($text, $depth = 1)
{
if ($this->containerType != 'section') {
throw new \BadMethodCallException();
}
if (!String::isUTF8($text)) {
$text = utf8_encode($text);
}
$styles = Style::getStyles();
if (array_key_exists('Heading_' . $depth, $styles)) {
$style = 'Heading' . $depth;
} else {
$style = null;
}
$title = new Title($text, $depth, $style);
$data = TOC::addTitle($text, $depth);
$anchor = $data[0];
$bookmarkId = $data[1];
$title->setAnchor($anchor);
$title->setBookmarkId($bookmarkId);
$this->elements[] = $title;
return $title;
}
/**
* Add preserve text element
*
* @param string $text
* @param mixed $styleFont
* @param mixed $styleParagraph
* @return PreserveText
*/
public function addPreserveText($text, $styleFont = null, $styleParagraph = null)
{
if ($this->containerType == 'section') {
throw new \BadMethodCallException();
}
if (!String::isUTF8($text)) {
$text = utf8_encode($text);
}
$ptext = new PreserveText($text, $styleFont, $styleParagraph);
$this->elements[] = $ptext;
return $ptext;
}
/**
* Add listitem element
*
* @param string $text
* @param int $depth
* @param mixed $styleFont
* @param mixed $styleList
* @param mixed $styleParagraph
* @return ListItem
* @todo Enable list item element in header and footer
*/
public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null)
{
if ($this->containerType != 'section') {
throw new \BadMethodCallException();
}
if (!String::isUTF8($text)) {
$text = utf8_encode($text);
}
$listItem = new ListItem($text, $depth, $styleFont, $styleList, $styleParagraph);
$this->elements[] = $listItem;
return $listItem;
}
/**
* Add table element
*
* @param mixed $style
* @return Table
*/
public function addTable($style = null)
{
$table = new Table($this->containerType, $this->sectionId, $style);
$this->elements[] = $table;
return $table;
}
/**
* Add image element
*
* @param string $src
* @param mixed $style Image style
* @param boolean $isWatermark
* @return Image
*/
public function addImage($src, $style = null, $isWatermark = false)
{
$image = new Image($src, $style, $isWatermark);
if (!is_null($image->getSource())) {
switch ($this->containerType) {
case 'section':
$rID = Media::addSectionMediaElement($src, 'image', $image);
break;
case 'header':
$rID = Media::addHeaderMediaElement($this->sectionId, $src, $image);
break;
case 'footer':
$rID = Media::addFooterMediaElement($this->sectionId, $src, $image);
break;
}
$image->setRelationId($rID);
$this->elements[] = $image;
return $image;
} else {
throw new InvalidImageException;
}
}
/**
* Add OLE-object element
*
* All exceptions should be handled by PhpOffice\PhpWord\Element\Object
*
* @param string $src
* @param mixed $style
* @return Object
* @todo Enable OLE object element in header and footer
*/
public function addObject($src, $style = null)
{
if ($this->containerType != 'section') {
throw new \BadMethodCallException();
}
$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);
}
$icon = __DIR__ . "/../_staticDocParts/_{$ext}.png";
$rIDimg = Media::addSectionMediaElement($icon, 'image', new Image($icon));
$data = Media::addSectionMediaElement($src, 'oleObject');
$rID = $data[0];
$objectId = $data[1];
$object->setRelationId($rID);
$object->setObjectId($objectId);
$object->setImageRelationId($rIDimg);
$this->elements[] = $object;
return $object;
} else {
throw new InvalidObjectException();
}
}
/**
* Add footnote element
*
* @param mixed $styleParagraph
* @return FootnoteElement
* @todo Enable footnote element in header and footer
*/
public function addFootnote($styleParagraph = null)
{
if ($this->containerType != 'section') {
throw new \BadMethodCallException();
}
$footnote = new FootnoteElement($styleParagraph);
$refID = Footnote::addFootnoteElement($footnote);
$footnote->setReferenceId($refID);
$this->elements[] = $footnote;
return $footnote;
}
/**
* Add a CheckBox Element
*
* @param string $name
* @param string $text
* @param mixed $styleFont
* @param mixed $styleParagraph
* @return CheckBox
* @todo Enable checkbox element in header and footer
*/
public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null)
{
if ($this->containerType != 'section') {
throw new \BadMethodCallException();
}
if (!String::isUTF8($name)) {
$name = utf8_encode($name);
}
if (!String::isUTF8($text)) {
$text = utf8_encode($text);
}
$element = new CheckBox($name, $text, $styleFont, $styleParagraph);
$this->elements[] = $element;
return $element;
}
/**
* Get section number
* getFooterCount
*/
public function getSectionId()
{
return $this->sectionId;
}
/**
* Get all elements
*
* @return array
*/
public function getElements()
{
return $this->elements;
}
/**
* Get relation Id
*
* @return int
*/
public function getRelationId()
{
if ($this->containerType == 'section') {
throw new \BadMethodCallException();
}
return $this->relationId;
}
/**
* Set relation Id
*
* @param int $rId
*/
public function setRelationId($rId)
{
if ($this->containerType == 'section') {
throw new \BadMethodCallException();
}
$this->relationId = $rId;
}
/**
* Add memory image element
*
* @param string $src
* @param mixed $style
* @deprecated 0.9.0
*/
public function addMemoryImage($src, $style = null)
{
return $this->addImage($src, $style);
}
/**
* Create textrun element
*
* @param mixed $styleParagraph
* @deprecated 0.9.2
*/
public function createTextRun($styleParagraph = null)
{
return $this->addTextRun($styleParagraph);
}
/**
* Create footnote element
*
* @param mixed $styleParagraph
* @deprecated 0.9.2
*/
public function createFootnote($styleParagraph = null)
{
return $this->addFootnote($styleParagraph);
}
}

View File

@ -9,192 +9,19 @@
namespace PhpOffice\PhpWord\Container;
use PhpOffice\PhpWord\Exception\InvalidImageException;
use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Element\PreserveText;
use PhpOffice\PhpWord\Element\TextBreak;
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\Element\Image;
/**
* Footer element
*/
class Footer
class Footer extends Container
{
/**
* Footer Count
* Create new instance
*
* @var int
* @param int $sectionId
*/
private $_footerCount;
/**
* Footer Relation ID
*
* @var int
*/
private $_rId;
/**
* Footer Element Collection
*
* @var int
*/
private $_elementCollection = array();
/**
* Create a new Footer
*
* @param int $sectionCount
*/
public function __construct($sectionCount)
public function __construct($sectionId)
{
$this->_footerCount = $sectionCount;
}
/**
* Add a Text Element
*
* @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 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);
}
}
/**
* 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 Table Element
*
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\Table
*/
public function addTable($style = null)
{
$table = new Table('footer', $this->_footerCount, $style);
$this->_elementCollection[] = $table;
return $table;
}
/**
* 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())) {
$rID = Media::addFooterMediaElement($this->_footerCount, $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 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 (!String::isUTF8($text)) {
$text = utf8_encode($text);
}
$ptext = new PreserveText($text, $styleFont, $styleParagraph);
$this->_elementCollection[] = $ptext;
return $ptext;
}
/**
* Get Footer Relation ID
*/
public function getRelationId()
{
return $this->_rId;
}
/**
* Set Footer Relation ID
*
* @param int $rId
*/
public function setRelationId($rId)
{
$this->_rId = $rId;
}
/**
* Get all Footer Elements
* @return array
*/
public function getElements()
{
return $this->_elementCollection;
}
/**
* Get Footer Count
*/
public function getFooterCount()
{
return $this->_footerCount;
$this->containerType = 'footer';
$this->sectionId = $sectionId;
}
}

View File

@ -9,187 +9,39 @@
namespace PhpOffice\PhpWord\Container;
use PhpOffice\PhpWord\Exception\InvalidImageException;
use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Element\PreserveText;
use PhpOffice\PhpWord\Element\TextBreak;
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\Element\Image;
/**
* Header element
*/
class Header
class Header extends Container
{
/**
* Header Count
* Header types constants
*
* @var int
* @var string
* @link http://www.schemacentral.com/sc/ooxml/a-wheaderType-4.html Header or Footer Type
*/
private $_headerCount;
/**
* Header Relation ID
*
* @var int
*/
private $_rId;
const AUTO = 'default'; // Did not use DEFAULT because it is a PHP keyword
const EVEN = 'even';
const FIRST = 'first';
/**
* Header type
*
* @var string
* @link http://www.schemacentral.com/sc/ooxml/a-w_type-4.html Header or Footer Type
*/
private $_type = self::AUTO;
private $headerType = self::AUTO;
/**
* Even Numbered Pages Only
* @var string
* @link http://www.schemacentral.com/sc/ooxml/a-w_type-4.html Header or Footer Type
*/
const EVEN = 'even';
/**
* Default Header or Footer
* @var string
* @link http://www.schemacentral.com/sc/ooxml/a-w_type-4.html Header or Footer Type
*/
const AUTO = 'default'; // Did not use DEFAULT because it is a PHP keyword
/**
* First Page Only
* @var string
* @link http://www.schemacentral.com/sc/ooxml/a-w_type-4.html Header or Footer Type
*/
const FIRST = 'first';
/**
* Header Element Collection
*
* @var int
*/
private $_elementCollection = array();
/**
* Create a new Header
* Create new instance
*
* @param int $sectionCount
*/
public function __construct($sectionCount)
public function __construct($sectionId)
{
$this->_headerCount = $sectionCount;
}
/**
* Add a Text Element
*
* @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 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);
}
}
/**
* 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 Table Element
*
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\Table
*/
public function addTable($style = null)
{
$table = new Table('header', $this->_headerCount, $style);
$this->_elementCollection[] = $table;
return $table;
}
/**
* 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())) {
$rID = Media::addHeaderMediaElement($this->_headerCount, $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 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 (!String::isUTF8($text)) {
$text = utf8_encode($text);
}
$ptext = new PreserveText($text, $styleFont, $styleParagraph);
$this->_elementCollection[] = $ptext;
return $ptext;
$this->containerType = 'header';
$this->sectionId = $sectionId;
}
/**
@ -197,69 +49,27 @@ class Header
*
* @param string $src
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\Image
* @return Image
*/
public function addWatermark($src, $style = null)
{
$image = new Image($src, $style, true);
if (!is_null($image->getSource())) {
$rID = Media::addHeaderMediaElement($this->_headerCount, $src, $image);
$image->setRelationId($rID);
$this->_elementCollection[] = $image;
return $image;
} else {
throw new InvalidImageException;
}
return $this->addImage($src, $style, true);
}
/**
* Get Header Relation ID
*/
public function getRelationId()
{
return $this->_rId;
}
/**
* Set Header Relation ID
*
* @param int $rId
*/
public function setRelationId($rId)
{
$this->_rId = $rId;
}
/**
* Get all Header Elements
*/
public function getElements()
{
return $this->_elementCollection;
}
/**
* Get Header Count
*/
public function getHeaderCount()
{
return $this->_headerCount;
}
/**
* Get Header Type
* Get header type
*/
public function getType()
{
return $this->_type;
return $this->headerType;
}
/**
* Reset back to default
* Reset type to default
*/
public function resetType()
{
return $this->_type = self::AUTO;
return $this->headerType = self::AUTO;
}
/**
@ -267,14 +77,14 @@ class Header
*/
public function firstPage()
{
return $this->_type = self::FIRST;
return $this->headerType = self::FIRST;
}
/**
* Even numbered Pages only
* Even numbered pages only
*/
public function evenPage()
{
return $this->_type = self::EVEN;
return $this->headerType = self::EVEN;
}
}

View File

@ -9,83 +9,55 @@
namespace PhpOffice\PhpWord\Container;
use PhpOffice\PhpWord\Exception\InvalidImageException;
use PhpOffice\PhpWord\Exception\InvalidObjectException;
use PhpOffice\PhpWord\Footnote;
use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\TOC;
use PhpOffice\PhpWord\Container\Footer;
use PhpOffice\PhpWord\Container\Header;
use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Element\Title;
use PhpOffice\PhpWord\Element\Link;
use PhpOffice\PhpWord\Element\TextBreak;
use PhpOffice\PhpWord\Container\Settings;
use PhpOffice\PhpWord\Element\PageBreak;
use PhpOffice\PhpWord\Element\ListItem;
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\Element\Image;
use PhpOffice\PhpWord\Element\Object;
use PhpOffice\PhpWord\Element\CheckBox;
use PhpOffice\PhpWord\Shared\String;
/**
* Section
*/
class Section
class Section extends Container
{
/**
* Section count
*
* @var int
*/
private $_sectionCount;
/**
* Section settings
*
* @var \PhpOffice\PhpWord\Container\Settings
* @var Settings
*/
private $_settings;
private $settings;
/**
* Section Element Collection
* Section headers
*
* @var array
* @var Header[]
*/
private $_elementCollection = array();
private $headers = array();
/**
* Section Headers
* Section footer
*
* @var array
* @var Footer
*/
private $_headers = array();
/**
* Section Footer
*
* @var \PhpOffice\PhpWord\Container\Footer
*/
private $_footer = null;
private $footer = null;
/**
* Create a new Section
* Create new instance
*
* @param int $sectionCount
* @param mixed $settings
*/
public function __construct($sectionCount, $settings = null)
{
$this->_sectionCount = $sectionCount;
$this->_settings = new \PhpOffice\PhpWord\Container\Settings();
$this->containerType = 'section';
$this->sectionId = $sectionCount;
$this->settings = new Settings();
$this->setSettings($settings);
}
/**
* Set Section Settings
* Set section settings
*
* @param array $settings
*/
@ -96,7 +68,7 @@ class Section
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$this->_settings->setSettingValue($key, $value);
$this->settings->setSettingValue($key, $value);
}
}
}
@ -104,71 +76,11 @@ class Section
/**
* Get Section Settings
*
* @return \PhpOffice\PhpWord\Container\Settings
* @return Settings
*/
public function getSettings()
{
return $this->_settings;
}
/**
* Add a Text Element
*
* @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 $styleFont
* @param mixed $styleParagraph
* @return \PhpOffice\PhpWord\Element\Link
*/
public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null)
{
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, $styleFont, $styleParagraph);
$rID = Media::addSectionLinkElement($linkSrc);
$link->setRelationId($rID);
$this->_elementCollection[] = $link;
return $link;
}
/**
* Add a TextBreak Element
*
* @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);
}
return $this->settings;
}
/**
@ -176,104 +88,7 @@ class Section
*/
public function addPageBreak()
{
$this->_elementCollection[] = new PageBreak();
}
/**
* Add a Table Element
*
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\Table
*/
public function addTable($style = null)
{
$table = new Table('section', $this->_sectionCount, $style);
$this->_elementCollection[] = $table;
return $table;
}
/**
* Add a ListItem Element
*
* @param string $text
* @param int $depth
* @param mixed $styleFont
* @param mixed $styleList
* @param mixed $styleParagraph
* @return \PhpOffice\PhpWord\Element\ListItem
*/
public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null)
{
if (!String::isUTF8($text)) {
$text = utf8_encode($text);
}
$listItem = new ListItem($text, $depth, $styleFont, $styleList, $styleParagraph);
$this->_elementCollection[] = $listItem;
return $listItem;
}
/**
* Add a OLE-Object Element
*
* All exceptions should be handled by PhpOffice\PhpWord\Element\Object
*
* @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);
}
$icon = __DIR__ . "/../_staticDocParts/_{$ext}.png";
$rIDimg = Media::addSectionMediaElement($icon, 'image', new Image($icon));
$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 image element
*
* All exceptions should be handled by PhpOffice\PhpWord\Element\Image
*
* @param string $src
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\Image
*/
public function addImage($src, $style = null)
{
$image = new Image($src, $style);
$rID = Media::addSectionMediaElement($src, 'image', $image);
$image->setRelationId($rID);
$this->_elementCollection[] = $image;
return $image;
}
/**
* Add memory image element
*
* @deprecated
*
* @param string $src
* @param mixed $style
*/
public function addMemoryImage($src, $style = null)
{
return $this->addImage($src, $style);
$this->elements[] = new PageBreak();
}
/**
@ -281,82 +96,39 @@ class Section
*
* @param mixed $styleFont
* @param mixed $styleTOC
* @return \PhpOffice\PhpWord\TOC
* @return TOC
*/
public function addTOC($styleFont = null, $styleTOC = null)
{
$toc = new TOC($styleFont, $styleTOC);
$this->_elementCollection[] = $toc;
$this->elements[] = $toc;
return $toc;
}
/**
* Add a Title Element
* Add header
*
* @param string $text
* @param int $depth
* @return \PhpOffice\PhpWord\Element\Title
* @return Header
*/
public function addTitle($text, $depth = 1)
public function addHeader()
{
if (!String::isUTF8($text)) {
$text = utf8_encode($text);
}
$styles = Style::getStyles();
if (array_key_exists('Heading_' . $depth, $styles)) {
$style = 'Heading' . $depth;
} else {
$style = null;
}
$title = new Title($text, $depth, $style);
$data = TOC::addTitle($text, $depth);
$anchor = $data[0];
$bookmarkId = $data[1];
$title->setAnchor($anchor);
$title->setBookmarkId($bookmarkId);
$this->_elementCollection[] = $title;
return $title;
}
/**
* 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;
}
/**
* Get all Elements
*
* @return array
*/
public function getElements()
{
return $this->_elementCollection;
}
/**
* Create a new Header
*
* @return \PhpOffice\PhpWord\Container\Header
*/
public function createHeader()
{
$header = new Header($this->_sectionCount);
$this->_headers[] = $header;
$header = new Header($this->sectionId);
$this->headers[] = $header;
return $header;
}
/**
* Add footer
*
* @return Footer
*/
public function addFooter()
{
$footer = new Footer($this->sectionId);
$this->footer = $footer;
return $footer;
}
/**
* Get Headers
*
@ -364,7 +136,17 @@ class Section
*/
public function getHeaders()
{
return $this->_headers;
return $this->headers;
}
/**
* Get footer element
*
* @return Footer
*/
public function getFooter()
{
return $this->footer;
}
/**
@ -373,73 +155,33 @@ class Section
* If any of the Header instances have a type of Header::FIRST then this method returns true.
* False otherwise.
*
* @return Boolean
* @return boolean
*/
public function hasDifferentFirstPage()
{
$value = array_filter($this->_headers, function (Header &$header) {
$value = array_filter($this->headers, function (Header &$header) {
return $header->getType() == Header::FIRST;
});
return count($value) > 0;
}
/**
* Create a new Footer
* Create header
*
* @return \PhpOffice\PhpWord\Container\Footer
* @deprecated 0.9.2
*/
public function createHeader()
{
return $this->addHeader();
}
/**
* Create footer
*
* @deprecated 0.9.2
*/
public function createFooter()
{
$footer = new Footer($this->_sectionCount);
$this->_footer = $footer;
return $footer;
}
/**
* Get footer element
*
* @return \PhpOffice\PhpWord\Container\Footer
*/
public function getFooter()
{
return $this->_footer;
}
/**
* 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 = Footnote::addFootnoteElement($footnote);
$footnote->setReferenceId($refID);
$this->_elementCollection[] = $footnote;
return $footnote;
}
/**
* 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);
}
$element = new CheckBox($name, $text, $styleFont, $styleParagraph);
$this->_elementCollection[] = $element;
return $element;
return $this->addFooter();
}
}

View File

@ -158,7 +158,7 @@ class Word2007 extends Writer implements IWriter
$footers[++$_cFtrs] = $_footer;
if (!is_null($_footer)) {
$_footer->setRelationId(++$rID);
$_footerCount = $_footer->getFooterCount();
$_footerCount = $_footer->getSectionId();
$_footerFile = 'footer' . $_footerCount . '.xml';
$sectionElements[] = array('target' => $_footerFile, 'type' => 'footer', 'rID' => $rID);
$objZip->addFromString('word/' . $_footerFile, $this->getWriterPart('footer')->writeFooter($_footer));

View File

@ -27,7 +27,7 @@ class FooterTest extends \PHPUnit_Framework_TestCase
$oFooter = new Footer($iVal);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Container\\Footer', $oFooter);
$this->assertEquals($oFooter->getFooterCount(), $iVal);
$this->assertEquals($oFooter->getSectionId(), $iVal);
}
/**

View File

@ -27,7 +27,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase
$oHeader = new Header($iVal);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Container\\Header', $oHeader);
$this->assertEquals($oHeader->getHeaderCount(), $iVal);
$this->assertEquals($oHeader->getSectionId(), $iVal);
$this->assertEquals($oHeader->getType(), Header::AUTO);
}

View File

@ -25,7 +25,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase
public function testGetSettings()
{
$oSection = new Section(0);
$this->assertAttributeEquals($oSection->getSettings(), '_settings', new Section(0));
$this->assertAttributeEquals($oSection->getSettings(), 'settings', new Section(0));
}
/**
@ -34,7 +34,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase
public function testGetElements()
{
$oSection = new Section(0);
$this->assertAttributeEquals($oSection->getElements(), '_elementCollection', new Section(0));
$this->assertAttributeEquals($oSection->getElements(), 'elements', new Section(0));
}
/**
@ -43,7 +43,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase
public function testGetFooter()
{
$oSection = new Section(0);
$this->assertAttributeEquals($oSection->getFooter(), '_footer', new Section(0));
$this->assertAttributeEquals($oSection->getFooter(), 'footer', new Section(0));
}
/**
@ -52,7 +52,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase
public function testGetHeaders()
{
$oSection = new Section(0);
$this->assertAttributeEquals($oSection->getHeaders(), '_headers', new Section(0));
$this->assertAttributeEquals($oSection->getHeaders(), 'headers', new Section(0));
}
/**