From 725162bc6b433f4eaa2beb0a28fe9ef791150a20 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 4 May 2014 00:57:44 +0700 Subject: [PATCH] Deprecate static classes Footnotes, Endnotes, and TOC (#206); Reactivate `phpcpd` and `phpmd` on Travis --- .travis.yml | 18 +- CHANGELOG.md | 5 +- samples/resources/Sample_11_ReadWord2007.docx | Bin 69796 -> 69796 bytes src/PhpWord/Collection/AbstractCollection.php | 87 ++++++++ src/PhpWord/Collection/Endnotes.php | 19 ++ src/PhpWord/Collection/Footnotes.php | 19 ++ src/PhpWord/Collection/Titles.php | 19 ++ src/PhpWord/Element/AbstractContainer.php | 188 +++++++----------- src/PhpWord/Element/AbstractElement.php | 26 +++ src/PhpWord/Element/CheckBox.php | 4 +- src/PhpWord/Element/Footer.php | 70 ++++++- src/PhpWord/Element/Header.php | 84 +------- src/PhpWord/Element/Link.php | 5 +- src/PhpWord/Element/ListItem.php | 3 +- src/PhpWord/Element/PreserveText.php | 4 +- src/PhpWord/Element/Row.php | 1 + src/PhpWord/Element/Section.php | 48 ++++- src/PhpWord/Element/TOC.php | 14 +- src/PhpWord/Element/Table.php | 1 + src/PhpWord/Element/Text.php | 3 +- src/PhpWord/Element/Title.php | 65 +++--- src/PhpWord/Endnotes.php | 95 --------- src/PhpWord/Footnotes.php | 23 +-- src/PhpWord/PhpWord.php | 129 ++++++++++-- src/PhpWord/Reader/Word2007/DocProps.php | 63 ------ src/PhpWord/Reader/Word2007/DocPropsApp.php | 9 +- src/PhpWord/Reader/Word2007/DocPropsCore.php | 35 +++- src/PhpWord/Reader/Word2007/Notes.php | 9 +- src/PhpWord/Style/TOC.php | 49 ++--- src/PhpWord/Style/Tab.php | 54 ++++- src/PhpWord/TOC.php | 6 + src/PhpWord/Writer/HTML.php | 6 +- src/PhpWord/Writer/HTML/Element/Endnote.php | 8 +- src/PhpWord/Writer/HTML/Element/Footnote.php | 23 ++- src/PhpWord/Writer/HTML/Element/Note.php | 34 ---- src/PhpWord/Writer/Word2007.php | 28 ++- .../Writer/Word2007/Element/Endnote.php | 12 +- .../Writer/Word2007/Element/Footnote.php | 27 ++- src/PhpWord/Writer/Word2007/Element/Note.php | 48 ----- src/PhpWord/Writer/Word2007/Element/TOC.php | 65 +++--- src/PhpWord/Writer/Word2007/Element/Title.php | 2 +- src/PhpWord/Writer/Word2007/Style/Tab.php | 2 +- tests/PhpWord/Tests/Element/SectionTest.php | 5 +- tests/PhpWord/Tests/Element/TOCTest.php | 18 +- tests/PhpWord/Tests/Element/TextRunTest.php | 2 + tests/PhpWord/Tests/Element/TitleTest.php | 22 -- tests/PhpWord/Tests/EndnotesTest.php | 39 ---- tests/PhpWord/Tests/FootnotesTest.php | 39 ---- tests/PhpWord/Tests/PhpWordTest.php | 3 +- tests/PhpWord/Tests/Style/TOCTest.php | 8 +- tests/PhpWord/Tests/TOCTest.php | 53 ----- 51 files changed, 783 insertions(+), 816 deletions(-) create mode 100644 src/PhpWord/Collection/AbstractCollection.php create mode 100644 src/PhpWord/Collection/Endnotes.php create mode 100644 src/PhpWord/Collection/Footnotes.php create mode 100644 src/PhpWord/Collection/Titles.php delete mode 100644 src/PhpWord/Endnotes.php delete mode 100644 src/PhpWord/Reader/Word2007/DocProps.php delete mode 100644 src/PhpWord/Writer/HTML/Element/Note.php delete mode 100644 src/PhpWord/Writer/Word2007/Element/Note.php delete mode 100644 tests/PhpWord/Tests/EndnotesTest.php delete mode 100644 tests/PhpWord/Tests/FootnotesTest.php delete mode 100644 tests/PhpWord/Tests/TOCTest.php diff --git a/.travis.yml b/.travis.yml index ad3df613..cf69291f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index e23edf91..ccdac7ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/samples/resources/Sample_11_ReadWord2007.docx b/samples/resources/Sample_11_ReadWord2007.docx index 2143c628c5132f5948c339538bef2c2fabdab545..aefe9f485ea617defd77ba8129edd70dd2fbb789 100644 GIT binary patch delta 646 zcmV;10(t$Up#-F%1c0;wB?lyU#3eR90ssKE1pojL0QUf7Z(~q$Z*X%jVQ_FRcx`MT zqVoWi&H?j(v{YSh(=Zf$N8&$3d0(Awqm0V+ggT)j4OWqCd-j~0*21wZ+pAW7J=bxY zrO+XUM4s+fuCLGWx%Bd>RoModmqyR>@ubD=G1C}-*Ybd_TM^SVQoxlFk1gmxPK#nf z;3T`A6O1I2&ez;=5fDQ5;q;u6flohLtEAuo!DU~haK@V^WH%iQ zS^S@Wl3@u&Xi5-|PKGTjC>gr+T56o+JtfJ&$sAufZr7gS$ODg-f-9Kg{aM462TGm; zbOAhayW|q*rJFF_fC$EAUj9IKk?aTVA)+?RH{40C1ELqplGEAB`rue4gTmgJOMYh# z%Z9c*j~HSt8h?UjSF}gu|GIsJFZ6$Zjb18QL)L-wn(<#iC*CPBgs>fQmyWn; zkoj;(yU#3eR90ssKEmuV0IJIujB8~^|S delta 622 zcmV-!0+Icsp#-F%1c0;wB?lbkzG9y(0ssK71pojL00;qOZ(~q$Z*X%jVQ_G>Q2Go3 zf3#FxZ`&XgeV?@dVEMhlNwchr2-TEo%A{_M7Poz-_&PzufPhBLe*G>c@tiK3lu6_1 zesMV5%Yk0MG%DMI^U~;9J{%Nz23nX(>h&zYFF##e<(Us$S6mqlvwR0Wzh-aW(4{jL zT#(>1gwTGLZ$hwBLcG{O!~Fo$8Z)(Ve+>`lx-~I%Ed|^d@!Wt8WKZaf=BdYwmCf-9Kg{aMYG2TFbg=mL10?UKvL zZyTmt5W%?2%O5yh9QG6U5W61y6sW6|T)O~Sv*;jNgMOPSwD+>G8sjLgo z1$N!3cmVFzYp4!2zbE1!@sR#sHhecIaJ8SpA%@cKK1TKvw{!{t>;(zszG9y(0ssK7 ImuV0II|A=0@c;k- diff --git a/src/PhpWord/Collection/AbstractCollection.php b/src/PhpWord/Collection/AbstractCollection.php new file mode 100644 index 00000000..7b5d4579 --- /dev/null +++ b/src/PhpWord/Collection/AbstractCollection.php @@ -0,0 +1,87 @@ +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); + } +} diff --git a/src/PhpWord/Collection/Endnotes.php b/src/PhpWord/Collection/Endnotes.php new file mode 100644 index 00000000..36c547a3 --- /dev/null +++ b/src/PhpWord/Collection/Endnotes.php @@ -0,0 +1,19 @@ +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 diff --git a/src/PhpWord/Element/AbstractElement.php b/src/PhpWord/Element/AbstractElement.php index 6e993748..fdad7daf 100644 --- a/src/PhpWord/Element/AbstractElement.php +++ b/src/PhpWord/Element/AbstractElement.php @@ -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 * diff --git a/src/PhpWord/Element/CheckBox.php b/src/PhpWord/Element/CheckBox.php index f273c128..6353e2d4 100644 --- a/src/PhpWord/Element/CheckBox.php +++ b/src/PhpWord/Element/CheckBox.php @@ -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; } diff --git a/src/PhpWord/Element/Footer.php b/src/PhpWord/Element/Footer.php index 50ec8236..9090720d 100644 --- a/src/PhpWord/Element/Footer.php +++ b/src/PhpWord/Element/Footer.php @@ -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; + } } diff --git a/src/PhpWord/Element/Header.php b/src/PhpWord/Element/Header.php index 2c963d84..774e63f9 100644 --- a/src/PhpWord/Element/Header.php +++ b/src/PhpWord/Element/Header.php @@ -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; - } } diff --git a/src/PhpWord/Element/Link.php b/src/PhpWord/Element/Link.php index 31b06c68..e4ead5d5 100644 --- a/src/PhpWord/Element/Link.php +++ b/src/PhpWord/Element/Link.php @@ -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); diff --git a/src/PhpWord/Element/ListItem.php b/src/PhpWord/Element/ListItem.php index 66061537..e52d0252 100644 --- a/src/PhpWord/Element/ListItem.php +++ b/src/PhpWord/Element/ListItem.php @@ -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 diff --git a/src/PhpWord/Element/PreserveText.php b/src/PhpWord/Element/PreserveText.php index ac7d1f52..bf280d66 100644 --- a/src/PhpWord/Element/PreserveText.php +++ b/src/PhpWord/Element/PreserveText.php @@ -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; } diff --git a/src/PhpWord/Element/Row.php b/src/PhpWord/Element/Row.php index dc4b8a44..ed8e1141 100644 --- a/src/PhpWord/Element/Row.php +++ b/src/PhpWord/Element/Row.php @@ -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; } diff --git a/src/PhpWord/Element/Section.php b/src/PhpWord/Element/Section.php index 64e226ab..dd1c0afb 100644 --- a/src/PhpWord/Element/Section.php +++ b/src/PhpWord/Element/Section.php @@ -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 { diff --git a/src/PhpWord/Element/TOC.php b/src/PhpWord/Element/TOC.php index ac48a49d..b8eee6e5 100644 --- a/src/PhpWord/Element/TOC.php +++ b/src/PhpWord/Element/TOC.php @@ -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; } diff --git a/src/PhpWord/Element/Table.php b/src/PhpWord/Element/Table.php index 1fa729e1..89bef829 100644 --- a/src/PhpWord/Element/Table.php +++ b/src/PhpWord/Element/Table.php @@ -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; } diff --git a/src/PhpWord/Element/Text.php b/src/PhpWord/Element/Text.php index 96e0e164..481f532b 100644 --- a/src/PhpWord/Element/Text.php +++ b/src/PhpWord/Element/Text.php @@ -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; } diff --git a/src/PhpWord/Element/Title.php b/src/PhpWord/Element/Title.php index 93b48d62..f6d3bba9 100644 --- a/src/PhpWord/Element/Title.php +++ b/src/PhpWord/Element/Title.php @@ -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); + } } diff --git a/src/PhpWord/Endnotes.php b/src/PhpWord/Endnotes.php deleted file mode 100644 index 6587dfe9..00000000 --- a/src/PhpWord/Endnotes.php +++ /dev/null @@ -1,95 +0,0 @@ -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 * diff --git a/src/PhpWord/Reader/Word2007/DocProps.php b/src/PhpWord/Reader/Word2007/DocProps.php deleted file mode 100644 index 2c76417a..00000000 --- a/src/PhpWord/Reader/Word2007/DocProps.php +++ /dev/null @@ -1,63 +0,0 @@ -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); - } - } - } - } -} diff --git a/src/PhpWord/Reader/Word2007/DocPropsApp.php b/src/PhpWord/Reader/Word2007/DocPropsApp.php index 7797528a..40d08667 100644 --- a/src/PhpWord/Reader/Word2007/DocPropsApp.php +++ b/src/PhpWord/Reader/Word2007/DocPropsApp.php @@ -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(); } diff --git a/src/PhpWord/Reader/Word2007/DocPropsCore.php b/src/PhpWord/Reader/Word2007/DocPropsCore.php index fd943875..a6a8902b 100644 --- a/src/PhpWord/Reader/Word2007/DocPropsCore.php +++ b/src/PhpWord/Reader/Word2007/DocPropsCore.php @@ -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); + } + } + } + } } diff --git a/src/PhpWord/Reader/Word2007/Notes.php b/src/PhpWord/Reader/Word2007/Notes.php index bdb0b9e8..f92388dc 100644 --- a/src/PhpWord/Reader/Word2007/Notes.php +++ b/src/PhpWord/Reader/Word2007/Notes.php @@ -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); } } } diff --git a/src/PhpWord/Style/TOC.php b/src/PhpWord/Style/TOC.php index e8a781b0..5333a0b6 100644 --- a/src/PhpWord/Style/TOC.php +++ b/src/PhpWord/Style/TOC.php @@ -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; } } diff --git a/src/PhpWord/Style/Tab.php b/src/PhpWord/Style/Tab.php index dfcb3b95..62785cfb 100644 --- a/src/PhpWord/Style/Tab.php +++ b/src/PhpWord/Style/Tab.php @@ -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); + } } diff --git a/src/PhpWord/TOC.php b/src/PhpWord/TOC.php index ea75c10d..577fe4ab 100644 --- a/src/PhpWord/TOC.php +++ b/src/PhpWord/TOC.php @@ -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 { diff --git a/src/PhpWord/Writer/HTML.php b/src/PhpWord/Writer/HTML.php index d4b8adb3..a11a92ff 100644 --- a/src/PhpWord/Writer/HTML.php +++ b/src/PhpWord/Writer/HTML.php @@ -167,14 +167,16 @@ class HTML extends AbstractWriter implements WriterInterface */ private function writeNotes() { + $phpWord = $this->getPhpWord(); $html = ''; + if (!empty($this->notes)) { $html .= "
"; 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); diff --git a/src/PhpWord/Writer/HTML/Element/Endnote.php b/src/PhpWord/Writer/HTML/Element/Endnote.php index 66efcf4d..f9c62f41 100644 --- a/src/PhpWord/Writer/HTML/Element/Endnote.php +++ b/src/PhpWord/Writer/HTML/Element/Endnote.php @@ -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'; } diff --git a/src/PhpWord/Writer/HTML/Element/Footnote.php b/src/PhpWord/Writer/HTML/Element/Footnote.php index 5fdc55a2..ab723d69 100644 --- a/src/PhpWord/Writer/HTML/Element/Footnote.php +++ b/src/PhpWord/Writer/HTML/Element/Footnote.php @@ -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 = "{$noteId}"; + + return $html; + } } diff --git a/src/PhpWord/Writer/HTML/Element/Note.php b/src/PhpWord/Writer/HTML/Element/Note.php deleted file mode 100644 index cac84cbe..00000000 --- a/src/PhpWord/Writer/HTML/Element/Note.php +++ /dev/null @@ -1,34 +0,0 @@ -parentWriter->getNotes()) + 1; - $prefix = ($this->element instanceof \PhpOffice\PhpWord\Element\Endnote) ? 'endnote' : 'footnote'; - $noteMark = $prefix . '-' . $this->element->getRelationId(); - $this->parentWriter->addNote($noteId, $noteMark); - $html = "{$noteId}"; - - return $html; - } -} diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index b87f1d24..59fa2194 100644 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -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); } } diff --git a/src/PhpWord/Writer/Word2007/Element/Endnote.php b/src/PhpWord/Writer/Word2007/Element/Endnote.php index fbdf7a75..e1865e64 100644 --- a/src/PhpWord/Writer/Word2007/Element/Endnote.php +++ b/src/PhpWord/Writer/Word2007/Element/Endnote.php @@ -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'; } diff --git a/src/PhpWord/Writer/Word2007/Element/Footnote.php b/src/PhpWord/Writer/Word2007/Element/Footnote.php index 20360ebc..d962d8bc 100644 --- a/src/PhpWord/Writer/Word2007/Element/Footnote.php +++ b/src/PhpWord/Writer/Word2007/Element/Footnote.php @@ -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 + } } } diff --git a/src/PhpWord/Writer/Word2007/Element/Note.php b/src/PhpWord/Writer/Word2007/Element/Note.php deleted file mode 100644 index 1ba73de8..00000000 --- a/src/PhpWord/Writer/Word2007/Element/Note.php +++ /dev/null @@ -1,48 +0,0 @@ -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 - } - } -} diff --git a/src/PhpWord/Writer/Word2007/Element/TOC.php b/src/PhpWord/Writer/Word2007/Element/TOC.php index 5be287a3..37888b7f 100644 --- a/src/PhpWord/Writer/Word2007/Element/TOC.php +++ b/src/PhpWord/Writer/Word2007/Element/TOC.php @@ -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(); diff --git a/src/PhpWord/Writer/Word2007/Element/Title.php b/src/PhpWord/Writer/Word2007/Element/Title.php index 5570921f..9b9f931d 100644 --- a/src/PhpWord/Writer/Word2007/Element/Title.php +++ b/src/PhpWord/Writer/Word2007/Element/Title.php @@ -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); diff --git a/src/PhpWord/Writer/Word2007/Style/Tab.php b/src/PhpWord/Writer/Word2007/Style/Tab.php index 403c6173..21a5d130 100644 --- a/src/PhpWord/Writer/Word2007/Style/Tab.php +++ b/src/PhpWord/Writer/Word2007/Style/Tab.php @@ -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(); diff --git a/tests/PhpWord/Tests/Element/SectionTest.php b/tests/PhpWord/Tests/Element/SectionTest.php index 415e43ad..5377a6c2 100644 --- a/tests/PhpWord/Tests/Element/SectionTest.php +++ b/tests/PhpWord/Tests/Element/SectionTest.php @@ -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(); diff --git a/tests/PhpWord/Tests/Element/TOCTest.php b/tests/PhpWord/Tests/Element/TOCTest.php index 207f6969..49d4ca06 100644 --- a/tests/PhpWord/Tests/Element/TOCTest.php +++ b/tests/PhpWord/Tests/Element/TOCTest.php @@ -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()); diff --git a/tests/PhpWord/Tests/Element/TextRunTest.php b/tests/PhpWord/Tests/Element/TextRunTest.php index 0916e40b..16b71698 100644 --- a/tests/PhpWord/Tests/Element/TextRunTest.php +++ b/tests/PhpWord/Tests/Element/TextRunTest.php @@ -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); diff --git a/tests/PhpWord/Tests/Element/TitleTest.php b/tests/PhpWord/Tests/Element/TitleTest.php index 6c472b0d..ee111634 100644 --- a/tests/PhpWord/Tests/Element/TitleTest.php +++ b/tests/PhpWord/Tests/Element/TitleTest.php @@ -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 */ diff --git a/tests/PhpWord/Tests/EndnotesTest.php b/tests/PhpWord/Tests/EndnotesTest.php deleted file mode 100644 index a72e85f9..00000000 --- a/tests/PhpWord/Tests/EndnotesTest.php +++ /dev/null @@ -1,39 +0,0 @@ -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()); - } -} diff --git a/tests/PhpWord/Tests/FootnotesTest.php b/tests/PhpWord/Tests/FootnotesTest.php deleted file mode 100644 index 869c69a7..00000000 --- a/tests/PhpWord/Tests/FootnotesTest.php +++ /dev/null @@ -1,39 +0,0 @@ -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()); - } -} diff --git a/tests/PhpWord/Tests/PhpWordTest.php b/tests/PhpWord/Tests/PhpWordTest.php index 8de6f808..d7c5a7f6 100644 --- a/tests/PhpWord/Tests/PhpWordTest.php +++ b/tests/PhpWord/Tests/PhpWordTest.php @@ -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())); } /** diff --git a/tests/PhpWord/Tests/Style/TOCTest.php b/tests/PhpWord/Tests/Style/TOCTest.php index 2c10de8d..dab87d67 100644 --- a/tests/PhpWord/Tests/Style/TOCTest.php +++ b/tests/PhpWord/Tests/Style/TOCTest.php @@ -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()); } } } diff --git a/tests/PhpWord/Tests/TOCTest.php b/tests/PhpWord/Tests/TOCTest.php deleted file mode 100644 index d0b73352..00000000 --- a/tests/PhpWord/Tests/TOCTest.php +++ /dev/null @@ -1,53 +0,0 @@ - 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())); - } -}