Deprecate static classes Footnotes, Endnotes, and TOC (#206); Reactivate `phpcpd` and `phpmd` on Travis

This commit is contained in:
Ivan Lanin 2014-05-04 00:57:44 +07:00
parent 475d948ee3
commit 725162bc6b
51 changed files with 783 additions and 816 deletions

View File

@ -25,14 +25,14 @@ before_script:
- pyrus install pear/PHP_CodeSniffer
- phpenv rehash
## PHP Copy/Paste Detector
#- curl -o phpcpd.phar https://phar.phpunit.de/phpcpd.phar
- curl -o phpcpd.phar https://phar.phpunit.de/phpcpd.phar
## PHP Mess Detector
#- pear config-set preferred_state beta
#- printf "\n" | pecl install imagick
#- pear channel-discover pear.phpmd.org
#- pear channel-discover pear.pdepend.org
#- pear install --alldeps phpmd/PHP_PMD
#- phpenv rehash
- pear config-set preferred_state beta
- printf "\n" | pecl install imagick
- pear channel-discover pear.phpmd.org
- pear channel-discover pear.pdepend.org
- pear install --alldeps phpmd/PHP_PMD
- phpenv rehash
## PHPLOC
#- curl -o phploc.phar https://phar.phpunit.de/phploc.phar
@ -41,9 +41,9 @@ script:
- phpcs --standard=PSR2 -n src/ --ignore=src/PhpWord/Shared/PCLZip
- phpcs --standard=PSR2 -n tests/
## PHP Copy/Paste Detector
#- php phpcpd.phar --verbose src/
- php phpcpd.phar --verbose src/
## PHP Mess Detector
#- phpmd src/ text unusedcode,naming,design
- phpmd src/ text phpmd.xml --exclude pclzip.lib.php
## PHPLOC
#- php phploc.phar src/
## PHPUnit

View File

@ -26,7 +26,7 @@ This release marked heavy refactorings on internal code structure with the creat
- Link: Ability to add link in header/footer - @ivanlanin GH-187
- Object: Ability to add object in header, footer, textrun, and footnote - @ivanlanin GH-187
- Media: Add `Media::resetElements()` to reset all media data - @juzi GH-19
- General: Add `Style::resetStyles()`, `Footnote::resetElements()`, and `TOC::resetTitles()` - @ivanlanin GH-187
- General: Add `Style::resetStyles()` - @ivanlanin GH-187
- DOCX Reader: Ability to read header, footer, footnotes, link, preservetext, textbreak, pagebreak, table, list, image, and title - @ivanlanin
- Endnote: Ability to add endnotes - @ivanlanin
- ListItem: Ability to create custom list and reset list number - @ivanlanin GH-10 GH-198
@ -66,6 +66,7 @@ This release marked heavy refactorings on internal code structure with the creat
- `Element\Link::getLinkSrc` replaced by `Element\Link::getTarget`
- `Element\Link::getLinkName` replaced by `Element\Link::getText`
- `Style\Cell::getDefaultBorderColor`
- Static classes `Footnotes`, `Endnotes`, and `TOC`
### Miscellaneous
@ -87,6 +88,8 @@ This release marked heavy refactorings on internal code structure with the creat
- Refactor: Apply composite pattern for writers - @ivanlanin
- Refactor: Split `AbstractContainer` from `AbstractElement` - @ivanlanin
- Refactor: Apply composite pattern for Word2007 reader - @ivanlanin
- Refactor: Replace static classes `Footnotes`, `Endnotes`, and `TOC` with `Collections` - @ivanlanin GH-206
- QA: Reactivate `phpcpd` and `phpmd` on Travis - @ivanlanin
## 0.9.1 - 27 Mar 2014

View File

@ -0,0 +1,87 @@
<?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\Collection;
/**
* Collection abstract class
*
* @since 0.10.0
*/
abstract class AbstractCollection
{
/**
* Items
*
* @var array
*/
private $items = array();
/**
* Get items
*
* @return array
*/
public function getItems()
{
return $this->items;
}
/**
* Get item by index
*
* @param int $index
* @return mixed
*/
public function getItem($index)
{
if (array_key_exists($index, $this->items)) {
return $this->items[$index];
} else {
return null;
}
}
/**
* Set item
*
* @param int $index
* @param mixed $item
*/
public function setItem($index, $item)
{
if (array_key_exists($index, $this->items)) {
$this->items[$index] = $item;
}
}
/**
* Add new item
*
* @param mixed $item
* @return int
*/
public function addItem($item)
{
$index = $this->countItems() + 1;
$this->items[$index] = $item;
return $index;
}
/**
* Get item count
*
* @return int
*/
public function countItems()
{
return count($this->items);
}
}

View File

@ -0,0 +1,19 @@
<?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\Collection;
/**
* Endnotes collection
*
* @since 0.10.0
*/
class Endnotes extends AbstractCollection
{
}

View File

@ -0,0 +1,19 @@
<?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\Collection;
/**
* Footnotes collection
*
* @since 0.10.0
*/
class Footnotes extends AbstractCollection
{
}

View File

@ -0,0 +1,19 @@
<?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\Collection;
/**
* Titles collection
*
* @since 0.10.0
*/
class Titles extends AbstractCollection
{
}

View File

@ -9,13 +9,17 @@
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Endnotes;
use PhpOffice\PhpWord\Footnotes;
use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\TOC as Titles;
use PhpOffice\PhpWord\Element\CheckBox;
use PhpOffice\PhpWord\Element\Image;
use PhpOffice\PhpWord\Element\Link;
use PhpOffice\PhpWord\Element\ListItem;
use PhpOffice\PhpWord\Element\Object;
use PhpOffice\PhpWord\Element\PreserveText;
use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Element\TextBreak;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Exception\InvalidObjectException;
use PhpOffice\PhpWord\Shared\String;
/**
* Container abstract class
@ -36,8 +40,11 @@ abstract class AbstractContainer extends AbstractElement
*/
protected function addElement(AbstractElement $element)
{
$type = get_class($element);
$type = str_replace('PhpOffice\\PhpWord\\Element\\', '', $type);
$element->setElementIndex($this->countElements() + 1);
$element->setElementId();
$element->setPhpWord($this->phpWord);
$this->elements[] = $element;
}
@ -67,18 +74,17 @@ abstract class AbstractContainer extends AbstractElement
* @param string $text
* @param mixed $fontStyle
* @param mixed $paragraphStyle
* @return Text
* @return \PhpOffice\PhpWord\Element\Text
*/
public function addText($text, $fontStyle = null, $paragraphStyle = null)
{
$this->checkValidity('text');
$this->checkValidity('Text');
// Reset paragraph style for footnote and textrun. They have their own
if (in_array($this->container, array('textrun', 'footnote', 'endnote'))) {
$paragraphStyle = null;
}
$text = String::toUTF8($text);
$textObject = new Text($text, $fontStyle, $paragraphStyle);
$textObject->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($textObject);
@ -90,11 +96,11 @@ abstract class AbstractContainer extends AbstractElement
* Add textrun element
*
* @param mixed $paragraphStyle
* @return TextRun
* @return \PhpOffice\PhpWord\Element\TextRun
*/
public function addTextRun($paragraphStyle = null)
{
$this->checkValidity('textrun');
$this->checkValidity('Textrun');
$textRun = new TextRun($paragraphStyle);
$textRun->setDocPart($this->getDocPart(), $this->getDocPartId());
@ -106,70 +112,39 @@ abstract class AbstractContainer extends AbstractElement
/**
* Add link element
*
* @param string $linkSrc
* @param string $linkName
* @param string $target
* @param string $text
* @param mixed $fontStyle
* @param mixed $paragraphStyle
* @return Link
* @return \PhpOffice\PhpWord\Element\Link
*/
public function addLink($linkSrc, $linkName = null, $fontStyle = null, $paragraphStyle = null)
public function addLink($target, $text = null, $fontStyle = null, $paragraphStyle = null)
{
$this->checkValidity('link');
$this->checkValidity('Link');
$elementDocPart = $this->checkElementDocPart();
$link = new Link(String::toUTF8($linkSrc), String::toUTF8($linkName), $fontStyle, $paragraphStyle);
$link = new Link($target, $text, $fontStyle, $paragraphStyle);
$link->setDocPart($this->getDocPart(), $this->getDocPartId());
$rId = Media::addElement($elementDocPart, 'link', $linkSrc);
$rId = Media::addElement($elementDocPart, 'link', $target);
$link->setRelationId($rId);
$this->addElement($link);
return $link;
}
/**
* Add a Title Element
*
* @param string $text
* @param int $depth
* @return Title
* @todo Enable title element in other containers
*/
public function addTitle($text, $depth = 1)
{
$this->checkValidity('title');
$styles = Style::getStyles();
if (array_key_exists('Heading_' . $depth, $styles)) {
$style = 'Heading' . $depth;
} else {
$style = null;
}
$text = String::toUTF8($text);
$title = new Title($text, $depth, $style);
$title->setDocPart($this->getDocPart(), $this->getDocPartId());
$data = Titles::addTitle($text, $depth);
$anchor = $data[0];
$bookmarkId = $data[1];
$title->setAnchor($anchor);
$title->setBookmarkId($bookmarkId);
$this->addElement($title);
return $title;
}
/**
* Add preserve text element
*
* @param string $text
* @param mixed $fontStyle
* @param mixed $paragraphStyle
* @return PreserveText
* @return \PhpOffice\PhpWord\Element\PreserveText
*/
public function addPreserveText($text, $fontStyle = null, $paragraphStyle = null)
{
$this->checkValidity('preservetext');
$this->checkValidity('PreserveText');
$preserveText = new PreserveText(String::toUTF8($text), $fontStyle, $paragraphStyle);
$preserveText = new PreserveText($text, $fontStyle, $paragraphStyle);
$preserveText->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($preserveText);
@ -185,7 +160,7 @@ abstract class AbstractContainer extends AbstractElement
*/
public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null)
{
$this->checkValidity('textbreak');
$this->checkValidity('TextBreak');
for ($i = 1; $i <= $count; $i++) {
$textBreak = new TextBreak($fontStyle, $paragraphStyle);
@ -200,48 +175,32 @@ abstract class AbstractContainer extends AbstractElement
* @param string $text
* @param int $depth
* @param mixed $fontStyle
* @param mixed $styleList
* @param mixed $listStyle
* @param mixed $paragraphStyle
* @return ListItem
* @return \PhpOffice\PhpWord\Element\ListItem
*/
public function addListItem($text, $depth = 0, $fontStyle = null, $styleList = null, $paragraphStyle = null)
public function addListItem($text, $depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null)
{
$this->checkValidity('listitem');
$this->checkValidity('ListItem');
$listItem = new ListItem(String::toUTF8($text), $depth, $fontStyle, $styleList, $paragraphStyle);
$listItem = new ListItem($text, $depth, $fontStyle, $listStyle, $paragraphStyle);
$listItem->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($listItem);
return $listItem;
}
/**
* Add table element
*
* @param mixed $style
* @return Table
*/
public function addTable($style = null)
{
$this->checkValidity('table');
$table = new Table($this->getDocPart(), $this->getDocPartId(), $style);
$this->addElement($table);
return $table;
}
/**
* Add image element
*
* @param string $src
* @param mixed $style Image style
* @param boolean $isWatermark
* @return Image
* @return \PhpOffice\PhpWord\Element\Image
*/
public function addImage($src, $style = null, $isWatermark = false)
{
$this->checkValidity('image');
$this->checkValidity('Image');
$elementDocPart = $this->checkElementDocPart();
$image = new Image($src, $style, $isWatermark);
@ -260,13 +219,12 @@ abstract class AbstractContainer extends AbstractElement
*
* @param string $src
* @param mixed $style
* @return Object
* @return \PhpOffice\PhpWord\Element\Object
* @throws \PhpOffice\PhpWord\Exception\Exception
* @todo Enable OLE object element in header and footer
*/
public function addObject($src, $style = null)
{
$this->checkValidity('object');
$this->checkValidity('Object');
$elementDocPart = $this->checkElementDocPart();
$object = new Object($src, $style);
@ -294,40 +252,36 @@ abstract class AbstractContainer extends AbstractElement
* Add footnote element
*
* @param mixed $paragraphStyle
* @return Footnote
* @param string $elementName
* @return \PhpOffice\PhpWord\Element\Footnote
*/
public function addFootnote($paragraphStyle = null)
public function addFootnote($paragraphStyle = null, $elementName = 'Footnote')
{
$this->checkValidity('footnote');
$this->checkValidity($elementName);
$docPart = strtolower($elementName);
$addMethod = "add{$elementName}";
$elementClass = 'PhpOffice\\PhpWord\\Element\\' . $elementName;
$footnote = new Footnote($paragraphStyle);
$rId = Footnotes::addElement($footnote);
$note = new $elementClass($paragraphStyle);
// if ($this->phpWord instanceof PhpWord) {
$rId = $this->phpWord->$addMethod($note);
// }
$note->setDocPart($docPart, $this->getDocPartId());
$note->setRelationId($rId);
$this->addElement($note);
$footnote->setDocPart('footnote', $this->getDocPartId());
$footnote->setRelationId($rId);
$this->addElement($footnote);
return $footnote;
return $note;
}
/**
* Add endnote element
*
* @param mixed $paragraphStyle
* @return Endnote
* @return \PhpOffice\PhpWord\Element\Endnote
*/
public function addEndnote($paragraphStyle = null)
{
$this->checkValidity('endnote');
$endnote = new Endnote($paragraphStyle);
$rId = Endnotes::addElement($endnote);
$endnote->setDocPart('endnote', $this->getDocPartId());
$endnote->setRelationId($rId);
$this->addElement($endnote);
return $endnote;
return $this->addFootnote($paragraphStyle, 'Endnote');
}
/**
@ -337,13 +291,13 @@ abstract class AbstractContainer extends AbstractElement
* @param string $text
* @param mixed $fontStyle
* @param mixed $paragraphStyle
* @return CheckBox
* @return \PhpOffice\PhpWord\Element\CheckBox
*/
public function addCheckBox($name, $text, $fontStyle = null, $paragraphStyle = null)
{
$this->checkValidity('checkbox');
$this->checkValidity('CheckBox');
$checkBox = new CheckBox(String::toUTF8($name), String::toUTF8($text), $fontStyle, $paragraphStyle);
$checkBox = new CheckBox($name, $text, $fontStyle, $paragraphStyle);
$checkBox->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($checkBox);
@ -361,26 +315,24 @@ abstract class AbstractContainer extends AbstractElement
// Valid containers for each element
$allContainers = array('section', 'header', 'footer', 'cell', 'textrun', 'footnote', 'endnote');
$validContainers = array(
'text' => $allContainers,
'link' => $allContainers,
'textbreak' => $allContainers,
'image' => $allContainers,
'object' => $allContainers,
'textrun' => array('section', 'header', 'footer', 'cell'),
'listitem' => array('section', 'header', 'footer', 'cell'),
'checkbox' => array('section', 'header', 'footer', 'cell'),
'table' => array('section', 'header', 'footer'),
'footnote' => array('section', 'textrun', 'cell'),
'endnote' => array('section', 'textrun', 'cell'),
'preservetext' => array('header', 'footer', 'cell'),
'title' => array('section'),
'Text' => $allContainers,
'Link' => $allContainers,
'TextBreak' => $allContainers,
'Image' => $allContainers,
'Object' => $allContainers,
'TextRun' => array('section', 'header', 'footer', 'cell'),
'ListItem' => array('section', 'header', 'footer', 'cell'),
'CheckBox' => array('section', 'header', 'footer', 'cell'),
'Footnote' => array('section', 'textrun', 'cell'),
'Endnote' => array('section', 'textrun', 'cell'),
'PreserveText' => array('header', 'footer', 'cell'),
);
// Special condition, e.g. preservetext can only exists in cell when
// the cell is located in header or footer
$validSubcontainers = array(
'preservetext' => array(array('cell'), array('header', 'footer')),
'footnote' => array(array('cell', 'textrun'), array('section')),
'endnote' => array(array('cell', 'textrun'), array('section')),
'PreserveText' => array(array('cell'), array('header', 'footer')),
'Footnote' => array(array('cell', 'textrun'), array('section')),
'Endnote' => array(array('cell', 'textrun'), array('section')),
);
// Check if a method is valid for current container

View File

@ -9,6 +9,7 @@
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style;
/**
@ -18,6 +19,11 @@ use PhpOffice\PhpWord\Style;
*/
abstract class AbstractElement
{
/**
* PhpWord object
*/
protected $phpWord;
/**
* Container type section|header|footer|cell|textrun|footnote|endnote
*
@ -75,6 +81,26 @@ abstract class AbstractElement
*/
protected $relationId;
/**
* Get PhpWord
*
* @return \PhpOffice\PhpWord\PhpWord
*/
public function getPhpWord()
{
return $this->phpWord;
}
/**
* Set PhpWord
*
* @param \PhpOffice\PhpWord\PhpWord
*/
public function setPhpWord(PhpWord &$phpWord = null)
{
$this->phpWord = $phpWord;
}
/**
* Get section number
*

View File

@ -9,6 +9,8 @@
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Shared\String;
/**
* Check box element
*/
@ -44,7 +46,7 @@ class CheckBox extends Text
*/
public function setName($name)
{
$this->name = $name;
$this->name = String::toUTF8($name);
return $this;
}

View File

@ -9,21 +9,36 @@
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Element\Table;
/**
* Footer element
*/
class Footer extends AbstractContainer
{
/**
* Header/footer types constants
*
* @var string
* @link http://www.schemacentral.com/sc/ooxml/a-wtype-4.html Header or Footer Type
*/
const AUTO = 'default'; // default and odd pages
const FIRST = 'first';
const EVEN = 'even';
/**
* Container type
*
* @var string
*/
protected $container = 'footer';
/**
* Header type
*
* @var string
*/
private $type = self::AUTO;
protected $type = self::AUTO;
/**
* Create new instance
@ -32,12 +47,11 @@ class Footer extends AbstractContainer
* @param int $footerId
* @param string $type
*/
public function __construct($sectionId, $footerId = 1, $type = self::AUTO)
public function __construct($sectionId, $containerId = 1, $type = self::AUTO)
{
$this->container = 'footer';
$this->sectionId = $sectionId;
$this->setType($type);
$this->setDocPart($this->container, ($sectionId - 1) * 3 + $footerId);
$this->setDocPart($this->container, ($sectionId - 1) * 3 + $containerId);
}
/**
@ -48,6 +62,9 @@ class Footer extends AbstractContainer
*/
public function setType($value = self::AUTO)
{
if (!in_array($value, array(self::AUTO, self::FIRST, self::EVEN))) {
$value = self::AUTO;
}
$this->type = $value;
}
@ -61,4 +78,49 @@ class Footer extends AbstractContainer
{
return $this->type;
}
/**
* Reset type to default
*
* @return string
*/
public function resetType()
{
return $this->type = self::AUTO;
}
/**
* First page only header
*
* @return string
*/
public function firstPage()
{
return $this->type = self::FIRST;
}
/**
* Even numbered pages only
*
* @return string
*/
public function evenPage()
{
return $this->type = self::EVEN;
}
/**
* Add table element
*
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\Table
* @todo Merge with the same function on Section
*/
public function addTable($style = null)
{
$table = new Table($this->getDocPart(), $this->getDocPartId(), $style);
$this->addElement($table);
return $table;
}
}

View File

@ -14,39 +14,15 @@ use PhpOffice\PhpWord\Element\Image;
/**
* Header element
*/
class Header extends AbstractContainer
class Header extends Footer
{
/**
* Header types constants
*
* @var string
* @link http://www.schemacentral.com/sc/ooxml/a-wtype-4.html Header or Footer Type
*/
const AUTO = 'default'; // default and odd pages
const FIRST = 'first';
const EVEN = 'even';
/**
* Header type
* Container type
*
* @var string
*/
private $type = self::AUTO;
/**
* Create new instance
*
* @param int $sectionId
* @param int $headerId
* @param string $type
*/
public function __construct($sectionId, $headerId = 1, $type = self::AUTO)
{
$this->container = 'header';
$this->sectionId = $sectionId;
$this->setType($type);
$this->setDocPart($this->container, ($sectionId - 1) * 3 + $headerId);
}
protected $container = 'header';
/**
* Add a Watermark Element
@ -59,58 +35,4 @@ class Header extends AbstractContainer
{
return $this->addImage($src, $style, true);
}
/**
* Set header type
*
* @param string $value
* @since 0.10.0
*/
public function setType($value = self::AUTO)
{
if (!in_array($value, array(self::AUTO, self::FIRST, self::EVEN))) {
$value = self::AUTO;
}
$this->type = $value;
}
/**
* Get header type
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Reset type to default
*
* @return string
*/
public function resetType()
{
return $this->type = self::AUTO;
}
/**
* First page only header
*
* @return string
*/
public function firstPage()
{
return $this->type = self::FIRST;
}
/**
* Even numbered pages only
*
* @return string
*/
public function evenPage()
{
return $this->type = self::EVEN;
}
}

View File

@ -9,6 +9,7 @@
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
@ -56,8 +57,8 @@ class Link extends AbstractElement
*/
public function __construct($target, $text = null, $fontStyle = null, $paragraphStyle = null)
{
$this->target = $target;
$this->text = is_null($text) ? $target : $text;
$this->target = String::toUTF8($target);
$this->text = is_null($text) ? $this->target : String::toUTF8($text);
$this->fontStyle = $this->setStyle(new Font('text'), $fontStyle);
$this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle);

View File

@ -9,6 +9,7 @@
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Style\ListItem as ListItemStyle;
/**
@ -49,7 +50,7 @@ class ListItem extends AbstractElement
*/
public function __construct($text, $depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null)
{
$this->textObject = new Text($text, $fontStyle, $paragraphStyle);
$this->textObject = new Text(String::toUTF8($text), $fontStyle, $paragraphStyle);
$this->depth = $depth;
// Version >= 0.10.0 will pass numbering style name. Older version will use old method

View File

@ -9,6 +9,7 @@
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
@ -52,7 +53,8 @@ class PreserveText extends AbstractElement
$this->fontStyle = $this->setStyle(new Font('text'), $fontStyle);
$this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle);
$matches = preg_split('/({.*?})/', $text, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$this->text = String::toUTF8($text);
$matches = preg_split('/({.*?})/', $this->text, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
if (isset($matches[0])) {
$this->text = $matches;
}

View File

@ -63,6 +63,7 @@ class Row extends AbstractElement
public function addCell($width = null, $style = null)
{
$cell = new Cell($this->getDocPart(), $this->getDocPartId(), $width, $style);
$cell->setPhpWord($this->phpWord);
$this->cells[] = $cell;
return $cell;
}

View File

@ -9,10 +9,12 @@
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Element\PageBreak;
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\Element\TOC;
use PhpOffice\PhpWord\Element\Title;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Style\Section as SectionSettings;
use PhpOffice\PhpWord\Element\PageBreak;
use PhpOffice\PhpWord\Element\TOC;
/**
* Section
@ -82,12 +84,47 @@ class Section extends AbstractContainer
return $this->settings;
}
/**
* Add a Title Element
*
* @param string $text
* @param int $depth
* @return \PhpOffice\PhpWord\Element\Title
*/
public function addTitle($text, $depth = 1)
{
$title = new Title($text, $depth);
$title->setDocPart($this->getDocPart(), $this->getDocPartId());
if ($this->phpWord instanceof PhpWord) {
$bookmarkId = $this->phpWord->addTitle($title);
$title->setBookmarkId($bookmarkId);
}
$this->addElement($title);
return $title;
}
/**
* Add a PageBreak Element
*/
public function addPageBreak()
{
$this->elements[] = new PageBreak();
$this->addElement(new PageBreak());
}
/**
* Add table element
*
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\Table
* @todo Merge with the same function on Footer
*/
public function addTable($style = null)
{
$table = new Table($this->getDocPart(), $this->getDocPartId(), $style);
$this->addElement($table);
return $table;
}
/**
@ -102,7 +139,8 @@ class Section extends AbstractContainer
public function addTOC($fontStyle = null, $tocStyle = null, $minDepth = 1, $maxDepth = 9)
{
$toc = new TOC($fontStyle, $tocStyle, $minDepth, $maxDepth);
$this->elements[] = $toc;
$this->addElement($toc);
return $toc;
}
@ -187,6 +225,8 @@ class Section extends AbstractContainer
if (in_array($type, array(Header::AUTO, Header::FIRST, Header::EVEN))) {
$index = count($collection);
$container = new $containerClass($this->sectionId, ++$index, $type);
$container->setPhpWord($this->phpWord);
$collection[$index] = $container;
return $container;
} else {

View File

@ -9,7 +9,7 @@
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\TOC as Titles;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\TOC as TOCStyle;
@ -87,16 +87,20 @@ class TOC extends AbstractElement
*/
public function getTitles()
{
$titles = Titles::getTitles();
if (!$this->phpWord instanceof PhpWord) {
return array();
}
$titles = $this->phpWord->getTitles()->getItems();
foreach ($titles as $i => $title) {
if ($this->minDepth > $title['depth']) {
$depth = $title->getDepth();
if ($this->minDepth > $depth) {
unset($titles[$i]);
}
if (($this->maxDepth != 0) && ($this->maxDepth < $title['depth'])) {
if (($this->maxDepth != 0) && ($this->maxDepth < $depth)) {
unset($titles[$i]);
}
}
$titles = array_merge(array(), $titles);
return $titles;
}

View File

@ -60,6 +60,7 @@ class Table extends AbstractElement
public function addRow($height = null, $style = null)
{
$row = new Row($this->getDocPart(), $this->getDocPartId(), $height, $style);
$row->setPhpWord($this->phpWord);
$this->rows[] = $row;
return $row;
}

View File

@ -9,6 +9,7 @@
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
@ -127,7 +128,7 @@ class Text extends AbstractElement
*/
public function setText($text)
{
$this->text = $text;
$this->text = String::toUTF8($text);
return $this;
}

View File

@ -9,6 +9,9 @@
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Shared\String;
/**
* Title element
*/
@ -40,10 +43,10 @@ class Title extends AbstractElement
*
* @var int
*/
private $bookmarkId;
private $bookmarkId = 1;
/**
* Title style
* Name of the heading style, e.g. 'Heading1'
*
* @var string
*/
@ -55,40 +58,20 @@ class Title extends AbstractElement
*
* @param string $text
* @param int $depth
* @param string $style Name of the heading style, e.g. 'Heading1'
* @param string $style
*/
public function __construct($text, $depth = 1, $style = null)
public function __construct($text, $depth = 1)
{
if (!is_null($style)) {
$this->style = $style;
$this->text = String::toUTF8($text);
$this->depth = $depth;
if (array_key_exists('Heading_' . $this->depth, Style::getStyles())) {
$this->style = 'Heading' . $this->depth;
}
$this->text = $text;
$this->depth = $depth;
return $this;
}
/**
* Set Anchor
*
* @param int $anchor
*/
public function setAnchor($anchor)
{
$this->anchor = $anchor;
}
/**
* Get Anchor
*
* @return int
*/
public function getAnchor()
{
return $this->anchor;
}
/**
* Set Bookmark ID
*
@ -138,4 +121,28 @@ class Title extends AbstractElement
{
return $this->style;
}
/**
* Set Anchor
*
* @param int $anchor
* @deprecated 0.10.0
* @codeCoverageIgnore
*/
public function setAnchor($anchor)
{
$this->anchor = $anchor;
}
/**
* Get Anchor
*
* @return int
* @deprecated 0.10.0
* @codeCoverageIgnore
*/
public function getAnchor()
{
return '_Toc' . (252634154 + $this->bookmarkId);
}
}

View File

@ -1,95 +0,0 @@
<?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;
/**
* Endnote collection
*
* @since 0.10.0
*/
class Endnotes
{
/**
* Elements
*
* @var array
*/
private static $elements = array();
/**
* Add new element
*
* @param \PhpOffice\PhpWord\Element\Endnote $element
* @return integer Reference ID
*/
public static function addElement($element)
{
$rId = self::countElements() + 1;
self::$elements[$rId] = $element;
return $rId;
}
/**
* Set element
*
* @param integer $index
* @param \PhpOffice\PhpWord\Element\Endnote $element
*/
public static function setElement($index, $element)
{
if (array_key_exists($index, self::$elements)) {
self::$elements[$index] = $element;
}
}
/**
* Get element by index
*
* @param integer $index
* @return \PhpOffice\PhpWord\Element\Endnote
*/
public static function getElement($index)
{
if (array_key_exists($index, self::$elements)) {
return self::$elements[$index];
} else {
return null;
}
}
/**
* Get elements
*
* @return array
*/
public static function getElements()
{
return self::$elements;
}
/**
* Get element count
*
* @return integer
*/
public static function countElements()
{
return count(self::$elements);
}
/**
* Reset elements
*/
public static function resetElements()
{
self::$elements = array();
}
}

View File

@ -11,6 +11,12 @@ namespace PhpOffice\PhpWord;
/**
* Footnote collection
*
* This static class has been deprecated and replaced by Collection\Footnotes.
* File maintained for backward compatibility and will be removed on 1.0.
*
* @deprecated 0.10.0
* @codeCoverageIgnore
*/
class Footnotes
{
@ -26,7 +32,6 @@ class Footnotes
*
* @param \PhpOffice\PhpWord\Element\Footnote $element
* @return integer Reference ID
* @since 0.10.0
*/
public static function addElement($element)
{
@ -41,7 +46,6 @@ class Footnotes
*
* @param integer $index
* @param \PhpOffice\PhpWord\Element\Footnote $element
* @since 0.10.0
*/
public static function setElement($index, $element)
{
@ -55,7 +59,6 @@ class Footnotes
*
* @param integer $index
* @return \PhpOffice\PhpWord\Element\Footnote
* @since 0.10.0
*/
public static function getElement($index)
{
@ -70,7 +73,6 @@ class Footnotes
* Get elements
*
* @return array
* @since 0.10.0
*/
public static function getElements()
{
@ -81,7 +83,6 @@ class Footnotes
* Get element count
*
* @return integer
* @since 0.10.0
*/
public static function countElements()
{
@ -90,8 +91,6 @@ class Footnotes
/**
* Reset elements
*
* @since 0.10.0
*/
public static function resetElements()
{
@ -103,8 +102,6 @@ class Footnotes
*
* @param \PhpOffice\PhpWord\Element\Footnote $element
* @return integer Reference ID
* @deprecated 0.10.0
* @codeCoverageIgnore
*/
public static function addFootnoteElement($element)
{
@ -115,8 +112,6 @@ class Footnotes
* Get Footnote Elements
*
* @return array
* @deprecated 0.10.0
* @codeCoverageIgnore
*/
public static function getFootnoteElements()
{
@ -127,8 +122,6 @@ class Footnotes
* Get Footnote Elements Count
*
* @return integer
* @deprecated 0.10.0
* @codeCoverageIgnore
*/
public static function countFootnoteElements()
{
@ -140,8 +133,6 @@ class Footnotes
*
* @param string $linkSrc
* @return integer Reference ID
* @deprecated 0.10.0
* @codeCoverageIgnore
*/
public static function addFootnoteLinkElement($linkSrc)
{
@ -152,8 +143,6 @@ class Footnotes
* Get Footnote Link Elements
*
* @return array
* @deprecated 0.10.0
* @codeCoverageIgnore
*/
public static function getFootnoteLinkElements()
{

View File

@ -10,10 +10,13 @@
namespace PhpOffice\PhpWord;
use PhpOffice\PhpWord\DocumentProperties;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Template;
use PhpOffice\PhpWord\Collection\Titles;
use PhpOffice\PhpWord\Collection\Footnotes;
use PhpOffice\PhpWord\Collection\Endnotes;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Element\Section;
/**
* PHPWord main class
@ -39,6 +42,34 @@ class PhpWord
*/
private $documentProperties;
/**
* Collection of sections
*
* @var \PhpOffice\PhpWord\Element\Section[]
*/
private $sections = array();
/**
* Collection of titles
*
* @var \PhpOffice\PhpWord\Collection\Titles
*/
private $titles;
/**
* Collection of footnotes
*
* @var \PhpOffice\PhpWord\Collection\Footnotes
*/
private $footnotes;
/**
* Collection of endnotes
*
* @var \PhpOffice\PhpWord\Collection\Endnotes
*/
private $endnotes;
/**
* Default font name
*
@ -52,19 +83,15 @@ class PhpWord
*/
private $defaultFontSize;
/**
* Collection of sections
*
* @var \PhpOffice\PhpWord\Element\Section[]
*/
private $sections = array();
/**
* Create new
*/
public function __construct()
{
$this->documentProperties = new DocumentProperties();
$this->titles = new Titles();
$this->footnotes = new Footnotes();
$this->endnotes = new Endnotes();
$this->defaultFontName = self::DEFAULT_FONT_NAME;
$this->defaultFontSize = self::DEFAULT_FONT_SIZE;
}
@ -92,6 +119,16 @@ class PhpWord
return $this;
}
/**
* Get all sections
*
* @return \PhpOffice\PhpWord\Element\Section[]
*/
public function getSections()
{
return $this->sections;
}
/**
* Create new section
*
@ -101,11 +138,75 @@ class PhpWord
public function addSection($settings = null)
{
$section = new Section(count($this->sections) + 1, $settings);
$section->setPhpWord($this);
$this->sections[] = $section;
return $section;
}
/**
* Get titles
*
* @return \PhpOffice\PhpWord\Collection\Titles
*/
public function getTitles()
{
return $this->titles;
}
/**
* Add new title
*
* @param \PhpOffice\PhpWord\Element\Title $title
* @return int
*/
public function addTitle($title)
{
return $this->titles->addItem($title);
}
/**
* Get footnotes
*
* @return \PhpOffice\PhpWord\Collection\Footnotes
*/
public function getFootnotes()
{
return $this->footnotes;
}
/**
* Add new footnote
*
* @param \PhpOffice\PhpWord\Element\Footnote $footnote
* @return int
*/
public function addFootnote($footnote)
{
return $this->footnotes->addItem($footnote);
}
/**
* Get endnotes
*
* @return \PhpOffice\PhpWord\Collection\Endnotes
*/
public function getEndnotes()
{
return $this->endnotes;
}
/**
* Add new endnote
*
* @param \PhpOffice\PhpWord\Element\Endnote $endnote
* @return int
*/
public function addEndnote($endnote)
{
return $this->endnotes->addItem($endnote);
}
/**
* Get default font name
*
@ -225,16 +326,6 @@ class PhpWord
Style::addNumberingStyle($styleName, $styles);
}
/**
* Get all sections
*
* @return \PhpOffice\PhpWord\Element\Section[]
*/
public function getSections()
{
return $this->sections;
}
/**
* Load template by filename
*

View File

@ -1,63 +0,0 @@
<?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\Reader\Word2007;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Core/extended document properties reader
*/
class DocProps extends AbstractPart
{
/**
* Property mapping
*
* @var array
*/
protected $mapping = array();
/**
* Callback functions
*
* @var array
*/
protected $callbacks = array();
/**
* Read core/extended document properties
*
* @param \PhpOffice\PhpWord\PhpWord $phpWord
*/
public function read(PhpWord &$phpWord)
{
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
$docProps = $phpWord->getDocumentProperties();
$nodes = $xmlReader->getElements('*');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
if (!array_key_exists($node->nodeName, $this->mapping)) {
continue;
}
$method = $this->mapping[$node->nodeName];
$value = $node->nodeValue == '' ? null : $node->nodeValue;
if (array_key_exists($node->nodeName, $this->callbacks)) {
$value = $this->callbacks[$node->nodeName]($value);
}
if (method_exists($docProps, $method)) {
$docProps->$method($value);
}
}
}
}
}

View File

@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Reader\Word2007;
/**
* Extended properties reader
*/
class DocPropsApp extends DocProps
class DocPropsApp extends DocPropsCore
{
/**
* Property mapping
@ -20,4 +20,11 @@ class DocPropsApp extends DocProps
* @var array
*/
protected $mapping = array('Company' => 'setCompany', 'Manager' => 'setManager');
/**
* Callback functions
*
* @var array
*/
protected $callbacks = array();
}

View File

@ -9,10 +9,13 @@
namespace PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Core properties reader
*/
class DocPropsCore extends DocProps
class DocPropsCore extends AbstractPart
{
/**
* Property mapping
@ -37,4 +40,34 @@ class DocPropsCore extends DocProps
* @var array
*/
protected $callbacks = array('dcterms:created' => 'strtotime', 'dcterms:modified' => 'strtotime');
/**
* Read core/extended document properties
*
* @param \PhpOffice\PhpWord\PhpWord $phpWord
*/
public function read(PhpWord &$phpWord)
{
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
$docProps = $phpWord->getDocumentProperties();
$nodes = $xmlReader->getElements('*');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
if (!array_key_exists($node->nodeName, $this->mapping)) {
continue;
}
$method = $this->mapping[$node->nodeName];
$value = $node->nodeValue == '' ? null : $node->nodeValue;
if (array_key_exists($node->nodeName, $this->callbacks)) {
$value = $this->callbacks[$node->nodeName]($value);
}
if (method_exists($docProps, $method)) {
$docProps->$method($value);
}
}
}
}
}

View File

@ -32,8 +32,8 @@ class Notes extends AbstractPart
public function read(PhpWord &$phpWord)
{
$this->type = ($this->type == 'endnotes') ? 'endnotes' : 'footnotes';
$collectionClass = 'PhpOffice\\PhpWord\\' . ucfirst($this->type);
$collection = $collectionClass::getElements();
$getMethod = 'get' . $this->type;
$collection = $phpWord->$getMethod()->getItems();
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
@ -49,9 +49,10 @@ class Notes extends AbstractPart
$element = $collection[$id];
$pNodes = $xmlReader->getElements('w:p/*', $node);
foreach ($pNodes as $pNode) {
$this->readRun($xmlReader, $pNode, $element, $type);
$this->readRun($xmlReader, $pNode, $element, $this->type);
}
$collectionClass::setElement($id, $element);
$addMethod = 'add' . ($this->type == 'endnotes' ? 'endnote' : 'footnote');
$phpWord->$addMethod($element);
}
}
}

View File

@ -12,26 +12,12 @@ namespace PhpOffice\PhpWord\Style;
/**
* TOC style
*/
class TOC extends AbstractStyle
class TOC extends Tab
{
const TABLEADER_DOT = 'dot';
const TABLEADER_UNDERSCORE = 'underscore';
const TABLEADER_LINE = 'hyphen';
const TABLEADER_NONE = '';
/**
* Tab Leader
*
* @var string
*/
private $tabLeader;
/**
* Tab Position
*
* @var int
*/
private $tabPos;
const TABLEADER_DOT = self::TAB_LEADER_DOT;
const TABLEADER_UNDERSCORE = self::TAB_LEADER_UNDERSCORE;
const TABLEADER_LINE = self::TAB_LEADER_HYPHEN;
const TABLEADER_NONE = self::TAB_LEADER_NONE;
/**
* Indent
@ -46,8 +32,7 @@ class TOC extends AbstractStyle
*/
public function __construct()
{
$this->tabPos = 9062;
$this->tabLeader = self::TABLEADER_DOT;
parent::__construct(self::TAB_STOP_RIGHT, 9062, self::TABLEADER_DOT);
$this->indent = 200;
}
@ -58,17 +43,17 @@ class TOC extends AbstractStyle
*/
public function getTabPos()
{
return $this->tabPos;
return $this->getPosition();
}
/**
* Set Tab Position
*
* @param int $pValue
* @param int $value
*/
public function setTabPos($pValue)
public function setTabPos($value)
{
$this->tabPos = $pValue;
$this->position = $value;
}
/**
@ -78,17 +63,17 @@ class TOC extends AbstractStyle
*/
public function getTabLeader()
{
return $this->tabLeader;
return $this->getLeader();
}
/**
* Set Tab Leader
*
* @param string $pValue
* @param string $value
*/
public function setTabLeader($pValue = self::TABLEADER_DOT)
public function setTabLeader($value = self::TABLEADER_DOT)
{
$this->tabLeader = $pValue;
$this->leader = $value;
}
/**
@ -104,10 +89,10 @@ class TOC extends AbstractStyle
/**
* Set Indent
*
* @param string $pValue
* @param string $value
*/
public function setIndent($pValue)
public function setIndent($value)
{
$this->indent = $pValue;
$this->indent = $value;
}
}

View File

@ -44,7 +44,7 @@ class Tab extends AbstractStyle
*
* @var string
*/
private $val = self::TAB_STOP_CLEAR;
private $type = self::TAB_STOP_CLEAR;
/**
* Tab leader character
@ -54,22 +54,22 @@ class Tab extends AbstractStyle
private $leader = self::TAB_LEADER_NONE;
/**
* Tab stop position
* Tab stop position (twip)
*
* @var int
* @var int|float
*/
private $position = 0;
/**
* Create a new instance of Tab. Both $val and $leader
* Create a new instance of Tab. Both $type and $leader
* must conform to the values put forth in the schema. If they do not
* they will be changed to default values.
*
* @param string $val Defaults to 'clear' if value is not possible.
* @param string $type Defaults to 'clear' if value is not possible.
* @param int $position Must be numeric; otherwise defaults to 0.
* @param string $leader Defaults to null if value is not possible.
*/
public function __construct($val = null, $position = 0, $leader = null)
public function __construct($type = null, $position = 0, $leader = null)
{
$stopTypes = array(
self::TAB_STOP_CLEAR, self::TAB_STOP_LEFT,self::TAB_STOP_CENTER,
@ -80,7 +80,7 @@ class Tab extends AbstractStyle
self::TAB_LEADER_UNDERSCORE, self::TAB_LEADER_HEAVY, self::TAB_LEADER_MIDDLEDOT
);
$this->val = $this->setEnumVal($val, $stopTypes, $this->val);
$this->type = $this->setEnumVal($type, $stopTypes, $this->type);
$this->position = $this->setNumericVal($position, $this->position);
$this->leader = $this->setEnumVal($leader, $leaderTypes, $this->leader);
}
@ -90,9 +90,21 @@ class Tab extends AbstractStyle
*
* @return string
*/
public function getStopType()
public function getType()
{
return $this->val;
return $this->type;
}
/**
* Set stop type
*
* @param string $value
*/
public function setType($value)
{
$enum = array(self::TAB_STOP_CLEAR, self::TAB_STOP_LEFT, self::TAB_STOP_CENTER,
self::TAB_STOP_RIGHT, self::TAB_STOP_DECIMAL, self::TAB_STOP_BAR, self::TAB_STOP_NUM);
$this->type = $this->setEnumVal($value, $enum, $this->type);
}
/**
@ -105,13 +117,35 @@ class Tab extends AbstractStyle
return $this->leader;
}
/**
* Set leader
*
* @param string $value
*/
public function setLeader($value)
{
$enum = array(self::TAB_LEADER_NONE, self::TAB_LEADER_DOT, self::TAB_LEADER_HYPHEN,
self::TAB_LEADER_UNDERSCORE, self::TAB_LEADER_HEAVY, self::TAB_LEADER_MIDDLEDOT);
$this->leader = $this->setEnumVal($value, $enum, $this->leader);
}
/**
* Get position
*
* @return integer
* @return int|float
*/
public function getPosition()
{
return $this->position;
}
/**
* Set position
*
* @param int|float $value
*/
public function setPosition($value)
{
$this->position = $this->setNumericVal($value, $this->position);
}
}

View File

@ -11,6 +11,12 @@ namespace PhpOffice\PhpWord;
/**
* Table of contents
*
* This static class has been deprecated and replaced by Collection\Titles.
* File maintained for backward compatibility and will be removed on 1.0.
*
* @deprecated 0.10.0
* @codeCoverageIgnore
*/
class TOC
{

View File

@ -167,14 +167,16 @@ class HTML extends AbstractWriter implements WriterInterface
*/
private function writeNotes()
{
$phpWord = $this->getPhpWord();
$html = '';
if (!empty($this->notes)) {
$html .= "<hr />";
foreach ($this->notes as $noteId => $noteMark) {
$noteAnchor = "note-{$noteId}";
list($noteType, $noteTypeId) = explode('-', $noteMark);
$collectionObject = 'PhpOffice\\PhpWord\\' . ($noteType == 'endnote' ? 'Endnotes' : 'Footnotes');
$collection = $collectionObject::getElements();
$method = 'get' . ($noteType == 'endnote' ? 'Endnotes' : 'Footnotes');
$collection = $phpWord->$method()->getItems();
if (array_key_exists($noteTypeId, $collection)) {
$element = $collection[$noteTypeId];
$elmWriter = new TextRunWriter($this, $element, true);

View File

@ -14,6 +14,12 @@ namespace PhpOffice\PhpWord\Writer\HTML\Element;
*
* @since 0.10.0
*/
class Endnote extends Note
class Endnote extends Footnote
{
/**
* Note type
*
* @var string
*/
protected $noteType = 'endnote';
}

View File

@ -14,6 +14,27 @@ namespace PhpOffice\PhpWord\Writer\HTML\Element;
*
* @since 0.10.0
*/
class Footnote extends Note
class Footnote extends Element
{
/**
* Note type footnote|endnote
*
* @var string
*/
protected $noteType = 'footnote';
/**
* Write footnote/endnote marks
*
* @return string
*/
public function write()
{
$noteId = count($this->parentWriter->getNotes()) + 1;
$noteMark = $this->noteType . '-' . $this->element->getRelationId();
$this->parentWriter->addNote($noteId, $noteMark);
$html = "<a name=\"{$noteMark}\"><a href=\"#note-{$noteId}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>";
return $html;
}
}

View File

@ -1,34 +0,0 @@
<?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\Writer\HTML\Element;
/**
* Note element HTML writer
*
* @since 0.10.0
*/
class Note extends Element
{
/**
* Write footnote/endnote marks
*
* @return string
*/
public function write()
{
$noteId = count($this->parentWriter->getNotes()) + 1;
$prefix = ($this->element instanceof \PhpOffice\PhpWord\Element\Endnote) ? 'endnote' : 'footnote';
$noteMark = $prefix . '-' . $this->element->getRelationId();
$this->parentWriter->addNote($noteId, $noteMark);
$html = "<a name=\"{$noteMark}\"><a href=\"#note-{$noteId}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>";
return $html;
}
}

View File

@ -179,29 +179,27 @@ class Word2007 extends AbstractWriter implements WriterInterface
*
* @param mixed $objZip
* @param integer $rId
* @param string $notesType
* @param string $noteType
*/
private function addNotes($objZip, &$rId, $notesType = 'footnote')
private function addNotes($objZip, &$rId, $noteType = 'footnote')
{
$notesType = ($notesType == 'endnote') ? 'endnote' : 'footnote';
$notesTypes = "{$notesType}s";
$collection = 'PhpOffice\\PhpWord\\' . ucfirst($notesTypes);
$xmlFile = "{$notesTypes}.xml";
$relsFile = "word/_rels/{$xmlFile}.rels";
$xmlPath = "word/{$xmlFile}";
$noteType = ($noteType == 'endnote') ? 'endnote' : 'footnote';
$noteTypePlural = "{$noteType}s";
$method = 'get' . $noteTypePlural;
$collection = $this->phpWord->$method();
// Add footnotes media files, relations, and contents
if ($collection::countElements() > 0) {
$media = Media::getElements($notesType);
if ($collection->countItems() > 0) {
$media = Media::getElements($noteType);
$this->addFilesToPackage($objZip, $media);
$this->registerContentTypes($media);
if (!empty($media)) {
$objZip->addFromString($relsFile, $this->getWriterPart('rels')->writeMediaRels($media));
$objZip->addFromString("word/_rels/{$noteTypePlural}.xml.rels", $this->getWriterPart('rels')->writeMediaRels($media));
}
$elements = $collection::getElements();
$objZip->addFromString($xmlPath, $this->getWriterPart($notesTypes)->write($elements));
$this->cTypes['override']["/{$xmlPath}"] = $notesTypes;
$this->docRels[] = array('target' => $xmlFile, 'type' => $notesTypes, 'rID' => ++$rId);
$elements = $collection->getItems();
$objZip->addFromString("word/{$noteTypePlural}.xml", $this->getWriterPart($noteTypePlural)->write($elements));
$this->cTypes['override']["/word/{$noteTypePlural}.xml"] = $noteTypePlural;
$this->docRels[] = array('target' => "{$noteTypePlural}.xml", 'type' => $noteTypePlural, 'rID' => ++$rId);
}
}

View File

@ -14,14 +14,12 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element;
*
* @since 0.10.0
*/
class Endnote extends Note
class Endnote extends Footnote
{
/**
* Write element
* Reference type
*
* @var string
*/
public function write()
{
$this->referenceType = 'endnoteReference';
parent::write();
}
protected $referenceType = 'endnoteReference';
}

View File

@ -14,14 +14,35 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element;
*
* @since 0.10.0
*/
class Footnote extends Note
class Footnote extends Element
{
/**
* Reference type footnoteReference|endnoteReference
*
* @var string
*/
protected $referenceType = 'footnoteReference';
/**
* Write element
*/
public function write()
{
$this->referenceType = 'footnoteReference';
parent::write();
if (!$this->withoutP) {
$this->xmlWriter->startElement('w:p');
}
$this->xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:rPr');
$this->xmlWriter->startElement('w:rStyle');
$this->xmlWriter->writeAttribute('w:val', ucfirst($this->referenceType));
$this->xmlWriter->endElement(); // w:rStyle
$this->xmlWriter->endElement(); // w:rPr
$this->xmlWriter->startElement("w:{$this->referenceType}");
$this->xmlWriter->writeAttribute('w:id', $this->element->getRelationId());
$this->xmlWriter->endElement(); // w:$referenceType
$this->xmlWriter->endElement(); // w:r
if (!$this->withoutP) {
$this->xmlWriter->endElement(); // w:p
}
}
}

View File

@ -1,48 +0,0 @@
<?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\Writer\Word2007\Element;
/**
* Note element writer
*
* @since 0.10.0
*/
class Note extends Element
{
/**
* Reference type footnoteReference|endnoteReference
*
* @var string
*/
protected $referenceType = 'footnoteReference';
/**
* Write element
*/
public function write()
{
if (!$this->withoutP) {
$this->xmlWriter->startElement('w:p');
}
$this->xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:rPr');
$this->xmlWriter->startElement('w:rStyle');
$this->xmlWriter->writeAttribute('w:val', ucfirst($this->referenceType));
$this->xmlWriter->endElement(); // w:rStyle
$this->xmlWriter->endElement(); // w:rPr
$this->xmlWriter->startElement("w:{$this->referenceType}");
$this->xmlWriter->writeAttribute('w:id', $this->element->getRelationId());
$this->xmlWriter->endElement(); // w:$referenceType
$this->xmlWriter->endElement(); // w:r
if (!$this->withoutP) {
$this->xmlWriter->endElement(); // w:p
}
}
}

View File

@ -12,6 +12,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Tab as TabStyleWriter;
/**
* TOC element writer
@ -26,59 +27,53 @@ class TOC extends Element
public function write()
{
$titles = $this->element->getTitles();
$fontStyle = $this->element->getStyleFont();
$tocStyle = $this->element->getStyleTOC();
$fIndent = $tocStyle->getIndent();
$tabLeader = $tocStyle->getTabLeader();
$tabPos = $tocStyle->getTabPos();
$maxDepth = $this->element->getMaxDepth();
$minDepth = $this->element->getMinDepth();
$fontStyle = $this->element->getStyleFont();
$isObject = ($fontStyle instanceof Font) ? true : false;
for ($i = 0; $i < count($titles); $i++) {
$title = $titles[$i];
$indent = ($title['depth'] - 1) * $fIndent;
$tocFieldWritten = false;
foreach ($titles as $title) {
$indent = ($title->getDepth() - 1) * $tocStyle->getIndent();
$anchor = '_Toc' . ($title->getBookmarkId() + 252634154);
$this->xmlWriter->startElement('w:p');
// Style
$this->xmlWriter->startElement('w:pPr');
// Paragraph
if ($isObject && !is_null($fontStyle->getParagraphStyle())) {
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $fontStyle->getParagraphStyle());
$styleWriter->write();
}
// Font
if (!empty($fontStyle) && !$isObject) {
$this->xmlWriter->startElement('w:rPr');
$this->xmlWriter->startElement('w:rStyle');
$this->xmlWriter->writeAttribute('w:val', $fontStyle);
$this->xmlWriter->endElement();
$this->xmlWriter->endElement(); // w:rPr
}
// Tab
$this->xmlWriter->startElement('w:tabs');
$styleWriter = new TabStyleWriter($this->xmlWriter, $tocStyle);
$styleWriter->write();
$this->xmlWriter->endElement();
// Indent
if ($indent > 0) {
$this->xmlWriter->startElement('w:ind');
$this->xmlWriter->writeAttribute('w:left', $indent);
$this->xmlWriter->endElement();
}
if (!empty($fontStyle) && !$isObject) {
$this->xmlWriter->startElement('w:pPr');
$this->xmlWriter->startElement('w:pStyle');
$this->xmlWriter->writeAttribute('w:val', $fontStyle);
$this->xmlWriter->endElement();
$this->xmlWriter->endElement();
}
$this->xmlWriter->startElement('w:tabs');
$this->xmlWriter->startElement('w:tab');
$this->xmlWriter->writeAttribute('w:val', 'right');
if (!empty($tabLeader)) {
$this->xmlWriter->writeAttribute('w:leader', $tabLeader);
}
$this->xmlWriter->writeAttribute('w:pos', $tabPos);
$this->xmlWriter->endElement();
$this->xmlWriter->endElement();
$this->xmlWriter->endElement(); // w:pPr
if ($tocFieldWritten !== true) {
$tocFieldWritten = true;
if ($i == 0) {
$this->xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:fldChar');
$this->xmlWriter->writeAttribute('w:fldCharType', 'begin');
@ -88,7 +83,7 @@ class TOC extends Element
$this->xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:instrText');
$this->xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->writeRaw('TOC \o "' . $minDepth . '-' . $maxDepth . '" \h \z \u');
$this->xmlWriter->writeRaw('TOC \o "' . $this->element->getMinDepth() . '-' . $this->element->getMaxDepth() . '" \h \z \u');
$this->xmlWriter->endElement();
$this->xmlWriter->endElement();
@ -100,7 +95,7 @@ class TOC extends Element
}
$this->xmlWriter->startElement('w:hyperlink');
$this->xmlWriter->writeAttribute('w:anchor', $title['anchor']);
$this->xmlWriter->writeAttribute('w:anchor', $anchor);
$this->xmlWriter->writeAttribute('w:history', '1');
$this->xmlWriter->startElement('w:r');
@ -111,7 +106,7 @@ class TOC extends Element
}
$this->xmlWriter->startElement('w:t');
$this->xmlWriter->writeRaw($title['text']);
$this->xmlWriter->writeRaw($title->getText());
$this->xmlWriter->endElement();
$this->xmlWriter->endElement();
@ -128,7 +123,7 @@ class TOC extends Element
$this->xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:instrText');
$this->xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->writeRaw('PAGEREF ' . $title['anchor'] . ' \h');
$this->xmlWriter->writeRaw('PAGEREF ' . $anchor . ' \h');
$this->xmlWriter->endElement();
$this->xmlWriter->endElement();

View File

@ -23,8 +23,8 @@ class Title extends Element
*/
public function write()
{
$anchor = $this->element->getAnchor();
$bookmarkId = $this->element->getBookmarkId();
$anchor = '_Toc' . ($bookmarkId + 252634154);
$style = $this->element->getStyle();
$text = htmlspecialchars($this->element->getText());
$text = String::controlCharacterPHP2OOXML($text);

View File

@ -26,7 +26,7 @@ class Tab extends AbstractStyle
}
$this->xmlWriter->startElement("w:tab");
$this->xmlWriter->writeAttribute("w:val", $this->style->getStopType());
$this->xmlWriter->writeAttribute("w:val", $this->style->getType());
$this->xmlWriter->writeAttribute("w:leader", $this->style->getLeader());
$this->xmlWriter->writeAttribute('w:pos', $this->convertTwip($this->style->getPosition()));
$this->xmlWriter->endElement();

View File

@ -9,10 +9,11 @@
namespace PhpOffice\PhpWord\Tests\Element;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Element\Header;
use PhpOffice\PhpWord\Style;
/**
* Test class for PhpOffice\PhpWord\Element\Section
@ -78,6 +79,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase
$imageUrl = 'http://php.net//images/logos/php-med-trans-light.gif';
$section = new Section(0);
$section->setPhpWord(new PhpWord());
$section->addText(utf8_decode('ä'));
$section->addLink(utf8_decode('http://äää.com'), utf8_decode('ä'));
$section->addTextBreak();
@ -122,6 +124,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase
{
Style::addTitleStyle(1, array('size' => 14));
$section = new Section(0);
$section->setPhpWord(new PhpWord());
$section->addTitle('Test', 1);
$elementCollection = $section->getElements();

View File

@ -9,6 +9,8 @@
namespace PhpOffice\PhpWord\Tests\Element;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Element\Title;
use PhpOffice\PhpWord\Element\TOC;
/**
@ -24,11 +26,11 @@ class TOCTest extends \PHPUnit_Framework_TestCase
public function testConstructWithStyleArray()
{
$expected = array(
'tabPos' => 9062,
'tabLeader' => \PhpOffice\PhpWord\Style\TOC::TABLEADER_DOT,
'position' => 9062,
'leader' => \PhpOffice\PhpWord\Style\Tab::TAB_LEADER_DOT,
'indent' => 200,
);
$object = new TOC(array('_size' => 11), array('_tabPos' => $expected['tabPos']));
$object = new TOC(array('size' => 11), array('position' => $expected['position']));
$tocStyle = $object->getStyleTOC();
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\TOC', $tocStyle);
@ -56,17 +58,19 @@ class TOCTest extends \PHPUnit_Framework_TestCase
*/
public function testSetGetMinMaxDepth()
{
$toc = new TOC();
$titles = array(
'Heading 1' => 1,
'Heading 2' => 2,
'Heading 3' => 3,
'Heading 4' => 4,
);
foreach ($titles as $text => $depth) {
\PhpOffice\PhpWord\TOC::addTitle($text, $depth);
}
$phpWord = new PhpWord();
foreach ($titles as $text => $depth) {
$phpWord->addTitle(new Title($text, $depth));
}
$toc = new TOC();
$toc->setPhpWord($phpWord);
$this->assertEquals(1, $toc->getMinDepth());
$this->assertEquals(9, $toc->getMaxDepth());

View File

@ -9,6 +9,7 @@
namespace PhpOffice\PhpWord\Tests\Element;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Element\TextRun;
/**
@ -138,6 +139,7 @@ class TextRunTest extends \PHPUnit_Framework_TestCase
public function testCreateFootnote()
{
$oTextRun = new TextRun();
$oTextRun->setPhpWord(new PhpWord());
$element = $oTextRun->addFootnote();
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Footnote', $element);

View File

@ -40,28 +40,6 @@ class TitleTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($oTitle->getStyle(), null);
}
/**
* Get style not null
*/
public function testStyleNotNull()
{
$oTitle = new Title('text', 1, 'style');
$this->assertEquals($oTitle->getStyle(), 'style');
}
/**
* Get anchor
*/
public function testAnchor()
{
$oTitle = new Title('text');
$iVal = rand(1, 1000);
$oTitle->setAnchor($iVal);
$this->assertEquals($oTitle->getAnchor(), $iVal);
}
/**
* Get bookmark Id
*/

View File

@ -1,39 +0,0 @@
<?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\Tests;
use PhpOffice\PhpWord\Endnotes;
/**
* Test class for PhpOffice\PhpWord\Endnotes
*
* @runTestsInSeparateProcesses
*/
class EndnotesTest extends \PHPUnit_Framework_TestCase
{
/**
* Test endnote collection
*/
public function testEndnotes()
{
$endnote1 = new \PhpOffice\PhpWord\Element\Endnote();
$endnote2 = new \PhpOffice\PhpWord\Element\Endnote();
$rId = Endnotes::addElement($endnote1);
Endnotes::setElement(1, $endnote2);
$this->assertEquals(1, $rId);
$this->assertEquals(1, count(Endnotes::getElements()));
$this->assertEquals($endnote2, Endnotes::getElement(1));
$this->assertNull(Endnotes::getElement(2));
Endnotes::resetElements();
$this->assertEquals(0, Endnotes::countElements());
}
}

View File

@ -1,39 +0,0 @@
<?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\Tests;
use PhpOffice\PhpWord\Footnotes;
/**
* Test class for PhpOffice\PhpWord\Footnotes
*
* @runTestsInSeparateProcesses
*/
class FootnotesTest extends \PHPUnit_Framework_TestCase
{
/**
* Test footnote collection
*/
public function testFootnotes()
{
$footnote1 = new \PhpOffice\PhpWord\Element\Footnote();
$footnote2 = new \PhpOffice\PhpWord\Element\Footnote();
$rId = Footnotes::addElement($footnote1);
Footnotes::setElement(1, $footnote2);
$this->assertEquals(1, $rId);
$this->assertEquals(1, count(Footnotes::getElements()));
$this->assertEquals($footnote2, Footnotes::getElement(1));
$this->assertNull(Footnotes::getElement(2));
Footnotes::resetElements();
$this->assertEquals(0, Footnotes::countElements());
}
}

View File

@ -51,9 +51,8 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase
public function testCreateGetSections()
{
$phpWord = new PhpWord();
$this->assertEquals(new Section(1), $phpWord->addSection());
$phpWord->addSection();
$this->assertEquals(2, count($phpWord->getSections()));
$this->assertEquals(1, count($phpWord->getSections()));
}
/**

View File

@ -27,8 +27,8 @@ class TOCTest extends \PHPUnit_Framework_TestCase
$object = new TOC();
$properties = array(
'tabPos' => 9062,
'tabLeader' => TOC::TABLEADER_DOT,
'position' => 9062,
'leader' => \PhpOffice\PhpWord\Style\Tab::TAB_LEADER_DOT,
'indent' => 200,
);
foreach ($properties as $key => $value) {
@ -37,10 +37,6 @@ class TOCTest extends \PHPUnit_Framework_TestCase
$get = "get{$key}";
$object->$set($value);
$this->assertEquals($value, $object->$get());
// setStyleValue
$object->setStyleValue("{$key}", null);
$this->assertEquals(null, $object->$get());
}
}
}

View File

@ -1,53 +0,0 @@
<?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\Tests;
use PhpOffice\PhpWord\TOC;
/**
* Test class for PhpOffice\PhpWord\TOC
*
* @runTestsInSeparateProcesses
*/
class TOCTest extends \PHPUnit_Framework_TestCase
{
/**
* Add and get title
*/
public function testAddAndGetTitle()
{
$titleCount = 3;
$anchor = '_Toc' . (252634154 + $titleCount);
$bookmark = $titleCount - 1;
$titles = array(
'Heading 1' => 1,
'Heading 2' => 2,
'Heading 3' => 3,
);
$toc = new TOC();
foreach ($titles as $text => $depth) {
$response = $toc->addTitle($text, $depth);
}
$this->assertEquals($anchor, $response[0]);
$this->assertEquals($bookmark, $response[1]);
$i = 0;
$savedTitles = $toc->getTitles();
foreach ($titles as $text => $depth) {
$this->assertEquals($text, $savedTitles[$i]['text']);
$this->assertEquals($depth, $savedTitles[$i]['depth']);
$i++;
}
TOC::resetTitles();
$this->assertEquals(0, count($toc->getTitles()));
}
}