From 13e5ca0a84a6c4de677b8eaba5899876256ff7f6 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Mon, 31 Mar 2014 01:13:02 +0700 Subject: [PATCH 01/27] #160 Refactor folders: Element, Container, and Exception - Rename folder Section to Element - Rename folder Exceptions to Exception - Move Section, Header, Footer, Settings to Container folder - Move Element\Footer\PreserveText to Element\PreserveText --- CHANGELOG.md | 1 + src/PhpWord/{Section => Container}/Footer.php | 21 +++-- src/PhpWord/{Section => Container}/Header.php | 23 +++--- src/PhpWord/{ => Container}/Section.php | 77 ++++++++++--------- .../{Section => Container}/Settings.php | 2 +- src/PhpWord/{Section => Element}/CheckBox.php | 2 +- src/PhpWord/{Section => Element}/Footnote.php | 6 +- src/PhpWord/{Section => Element}/Image.php | 10 +-- src/PhpWord/{Section => Element}/Link.php | 2 +- src/PhpWord/{Section => Element}/ListItem.php | 4 +- src/PhpWord/{Section => Element}/Object.php | 2 +- .../{Section => Element}/PageBreak.php | 2 +- .../Footer => Element}/PreserveText.php | 2 +- src/PhpWord/{Section => Element}/Table.php | 6 +- .../{Section => Element}/Table/Cell.php | 42 +++++----- .../{Section => Element}/Table/Row.php | 4 +- src/PhpWord/{Section => Element}/Text.php | 2 +- .../{Section => Element}/TextBreak.php | 2 +- src/PhpWord/{Section => Element}/TextRun.php | 14 ++-- src/PhpWord/{Section => Element}/Title.php | 2 +- .../{Exceptions => Exception}/Exception.php | 2 +- .../InvalidImageException.php | 2 +- .../InvalidObjectException.php | 2 +- .../InvalidStyleException.php | 2 +- .../UnsupportedImageTypeException.php | 2 +- src/PhpWord/Footnote.php | 2 +- src/PhpWord/HashTable.php | 4 +- src/PhpWord/IOFactory.php | 6 +- src/PhpWord/Media.php | 8 +- src/PhpWord/PhpWord.php | 12 +-- src/PhpWord/Reader/Reader.php | 4 +- src/PhpWord/Reader/Word2007.php | 4 +- src/PhpWord/Shared/ZipArchive.php | 2 +- src/PhpWord/Shared/ZipStreamWrapper.php | 2 +- src/PhpWord/Style/Font.php | 4 +- src/PhpWord/Style/Paragraph.php | 4 +- src/PhpWord/Template.php | 12 +-- src/PhpWord/Writer/ODText.php | 2 +- src/PhpWord/Writer/ODText/Content.php | 22 +++--- src/PhpWord/Writer/ODText/Manifest.php | 2 +- src/PhpWord/Writer/RTF.php | 22 +++--- src/PhpWord/Writer/Word2007.php | 2 +- src/PhpWord/Writer/Word2007/Base.php | 24 +++--- src/PhpWord/Writer/Word2007/ContentTypes.php | 2 +- src/PhpWord/Writer/Word2007/Document.php | 26 +++---- src/PhpWord/Writer/Word2007/DocumentRels.php | 2 +- src/PhpWord/Writer/Word2007/Footer.php | 14 ++-- src/PhpWord/Writer/Word2007/Footnotes.php | 8 +- src/PhpWord/Writer/Word2007/FootnotesRels.php | 2 +- src/PhpWord/Writer/Word2007/Header.php | 14 ++-- src/PhpWord/Writer/Word2007/Rels.php | 2 +- src/PhpWord/Writer/Word2007/WriterPart.php | 2 +- src/PhpWord/Writer/Writer.php | 2 +- tests/PhpWord/Tests/AutoloaderTest.php | 6 +- .../{Section => Container}/FooterTest.php | 24 +++--- .../{Section => Container}/HeaderTest.php | 26 +++---- .../Tests/{ => Container}/SectionTest.php | 18 ++--- .../{Section => Container}/SettingsTest.php | 12 +-- .../{Section => Element}/CheckBoxTest.php | 8 +- .../{Section => Element}/FootnoteTest.php | 12 +-- .../Tests/{Section => Element}/ImageTest.php | 22 +++--- .../Tests/{Section => Element}/LinkTest.php | 12 +-- .../{Section => Element}/ListItemTest.php | 10 +-- .../Tests/{Section => Element}/ObjectTest.php | 14 ++-- .../{Section => Element}/PageBreakTest.php | 10 +-- .../Footer => Element}/PreserveTextTest.php | 8 +- .../{Section => Element}/Table/CellTest.php | 46 +++++------ .../{Section => Element}/Table/RowTest.php | 12 +-- .../Tests/{Section => Element}/TableTest.php | 14 ++-- .../{Section => Element}/TextBreakTest.php | 8 +- .../{Section => Element}/TextRunTest.php | 24 +++--- .../Tests/{Section => Element}/TextTest.php | 8 +- .../Tests/{Section => Element}/TitleTest.php | 10 +-- .../ExceptionTest.php | 12 +-- .../InvalidImageExceptionTest.php | 12 +-- .../InvalidStyleExceptionTest.php | 12 +-- .../UnsupportedImageTypeExceptionTest.php | 12 +-- tests/PhpWord/Tests/FootnoteTest.php | 2 +- tests/PhpWord/Tests/IOFactoryTest.php | 4 +- tests/PhpWord/Tests/MediaTest.php | 4 +- tests/PhpWord/Tests/PhpWordTest.php | 4 +- tests/PhpWord/Tests/Reader/Word2007Test.php | 2 +- tests/PhpWord/Tests/Style/FontTest.php | 2 +- tests/PhpWord/Tests/Style/ParagraphTest.php | 2 +- tests/PhpWord/Tests/TemplateTest.php | 4 +- tests/PhpWord/Tests/Writer/ODTextTest.php | 6 +- tests/PhpWord/Tests/Writer/RTFTest.php | 4 +- .../Tests/Writer/Word2007/FooterTest.php | 2 +- .../Tests/Writer/Word2007/HeaderTest.php | 2 +- tests/PhpWord/Tests/Writer/Word2007Test.php | 4 +- 90 files changed, 429 insertions(+), 413 deletions(-) rename src/PhpWord/{Section => Container}/Footer.php (87%) rename src/PhpWord/{Section => Container}/Header.php (90%) rename src/PhpWord/{ => Container}/Section.php (83%) rename src/PhpWord/{Section => Container}/Settings.php (99%) rename src/PhpWord/{Section => Element}/CheckBox.php (98%) rename src/PhpWord/{Section => Element}/Footnote.php (96%) rename src/PhpWord/{Section => Element}/Image.php (95%) rename src/PhpWord/{Section => Element}/Link.php (98%) rename src/PhpWord/{Section => Element}/ListItem.php (95%) rename src/PhpWord/{Section => Element}/Object.php (98%) rename src/PhpWord/{Section => Element}/PageBreak.php (89%) rename src/PhpWord/{Section/Footer => Element}/PreserveText.php (98%) rename src/PhpWord/{Section => Element}/Table.php (95%) rename src/PhpWord/{Section => Element}/Table/Cell.php (89%) rename src/PhpWord/{Section => Element}/Table/Row.php (95%) rename src/PhpWord/{Section => Element}/Text.php (98%) rename src/PhpWord/{Section => Element}/TextBreak.php (98%) rename src/PhpWord/{Section => Element}/TextRun.php (91%) rename src/PhpWord/{Section => Element}/Title.php (98%) rename src/PhpWord/{Exceptions => Exception}/Exception.php (86%) rename src/PhpWord/{Exceptions => Exception}/InvalidImageException.php (88%) rename src/PhpWord/{Exceptions => Exception}/InvalidObjectException.php (88%) rename src/PhpWord/{Exceptions => Exception}/InvalidStyleException.php (89%) rename src/PhpWord/{Exceptions => Exception}/UnsupportedImageTypeException.php (88%) rename tests/PhpWord/Tests/{Section => Container}/FooterTest.php (81%) rename tests/PhpWord/Tests/{Section => Container}/HeaderTest.php (85%) rename tests/PhpWord/Tests/{ => Container}/SectionTest.php (86%) rename tests/PhpWord/Tests/{Section => Container}/SettingsTest.php (95%) rename tests/PhpWord/Tests/{Section => Element}/CheckBoxTest.php (91%) rename tests/PhpWord/Tests/{Section => Element}/FootnoteTest.php (87%) rename tests/PhpWord/Tests/{Section => Element}/ImageTest.php (89%) rename tests/PhpWord/Tests/{Section => Element}/LinkTest.php (87%) rename tests/PhpWord/Tests/{Section => Element}/ListItemTest.php (81%) rename tests/PhpWord/Tests/{Section => Element}/ObjectTest.php (86%) rename tests/PhpWord/Tests/{Section => Element}/PageBreakTest.php (65%) rename tests/PhpWord/Tests/{Section/Footer => Element}/PreserveTextTest.php (85%) rename tests/PhpWord/Tests/{Section => Element}/Table/CellTest.php (82%) rename tests/PhpWord/Tests/{Section => Element}/Table/RowTest.php (81%) rename tests/PhpWord/Tests/{Section => Element}/TableTest.php (83%) rename tests/PhpWord/Tests/{Section => Element}/TextBreakTest.php (89%) rename tests/PhpWord/Tests/{Section => Element}/TextRunTest.php (82%) rename tests/PhpWord/Tests/{Section => Element}/TextTest.php (90%) rename tests/PhpWord/Tests/{Section => Element}/TitleTest.php (84%) rename tests/PhpWord/Tests/{Exceptions => Exception}/ExceptionTest.php (55%) rename tests/PhpWord/Tests/{Exceptions => Exception}/InvalidImageExceptionTest.php (52%) rename tests/PhpWord/Tests/{Exceptions => Exception}/InvalidStyleExceptionTest.php (52%) rename tests/PhpWord/Tests/{Exceptions => Exception}/UnsupportedImageTypeExceptionTest.php (50%) diff --git a/CHANGELOG.md b/CHANGELOG.md index c785dbf4..63c295f0 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - Documentation: Simplify page level docblock - @ivanlanin GH-179 - Writer: Refactor writer classes and make a new Writer abstract class - @ivanlanin GH-160 - Reader: Rename AbstractReader > Reader - @ivanlanin +- General: Refactor folders: Element, Container, and Exception - @ivanlanin ## 0.9.1 - 27 Mar 2014 diff --git a/src/PhpWord/Section/Footer.php b/src/PhpWord/Container/Footer.php similarity index 87% rename from src/PhpWord/Section/Footer.php rename to src/PhpWord/Container/Footer.php index 58c439d2..c1e6034e 100755 --- a/src/PhpWord/Section/Footer.php +++ b/src/PhpWord/Container/Footer.php @@ -7,12 +7,17 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section; +namespace PhpOffice\PhpWord\Container; -use PhpOffice\PhpWord\Exceptions\InvalidImageException; +use PhpOffice\PhpWord\Exception\InvalidImageException; use PhpOffice\PhpWord\Media; -use PhpOffice\PhpWord\Section\Footer\PreserveText; use PhpOffice\PhpWord\Shared\String; +use PhpOffice\PhpWord\Element\Text; +use PhpOffice\PhpWord\Element\TextRun; +use PhpOffice\PhpWord\Element\PreserveText; +use PhpOffice\PhpWord\Element\TextBreak; +use PhpOffice\PhpWord\Element\Table; +use PhpOffice\PhpWord\Element\Image; /** * Footer element @@ -56,7 +61,7 @@ class Footer * @param string $text * @param mixed $styleFont * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Section\Text + * @return \PhpOffice\PhpWord\Element\Text */ public function addText($text, $styleFont = null, $styleParagraph = null) { @@ -86,7 +91,7 @@ class Footer * Create a new TextRun * * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Section\TextRun + * @return \PhpOffice\PhpWord\Element\TextRun */ public function createTextRun($styleParagraph = null) { @@ -99,7 +104,7 @@ class Footer * Add a Table Element * * @param mixed $style - * @return \PhpOffice\PhpWord\Section\Table + * @return \PhpOffice\PhpWord\Element\Table */ public function addTable($style = null) { @@ -113,7 +118,7 @@ class Footer * * @param string $src * @param mixed $style - * @return \PhpOffice\PhpWord\Section\Image + * @return \PhpOffice\PhpWord\Element\Image */ public function addImage($src, $style = null) { @@ -146,7 +151,7 @@ class Footer * @param string $text * @param mixed $styleFont * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Section\Footer\PreserveText + * @return \PhpOffice\PhpWord\Element\PreserveText */ public function addPreserveText($text, $styleFont = null, $styleParagraph = null) { diff --git a/src/PhpWord/Section/Header.php b/src/PhpWord/Container/Header.php similarity index 90% rename from src/PhpWord/Section/Header.php rename to src/PhpWord/Container/Header.php index 5a89c266..5b6fce42 100755 --- a/src/PhpWord/Section/Header.php +++ b/src/PhpWord/Container/Header.php @@ -7,12 +7,17 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section; +namespace PhpOffice\PhpWord\Container; -use PhpOffice\PhpWord\Exceptions\InvalidImageException; +use PhpOffice\PhpWord\Exception\InvalidImageException; use PhpOffice\PhpWord\Media; -use PhpOffice\PhpWord\Section\Footer\PreserveText; use PhpOffice\PhpWord\Shared\String; +use PhpOffice\PhpWord\Element\Text; +use PhpOffice\PhpWord\Element\TextRun; +use PhpOffice\PhpWord\Element\PreserveText; +use PhpOffice\PhpWord\Element\TextBreak; +use PhpOffice\PhpWord\Element\Table; +use PhpOffice\PhpWord\Element\Image; /** * Header element @@ -85,7 +90,7 @@ class Header * @param string $text * @param mixed $styleFont * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Section\Text + * @return \PhpOffice\PhpWord\Element\Text */ public function addText($text, $styleFont = null, $styleParagraph = null) { @@ -115,7 +120,7 @@ class Header * Create a new TextRun * * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Section\TextRun + * @return \PhpOffice\PhpWord\Element\TextRun */ public function createTextRun($styleParagraph = null) { @@ -128,7 +133,7 @@ class Header * Add a Table Element * * @param mixed $style - * @return \PhpOffice\PhpWord\Section\Table + * @return \PhpOffice\PhpWord\Element\Table */ public function addTable($style = null) { @@ -142,7 +147,7 @@ class Header * * @param string $src * @param mixed $style - * @return \PhpOffice\PhpWord\Section\Image + * @return \PhpOffice\PhpWord\Element\Image */ public function addImage($src, $style = null) { @@ -175,7 +180,7 @@ class Header * @param string $text * @param mixed $styleFont * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Section\Footer\PreserveText + * @return \PhpOffice\PhpWord\Element\PreserveText */ public function addPreserveText($text, $styleFont = null, $styleParagraph = null) { @@ -192,7 +197,7 @@ class Header * * @param string $src * @param mixed $style - * @return \PhpOffice\PhpWord\Section\Image + * @return \PhpOffice\PhpWord\Element\Image */ public function addWatermark($src, $style = null) { diff --git a/src/PhpWord/Section.php b/src/PhpWord/Container/Section.php similarity index 83% rename from src/PhpWord/Section.php rename to src/PhpWord/Container/Section.php index 090ca236..a3002cb3 100644 --- a/src/PhpWord/Section.php +++ b/src/PhpWord/Container/Section.php @@ -7,22 +7,27 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord; +namespace PhpOffice\PhpWord\Container; -use PhpOffice\PhpWord\Exceptions\InvalidObjectException; -use PhpOffice\PhpWord\Section\Footer; -use PhpOffice\PhpWord\Section\Image; -use PhpOffice\PhpWord\Section\Header; -use PhpOffice\PhpWord\Section\Link; -use PhpOffice\PhpWord\Section\ListItem; -use PhpOffice\PhpWord\Section\Object; -use PhpOffice\PhpWord\Section\PageBreak; -use PhpOffice\PhpWord\Section\Table; -use PhpOffice\PhpWord\Section\Text; -use PhpOffice\PhpWord\Section\TextBreak; -use PhpOffice\PhpWord\Section\TextRun; -use PhpOffice\PhpWord\Section\Title; -use PhpOffice\PhpWord\Section\CheckBox; +use PhpOffice\PhpWord\Exception\InvalidImageException; +use PhpOffice\PhpWord\Exception\InvalidObjectException; +use PhpOffice\PhpWord\Footnote; +use PhpOffice\PhpWord\Media; +use PhpOffice\PhpWord\Style; +use PhpOffice\PhpWord\TOC; +use PhpOffice\PhpWord\Container\Footer; +use PhpOffice\PhpWord\Container\Header; +use PhpOffice\PhpWord\Element\Text; +use PhpOffice\PhpWord\Element\TextRun; +use PhpOffice\PhpWord\Element\Title; +use PhpOffice\PhpWord\Element\Link; +use PhpOffice\PhpWord\Element\TextBreak; +use PhpOffice\PhpWord\Element\PageBreak; +use PhpOffice\PhpWord\Element\ListItem; +use PhpOffice\PhpWord\Element\Table; +use PhpOffice\PhpWord\Element\Image; +use PhpOffice\PhpWord\Element\Object; +use PhpOffice\PhpWord\Element\CheckBox; use PhpOffice\PhpWord\Shared\String; /** @@ -40,7 +45,7 @@ class Section /** * Section settings * - * @var \PhpOffice\PhpWord\Section\Settings + * @var \PhpOffice\PhpWord\Container\Settings */ private $_settings; @@ -61,7 +66,7 @@ class Section /** * Section Footer * - * @var \PhpOffice\PhpWord\Section\Footer + * @var \PhpOffice\PhpWord\Container\Footer */ private $_footer = null; @@ -75,7 +80,7 @@ class Section public function __construct($sectionCount, $settings = null) { $this->_sectionCount = $sectionCount; - $this->_settings = new \PhpOffice\PhpWord\Section\Settings(); + $this->_settings = new \PhpOffice\PhpWord\Container\Settings(); $this->setSettings($settings); } @@ -99,7 +104,7 @@ class Section /** * Get Section Settings * - * @return \PhpOffice\PhpWord\Section\Settings + * @return \PhpOffice\PhpWord\Container\Settings */ public function getSettings() { @@ -112,7 +117,7 @@ class Section * @param string $text * @param mixed $styleFont * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Section\Text + * @return \PhpOffice\PhpWord\Element\Text */ public function addText($text, $styleFont = null, $styleParagraph = null) { @@ -131,7 +136,7 @@ class Section * @param string $linkName * @param mixed $styleFont * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Section\Link + * @return \PhpOffice\PhpWord\Element\Link */ public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null) { @@ -178,7 +183,7 @@ class Section * Add a Table Element * * @param mixed $style - * @return \PhpOffice\PhpWord\Section\Table + * @return \PhpOffice\PhpWord\Element\Table */ public function addTable($style = null) { @@ -195,7 +200,7 @@ class Section * @param mixed $styleFont * @param mixed $styleList * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Section\ListItem + * @return \PhpOffice\PhpWord\Element\ListItem */ public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null) { @@ -210,11 +215,11 @@ class Section /** * Add a OLE-Object Element * - * All exceptions should be handled by PhpOffice\PhpWord\Section\Object + * All exceptions should be handled by PhpOffice\PhpWord\Element\Object * * @param string $src * @param mixed $style - * @return \PhpOffice\PhpWord\Section\Object + * @return \PhpOffice\PhpWord\Element\Object */ public function addObject($src, $style = null) { @@ -225,7 +230,7 @@ class Section if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') { $ext = substr($ext, 0, -1); } - $icon = __DIR__ . "/_staticDocParts/_{$ext}.png"; + $icon = __DIR__ . "/../_staticDocParts/_{$ext}.png"; $rIDimg = Media::addSectionMediaElement($icon, 'image', new Image($icon)); $data = Media::addSectionMediaElement($src, 'oleObject'); $rID = $data[0]; @@ -243,11 +248,11 @@ class Section /** * Add image element * - * All exceptions should be handled by PhpOffice\PhpWord\Section\Image + * All exceptions should be handled by PhpOffice\PhpWord\Element\Image * * @param string $src * @param mixed $style - * @return \PhpOffice\PhpWord\Section\Image + * @return \PhpOffice\PhpWord\Element\Image */ public function addImage($src, $style = null) { @@ -290,7 +295,7 @@ class Section * * @param string $text * @param int $depth - * @return \PhpOffice\PhpWord\Section\Title + * @return \PhpOffice\PhpWord\Element\Title */ public function addTitle($text, $depth = 1) { @@ -321,7 +326,7 @@ class Section * Create a new TextRun * * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Section\TextRun + * @return \PhpOffice\PhpWord\Element\TextRun */ public function createTextRun($styleParagraph = null) { @@ -343,7 +348,7 @@ class Section /** * Create a new Header * - * @return \PhpOffice\PhpWord\Section\Header + * @return \PhpOffice\PhpWord\Container\Header */ public function createHeader() { @@ -381,7 +386,7 @@ class Section /** * Create a new Footer * - * @return \PhpOffice\PhpWord\Section\Footer + * @return \PhpOffice\PhpWord\Container\Footer */ public function createFooter() { @@ -393,7 +398,7 @@ class Section /** * Get footer element * - * @return \PhpOffice\PhpWord\Section\Footer + * @return \PhpOffice\PhpWord\Container\Footer */ public function getFooter() { @@ -404,11 +409,11 @@ class Section * Create a new Footnote Element * * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Section\Footnote + * @return \PhpOffice\PhpWord\Element\Footnote */ public function createFootnote($styleParagraph = null) { - $footnote = new \PhpOffice\PhpWord\Section\Footnote($styleParagraph); + $footnote = new \PhpOffice\PhpWord\Element\Footnote($styleParagraph); $refID = Footnote::addFootnoteElement($footnote); $footnote->setReferenceId($refID); $this->_elementCollection[] = $footnote; @@ -422,7 +427,7 @@ class Section * @param string $text * @param mixed $styleFont * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Section\CheckBox + * @return \PhpOffice\PhpWord\Element\CheckBox */ public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null) { diff --git a/src/PhpWord/Section/Settings.php b/src/PhpWord/Container/Settings.php similarity index 99% rename from src/PhpWord/Section/Settings.php rename to src/PhpWord/Container/Settings.php index 2dbfe2d8..daf5d13b 100644 --- a/src/PhpWord/Section/Settings.php +++ b/src/PhpWord/Container/Settings.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section; +namespace PhpOffice\PhpWord\Container; /** * Section settings diff --git a/src/PhpWord/Section/CheckBox.php b/src/PhpWord/Element/CheckBox.php similarity index 98% rename from src/PhpWord/Section/CheckBox.php rename to src/PhpWord/Element/CheckBox.php index 5e101d9f..9a3fcc79 100644 --- a/src/PhpWord/Section/CheckBox.php +++ b/src/PhpWord/Element/CheckBox.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section; +namespace PhpOffice\PhpWord\Element; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; diff --git a/src/PhpWord/Section/Footnote.php b/src/PhpWord/Element/Footnote.php similarity index 96% rename from src/PhpWord/Section/Footnote.php rename to src/PhpWord/Element/Footnote.php index c86d8e44..d1cb61cf 100644 --- a/src/PhpWord/Section/Footnote.php +++ b/src/PhpWord/Element/Footnote.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section; +namespace PhpOffice\PhpWord\Element; use PhpOffice\PhpWord\Style\Paragraph; @@ -67,7 +67,7 @@ class Footnote * * @param string $text * @param mixed $styleFont - * @return \PhpOffice\PhpWord\Section\Text + * @return \PhpOffice\PhpWord\Element\Text */ public function addText($text = null, $styleFont = null) { @@ -97,7 +97,7 @@ class Footnote * @param string $linkSrc * @param string $linkName * @param mixed $styleFont - * @return \PhpOffice\PhpWord\Section\Link + * @return \PhpOffice\PhpWord\Element\Link */ public function addLink($linkSrc, $linkName = null, $styleFont = null) { diff --git a/src/PhpWord/Section/Image.php b/src/PhpWord/Element/Image.php similarity index 95% rename from src/PhpWord/Section/Image.php rename to src/PhpWord/Element/Image.php index 9f5a7838..89ee6f37 100755 --- a/src/PhpWord/Section/Image.php +++ b/src/PhpWord/Element/Image.php @@ -7,10 +7,10 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section; +namespace PhpOffice\PhpWord\Element; -use PhpOffice\PhpWord\Exceptions\InvalidImageException; -use PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException; +use PhpOffice\PhpWord\Exception\InvalidImageException; +use PhpOffice\PhpWord\Exception\UnsupportedImageTypeException; /** * Image element @@ -86,8 +86,8 @@ class Image * @param string $source * @param mixed $style * @param bool $isWatermark - * @throws \PhpOffice\PhpWord\Exceptions\InvalidImageException - * @throws \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException + * @throws \PhpOffice\PhpWord\Exception\InvalidImageException + * @throws \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException */ public function __construct($source, $style = null, $isWatermark = false) { diff --git a/src/PhpWord/Section/Link.php b/src/PhpWord/Element/Link.php similarity index 98% rename from src/PhpWord/Section/Link.php rename to src/PhpWord/Element/Link.php index 240d0445..1b6cf22a 100644 --- a/src/PhpWord/Section/Link.php +++ b/src/PhpWord/Element/Link.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section; +namespace PhpOffice\PhpWord\Element; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; diff --git a/src/PhpWord/Section/ListItem.php b/src/PhpWord/Element/ListItem.php similarity index 95% rename from src/PhpWord/Section/ListItem.php rename to src/PhpWord/Element/ListItem.php index 3981814e..e920d4b8 100644 --- a/src/PhpWord/Section/ListItem.php +++ b/src/PhpWord/Element/ListItem.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section; +namespace PhpOffice\PhpWord\Element; /** * List item element @@ -24,7 +24,7 @@ class ListItem /** * Textrun * - * @var \PhpOffice\PhpWord\Section\Text + * @var \PhpOffice\PhpWord\Element\Text */ private $_textObject; diff --git a/src/PhpWord/Section/Object.php b/src/PhpWord/Element/Object.php similarity index 98% rename from src/PhpWord/Section/Object.php rename to src/PhpWord/Element/Object.php index 9b6cc328..61bf6f8e 100644 --- a/src/PhpWord/Section/Object.php +++ b/src/PhpWord/Element/Object.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section; +namespace PhpOffice\PhpWord\Element; /** * Object element diff --git a/src/PhpWord/Section/PageBreak.php b/src/PhpWord/Element/PageBreak.php similarity index 89% rename from src/PhpWord/Section/PageBreak.php rename to src/PhpWord/Element/PageBreak.php index e12c3e3d..4e49582d 100644 --- a/src/PhpWord/Section/PageBreak.php +++ b/src/PhpWord/Element/PageBreak.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section; +namespace PhpOffice\PhpWord\Element; /** * Page break element diff --git a/src/PhpWord/Section/Footer/PreserveText.php b/src/PhpWord/Element/PreserveText.php similarity index 98% rename from src/PhpWord/Section/Footer/PreserveText.php rename to src/PhpWord/Element/PreserveText.php index 6fbd7bba..453e9b84 100644 --- a/src/PhpWord/Section/Footer/PreserveText.php +++ b/src/PhpWord/Element/PreserveText.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section\Footer; +namespace PhpOffice\PhpWord\Element; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; diff --git a/src/PhpWord/Section/Table.php b/src/PhpWord/Element/Table.php similarity index 95% rename from src/PhpWord/Section/Table.php rename to src/PhpWord/Element/Table.php index aff9a23a..fc168349 100644 --- a/src/PhpWord/Section/Table.php +++ b/src/PhpWord/Element/Table.php @@ -7,9 +7,9 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section; +namespace PhpOffice\PhpWord\Element; -use PhpOffice\PhpWord\Section\Table\Row; +use PhpOffice\PhpWord\Element\Table\Row; /** * Table element @@ -98,7 +98,7 @@ class Table * * @param int $width * @param mixed $style - * @return \PhpOffice\PhpWord\Section\Table\Cell + * @return \PhpOffice\PhpWord\Element\Table\Cell */ public function addCell($width = null, $style = null) { diff --git a/src/PhpWord/Section/Table/Cell.php b/src/PhpWord/Element/Table/Cell.php similarity index 89% rename from src/PhpWord/Section/Table/Cell.php rename to src/PhpWord/Element/Table/Cell.php index e0004350..0e0d688e 100755 --- a/src/PhpWord/Section/Table/Cell.php +++ b/src/PhpWord/Element/Table/Cell.php @@ -7,21 +7,21 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section\Table; +namespace PhpOffice\PhpWord\Element\Table; -use PhpOffice\PhpWord\Exceptions\Exception; -use PhpOffice\PhpWord\Exceptions\InvalidObjectException; -use PhpOffice\PhpWord\Exceptions\InvalidImageException; +use PhpOffice\PhpWord\Exception\Exception; +use PhpOffice\PhpWord\Exception\InvalidObjectException; +use PhpOffice\PhpWord\Exception\InvalidImageException; use PhpOffice\PhpWord\Media; -use PhpOffice\PhpWord\Section\Footer\PreserveText; -use PhpOffice\PhpWord\Section\Image; -use PhpOffice\PhpWord\Section\Link; -use PhpOffice\PhpWord\Section\ListItem; -use PhpOffice\PhpWord\Section\Object; -use PhpOffice\PhpWord\Section\Text; -use PhpOffice\PhpWord\Section\TextBreak; -use PhpOffice\PhpWord\Section\TextRun; -use PhpOffice\PhpWord\Section\CheckBox; +use PhpOffice\PhpWord\Element\PreserveText; +use PhpOffice\PhpWord\Element\Image; +use PhpOffice\PhpWord\Element\Link; +use PhpOffice\PhpWord\Element\ListItem; +use PhpOffice\PhpWord\Element\Object; +use PhpOffice\PhpWord\Element\Text; +use PhpOffice\PhpWord\Element\TextBreak; +use PhpOffice\PhpWord\Element\TextRun; +use PhpOffice\PhpWord\Element\CheckBox; use PhpOffice\PhpWord\Shared\String; /** @@ -100,7 +100,7 @@ class Cell * @param string $text * @param mixed $styleFont * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Section\Text + * @return \PhpOffice\PhpWord\Element\Text */ public function addText($text, $styleFont = null, $styleParagraph = null) { @@ -118,7 +118,7 @@ class Cell * @param string $linkSrc * @param string $linkName * @param mixed $style - * @return \PhpOffice\PhpWord\Section\Link + * @return \PhpOffice\PhpWord\Element\Link */ public function addLink($linkSrc, $linkName = null, $style = null) { @@ -165,7 +165,7 @@ class Cell * @param int $depth * @param mixed $styleText * @param mixed $styleList - * @return \PhpOffice\PhpWord\Section\ListItem + * @return \PhpOffice\PhpWord\Element\ListItem */ public function addListItem($text, $depth = 0, $styleText = null, $styleList = null) { @@ -182,7 +182,7 @@ class Cell * * @param string $src * @param mixed $style - * @return \PhpOffice\PhpWord\Section\Image + * @return \PhpOffice\PhpWord\Element\Image */ public function addImage($src, $style = null) { @@ -220,7 +220,7 @@ class Cell * * @param string $src * @param mixed $style - * @return \PhpOffice\PhpWord\Section\Object + * @return \PhpOffice\PhpWord\Element\Object */ public function addObject($src, $style = null) { @@ -262,7 +262,7 @@ class Cell * @param string $text * @param mixed $styleFont * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Section\Footer\PreserveText + * @return \PhpOffice\PhpWord\Element\PreserveText */ public function addPreserveText($text, $styleFont = null, $styleParagraph = null) { @@ -282,7 +282,7 @@ class Cell * Create a new TextRun * * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Section\TextRun + * @return \PhpOffice\PhpWord\Element\TextRun */ public function createTextRun($styleParagraph = null) { @@ -298,7 +298,7 @@ class Cell * @param string $text * @param mixed $styleFont * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Section\CheckBox + * @return \PhpOffice\PhpWord\Element\CheckBox */ public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null) { diff --git a/src/PhpWord/Section/Table/Row.php b/src/PhpWord/Element/Table/Row.php similarity index 95% rename from src/PhpWord/Section/Table/Row.php rename to src/PhpWord/Element/Table/Row.php index 8410f571..c027d373 100644 --- a/src/PhpWord/Section/Table/Row.php +++ b/src/PhpWord/Element/Table/Row.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section\Table; +namespace PhpOffice\PhpWord\Element\Table; /** * Table row element @@ -83,7 +83,7 @@ class Row * * @param int $width * @param mixed $style - * @return \PhpOffice\PhpWord\Section\Table\Cell + * @return \PhpOffice\PhpWord\Element\Table\Cell */ public function addCell($width = null, $style = null) { diff --git a/src/PhpWord/Section/Text.php b/src/PhpWord/Element/Text.php similarity index 98% rename from src/PhpWord/Section/Text.php rename to src/PhpWord/Element/Text.php index 4e228cda..75e1727e 100644 --- a/src/PhpWord/Section/Text.php +++ b/src/PhpWord/Element/Text.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section; +namespace PhpOffice\PhpWord\Element; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; diff --git a/src/PhpWord/Section/TextBreak.php b/src/PhpWord/Element/TextBreak.php similarity index 98% rename from src/PhpWord/Section/TextBreak.php rename to src/PhpWord/Element/TextBreak.php index 4df4c780..39f3ac16 100755 --- a/src/PhpWord/Section/TextBreak.php +++ b/src/PhpWord/Element/TextBreak.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section; +namespace PhpOffice\PhpWord\Element; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; diff --git a/src/PhpWord/Section/TextRun.php b/src/PhpWord/Element/TextRun.php similarity index 91% rename from src/PhpWord/Section/TextRun.php rename to src/PhpWord/Element/TextRun.php index bcb5173a..a890bb04 100755 --- a/src/PhpWord/Section/TextRun.php +++ b/src/PhpWord/Element/TextRun.php @@ -7,9 +7,9 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section; +namespace PhpOffice\PhpWord\Element; -use PhpOffice\PhpWord\Exceptions\InvalidImageException; +use PhpOffice\PhpWord\Exception\InvalidImageException; use PhpOffice\PhpWord\Media; use PhpOffice\PhpWord\Shared\String; use PhpOffice\PhpWord\Style\Font; @@ -65,7 +65,7 @@ class TextRun * * @param string $text * @param mixed $styleFont - * @return \PhpOffice\PhpWord\Section\Text + * @return \PhpOffice\PhpWord\Element\Text */ public function addText($text = null, $styleFont = null) { @@ -83,7 +83,7 @@ class TextRun * @param string $linkSrc * @param string $linkName * @param mixed $styleFont - * @return \PhpOffice\PhpWord\Section\Link + * @return \PhpOffice\PhpWord\Element\Link */ public function addLink($linkSrc, $linkName = null, $styleFont = null) { @@ -105,7 +105,7 @@ class TextRun * * @param string $imageSrc * @param mixed $style - * @return \PhpOffice\PhpWord\Section\Image + * @return \PhpOffice\PhpWord\Element\Image */ public function addImage($imageSrc, $style = null) { @@ -138,11 +138,11 @@ class TextRun * Create a new Footnote Element * * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Section\Footnote + * @return \PhpOffice\PhpWord\Element\Footnote */ public function createFootnote($styleParagraph = null) { - $footnote = new \PhpOffice\PhpWord\Section\Footnote($styleParagraph); + $footnote = new \PhpOffice\PhpWord\Element\Footnote($styleParagraph); $refID = \PhpOffice\PhpWord\Footnote::addFootnoteElement($footnote); $footnote->setReferenceId($refID); $this->_elementCollection[] = $footnote; diff --git a/src/PhpWord/Section/Title.php b/src/PhpWord/Element/Title.php similarity index 98% rename from src/PhpWord/Section/Title.php rename to src/PhpWord/Element/Title.php index c93f6687..3921d66c 100644 --- a/src/PhpWord/Section/Title.php +++ b/src/PhpWord/Element/Title.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Section; +namespace PhpOffice\PhpWord\Element; /** * Title element diff --git a/src/PhpWord/Exceptions/Exception.php b/src/PhpWord/Exception/Exception.php similarity index 86% rename from src/PhpWord/Exceptions/Exception.php rename to src/PhpWord/Exception/Exception.php index 44323baa..470698a8 100755 --- a/src/PhpWord/Exceptions/Exception.php +++ b/src/PhpWord/Exception/Exception.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Exceptions; +namespace PhpOffice\PhpWord\Exception; /** * General exception diff --git a/src/PhpWord/Exceptions/InvalidImageException.php b/src/PhpWord/Exception/InvalidImageException.php similarity index 88% rename from src/PhpWord/Exceptions/InvalidImageException.php rename to src/PhpWord/Exception/InvalidImageException.php index 319a6203..c8d2143c 100644 --- a/src/PhpWord/Exceptions/InvalidImageException.php +++ b/src/PhpWord/Exception/InvalidImageException.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Exceptions; +namespace PhpOffice\PhpWord\Exception; /** * Exception used for when an image is not found diff --git a/src/PhpWord/Exceptions/InvalidObjectException.php b/src/PhpWord/Exception/InvalidObjectException.php similarity index 88% rename from src/PhpWord/Exceptions/InvalidObjectException.php rename to src/PhpWord/Exception/InvalidObjectException.php index c6a89ac7..b27de805 100644 --- a/src/PhpWord/Exceptions/InvalidObjectException.php +++ b/src/PhpWord/Exception/InvalidObjectException.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Exceptions; +namespace PhpOffice\PhpWord\Exception; /** * Exception used for when an image is not found diff --git a/src/PhpWord/Exceptions/InvalidStyleException.php b/src/PhpWord/Exception/InvalidStyleException.php similarity index 89% rename from src/PhpWord/Exceptions/InvalidStyleException.php rename to src/PhpWord/Exception/InvalidStyleException.php index 9b22e0ae..37290e63 100644 --- a/src/PhpWord/Exceptions/InvalidStyleException.php +++ b/src/PhpWord/Exception/InvalidStyleException.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Exceptions; +namespace PhpOffice\PhpWord\Exception; use InvalidArgumentException; diff --git a/src/PhpWord/Exceptions/UnsupportedImageTypeException.php b/src/PhpWord/Exception/UnsupportedImageTypeException.php similarity index 88% rename from src/PhpWord/Exceptions/UnsupportedImageTypeException.php rename to src/PhpWord/Exception/UnsupportedImageTypeException.php index a2ea1ee9..2b1a25ce 100644 --- a/src/PhpWord/Exceptions/UnsupportedImageTypeException.php +++ b/src/PhpWord/Exception/UnsupportedImageTypeException.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Exceptions; +namespace PhpOffice\PhpWord\Exception; /** * Exception used for when an image type is unsupported diff --git a/src/PhpWord/Footnote.php b/src/PhpWord/Footnote.php index 69228ee8..af104bc8 100644 --- a/src/PhpWord/Footnote.php +++ b/src/PhpWord/Footnote.php @@ -33,7 +33,7 @@ class Footnote * * @return int Reference ID */ - public static function addFootnoteElement(\PhpOffice\PhpWord\Section\Footnote $footnote) + public static function addFootnoteElement(\PhpOffice\PhpWord\Element\Footnote $footnote) { $refID = self::countFootnoteElements() + 2; diff --git a/src/PhpWord/HashTable.php b/src/PhpWord/HashTable.php index 97e9be45..93bcb689 100644 --- a/src/PhpWord/HashTable.php +++ b/src/PhpWord/HashTable.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; /** * Hash table @@ -48,7 +48,7 @@ class HashTable * Add HashTable items from source * * @param \PhpOffice\PhpWord\IComparable[] $pSource Source array to create HashTable from - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws \PhpOffice\PhpWord\Exception\Exception */ public function addFromSource($pSource = null) { diff --git a/src/PhpWord/IOFactory.php b/src/PhpWord/IOFactory.php index 6965e270..f99f7a00 100644 --- a/src/PhpWord/IOFactory.php +++ b/src/PhpWord/IOFactory.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; /** * IO factory @@ -22,7 +22,7 @@ abstract class IOFactory * @param \PhpOffice\PhpWord\PhpWord $phpWord * @param string $name * @return \PhpOffice\PhpWord\Writer\IWriter - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws \PhpOffice\PhpWord\Exception\Exception */ public static function createWriter(PhpWord $phpWord, $name) { @@ -39,7 +39,7 @@ abstract class IOFactory * * @param string $name * @return \PhpOffice\PhpWord\Reader\IReader - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws \PhpOffice\PhpWord\Exception\Exception */ public static function createReader($name) { diff --git a/src/PhpWord/Media.php b/src/PhpWord/Media.php index a9a9a6af..35800f92 100755 --- a/src/PhpWord/Media.php +++ b/src/PhpWord/Media.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord; -use PhpOffice\PhpWord\Section\Image; +use PhpOffice\PhpWord\Element\Image; /** * Media @@ -53,7 +53,7 @@ class Media * * @param string $src * @param string $type - * @param \PhpOffice\PhpWord\Section\Image $image + * @param \PhpOffice\PhpWord\Element\Image $image * @return mixed */ public static function addSectionMediaElement($src, $type, Image $image = null) @@ -165,7 +165,7 @@ class Media * * @param int $headerCount * @param string $src - * @param \PhpOffice\PhpWord\Section\Image $image + * @param \PhpOffice\PhpWord\Element\Image $image * @return int */ public static function addHeaderMediaElement($headerCount, $src, Image $image = null) @@ -228,7 +228,7 @@ class Media * * @param int $footerCount * @param string $src - * @param \PhpOffice\PhpWord\Section\Image $image + * @param \PhpOffice\PhpWord\Element\Image $image * @return int */ public static function addFooterMediaElement($footerCount, $src, Image $image = null) diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index e0506803..894d7df7 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -10,8 +10,8 @@ namespace PhpOffice\PhpWord; use PhpOffice\PhpWord\DocumentProperties; -use PhpOffice\PhpWord\Exceptions\Exception; -use PhpOffice\PhpWord\Section; +use PhpOffice\PhpWord\Exception\Exception; +use PhpOffice\PhpWord\Container\Section; use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Template; @@ -94,8 +94,8 @@ class PhpWord /** * Create new section * - * @param \PhpOffice\PhpWord\Section\Settings $settings - * @return \PhpOffice\PhpWord\Section + * @param \PhpOffice\PhpWord\Container\Settings $settings + * @return \PhpOffice\PhpWord\Container\Section */ public function createSection($settings = null) { @@ -216,7 +216,7 @@ class PhpWord /** * Get all sections * - * @return \PhpOffice\PhpWord\Section[] + * @return \PhpOffice\PhpWord\Container\Section[] */ public function getSections() { @@ -228,7 +228,7 @@ class PhpWord * * @param string $filename Fully qualified filename. * @return \PhpOffice\PhpWord\Template - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws \PhpOffice\PhpWord\Exception\Exception */ public function loadTemplate($filename) { diff --git a/src/PhpWord/Reader/Reader.php b/src/PhpWord/Reader/Reader.php index 153a4013..e50ca7b5 100644 --- a/src/PhpWord/Reader/Reader.php +++ b/src/PhpWord/Reader/Reader.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord\Reader; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; /** * Reader abstract class @@ -60,7 +60,7 @@ abstract class Reader implements IReader * * @param string $pFilename * @return resource - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws \PhpOffice\PhpWord\Exception\Exception */ protected function openFile($pFilename) { diff --git a/src/PhpWord/Reader/Word2007.php b/src/PhpWord/Reader/Word2007.php index 9161d162..12cf4e6b 100644 --- a/src/PhpWord/Reader/Word2007.php +++ b/src/PhpWord/Reader/Word2007.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Reader; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\DocumentProperties; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; /** * Reader for Word2007 @@ -24,7 +24,7 @@ class Word2007 extends Reader implements IReader * * @param string $pFilename * @return bool - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws \PhpOffice\PhpWord\Exception\Exception */ public function canRead($pFilename) { diff --git a/src/PhpWord/Shared/ZipArchive.php b/src/PhpWord/Shared/ZipArchive.php index ae22357a..f207385c 100644 --- a/src/PhpWord/Shared/ZipArchive.php +++ b/src/PhpWord/Shared/ZipArchive.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord\Shared; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; // @codeCoverageIgnoreStart if (!defined('PCLZIP_TEMPORARY_DIR')) { diff --git a/src/PhpWord/Shared/ZipStreamWrapper.php b/src/PhpWord/Shared/ZipStreamWrapper.php index 87e17983..23b69d84 100644 --- a/src/PhpWord/Shared/ZipStreamWrapper.php +++ b/src/PhpWord/Shared/ZipStreamWrapper.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord\Shared; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Settings; /** diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index d1f5353a..cc1f530c 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -10,7 +10,7 @@ namespace PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Exceptions\InvalidStyleException; +use PhpOffice\PhpWord\Exception\InvalidStyleException; /** * Font style @@ -513,7 +513,7 @@ class Font * * @param int|float|string $lineHeight * @return $this - * @throws \PhpOffice\PhpWord\Exceptions\InvalidStyleException + * @throws \PhpOffice\PhpWord\Exception\InvalidStyleException */ public function setLineHeight($lineHeight) { diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index 982aee44..dbb04610 100755 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord\Style; -use PhpOffice\PhpWord\Exceptions\InvalidStyleException; +use PhpOffice\PhpWord\Exception\InvalidStyleException; /** * Paragraph style @@ -468,7 +468,7 @@ class Paragraph * * @param int|float|string $lineHeight * @return $this - * @throws \PhpOffice\PhpWord\Exceptions\InvalidStyleException + * @throws \PhpOffice\PhpWord\Exception\InvalidStyleException */ public function setLineHeight($lineHeight) { diff --git a/src/PhpWord/Template.php b/src/PhpWord/Template.php index f54d729e..0711ecd5 100644 --- a/src/PhpWord/Template.php +++ b/src/PhpWord/Template.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Shared\String; @@ -44,7 +44,7 @@ class Template * Create a new Template Object * * @param string $strFilename - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws \PhpOffice\PhpWord\Exception\Exception */ public function __construct($strFilename) { @@ -71,7 +71,7 @@ class Template * @param \DOMDocument $xslDOMDocument * @param array $xslOptions * @param string $xslOptionsURI - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws \PhpOffice\PhpWord\Exception\Exception */ public function applyXslStyleSheet(&$xslDOMDocument, $xslOptions = array(), $xslOptionsURI = '') { @@ -147,7 +147,7 @@ class Template * * @param int $offset * @return int - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws \PhpOffice\PhpWord\Exception\Exception */ private function _findRowStart($offset) { @@ -193,7 +193,7 @@ class Template * * @param string $search * @param int $numberOfClones - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws \PhpOffice\PhpWord\Exception\Exception */ public function cloneRow($search, $numberOfClones) { @@ -247,7 +247,7 @@ class Template * Save XML to temporary file * * @return string - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws \PhpOffice\PhpWord\Exception\Exception */ public function save() { diff --git a/src/PhpWord/Writer/ODText.php b/src/PhpWord/Writer/ODText.php index 7ff31bf0..72955ef1 100755 --- a/src/PhpWord/Writer/ODText.php +++ b/src/PhpWord/Writer/ODText.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord\Writer; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\HashTable; use PhpOffice\PhpWord\Settings; diff --git a/src/PhpWord/Writer/ODText/Content.php b/src/PhpWord/Writer/ODText/Content.php index 2e9a61e6..ccbdcfce 100644 --- a/src/PhpWord/Writer/ODText/Content.php +++ b/src/PhpWord/Writer/ODText/Content.php @@ -10,17 +10,17 @@ namespace PhpOffice\PhpWord\Writer\ODText; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Section; -use PhpOffice\PhpWord\Section\Image; -use PhpOffice\PhpWord\Section\Link; -use PhpOffice\PhpWord\Section\ListItem; -use PhpOffice\PhpWord\Section\Object; -use PhpOffice\PhpWord\Section\PageBreak; -use PhpOffice\PhpWord\Section\Table; -use PhpOffice\PhpWord\Section\Text; -use PhpOffice\PhpWord\Section\TextBreak; -use PhpOffice\PhpWord\Section\TextRun; -use PhpOffice\PhpWord\Section\Title; +use PhpOffice\PhpWord\Container\Section; +use PhpOffice\PhpWord\Element\Image; +use PhpOffice\PhpWord\Element\Link; +use PhpOffice\PhpWord\Element\ListItem; +use PhpOffice\PhpWord\Element\Object; +use PhpOffice\PhpWord\Element\PageBreak; +use PhpOffice\PhpWord\Element\Table; +use PhpOffice\PhpWord\Element\Text; +use PhpOffice\PhpWord\Element\TextBreak; +use PhpOffice\PhpWord\Element\TextRun; +use PhpOffice\PhpWord\Element\Title; use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Font; diff --git a/src/PhpWord/Writer/ODText/Manifest.php b/src/PhpWord/Writer/ODText/Manifest.php index 31168e3d..69949c73 100755 --- a/src/PhpWord/Writer/ODText/Manifest.php +++ b/src/PhpWord/Writer/ODText/Manifest.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord\Writer\ODText; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Shared\XMLWriter; diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index e37e53b3..b5ce20a7 100755 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -9,19 +9,19 @@ namespace PhpOffice\PhpWord\Writer; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\HashTable; -use PhpOffice\PhpWord\Section\Image; -use PhpOffice\PhpWord\Section\Link; -use PhpOffice\PhpWord\Section\ListItem; -use PhpOffice\PhpWord\Section\Object; -use PhpOffice\PhpWord\Section\PageBreak; -use PhpOffice\PhpWord\Section\Table; -use PhpOffice\PhpWord\Section\Text; -use PhpOffice\PhpWord\Section\TextBreak; -use PhpOffice\PhpWord\Section\TextRun; -use PhpOffice\PhpWord\Section\Title; +use PhpOffice\PhpWord\Element\Image; +use PhpOffice\PhpWord\Element\Link; +use PhpOffice\PhpWord\Element\ListItem; +use PhpOffice\PhpWord\Element\Object; +use PhpOffice\PhpWord\Element\PageBreak; +use PhpOffice\PhpWord\Element\Table; +use PhpOffice\PhpWord\Element\Text; +use PhpOffice\PhpWord\Element\TextBreak; +use PhpOffice\PhpWord\Element\TextRun; +use PhpOffice\PhpWord\Element\Title; use PhpOffice\PhpWord\Shared\Drawing; use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Font; diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index 687bf8d2..5f247707 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord\Writer; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Footnote; use PhpOffice\PhpWord\Media; diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index 3acf6c1d..b6e4c32c 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -10,18 +10,18 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Section\Text; -use PhpOffice\PhpWord\Section\TextRun; -use PhpOffice\PhpWord\Section\Link; -use PhpOffice\PhpWord\Section\Title; -use PhpOffice\PhpWord\Section\Footer\PreserveText; -use PhpOffice\PhpWord\Section\TextBreak; -use PhpOffice\PhpWord\Section\ListItem; -use PhpOffice\PhpWord\Section\Table; -use PhpOffice\PhpWord\Section\Image; -use PhpOffice\PhpWord\Section\Object; -use PhpOffice\PhpWord\Section\Footnote; -use PhpOffice\PhpWord\Section\CheckBox; +use PhpOffice\PhpWord\Element\Text; +use PhpOffice\PhpWord\Element\TextRun; +use PhpOffice\PhpWord\Element\Link; +use PhpOffice\PhpWord\Element\Title; +use PhpOffice\PhpWord\Element\PreserveText; +use PhpOffice\PhpWord\Element\TextBreak; +use PhpOffice\PhpWord\Element\ListItem; +use PhpOffice\PhpWord\Element\Table; +use PhpOffice\PhpWord\Element\Image; +use PhpOffice\PhpWord\Element\Object; +use PhpOffice\PhpWord\Element\Footnote; +use PhpOffice\PhpWord\Element\CheckBox; use PhpOffice\PhpWord\Shared\String; use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Style\Paragraph; diff --git a/src/PhpWord/Writer/Word2007/ContentTypes.php b/src/PhpWord/Writer/Word2007/ContentTypes.php index 65c75384..98121133 100755 --- a/src/PhpWord/Writer/Word2007/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/ContentTypes.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Shared\XMLWriter; /** diff --git a/src/PhpWord/Writer/Word2007/Document.php b/src/PhpWord/Writer/Word2007/Document.php index 03184bd1..c4f8278f 100644 --- a/src/PhpWord/Writer/Word2007/Document.php +++ b/src/PhpWord/Writer/Word2007/Document.php @@ -10,19 +10,19 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Section; -use PhpOffice\PhpWord\Section\Footnote; -use PhpOffice\PhpWord\Section\Image; -use PhpOffice\PhpWord\Section\Link; -use PhpOffice\PhpWord\Section\ListItem; -use PhpOffice\PhpWord\Section\Object; -use PhpOffice\PhpWord\Section\PageBreak; -use PhpOffice\PhpWord\Section\Table; -use PhpOffice\PhpWord\Section\Text; -use PhpOffice\PhpWord\Section\TextBreak; -use PhpOffice\PhpWord\Section\TextRun; -use PhpOffice\PhpWord\Section\Title; -use PhpOffice\PhpWord\Section\CheckBox; +use PhpOffice\PhpWord\Container\Section; +use PhpOffice\PhpWord\Element\Footnote; +use PhpOffice\PhpWord\Element\Image; +use PhpOffice\PhpWord\Element\Link; +use PhpOffice\PhpWord\Element\ListItem; +use PhpOffice\PhpWord\Element\Object; +use PhpOffice\PhpWord\Element\PageBreak; +use PhpOffice\PhpWord\Element\Table; +use PhpOffice\PhpWord\Element\Text; +use PhpOffice\PhpWord\Element\TextBreak; +use PhpOffice\PhpWord\Element\TextRun; +use PhpOffice\PhpWord\Element\Title; +use PhpOffice\PhpWord\Element\CheckBox; use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; diff --git a/src/PhpWord/Writer/Word2007/DocumentRels.php b/src/PhpWord/Writer/Word2007/DocumentRels.php index 53a5ded5..648d0864 100755 --- a/src/PhpWord/Writer/Word2007/DocumentRels.php +++ b/src/PhpWord/Writer/Word2007/DocumentRels.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Shared\XMLWriter; /** diff --git a/src/PhpWord/Writer/Word2007/Footer.php b/src/PhpWord/Writer/Word2007/Footer.php index 4a3b97f6..de2bf44d 100644 --- a/src/PhpWord/Writer/Word2007/Footer.php +++ b/src/PhpWord/Writer/Word2007/Footer.php @@ -9,13 +9,13 @@ namespace PhpOffice\PhpWord\Writer\Word2007; -use PhpOffice\PhpWord\Section\Footer\PreserveText; -use PhpOffice\PhpWord\Section\Image; -use PhpOffice\PhpWord\Section\Table; -use PhpOffice\PhpWord\Section\Text; -use PhpOffice\PhpWord\Section\TextBreak; -use PhpOffice\PhpWord\Section\TextRun; -use PhpOffice\PhpWord\Section\Footer as FooterElement; +use PhpOffice\PhpWord\Element\PreserveText; +use PhpOffice\PhpWord\Element\Image; +use PhpOffice\PhpWord\Element\Table; +use PhpOffice\PhpWord\Element\Text; +use PhpOffice\PhpWord\Element\TextBreak; +use PhpOffice\PhpWord\Element\TextRun; +use PhpOffice\PhpWord\Container\Footer as FooterElement; use PhpOffice\PhpWord\Shared\XMLWriter; /** diff --git a/src/PhpWord/Writer/Word2007/Footnotes.php b/src/PhpWord/Writer/Word2007/Footnotes.php index ce4aba60..d8ba4352 100644 --- a/src/PhpWord/Writer/Word2007/Footnotes.php +++ b/src/PhpWord/Writer/Word2007/Footnotes.php @@ -9,10 +9,10 @@ namespace PhpOffice\PhpWord\Writer\Word2007; -use PhpOffice\PhpWord\Section\Footnote; -use PhpOffice\PhpWord\Section\Text; -use PhpOffice\PhpWord\Section\Link; -use PhpOffice\PhpWord\Section\TextBreak; +use PhpOffice\PhpWord\Element\Footnote; +use PhpOffice\PhpWord\Element\Text; +use PhpOffice\PhpWord\Element\Link; +use PhpOffice\PhpWord\Element\TextBreak; use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Shared\XMLWriter; diff --git a/src/PhpWord/Writer/Word2007/FootnotesRels.php b/src/PhpWord/Writer/Word2007/FootnotesRels.php index d0665de3..405df69f 100644 --- a/src/PhpWord/Writer/Word2007/FootnotesRels.php +++ b/src/PhpWord/Writer/Word2007/FootnotesRels.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Shared\XMLWriter; /** diff --git a/src/PhpWord/Writer/Word2007/Header.php b/src/PhpWord/Writer/Word2007/Header.php index 03757693..07a16ec7 100644 --- a/src/PhpWord/Writer/Word2007/Header.php +++ b/src/PhpWord/Writer/Word2007/Header.php @@ -9,13 +9,13 @@ namespace PhpOffice\PhpWord\Writer\Word2007; -use PhpOffice\PhpWord\Section\Footer\PreserveText; -use PhpOffice\PhpWord\Section\Image; -use PhpOffice\PhpWord\Section\Table; -use PhpOffice\PhpWord\Section\Text; -use PhpOffice\PhpWord\Section\TextBreak; -use PhpOffice\PhpWord\Section\TextRun; -use PhpOffice\PhpWord\Section\Header as HeaderElement; +use PhpOffice\PhpWord\Element\PreserveText; +use PhpOffice\PhpWord\Element\Image; +use PhpOffice\PhpWord\Element\Table; +use PhpOffice\PhpWord\Element\Text; +use PhpOffice\PhpWord\Element\TextBreak; +use PhpOffice\PhpWord\Element\TextRun; +use PhpOffice\PhpWord\Container\Header as HeaderElement; use PhpOffice\PhpWord\Shared\XMLWriter; /** diff --git a/src/PhpWord/Writer/Word2007/Rels.php b/src/PhpWord/Writer/Word2007/Rels.php index 3e41033f..a675057f 100755 --- a/src/PhpWord/Writer/Word2007/Rels.php +++ b/src/PhpWord/Writer/Word2007/Rels.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Shared\XMLWriter; diff --git a/src/PhpWord/Writer/Word2007/WriterPart.php b/src/PhpWord/Writer/Word2007/WriterPart.php index ead47239..a61662e9 100755 --- a/src/PhpWord/Writer/Word2007/WriterPart.php +++ b/src/PhpWord/Writer/Word2007/WriterPart.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Writer\IWriter; use PhpOffice\PhpWord\Shared\XMLWriter; diff --git a/src/PhpWord/Writer/Writer.php b/src/PhpWord/Writer/Writer.php index bc749738..e53d215a 100644 --- a/src/PhpWord/Writer/Writer.php +++ b/src/PhpWord/Writer/Writer.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord\Writer; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; /** diff --git a/tests/PhpWord/Tests/AutoloaderTest.php b/tests/PhpWord/Tests/AutoloaderTest.php index 549f05a8..ac2f4a78 100644 --- a/tests/PhpWord/Tests/AutoloaderTest.php +++ b/tests/PhpWord/Tests/AutoloaderTest.php @@ -45,11 +45,11 @@ class AutoloaderTest extends \PHPUnit_Framework_TestCase 'classes outside of the PhpOffice\\PhpWord namespace' ); // TODO change this class to the main PhpWord class when it is namespaced - Autoloader::autoload('PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException'); + Autoloader::autoload('PhpOffice\\PhpWord\\Exception\\InvalidStyleException'); $this->assertTrue( - \in_array('PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException', \get_declared_classes()), + \in_array('PhpOffice\\PhpWord\\Exception\\InvalidStyleException', \get_declared_classes()), 'PhpOffice\\PhpWord\\Autoloader::autoload() failed to autoload the ' . - 'PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException class' + 'PhpOffice\\PhpWord\\Exception\\InvalidStyleException class' ); } } diff --git a/tests/PhpWord/Tests/Section/FooterTest.php b/tests/PhpWord/Tests/Container/FooterTest.php similarity index 81% rename from tests/PhpWord/Tests/Section/FooterTest.php rename to tests/PhpWord/Tests/Container/FooterTest.php index d14f125c..d9988ac5 100644 --- a/tests/PhpWord/Tests/Section/FooterTest.php +++ b/tests/PhpWord/Tests/Container/FooterTest.php @@ -7,12 +7,12 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section; +namespace PhpOffice\PhpWord\Tests\Container; -use PhpOffice\PhpWord\Section\Footer; +use PhpOffice\PhpWord\Container\Footer; /** - * Test class for PhpOffice\PhpWord\Section\Footer + * Test class for PhpOffice\PhpWord\Container\Footer * * @runTestsInSeparateProcesses */ @@ -26,7 +26,7 @@ class FooterTest extends \PHPUnit_Framework_TestCase $iVal = rand(1, 1000); $oFooter = new Footer($iVal); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Footer', $oFooter); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Container\\Footer', $oFooter); $this->assertEquals($oFooter->getFooterCount(), $iVal); } @@ -39,7 +39,7 @@ class FooterTest extends \PHPUnit_Framework_TestCase $element = $oFooter->addText('text'); $this->assertCount(1, $oFooter->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element); } /** @@ -51,7 +51,7 @@ class FooterTest extends \PHPUnit_Framework_TestCase $element = $oFooter->addText(utf8_decode('ééé')); $this->assertCount(1, $oFooter->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element); $this->assertEquals($element->getText(), 'ééé'); } @@ -76,7 +76,7 @@ class FooterTest extends \PHPUnit_Framework_TestCase $element = $oFooter->createTextRun(); $this->assertCount(1, $oFooter->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\TextRun', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $element); } /** @@ -88,7 +88,7 @@ class FooterTest extends \PHPUnit_Framework_TestCase $element = $oFooter->addTable(); $this->assertCount(1, $oFooter->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Table', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Table', $element); } /** @@ -102,7 +102,7 @@ class FooterTest extends \PHPUnit_Framework_TestCase $element2 = $oFooter->addMemoryImage($src); // @deprecated $this->assertCount(2, $oFooter->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element1); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element1); } /** @@ -116,7 +116,7 @@ class FooterTest extends \PHPUnit_Framework_TestCase ); $this->assertCount(1, $oFooter->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); } /** @@ -128,7 +128,7 @@ class FooterTest extends \PHPUnit_Framework_TestCase $element = $oFooter->addPreserveText('text'); $this->assertCount(1, $oFooter->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Footer\\PreserveText', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\PreserveText', $element); } /** @@ -140,7 +140,7 @@ class FooterTest extends \PHPUnit_Framework_TestCase $element = $oFooter->addPreserveText(utf8_decode('ééé')); $this->assertCount(1, $oFooter->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Footer\\PreserveText', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\PreserveText', $element); $this->assertEquals($element->getText(), array('ééé')); } diff --git a/tests/PhpWord/Tests/Section/HeaderTest.php b/tests/PhpWord/Tests/Container/HeaderTest.php similarity index 85% rename from tests/PhpWord/Tests/Section/HeaderTest.php rename to tests/PhpWord/Tests/Container/HeaderTest.php index edc5d2c6..df1435de 100644 --- a/tests/PhpWord/Tests/Section/HeaderTest.php +++ b/tests/PhpWord/Tests/Container/HeaderTest.php @@ -7,12 +7,12 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section; +namespace PhpOffice\PhpWord\Tests\Container; -use PhpOffice\PhpWord\Section\Header; +use PhpOffice\PhpWord\Container\Header; /** - * Test class for PhpOffice\PhpWord\Section\Header + * Test class for PhpOffice\PhpWord\Container\Header * * @runTestsInSeparateProcesses */ @@ -26,7 +26,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $iVal = rand(1, 1000); $oHeader = new Header($iVal); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Header', $oHeader); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Container\\Header', $oHeader); $this->assertEquals($oHeader->getHeaderCount(), $iVal); $this->assertEquals($oHeader->getType(), Header::AUTO); } @@ -39,7 +39,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $oHeader = new Header(1); $element = $oHeader->addText('text'); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element); $this->assertCount(1, $oHeader->getElements()); $this->assertEquals($element->getText(), 'text'); } @@ -52,7 +52,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $oHeader = new Header(1); $element = $oHeader->addText(utf8_decode('ééé')); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element); $this->assertCount(1, $oHeader->getElements()); $this->assertEquals($element->getText(), 'ééé'); } @@ -85,7 +85,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase { $oHeader = new Header(1); $element = $oHeader->createTextRun(); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\TextRun', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $element); $this->assertCount(1, $oHeader->getElements()); } @@ -96,7 +96,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase { $oHeader = new Header(1); $element = $oHeader->addTable(); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Table', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Table', $element); $this->assertCount(1, $oHeader->getElements()); } @@ -111,7 +111,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $element2 = $oHeader->addMemoryImage($src); // @deprecated $this->assertCount(2, $oHeader->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element1); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element1); } /** @@ -125,7 +125,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase ); $this->assertCount(1, $oHeader->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); } /** @@ -137,7 +137,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $element = $oHeader->addPreserveText('text'); $this->assertCount(1, $oHeader->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Footer\\PreserveText', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\PreserveText', $element); } /** @@ -149,7 +149,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $element = $oHeader->addPreserveText(utf8_decode('ééé')); $this->assertCount(1, $oHeader->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Footer\\PreserveText', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\PreserveText', $element); $this->assertEquals($element->getText(), array('ééé')); } @@ -163,7 +163,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $element = $oHeader->addWatermark($src); $this->assertCount(1, $oHeader->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); } /** diff --git a/tests/PhpWord/Tests/SectionTest.php b/tests/PhpWord/Tests/Container/SectionTest.php similarity index 86% rename from tests/PhpWord/Tests/SectionTest.php rename to tests/PhpWord/Tests/Container/SectionTest.php index 48b5bb73..a61498ea 100644 --- a/tests/PhpWord/Tests/SectionTest.php +++ b/tests/PhpWord/Tests/Container/SectionTest.php @@ -7,13 +7,13 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests; +namespace PhpOffice\PhpWord\Tests\Container; -use PhpOffice\PhpWord\Section; +use PhpOffice\PhpWord\Container\Section; use PhpOffice\PhpWord\Style; /** - * Test class for PhpOffice\PhpWord\Section + * Test class for PhpOffice\PhpWord\Container\Section * * @runTestsInSeparateProcesses */ @@ -71,8 +71,8 @@ class SectionTest extends \PHPUnit_Framework_TestCase */ public function testAddElements() { - $objectSource = __DIR__ . "/_files/documents/reader.docx"; - $imageSource = __DIR__ . "/_files/images/PhpWord.png"; + $objectSource = __DIR__ . "/../_files/documents/reader.docx"; + $imageSource = __DIR__ . "/../_files/images/PhpWord.png"; $imageUrl = 'http://php.net//images/logos/php-med-trans-light.gif'; $section = new Section(0); @@ -97,7 +97,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase 'Title', 'TextRun', 'Footnote', 'CheckBox'); $i = 0; foreach ($elementTypes as $elementType) { - $this->assertInstanceOf("PhpOffice\\PhpWord\\Section\\{$elementType}", $elementCollection[$i]); + $this->assertInstanceOf("PhpOffice\\PhpWord\\Element\\{$elementType}", $elementCollection[$i]); $i++; } $this->assertInstanceOf("PhpOffice\\PhpWord\\TOC", $elementCollection[$i]); @@ -106,7 +106,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase /** * Test add object exception * - * @expectedException \PhpOffice\PhpWord\Exceptions\InvalidObjectException + * @expectedException \PhpOffice\PhpWord\Exception\InvalidObjectException */ public function testAddObjectException() { @@ -125,7 +125,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase $section->addTitle('Test', 1); $elementCollection = $section->getElements(); - $this->assertInstanceOf("PhpOffice\\PhpWord\\Section\\Title", $elementCollection[0]); + $this->assertInstanceOf("PhpOffice\\PhpWord\\Element\\Title", $elementCollection[0]); } /** @@ -138,7 +138,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase foreach ($elements as $element) { $method = "create{$element}"; - $this->assertInstanceOf("PhpOffice\\PhpWord\\Section\\{$element}", $object->$method()); + $this->assertInstanceOf("PhpOffice\\PhpWord\\Container\\{$element}", $object->$method()); } $this->assertFalse($object->hasDifferentFirstPage()); } diff --git a/tests/PhpWord/Tests/Section/SettingsTest.php b/tests/PhpWord/Tests/Container/SettingsTest.php similarity index 95% rename from tests/PhpWord/Tests/Section/SettingsTest.php rename to tests/PhpWord/Tests/Container/SettingsTest.php index 8fb62f1c..3d5ae43a 100644 --- a/tests/PhpWord/Tests/Section/SettingsTest.php +++ b/tests/PhpWord/Tests/Container/SettingsTest.php @@ -7,14 +7,14 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section; +namespace PhpOffice\PhpWord\Tests\Container; -use PhpOffice\PhpWord\Section\Settings; +use PhpOffice\PhpWord\Container\Settings; /** - * Test class for PhpOffice\PhpWord\Section\Settings + * Test class for PhpOffice\PhpWord\Container\Settings * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Settings + * @coversDefaultClass \PhpOffice\PhpWord\Container\Settings * @runTestsInSeparateProcesses */ class SettingsTest extends \PHPUnit_Framework_TestCase @@ -255,10 +255,10 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertEquals(720, $oSettings->getColsSpace()); $iVal = rand(1, 1000); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Settings', $oSettings->setColsSpace($iVal)); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Container\\Settings', $oSettings->setColsSpace($iVal)); $this->assertEquals($iVal, $oSettings->getColsSpace()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Settings', $oSettings->setColsSpace()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Container\\Settings', $oSettings->setColsSpace()); $this->assertEquals(720, $oSettings->getColsSpace()); } diff --git a/tests/PhpWord/Tests/Section/CheckBoxTest.php b/tests/PhpWord/Tests/Element/CheckBoxTest.php similarity index 91% rename from tests/PhpWord/Tests/Section/CheckBoxTest.php rename to tests/PhpWord/Tests/Element/CheckBoxTest.php index d07a0b69..e4611618 100644 --- a/tests/PhpWord/Tests/Section/CheckBoxTest.php +++ b/tests/PhpWord/Tests/Element/CheckBoxTest.php @@ -7,13 +7,13 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section; +namespace PhpOffice\PhpWord\Tests\Element; -use PhpOffice\PhpWord\Section\CheckBox; +use PhpOffice\PhpWord\Element\CheckBox; use PhpOffice\PhpWord\Style\Font; /** - * Test class for PhpOffice\PhpWord\Section\CheckBox + * Test class for PhpOffice\PhpWord\Element\CheckBox * * @runTestsInSeparateProcesses */ @@ -26,7 +26,7 @@ class CheckBoxTest extends \PHPUnit_Framework_TestCase { $oCheckBox = new CheckBox(); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\CheckBox', $oCheckBox); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\CheckBox', $oCheckBox); $this->assertEquals(null, $oCheckBox->getText()); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $oCheckBox->getFontStyle()); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oCheckBox->getParagraphStyle()); diff --git a/tests/PhpWord/Tests/Section/FootnoteTest.php b/tests/PhpWord/Tests/Element/FootnoteTest.php similarity index 87% rename from tests/PhpWord/Tests/Section/FootnoteTest.php rename to tests/PhpWord/Tests/Element/FootnoteTest.php index fc537ab8..21a18a4f 100644 --- a/tests/PhpWord/Tests/Section/FootnoteTest.php +++ b/tests/PhpWord/Tests/Element/FootnoteTest.php @@ -7,12 +7,12 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section; +namespace PhpOffice\PhpWord\Tests\Element; -use PhpOffice\PhpWord\Section\Footnote; +use PhpOffice\PhpWord\Element\Footnote; /** - * Test class for PhpOffice\PhpWord\Section\Footnote + * Test class for PhpOffice\PhpWord\Element\Footnote * * @runTestsInSeparateProcesses */ @@ -25,7 +25,7 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase { $oFootnote = new Footnote(); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Footnote', $oFootnote); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Footnote', $oFootnote); $this->assertCount(0, $oFootnote->getElements()); $this->assertEquals($oFootnote->getParagraphStyle(), null); } @@ -62,7 +62,7 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase $element = $oFootnote->addText('text'); $this->assertCount(1, $oFootnote->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element); } /** @@ -85,7 +85,7 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase $element = $oFootnote->addLink('http://www.google.fr'); $this->assertCount(1, $oFootnote->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Link', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element); } /** diff --git a/tests/PhpWord/Tests/Section/ImageTest.php b/tests/PhpWord/Tests/Element/ImageTest.php similarity index 89% rename from tests/PhpWord/Tests/Section/ImageTest.php rename to tests/PhpWord/Tests/Element/ImageTest.php index bd4bf394..b0c3d88c 100644 --- a/tests/PhpWord/Tests/Section/ImageTest.php +++ b/tests/PhpWord/Tests/Element/ImageTest.php @@ -7,12 +7,12 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section; +namespace PhpOffice\PhpWord\Tests\Element; -use PhpOffice\PhpWord\Section\Image; +use PhpOffice\PhpWord\Element\Image; /** - * Test class for PhpOffice\PhpWord\Section\Image + * Test class for PhpOffice\PhpWord\Element\Image * * @runTestsInSeparateProcesses */ @@ -26,7 +26,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase $src = __DIR__ . "/../_files/images/firefox.png"; $oImage = new Image($src); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $oImage); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage); $this->assertEquals($oImage->getSource(), $src); $this->assertEquals($oImage->getMediaId(), md5($src)); $this->assertEquals($oImage->getIsWatermark(), false); @@ -64,7 +64,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase /** * Image not found * - * @expectedException \PhpOffice\PhpWord\Exceptions\InvalidImageException + * @expectedException \PhpOffice\PhpWord\Exception\InvalidImageException */ public function testImageNotFound() { @@ -74,7 +74,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase /** * Invalid image types * - * @expectedException \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException + * @expectedException \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException */ public function testInvalidImageTypes() { @@ -123,7 +123,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase $src = __DIR__ . "/../_files/images/firefox.png"; $oImage = new Image($src); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $oImage); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage); $this->assertEquals($oImage->getSource(), $src); $this->assertEquals($oImage->getMediaId(), md5($src)); $this->assertEquals($oImage->getImageCreateFunction(), 'imagecreatefrompng'); @@ -140,7 +140,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase $src = __DIR__ . "/../_files/images/mario.gif"; $oImage = new Image($src); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $oImage); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage); $this->assertEquals($oImage->getSource(), $src); $this->assertEquals($oImage->getMediaId(), md5($src)); $this->assertEquals($oImage->getImageCreateFunction(), 'imagecreatefromgif'); @@ -157,7 +157,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase $src = __DIR__ . "/../_files/images/earth.jpg"; $oImage = new Image($src); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $oImage); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage); $this->assertEquals($oImage->getSource(), $src); $this->assertEquals($oImage->getMediaId(), md5($src)); $this->assertEquals($oImage->getImageCreateFunction(), 'imagecreatefromjpeg'); @@ -173,7 +173,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase { $oImage = new Image(__DIR__ . "/../_files/images/duke_nukem.bmp"); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $oImage); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage); $this->assertEquals($oImage->getImageCreateFunction(), null); $this->assertEquals($oImage->getImageFunction(), null); $this->assertEquals($oImage->getImageExtension(), 'bmp'); @@ -187,7 +187,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase { $oImage = new Image(__DIR__ . "/../_files/images/angela_merkel.tif"); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $oImage); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage); $this->assertEquals($oImage->getImageCreateFunction(), null); $this->assertEquals($oImage->getImageFunction(), null); $this->assertEquals($oImage->getImageType(), 'image/tiff'); diff --git a/tests/PhpWord/Tests/Section/LinkTest.php b/tests/PhpWord/Tests/Element/LinkTest.php similarity index 87% rename from tests/PhpWord/Tests/Section/LinkTest.php rename to tests/PhpWord/Tests/Element/LinkTest.php index ae1a3e09..9f89b21c 100644 --- a/tests/PhpWord/Tests/Section/LinkTest.php +++ b/tests/PhpWord/Tests/Element/LinkTest.php @@ -7,15 +7,15 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section; +namespace PhpOffice\PhpWord\Tests\Element; -use PhpOffice\PhpWord\Section\Link; +use PhpOffice\PhpWord\Element\Link; use PhpOffice\PhpWord\Style\Font; /** - * Test class for PhpOffice\PhpWord\Section\Link + * Test class for PhpOffice\PhpWord\Element\Link * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Link + * @coversDefaultClass \PhpOffice\PhpWord\Element\Link * @runTestsInSeparateProcesses */ class LinkTest extends \PHPUnit_Framework_TestCase @@ -27,7 +27,7 @@ class LinkTest extends \PHPUnit_Framework_TestCase { $oLink = new Link('http://www.google.com'); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Link', $oLink); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $oLink); $this->assertEquals($oLink->getLinkSrc(), 'http://www.google.com'); $this->assertEquals($oLink->getLinkName(), null); $this->assertEquals($oLink->getFontStyle(), null); @@ -46,7 +46,7 @@ class LinkTest extends \PHPUnit_Framework_TestCase array('marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600) ); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Link', $oLink); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $oLink); $this->assertEquals($oLink->getLinkSrc(), 'http://www.google.com'); $this->assertEquals($oLink->getLinkName(), 'Search Engine'); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $oLink->getFontStyle()); diff --git a/tests/PhpWord/Tests/Section/ListItemTest.php b/tests/PhpWord/Tests/Element/ListItemTest.php similarity index 81% rename from tests/PhpWord/Tests/Section/ListItemTest.php rename to tests/PhpWord/Tests/Element/ListItemTest.php index 1964cdac..6526f571 100644 --- a/tests/PhpWord/Tests/Section/ListItemTest.php +++ b/tests/PhpWord/Tests/Element/ListItemTest.php @@ -7,14 +7,14 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section; +namespace PhpOffice\PhpWord\Tests\Element; -use PhpOffice\PhpWord\Section\ListItem; +use PhpOffice\PhpWord\Element\ListItem; /** - * Test class for PhpOffice\PhpWord\Section\ListItem + * Test class for PhpOffice\PhpWord\Element\ListItem * - * @coversDefaultClass \PhpOffice\PhpWord\Section\ListItem + * @coversDefaultClass \PhpOffice\PhpWord\Element\ListItem * @runTestsInSeparateProcesses */ class ListItemTest extends \PHPUnit_Framework_TestCase @@ -26,7 +26,7 @@ class ListItemTest extends \PHPUnit_Framework_TestCase { $oListItem = new ListItem('text'); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $oListItem->getTextObject()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $oListItem->getTextObject()); } /** diff --git a/tests/PhpWord/Tests/Section/ObjectTest.php b/tests/PhpWord/Tests/Element/ObjectTest.php similarity index 86% rename from tests/PhpWord/Tests/Section/ObjectTest.php rename to tests/PhpWord/Tests/Element/ObjectTest.php index d6094de1..22b4787f 100644 --- a/tests/PhpWord/Tests/Section/ObjectTest.php +++ b/tests/PhpWord/Tests/Element/ObjectTest.php @@ -7,14 +7,14 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section; +namespace PhpOffice\PhpWord\Tests\Element; -use PhpOffice\PhpWord\Section\Object; +use PhpOffice\PhpWord\Element\Object; /** - * Test class for PhpOffice\PhpWord\Section\Object + * Test class for PhpOffice\PhpWord\Element\Object * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Object + * @coversDefaultClass \PhpOffice\PhpWord\Element\Object * @runTestsInSeparateProcesses */ class ObjectTest extends \PHPUnit_Framework_TestCase @@ -27,7 +27,7 @@ class ObjectTest extends \PHPUnit_Framework_TestCase $src = __DIR__ . "/../_files/documents/sheet.xls"; $oObject = new Object($src); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Object', $oObject); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Object', $oObject); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oObject->getStyle()); $this->assertEquals($oObject->getSource(), $src); } @@ -40,7 +40,7 @@ class ObjectTest extends \PHPUnit_Framework_TestCase $src = __DIR__ . "/../_files/xsl/passthrough.xsl"; $oObject = new Object($src); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Object', $oObject); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Object', $oObject); $this->assertEquals($oObject->getSource(), null); $this->assertEquals($oObject->getStyle(), null); } @@ -53,7 +53,7 @@ class ObjectTest extends \PHPUnit_Framework_TestCase $src = __DIR__ . "/../_files/documents/sheet.xls"; $oObject = new Object($src, array('width' => '230px')); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Object', $oObject); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Object', $oObject); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oObject->getStyle()); $this->assertEquals($oObject->getSource(), $src); } diff --git a/tests/PhpWord/Tests/Section/PageBreakTest.php b/tests/PhpWord/Tests/Element/PageBreakTest.php similarity index 65% rename from tests/PhpWord/Tests/Section/PageBreakTest.php rename to tests/PhpWord/Tests/Element/PageBreakTest.php index bf8b03a3..3dbaad87 100644 --- a/tests/PhpWord/Tests/Section/PageBreakTest.php +++ b/tests/PhpWord/Tests/Element/PageBreakTest.php @@ -7,14 +7,14 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section; +namespace PhpOffice\PhpWord\Tests\Element; -use PhpOffice\PhpWord\Section\PageBreak; +use PhpOffice\PhpWord\Element\PageBreak; /** - * Test class for PhpOffice\PhpWord\Section\PageBreak + * Test class for PhpOffice\PhpWord\Element\PageBreak * - * @coversDefaultClass \PhpOffice\PhpWord\Section\PageBreak + * @coversDefaultClass \PhpOffice\PhpWord\Element\PageBreak * @runTestsInSeparateProcesses */ class PageBreakTest extends \PHPUnit_Framework_TestCase @@ -27,6 +27,6 @@ class PageBreakTest extends \PHPUnit_Framework_TestCase // Section Settings $oPageBreak = new PageBreak(); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\PageBreak', $oPageBreak); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\PageBreak', $oPageBreak); } } diff --git a/tests/PhpWord/Tests/Section/Footer/PreserveTextTest.php b/tests/PhpWord/Tests/Element/PreserveTextTest.php similarity index 85% rename from tests/PhpWord/Tests/Section/Footer/PreserveTextTest.php rename to tests/PhpWord/Tests/Element/PreserveTextTest.php index 82ded881..16d4361d 100644 --- a/tests/PhpWord/Tests/Section/Footer/PreserveTextTest.php +++ b/tests/PhpWord/Tests/Element/PreserveTextTest.php @@ -7,12 +7,12 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section\Footer; +namespace PhpOffice\PhpWord\Tests\Element; -use PhpOffice\PhpWord\Section\Footer\PreserveText; +use PhpOffice\PhpWord\Element\PreserveText; /** - * Test class for PhpOffice\PhpWord\Section\Footer\PreserveText + * Test class for PhpOffice\PhpWord\Element\PreserveText * * @runTestsInSeparateProcesses */ @@ -25,7 +25,7 @@ class PreserveTextTest extends \PHPUnit_Framework_TestCase { $oPreserveText = new PreserveText(); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Footer\\PreserveText', $oPreserveText); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\PreserveText', $oPreserveText); $this->assertEquals($oPreserveText->getText(), null); $this->assertEquals($oPreserveText->getFontStyle(), null); $this->assertEquals($oPreserveText->getParagraphStyle(), null); diff --git a/tests/PhpWord/Tests/Section/Table/CellTest.php b/tests/PhpWord/Tests/Element/Table/CellTest.php similarity index 82% rename from tests/PhpWord/Tests/Section/Table/CellTest.php rename to tests/PhpWord/Tests/Element/Table/CellTest.php index 6f38d986..661120d4 100644 --- a/tests/PhpWord/Tests/Section/Table/CellTest.php +++ b/tests/PhpWord/Tests/Element/Table/CellTest.php @@ -7,12 +7,12 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section\Table; +namespace PhpOffice\PhpWord\Tests\Element\Table; -use PhpOffice\PhpWord\Section\Table\Cell; +use PhpOffice\PhpWord\Element\Table\Cell; /** - * Test class for PhpOffice\PhpWord\Section\Table\Cell + * Test class for PhpOffice\PhpWord\Element\Table\Cell * * @runTestsInSeparateProcesses */ @@ -26,7 +26,7 @@ class CellTest extends \PHPUnit_Framework_TestCase $iVal = rand(1, 1000); $oCell = new Cell('section', $iVal); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Table\\Cell', $oCell); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Table\\Cell', $oCell); $this->assertEquals($oCell->getWidth(), null); } @@ -62,7 +62,7 @@ class CellTest extends \PHPUnit_Framework_TestCase $element = $oCell->addText('text'); $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element); } /** @@ -74,7 +74,7 @@ class CellTest extends \PHPUnit_Framework_TestCase $element = $oCell->addText(utf8_decode('ééé')); $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element); $this->assertEquals($element->getText(), 'ééé'); } @@ -87,12 +87,12 @@ class CellTest extends \PHPUnit_Framework_TestCase $element = $oCell->addLink(utf8_decode('ééé'), utf8_decode('ééé')); $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Link', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element); } /** * Add link exception - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedException \PhpOffice\PhpWord\Exception\Exception */ public function testAddLinkException() { @@ -120,7 +120,7 @@ class CellTest extends \PHPUnit_Framework_TestCase $element = $oCell->addListItem('text'); $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\ListItem', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItem', $element); $this->assertEquals($element->getTextObject()->getText(), 'text'); } @@ -133,7 +133,7 @@ class CellTest extends \PHPUnit_Framework_TestCase $element = $oCell->addListItem(utf8_decode('ééé')); $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\ListItem', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItem', $element); $this->assertEquals($element->getTextObject()->getText(), 'ééé'); } @@ -148,7 +148,7 @@ class CellTest extends \PHPUnit_Framework_TestCase $element2 = $oCell->addMemoryImage($src); // @deprecated $this->assertCount(2, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element1); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element1); } /** @@ -161,7 +161,7 @@ class CellTest extends \PHPUnit_Framework_TestCase $element = $oCell->addImage($src); $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); } /** @@ -174,7 +174,7 @@ class CellTest extends \PHPUnit_Framework_TestCase $element = $oCell->addImage($src); $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); } /** @@ -188,7 +188,7 @@ class CellTest extends \PHPUnit_Framework_TestCase ); $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); } /** @@ -202,7 +202,7 @@ class CellTest extends \PHPUnit_Framework_TestCase ); $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); } /** @@ -216,7 +216,7 @@ class CellTest extends \PHPUnit_Framework_TestCase ); $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); } /** @@ -229,13 +229,13 @@ class CellTest extends \PHPUnit_Framework_TestCase $element = $oCell->addObject($src); $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Object', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Object', $element); } /** * Test add object exception * - * @expectedException \PhpOffice\PhpWord\Exceptions\InvalidObjectException + * @expectedException \PhpOffice\PhpWord\Exception\InvalidObjectException */ public function testAddObjectException() { @@ -253,7 +253,7 @@ class CellTest extends \PHPUnit_Framework_TestCase $element = $oCell->addPreserveText('text'); $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Footer\\PreserveText', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\PreserveText', $element); } /** @@ -265,14 +265,14 @@ class CellTest extends \PHPUnit_Framework_TestCase $element = $oCell->addPreserveText(utf8_decode('ééé')); $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Footer\\PreserveText', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\PreserveText', $element); $this->assertEquals($element->getText(), array('ééé')); } /** * Add preserve text exception * - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedException \PhpOffice\PhpWord\Exception\Exception */ public function testAddPreserveTextException() { @@ -289,7 +289,7 @@ class CellTest extends \PHPUnit_Framework_TestCase $element = $oCell->createTextRun(); $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\TextRun', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $element); } /** @@ -301,7 +301,7 @@ class CellTest extends \PHPUnit_Framework_TestCase $element = $oCell->addCheckBox(utf8_decode('ééé'), utf8_decode('ééé')); $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\CheckBox', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\CheckBox', $element); } /** diff --git a/tests/PhpWord/Tests/Section/Table/RowTest.php b/tests/PhpWord/Tests/Element/Table/RowTest.php similarity index 81% rename from tests/PhpWord/Tests/Section/Table/RowTest.php rename to tests/PhpWord/Tests/Element/Table/RowTest.php index 10d53915..f64c0cb4 100644 --- a/tests/PhpWord/Tests/Section/Table/RowTest.php +++ b/tests/PhpWord/Tests/Element/Table/RowTest.php @@ -7,14 +7,14 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section\Table; +namespace PhpOffice\PhpWord\Tests\Element\Table; -use PhpOffice\PhpWord\Section\Table\Row; +use PhpOffice\PhpWord\Element\Table\Row; /** - * Test class for PhpOffice\PhpWord\Section\Table\Row + * Test class for PhpOffice\PhpWord\Element\Table\Row * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Table\Row + * @coversDefaultClass \PhpOffice\PhpWord\Element\Table\Row * @runTestsInSeparateProcesses */ class RowTest extends \PHPUnit_Framework_TestCase @@ -27,7 +27,7 @@ class RowTest extends \PHPUnit_Framework_TestCase $iVal = rand(1, 1000); $oRow = new Row('section', $iVal); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Table\\Row', $oRow); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Table\\Row', $oRow); $this->assertEquals($oRow->getHeight(), null); $this->assertInternalType('array', $oRow->getCells()); $this->assertCount(0, $oRow->getCells()); @@ -60,7 +60,7 @@ class RowTest extends \PHPUnit_Framework_TestCase $oRow = new Row('section', 1); $element = $oRow->addCell(); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Table\\Cell', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Table\\Cell', $element); $this->assertCount(1, $oRow->getCells()); } } diff --git a/tests/PhpWord/Tests/Section/TableTest.php b/tests/PhpWord/Tests/Element/TableTest.php similarity index 83% rename from tests/PhpWord/Tests/Section/TableTest.php rename to tests/PhpWord/Tests/Element/TableTest.php index 9808485d..1b337fd6 100644 --- a/tests/PhpWord/Tests/Section/TableTest.php +++ b/tests/PhpWord/Tests/Element/TableTest.php @@ -7,14 +7,14 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section; +namespace PhpOffice\PhpWord\Tests\Element; -use PhpOffice\PhpWord\Section\Table; +use PhpOffice\PhpWord\Element\Table; /** - * Test class for PhpOffice\PhpWord\Section\Table + * Test class for PhpOffice\PhpWord\Element\Table * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Table + * @coversDefaultClass \PhpOffice\PhpWord\Element\Table * @runTestsInSeparateProcesses */ class TableTest extends \PHPUnit_Framework_TestCase @@ -26,7 +26,7 @@ class TableTest extends \PHPUnit_Framework_TestCase { $oTable = new Table('section', 1); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Table', $oTable); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Table', $oTable); $this->assertEquals($oTable->getStyle(), null); $this->assertEquals($oTable->getWidth(), null); $this->assertEquals($oTable->getRows(), array()); @@ -75,7 +75,7 @@ class TableTest extends \PHPUnit_Framework_TestCase { $oTable = new Table('section', 1); $element = $oTable->addRow(); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Table\\Row', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Table\\Row', $element); $this->assertCount(1, $oTable->getRows()); } @@ -87,6 +87,6 @@ class TableTest extends \PHPUnit_Framework_TestCase $oTable = new Table('section', 1); $oTable->addRow(); $element = $oTable->addCell(); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Table\\Cell', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Table\\Cell', $element); } } diff --git a/tests/PhpWord/Tests/Section/TextBreakTest.php b/tests/PhpWord/Tests/Element/TextBreakTest.php similarity index 89% rename from tests/PhpWord/Tests/Section/TextBreakTest.php rename to tests/PhpWord/Tests/Element/TextBreakTest.php index cf7a58c4..c228215f 100644 --- a/tests/PhpWord/Tests/Section/TextBreakTest.php +++ b/tests/PhpWord/Tests/Element/TextBreakTest.php @@ -7,16 +7,16 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section; +namespace PhpOffice\PhpWord\Tests\Element; -use PhpOffice\PhpWord\Section\TextBreak; +use PhpOffice\PhpWord\Element\TextBreak; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; /** - * Test class for PhpOffice\PhpWord\Section\TextBreak + * Test class for PhpOffice\PhpWord\Element\TextBreak * - * @coversDefaultClass \PhpOffice\PhpWord\Section\TextBreak + * @coversDefaultClass \PhpOffice\PhpWord\Element\TextBreak * @runTestsInSeparateProcesses */ class TextBreakTest extends \PHPUnit_Framework_TestCase diff --git a/tests/PhpWord/Tests/Section/TextRunTest.php b/tests/PhpWord/Tests/Element/TextRunTest.php similarity index 82% rename from tests/PhpWord/Tests/Section/TextRunTest.php rename to tests/PhpWord/Tests/Element/TextRunTest.php index 32b6d4dc..e71ce108 100644 --- a/tests/PhpWord/Tests/Section/TextRunTest.php +++ b/tests/PhpWord/Tests/Element/TextRunTest.php @@ -7,12 +7,12 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section; +namespace PhpOffice\PhpWord\Tests\Element; -use PhpOffice\PhpWord\Section\TextRun; +use PhpOffice\PhpWord\Element\TextRun; /** - * Test class for PhpOffice\PhpWord\Section\TextRun + * Test class for PhpOffice\PhpWord\Element\TextRun * * @runTestsInSeparateProcesses */ @@ -25,7 +25,7 @@ class TextRunTest extends \PHPUnit_Framework_TestCase { $oTextRun = new TextRun(); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\TextRun', $oTextRun); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun); $this->assertCount(0, $oTextRun->getElements()); $this->assertEquals($oTextRun->getParagraphStyle(), null); } @@ -37,7 +37,7 @@ class TextRunTest extends \PHPUnit_Framework_TestCase { $oTextRun = new TextRun('pStyle'); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\TextRun', $oTextRun); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun); $this->assertCount(0, $oTextRun->getElements()); $this->assertEquals($oTextRun->getParagraphStyle(), 'pStyle'); } @@ -49,7 +49,7 @@ class TextRunTest extends \PHPUnit_Framework_TestCase { $oTextRun = new TextRun(array('spacing' => 100)); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\TextRun', $oTextRun); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun); $this->assertCount(0, $oTextRun->getElements()); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle()); } @@ -62,7 +62,7 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $oTextRun = new TextRun(); $element = $oTextRun->addText('text'); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element); $this->assertCount(1, $oTextRun->getElements()); $this->assertEquals($element->getText(), 'text'); } @@ -75,7 +75,7 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $oTextRun = new TextRun(); $element = $oTextRun->addText(utf8_decode('ééé')); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element); $this->assertCount(1, $oTextRun->getElements()); $this->assertEquals($element->getText(), 'ééé'); } @@ -88,7 +88,7 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $oTextRun = new TextRun(); $element = $oTextRun->addLink('http://www.google.fr'); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Link', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element); $this->assertCount(1, $oTextRun->getElements()); $this->assertEquals($element->getLinkSrc(), 'http://www.google.fr'); } @@ -101,7 +101,7 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $oTextRun = new TextRun(); $element = $oTextRun->addLink('http://www.google.fr', utf8_decode('ééé')); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Link', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element); $this->assertCount(1, $oTextRun->getElements()); $this->assertEquals($element->getLinkSrc(), 'http://www.google.fr'); $this->assertEquals($element->getLinkName(), 'ééé'); @@ -128,7 +128,7 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $oTextRun = new TextRun(); $element = $oTextRun->addImage($src); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); $this->assertCount(1, $oTextRun->getElements()); } @@ -140,7 +140,7 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $oTextRun = new TextRun(); $element = $oTextRun->createFootnote(); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Footnote', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Footnote', $element); $this->assertCount(1, $oTextRun->getElements()); } } diff --git a/tests/PhpWord/Tests/Section/TextTest.php b/tests/PhpWord/Tests/Element/TextTest.php similarity index 90% rename from tests/PhpWord/Tests/Section/TextTest.php rename to tests/PhpWord/Tests/Element/TextTest.php index 5811c735..953dfc8f 100644 --- a/tests/PhpWord/Tests/Section/TextTest.php +++ b/tests/PhpWord/Tests/Element/TextTest.php @@ -7,13 +7,13 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section; +namespace PhpOffice\PhpWord\Tests\Element; -use PhpOffice\PhpWord\Section\Text; +use PhpOffice\PhpWord\Element\Text; use PhpOffice\PhpWord\Style\Font; /** - * Test class for PhpOffice\PhpWord\Section\Text + * Test class for PhpOffice\PhpWord\Element\Text * * @runTestsInSeparateProcesses */ @@ -26,7 +26,7 @@ class TextTest extends \PHPUnit_Framework_TestCase { $oText = new Text(); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $oText); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $oText); $this->assertEquals(null, $oText->getText()); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $oText->getFontStyle()); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oText->getParagraphStyle()); diff --git a/tests/PhpWord/Tests/Section/TitleTest.php b/tests/PhpWord/Tests/Element/TitleTest.php similarity index 84% rename from tests/PhpWord/Tests/Section/TitleTest.php rename to tests/PhpWord/Tests/Element/TitleTest.php index a63d184c..6c472b0d 100644 --- a/tests/PhpWord/Tests/Section/TitleTest.php +++ b/tests/PhpWord/Tests/Element/TitleTest.php @@ -7,14 +7,14 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Section; +namespace PhpOffice\PhpWord\Tests\Element; -use PhpOffice\PhpWord\Section\Title; +use PhpOffice\PhpWord\Element\Title; /** - * Test class for PhpOffice\PhpWord\Section\Title + * Test class for PhpOffice\PhpWord\Element\Title * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Title + * @coversDefaultClass \PhpOffice\PhpWord\Element\Title * @runTestsInSeparateProcesses */ class TitleTest extends \PHPUnit_Framework_TestCase @@ -26,7 +26,7 @@ class TitleTest extends \PHPUnit_Framework_TestCase { $oTitle = new Title('text'); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Title', $oTitle); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Title', $oTitle); $this->assertEquals($oTitle->getText(), 'text'); } diff --git a/tests/PhpWord/Tests/Exceptions/ExceptionTest.php b/tests/PhpWord/Tests/Exception/ExceptionTest.php similarity index 55% rename from tests/PhpWord/Tests/Exceptions/ExceptionTest.php rename to tests/PhpWord/Tests/Exception/ExceptionTest.php index 81732c97..04eb03e0 100644 --- a/tests/PhpWord/Tests/Exceptions/ExceptionTest.php +++ b/tests/PhpWord/Tests/Exception/ExceptionTest.php @@ -7,14 +7,14 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Exceptions; +namespace PhpOffice\PhpWord\Tests\Exception; -use PhpOffice\PhpWord\Exceptions\Exception; +use PhpOffice\PhpWord\Exception\Exception; /** - * Test class for PhpOffice\PhpWord\Exceptions\Exception + * Test class for PhpOffice\PhpWord\Exception\Exception * - * @coversDefaultClass \PhpOffice\PhpWord\Exceptions\Exception + * @coversDefaultClass \PhpOffice\PhpWord\Exception\Exception * @runTestsInSeparateProcesses */ class ExceptionTest extends \PHPUnit_Framework_TestCase @@ -22,8 +22,8 @@ class ExceptionTest extends \PHPUnit_Framework_TestCase /** * Throw new exception * - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception - * @covers \PhpOffice\PhpWord\Exceptions\Exception + * @expectedException \PhpOffice\PhpWord\Exception\Exception + * @covers \PhpOffice\PhpWord\Exception\Exception */ public function testThrowException() { diff --git a/tests/PhpWord/Tests/Exceptions/InvalidImageExceptionTest.php b/tests/PhpWord/Tests/Exception/InvalidImageExceptionTest.php similarity index 52% rename from tests/PhpWord/Tests/Exceptions/InvalidImageExceptionTest.php rename to tests/PhpWord/Tests/Exception/InvalidImageExceptionTest.php index 7db70993..c0dfedda 100644 --- a/tests/PhpWord/Tests/Exceptions/InvalidImageExceptionTest.php +++ b/tests/PhpWord/Tests/Exception/InvalidImageExceptionTest.php @@ -7,14 +7,14 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Exceptions; +namespace PhpOffice\PhpWord\Tests\Exception; -use PhpOffice\PhpWord\Exceptions\InvalidImageException; +use PhpOffice\PhpWord\Exception\InvalidImageException; /** - * Test class for PhpOffice\PhpWord\Exceptions\InvalidImageException + * Test class for PhpOffice\PhpWord\Exception\InvalidImageException * - * @coversDefaultClass \PhpOffice\PhpWord\Exceptions\InvalidImageException + * @coversDefaultClass \PhpOffice\PhpWord\Exception\InvalidImageException * @runTestsInSeparateProcesses */ class InvalidImageExceptionTest extends \PHPUnit_Framework_TestCase @@ -22,8 +22,8 @@ class InvalidImageExceptionTest extends \PHPUnit_Framework_TestCase /** * Throw new exception * - * @expectedException \PhpOffice\PhpWord\Exceptions\InvalidImageException - * @covers \PhpOffice\PhpWord\Exceptions\InvalidImageException + * @expectedException \PhpOffice\PhpWord\Exception\InvalidImageException + * @covers \PhpOffice\PhpWord\Exception\InvalidImageException */ public function testThrowException() { diff --git a/tests/PhpWord/Tests/Exceptions/InvalidStyleExceptionTest.php b/tests/PhpWord/Tests/Exception/InvalidStyleExceptionTest.php similarity index 52% rename from tests/PhpWord/Tests/Exceptions/InvalidStyleExceptionTest.php rename to tests/PhpWord/Tests/Exception/InvalidStyleExceptionTest.php index 174e07ac..1cc29a72 100644 --- a/tests/PhpWord/Tests/Exceptions/InvalidStyleExceptionTest.php +++ b/tests/PhpWord/Tests/Exception/InvalidStyleExceptionTest.php @@ -7,14 +7,14 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Exceptions; +namespace PhpOffice\PhpWord\Tests\Exception; -use PhpOffice\PhpWord\Exceptions\InvalidStyleException; +use PhpOffice\PhpWord\Exception\InvalidStyleException; /** - * Test class for PhpOffice\PhpWord\Exceptions\InvalidStyleException + * Test class for PhpOffice\PhpWord\Exception\InvalidStyleException * - * @coversDefaultClass \PhpOffice\PhpWord\Exceptions\InvalidStyleException + * @coversDefaultClass \PhpOffice\PhpWord\Exception\InvalidStyleException * @runTestsInSeparateProcesses */ class InvalidStyleExceptionTest extends \PHPUnit_Framework_TestCase @@ -22,8 +22,8 @@ class InvalidStyleExceptionTest extends \PHPUnit_Framework_TestCase /** * Throw new exception * - * @expectedException \PhpOffice\PhpWord\Exceptions\InvalidStyleException - * @covers \PhpOffice\PhpWord\Exceptions\InvalidStyleException + * @expectedException \PhpOffice\PhpWord\Exception\InvalidStyleException + * @covers \PhpOffice\PhpWord\Exception\InvalidStyleException */ public function testThrowException() { diff --git a/tests/PhpWord/Tests/Exceptions/UnsupportedImageTypeExceptionTest.php b/tests/PhpWord/Tests/Exception/UnsupportedImageTypeExceptionTest.php similarity index 50% rename from tests/PhpWord/Tests/Exceptions/UnsupportedImageTypeExceptionTest.php rename to tests/PhpWord/Tests/Exception/UnsupportedImageTypeExceptionTest.php index 027ec3a9..a73db599 100644 --- a/tests/PhpWord/Tests/Exceptions/UnsupportedImageTypeExceptionTest.php +++ b/tests/PhpWord/Tests/Exception/UnsupportedImageTypeExceptionTest.php @@ -7,14 +7,14 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Exceptions; +namespace PhpOffice\PhpWord\Tests\Exception; -use PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException; +use PhpOffice\PhpWord\Exception\UnsupportedImageTypeException; /** - * Test class for PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeExceptionTest + * Test class for PhpOffice\PhpWord\Exception\UnsupportedImageTypeExceptionTest * - * @coversDefaultClass \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeExceptionTest + * @coversDefaultClass \PhpOffice\PhpWord\Exception\UnsupportedImageTypeExceptionTest * @runTestsInSeparateProcesses */ class UnsupportedImageTypeExceptionTest extends \PHPUnit_Framework_TestCase @@ -22,8 +22,8 @@ class UnsupportedImageTypeExceptionTest extends \PHPUnit_Framework_TestCase /** * Throw new exception * - * @expectedException \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException - * @covers \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException + * @expectedException \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException + * @covers \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException */ public function testThrowException() { diff --git a/tests/PhpWord/Tests/FootnoteTest.php b/tests/PhpWord/Tests/FootnoteTest.php index 79bb75ad..5ea7ed80 100644 --- a/tests/PhpWord/Tests/FootnoteTest.php +++ b/tests/PhpWord/Tests/FootnoteTest.php @@ -23,7 +23,7 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase */ public function testFootnote() { - $footnoteElement = new \PhpOffice\PhpWord\Section\Footnote(); + $footnoteElement = new \PhpOffice\PhpWord\Element\Footnote(); $rIdFootnote = Footnote::addFootnoteElement($footnoteElement); $rIdLink = Footnote::addFootnoteLinkElement('http://test.com'); diff --git a/tests/PhpWord/Tests/IOFactoryTest.php b/tests/PhpWord/Tests/IOFactoryTest.php index bb2a6530..405eabe0 100644 --- a/tests/PhpWord/Tests/IOFactoryTest.php +++ b/tests/PhpWord/Tests/IOFactoryTest.php @@ -33,7 +33,7 @@ class IOFactoryTest extends \PHPUnit_Framework_TestCase /** * Create non-existing writer * - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedException \PhpOffice\PhpWord\Exception\Exception */ public function testNonexistentWriterCanNotBeCreated() { @@ -54,7 +54,7 @@ class IOFactoryTest extends \PHPUnit_Framework_TestCase /** * Create non-existing reader * - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedException \PhpOffice\PhpWord\Exception\Exception */ public function testNonexistentReaderCanNotBeCreated() { diff --git a/tests/PhpWord/Tests/MediaTest.php b/tests/PhpWord/Tests/MediaTest.php index 62ce56a9..852cedb7 100644 --- a/tests/PhpWord/Tests/MediaTest.php +++ b/tests/PhpWord/Tests/MediaTest.php @@ -10,8 +10,8 @@ namespace PhpOffice\PhpWord\Tests; use PhpOffice\PhpWord\Media; -use PhpOffice\PhpWord\Section; -use PhpOffice\PhpWord\Section\Image; +use PhpOffice\PhpWord\Container\Section; +use PhpOffice\PhpWord\Element\Image; /** * Test class for PhpOffice\PhpWord\Media diff --git a/tests/PhpWord/Tests/PhpWordTest.php b/tests/PhpWord/Tests/PhpWordTest.php index 500d46c8..2cf5ae62 100644 --- a/tests/PhpWord/Tests/PhpWordTest.php +++ b/tests/PhpWord/Tests/PhpWordTest.php @@ -11,7 +11,7 @@ namespace PhpOffice\PhpWord\Tests; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\DocumentProperties; -use PhpOffice\PhpWord\Section; +use PhpOffice\PhpWord\Container\Section; use PhpOffice\PhpWord\Style; /** @@ -140,7 +140,7 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase /** * Test load template exception * - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedException \PhpOffice\PhpWord\Exception\Exception */ public function testLoadTemplateException() { diff --git a/tests/PhpWord/Tests/Reader/Word2007Test.php b/tests/PhpWord/Tests/Reader/Word2007Test.php index 5aee8144..3a572f4d 100644 --- a/tests/PhpWord/Tests/Reader/Word2007Test.php +++ b/tests/PhpWord/Tests/Reader/Word2007Test.php @@ -43,7 +43,7 @@ class Word2007Test extends \PHPUnit_Framework_TestCase /** * Can read exception * - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedException \PhpOffice\PhpWord\Exception\Exception */ public function testCanReadFailed() { diff --git a/tests/PhpWord/Tests/Style/FontTest.php b/tests/PhpWord/Tests/Style/FontTest.php index ed4d61db..47ed57f4 100644 --- a/tests/PhpWord/Tests/Style/FontTest.php +++ b/tests/PhpWord/Tests/Style/FontTest.php @@ -144,7 +144,7 @@ class FontTest extends \PHPUnit_Framework_TestCase /** * Test line height exception by using nonnumeric value * - * @expectedException \PhpOffice\PhpWord\Exceptions\InvalidStyleException + * @expectedException \PhpOffice\PhpWord\Exception\InvalidStyleException */ public function testLineHeightException() { diff --git a/tests/PhpWord/Tests/Style/ParagraphTest.php b/tests/PhpWord/Tests/Style/ParagraphTest.php index 619d5497..4c424a93 100644 --- a/tests/PhpWord/Tests/Style/ParagraphTest.php +++ b/tests/PhpWord/Tests/Style/ParagraphTest.php @@ -146,7 +146,7 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase /** * Test line height exception by using nonnumeric value * - * @expectedException \PhpOffice\PhpWord\Exceptions\InvalidStyleException + * @expectedException \PhpOffice\PhpWord\Exception\InvalidStyleException */ public function testLineHeightException() { diff --git a/tests/PhpWord/Tests/TemplateTest.php b/tests/PhpWord/Tests/TemplateTest.php index 669ae946..eb766772 100644 --- a/tests/PhpWord/Tests/TemplateTest.php +++ b/tests/PhpWord/Tests/TemplateTest.php @@ -95,7 +95,7 @@ final class TemplateTest extends \PHPUnit_Framework_TestCase * XSL stylesheet cannot be applied on failure in setting parameter value * * @covers ::applyXslStyleSheet - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedException \PhpOffice\PhpWord\Exception\Exception * @expectedExceptionMessage Could not set values for the given XSL style sheet parameters. * @test */ @@ -117,7 +117,7 @@ final class TemplateTest extends \PHPUnit_Framework_TestCase * XSL stylesheet can be applied on failure of loading XML from template * * @covers ::applyXslStyleSheet - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedException \PhpOffice\PhpWord\Exception\Exception * @expectedExceptionMessage Could not load XML from the given template. * @test */ diff --git a/tests/PhpWord/Tests/Writer/ODTextTest.php b/tests/PhpWord/Tests/Writer/ODTextTest.php index 8c345f55..9ee31432 100644 --- a/tests/PhpWord/Tests/Writer/ODTextTest.php +++ b/tests/PhpWord/Tests/Writer/ODTextTest.php @@ -44,7 +44,7 @@ class ODTextTest extends \PHPUnit_Framework_TestCase /** * Construct with null * - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedException \PhpOffice\PhpWord\Exception\Exception * @expectedExceptionMessage No PhpWord assigned. */ public function testConstructWithNull() @@ -105,7 +105,7 @@ class ODTextTest extends \PHPUnit_Framework_TestCase /** * Save with no PhpWord object assigned * - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedException \PhpOffice\PhpWord\Exception\Exception * @expectedExceptionMessage PhpWord object unassigned. */ public function testSaveException() @@ -137,7 +137,7 @@ class ODTextTest extends \PHPUnit_Framework_TestCase /** * Use disk caching exception * - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedException \PhpOffice\PhpWord\Exception\Exception */ public function testSetUseDiskCachingException() { diff --git a/tests/PhpWord/Tests/Writer/RTFTest.php b/tests/PhpWord/Tests/Writer/RTFTest.php index 7b546438..4016cb85 100644 --- a/tests/PhpWord/Tests/Writer/RTFTest.php +++ b/tests/PhpWord/Tests/Writer/RTFTest.php @@ -32,7 +32,7 @@ class RTFTest extends \PHPUnit_Framework_TestCase /** * Construct with null * - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedException \PhpOffice\PhpWord\Exception\Exception * @expectedExceptionMessage No PhpWord assigned. */ public function testConstructWithNull() @@ -94,7 +94,7 @@ class RTFTest extends \PHPUnit_Framework_TestCase /** * Save with no PhpWord object assigned * - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedException \PhpOffice\PhpWord\Exception\Exception * @expectedExceptionMessage PhpWord object unassigned. */ public function testSaveException() diff --git a/tests/PhpWord/Tests/Writer/Word2007/FooterTest.php b/tests/PhpWord/Tests/Writer/Word2007/FooterTest.php index 6d303a0a..c0f94de6 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/FooterTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/FooterTest.php @@ -28,7 +28,7 @@ class FooterTest extends \PHPUnit_Framework_TestCase public function testWriteFooter() { $imageSrc = __DIR__ . "/../../_files/images/PhpWord.png"; - $container = new \PhpOffice\PhpWord\Section\Footer(1); + $container = new \PhpOffice\PhpWord\Container\Footer(1); $container->addText(''); $container->addPreserveText(''); $container->addTextBreak(); diff --git a/tests/PhpWord/Tests/Writer/Word2007/HeaderTest.php b/tests/PhpWord/Tests/Writer/Word2007/HeaderTest.php index de3ac010..253f663e 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/HeaderTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/HeaderTest.php @@ -26,7 +26,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase { $imageSrc = __DIR__ . "/../../_files/images/PhpWord.png"; - $container = new \PhpOffice\PhpWord\Section\Header(1); + $container = new \PhpOffice\PhpWord\Container\Header(1); $container->addText('Test'); $container->addPreserveText(''); $container->addTextBreak(); diff --git a/tests/PhpWord/Tests/Writer/Word2007Test.php b/tests/PhpWord/Tests/Writer/Word2007Test.php index fa4e301c..f486d18d 100644 --- a/tests/PhpWord/Tests/Writer/Word2007Test.php +++ b/tests/PhpWord/Tests/Writer/Word2007Test.php @@ -115,7 +115,7 @@ class Word2007Test extends \PHPUnit_Framework_TestCase /** * Save with no PhpWord object assigned * - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedException \PhpOffice\PhpWord\Exception\Exception * @expectedExceptionMessage PhpWord object unassigned. */ public function testSaveException() @@ -181,7 +181,7 @@ class Word2007Test extends \PHPUnit_Framework_TestCase /** * Use disk caching exception * - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedException \PhpOffice\PhpWord\Exception\Exception */ public function testSetUseDiskCachingException() { From 2bf0bbb094670e301e7b3ae2e0c0720f5b93e5e6 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Mon, 31 Mar 2014 23:10:51 +0700 Subject: [PATCH 02/27] Container abstract class --- CHANGELOG.md | 1 + src/PhpWord/Container/Container.php | 437 ++++++++++++++++++ src/PhpWord/Container/Footer.php | 185 +------- src/PhpWord/Container/Header.php | 232 +--------- src/PhpWord/Container/Section.php | 388 +++------------- src/PhpWord/Writer/Word2007.php | 2 +- tests/PhpWord/Tests/Container/FooterTest.php | 2 +- tests/PhpWord/Tests/Container/HeaderTest.php | 2 +- tests/PhpWord/Tests/Container/SectionTest.php | 8 +- 9 files changed, 537 insertions(+), 720 deletions(-) create mode 100644 src/PhpWord/Container/Container.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 63c295f0..c9c5d39a 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - Writer: Refactor writer classes and make a new Writer abstract class - @ivanlanin GH-160 - Reader: Rename AbstractReader > Reader - @ivanlanin - General: Refactor folders: Element, Container, and Exception - @ivanlanin +- Container: Create new Container abstract class - @ivanlanin ## 0.9.1 - 27 Mar 2014 diff --git a/src/PhpWord/Container/Container.php b/src/PhpWord/Container/Container.php new file mode 100644 index 00000000..7ca3ebb1 --- /dev/null +++ b/src/PhpWord/Container/Container.php @@ -0,0 +1,437 @@ +elements[] = $text; + + return $text; + } + + /** + * Add text break element + * + * @param int $count + * @param mixed $fontStyle + * @param mixed $paragraphStyle + */ + public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null) + { + for ($i = 1; $i <= $count; $i++) { + $this->elements[] = new TextBreak($fontStyle, $paragraphStyle); + } + } + + /** + * Add textrun element + * + * @param mixed $styleParagraph + * @return TextRun + */ + public function addTextRun($styleParagraph = null) + { + $textRun = new TextRun($styleParagraph); + $this->elements[] = $textRun; + + return $textRun; + } + + /** + * Add link element + * + * @param string $linkSrc + * @param string $linkName + * @param mixed $styleFont + * @param mixed $styleParagraph + * @return Link + * @todo Enable link element in header and footer + */ + public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null) + { + if ($this->containerType != 'section') { + throw new \BadMethodCallException(); + } + + if (!String::isUTF8($linkSrc)) { + $linkSrc = utf8_encode($linkSrc); + } + if (!is_null($linkName)) { + if (!String::isUTF8($linkName)) { + $linkName = utf8_encode($linkName); + } + } + $link = new Link($linkSrc, $linkName, $styleFont, $styleParagraph); + $rID = Media::addSectionLinkElement($linkSrc); + $link->setRelationId($rID); + $this->elements[] = $link; + return $link; + } + + /** + * Add a Title Element + * + * @param string $text + * @param int $depth + * @return Title + * @todo Enable title element in header and footer + */ + public function addTitle($text, $depth = 1) + { + if ($this->containerType != 'section') { + throw new \BadMethodCallException(); + } + + if (!String::isUTF8($text)) { + $text = utf8_encode($text); + } + $styles = Style::getStyles(); + if (array_key_exists('Heading_' . $depth, $styles)) { + $style = 'Heading' . $depth; + } else { + $style = null; + } + $title = new Title($text, $depth, $style); + $data = TOC::addTitle($text, $depth); + $anchor = $data[0]; + $bookmarkId = $data[1]; + $title->setAnchor($anchor); + $title->setBookmarkId($bookmarkId); + $this->elements[] = $title; + return $title; + } + + /** + * Add preserve text element + * + * @param string $text + * @param mixed $styleFont + * @param mixed $styleParagraph + * @return PreserveText + */ + public function addPreserveText($text, $styleFont = null, $styleParagraph = null) + { + if ($this->containerType == 'section') { + throw new \BadMethodCallException(); + } + + if (!String::isUTF8($text)) { + $text = utf8_encode($text); + } + $ptext = new PreserveText($text, $styleFont, $styleParagraph); + $this->elements[] = $ptext; + + return $ptext; + } + + /** + * Add listitem element + * + * @param string $text + * @param int $depth + * @param mixed $styleFont + * @param mixed $styleList + * @param mixed $styleParagraph + * @return ListItem + * @todo Enable list item element in header and footer + */ + public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null) + { + if ($this->containerType != 'section') { + throw new \BadMethodCallException(); + } + + if (!String::isUTF8($text)) { + $text = utf8_encode($text); + } + $listItem = new ListItem($text, $depth, $styleFont, $styleList, $styleParagraph); + $this->elements[] = $listItem; + return $listItem; + } + + /** + * Add table element + * + * @param mixed $style + * @return Table + */ + public function addTable($style = null) + { + $table = new Table($this->containerType, $this->sectionId, $style); + $this->elements[] = $table; + + return $table; + } + + /** + * Add image element + * + * @param string $src + * @param mixed $style Image style + * @param boolean $isWatermark + * @return Image + */ + public function addImage($src, $style = null, $isWatermark = false) + { + $image = new Image($src, $style, $isWatermark); + if (!is_null($image->getSource())) { + switch ($this->containerType) { + case 'section': + $rID = Media::addSectionMediaElement($src, 'image', $image); + break; + case 'header': + $rID = Media::addHeaderMediaElement($this->sectionId, $src, $image); + break; + case 'footer': + $rID = Media::addFooterMediaElement($this->sectionId, $src, $image); + break; + } + $image->setRelationId($rID); + $this->elements[] = $image; + return $image; + } else { + throw new InvalidImageException; + } + } + + /** + * Add OLE-object element + * + * All exceptions should be handled by PhpOffice\PhpWord\Element\Object + * + * @param string $src + * @param mixed $style + * @return Object + * @todo Enable OLE object element in header and footer + */ + public function addObject($src, $style = null) + { + if ($this->containerType != 'section') { + throw new \BadMethodCallException(); + } + + $object = new Object($src, $style); + if (!is_null($object->getSource())) { + $inf = pathinfo($src); + $ext = $inf['extension']; + if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') { + $ext = substr($ext, 0, -1); + } + $icon = __DIR__ . "/../_staticDocParts/_{$ext}.png"; + $rIDimg = Media::addSectionMediaElement($icon, 'image', new Image($icon)); + $data = Media::addSectionMediaElement($src, 'oleObject'); + $rID = $data[0]; + $objectId = $data[1]; + $object->setRelationId($rID); + $object->setObjectId($objectId); + $object->setImageRelationId($rIDimg); + $this->elements[] = $object; + return $object; + } else { + throw new InvalidObjectException(); + } + } + + /** + * Add footnote element + * + * @param mixed $styleParagraph + * @return FootnoteElement + * @todo Enable footnote element in header and footer + */ + public function addFootnote($styleParagraph = null) + { + if ($this->containerType != 'section') { + throw new \BadMethodCallException(); + } + + $footnote = new FootnoteElement($styleParagraph); + $refID = Footnote::addFootnoteElement($footnote); + $footnote->setReferenceId($refID); + $this->elements[] = $footnote; + return $footnote; + } + + /** + * Add a CheckBox Element + * + * @param string $name + * @param string $text + * @param mixed $styleFont + * @param mixed $styleParagraph + * @return CheckBox + * @todo Enable checkbox element in header and footer + */ + public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null) + { + if ($this->containerType != 'section') { + throw new \BadMethodCallException(); + } + + if (!String::isUTF8($name)) { + $name = utf8_encode($name); + } + if (!String::isUTF8($text)) { + $text = utf8_encode($text); + } + $element = new CheckBox($name, $text, $styleFont, $styleParagraph); + $this->elements[] = $element; + + return $element; + } + + /** + * Get section number + * getFooterCount + */ + public function getSectionId() + { + return $this->sectionId; + } + + /** + * Get all elements + * + * @return array + */ + public function getElements() + { + return $this->elements; + } + + /** + * Get relation Id + * + * @return int + */ + public function getRelationId() + { + if ($this->containerType == 'section') { + throw new \BadMethodCallException(); + } + + return $this->relationId; + } + + /** + * Set relation Id + * + * @param int $rId + */ + public function setRelationId($rId) + { + if ($this->containerType == 'section') { + throw new \BadMethodCallException(); + } + + $this->relationId = $rId; + } + + /** + * Add memory image element + * + * @param string $src + * @param mixed $style + * @deprecated 0.9.0 + */ + public function addMemoryImage($src, $style = null) + { + return $this->addImage($src, $style); + } + + /** + * Create textrun element + * + * @param mixed $styleParagraph + * @deprecated 0.9.2 + */ + public function createTextRun($styleParagraph = null) + { + return $this->addTextRun($styleParagraph); + } + + /** + * Create footnote element + * + * @param mixed $styleParagraph + * @deprecated 0.9.2 + */ + public function createFootnote($styleParagraph = null) + { + return $this->addFootnote($styleParagraph); + } +} diff --git a/src/PhpWord/Container/Footer.php b/src/PhpWord/Container/Footer.php index c1e6034e..37884b86 100755 --- a/src/PhpWord/Container/Footer.php +++ b/src/PhpWord/Container/Footer.php @@ -9,192 +9,19 @@ namespace PhpOffice\PhpWord\Container; -use PhpOffice\PhpWord\Exception\InvalidImageException; -use PhpOffice\PhpWord\Media; -use PhpOffice\PhpWord\Shared\String; -use PhpOffice\PhpWord\Element\Text; -use PhpOffice\PhpWord\Element\TextRun; -use PhpOffice\PhpWord\Element\PreserveText; -use PhpOffice\PhpWord\Element\TextBreak; -use PhpOffice\PhpWord\Element\Table; -use PhpOffice\PhpWord\Element\Image; - /** * Footer element */ -class Footer +class Footer extends Container { /** - * Footer Count + * Create new instance * - * @var int + * @param int $sectionId */ - private $_footerCount; - - /** - * Footer Relation ID - * - * @var int - */ - private $_rId; - - /** - * Footer Element Collection - * - * @var int - */ - private $_elementCollection = array(); - - /** - * Create a new Footer - * - * @param int $sectionCount - */ - public function __construct($sectionCount) + public function __construct($sectionId) { - $this->_footerCount = $sectionCount; - } - - /** - * Add a Text Element - * - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\Text - */ - public function addText($text, $styleFont = null, $styleParagraph = null) - { - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $text = new Text($text, $styleFont, $styleParagraph); - $this->_elementCollection[] = $text; - return $text; - } - - /** - * Add TextBreak - * - * @param int $count - * @param null|string|array|\PhpOffice\PhpWord\Style\Font $fontStyle - * @param null|string|array|\PhpOffice\PhpWord\Style\Paragraph $paragraphStyle - */ - public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null) - { - for ($i = 1; $i <= $count; $i++) { - $this->_elementCollection[] = new TextBreak($fontStyle, $paragraphStyle); - } - } - - /** - * Create a new TextRun - * - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\TextRun - */ - public function createTextRun($styleParagraph = null) - { - $textRun = new TextRun($styleParagraph); - $this->_elementCollection[] = $textRun; - return $textRun; - } - - /** - * Add a Table Element - * - * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Table - */ - public function addTable($style = null) - { - $table = new Table('footer', $this->_footerCount, $style); - $this->_elementCollection[] = $table; - return $table; - } - - /** - * Add a Image Element - * - * @param string $src - * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Image - */ - public function addImage($src, $style = null) - { - $image = new Image($src, $style); - if (!is_null($image->getSource())) { - $rID = Media::addFooterMediaElement($this->_footerCount, $src, $image); - $image->setRelationId($rID); - $this->_elementCollection[] = $image; - return $image; - } else { - throw new InvalidImageException; - } - } - - /** - * Add a by PHP created Image Element - * - * @param string $src - * @param mixed $style - * @deprecated - */ - public function addMemoryImage($src, $style = null) - { - return $this->addImage($src, $style); - } - - /** - * Add a PreserveText Element - * - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\PreserveText - */ - public function addPreserveText($text, $styleFont = null, $styleParagraph = null) - { - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $ptext = new PreserveText($text, $styleFont, $styleParagraph); - $this->_elementCollection[] = $ptext; - return $ptext; - } - - /** - * Get Footer Relation ID - */ - public function getRelationId() - { - return $this->_rId; - } - - /** - * Set Footer Relation ID - * - * @param int $rId - */ - public function setRelationId($rId) - { - $this->_rId = $rId; - } - - /** - * Get all Footer Elements - * @return array - */ - public function getElements() - { - return $this->_elementCollection; - } - - /** - * Get Footer Count - */ - public function getFooterCount() - { - return $this->_footerCount; + $this->containerType = 'footer'; + $this->sectionId = $sectionId; } } diff --git a/src/PhpWord/Container/Header.php b/src/PhpWord/Container/Header.php index 5b6fce42..3c702406 100755 --- a/src/PhpWord/Container/Header.php +++ b/src/PhpWord/Container/Header.php @@ -9,187 +9,39 @@ namespace PhpOffice\PhpWord\Container; -use PhpOffice\PhpWord\Exception\InvalidImageException; -use PhpOffice\PhpWord\Media; -use PhpOffice\PhpWord\Shared\String; -use PhpOffice\PhpWord\Element\Text; -use PhpOffice\PhpWord\Element\TextRun; -use PhpOffice\PhpWord\Element\PreserveText; -use PhpOffice\PhpWord\Element\TextBreak; -use PhpOffice\PhpWord\Element\Table; use PhpOffice\PhpWord\Element\Image; /** * Header element */ -class Header +class Header extends Container { /** - * Header Count + * Header types constants * - * @var int + * @var string + * @link http://www.schemacentral.com/sc/ooxml/a-wheaderType-4.html Header or Footer Type */ - private $_headerCount; - - /** - * Header Relation ID - * - * @var int - */ - private $_rId; + const AUTO = 'default'; // Did not use DEFAULT because it is a PHP keyword + const EVEN = 'even'; + const FIRST = 'first'; /** * Header type * * @var string - * @link http://www.schemacentral.com/sc/ooxml/a-w_type-4.html Header or Footer Type */ - private $_type = self::AUTO; + private $headerType = self::AUTO; /** - * Even Numbered Pages Only - * @var string - * @link http://www.schemacentral.com/sc/ooxml/a-w_type-4.html Header or Footer Type - */ - const EVEN = 'even'; - - /** - * Default Header or Footer - * @var string - * @link http://www.schemacentral.com/sc/ooxml/a-w_type-4.html Header or Footer Type - */ - const AUTO = 'default'; // Did not use DEFAULT because it is a PHP keyword - - /** - * First Page Only - * @var string - * @link http://www.schemacentral.com/sc/ooxml/a-w_type-4.html Header or Footer Type - */ - const FIRST = 'first'; - - /** - * Header Element Collection - * - * @var int - */ - private $_elementCollection = array(); - - /** - * Create a new Header + * Create new instance * * @param int $sectionCount */ - public function __construct($sectionCount) + public function __construct($sectionId) { - $this->_headerCount = $sectionCount; - } - - /** - * Add a Text Element - * - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\Text - */ - public function addText($text, $styleFont = null, $styleParagraph = null) - { - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $text = new Text($text, $styleFont, $styleParagraph); - $this->_elementCollection[] = $text; - return $text; - } - - /** - * Add TextBreak - * - * @param int $count - * @param null|string|array|\PhpOffice\PhpWord\Style\Font $fontStyle - * @param null|string|array|\PhpOffice\PhpWord\Style\Paragraph $paragraphStyle - */ - public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null) - { - for ($i = 1; $i <= $count; $i++) { - $this->_elementCollection[] = new TextBreak($fontStyle, $paragraphStyle); - } - } - - /** - * Create a new TextRun - * - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\TextRun - */ - public function createTextRun($styleParagraph = null) - { - $textRun = new TextRun($styleParagraph); - $this->_elementCollection[] = $textRun; - return $textRun; - } - - /** - * Add a Table Element - * - * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Table - */ - public function addTable($style = null) - { - $table = new Table('header', $this->_headerCount, $style); - $this->_elementCollection[] = $table; - return $table; - } - - /** - * Add a Image Element - * - * @param string $src - * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Image - */ - public function addImage($src, $style = null) - { - $image = new Image($src, $style); - if (!is_null($image->getSource())) { - $rID = Media::addHeaderMediaElement($this->_headerCount, $src, $image); - $image->setRelationId($rID); - $this->_elementCollection[] = $image; - return $image; - } else { - throw new InvalidImageException; - } - } - - /** - * Add a by PHP created Image Element - * - * @param string $src - * @param mixed $style - * @deprecated - */ - public function addMemoryImage($src, $style = null) - { - return $this->addImage($src, $style); - } - - /** - * Add a PreserveText Element - * - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\PreserveText - */ - public function addPreserveText($text, $styleFont = null, $styleParagraph = null) - { - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $ptext = new PreserveText($text, $styleFont, $styleParagraph); - $this->_elementCollection[] = $ptext; - return $ptext; + $this->containerType = 'header'; + $this->sectionId = $sectionId; } /** @@ -197,69 +49,27 @@ class Header * * @param string $src * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Image + * @return Image */ public function addWatermark($src, $style = null) { - $image = new Image($src, $style, true); - if (!is_null($image->getSource())) { - $rID = Media::addHeaderMediaElement($this->_headerCount, $src, $image); - $image->setRelationId($rID); - $this->_elementCollection[] = $image; - return $image; - } else { - throw new InvalidImageException; - } + return $this->addImage($src, $style, true); } /** - * Get Header Relation ID - */ - public function getRelationId() - { - return $this->_rId; - } - - /** - * Set Header Relation ID - * - * @param int $rId - */ - public function setRelationId($rId) - { - $this->_rId = $rId; - } - - /** - * Get all Header Elements - */ - public function getElements() - { - return $this->_elementCollection; - } - - /** - * Get Header Count - */ - public function getHeaderCount() - { - return $this->_headerCount; - } - - /** - * Get Header Type + * Get header type */ public function getType() { - return $this->_type; + return $this->headerType; } /** - * Reset back to default + * Reset type to default */ public function resetType() { - return $this->_type = self::AUTO; + return $this->headerType = self::AUTO; } /** @@ -267,14 +77,14 @@ class Header */ public function firstPage() { - return $this->_type = self::FIRST; + return $this->headerType = self::FIRST; } /** - * Even numbered Pages only + * Even numbered pages only */ public function evenPage() { - return $this->_type = self::EVEN; + return $this->headerType = self::EVEN; } } diff --git a/src/PhpWord/Container/Section.php b/src/PhpWord/Container/Section.php index a3002cb3..dcef4707 100644 --- a/src/PhpWord/Container/Section.php +++ b/src/PhpWord/Container/Section.php @@ -9,85 +9,57 @@ namespace PhpOffice\PhpWord\Container; -use PhpOffice\PhpWord\Exception\InvalidImageException; -use PhpOffice\PhpWord\Exception\InvalidObjectException; -use PhpOffice\PhpWord\Footnote; -use PhpOffice\PhpWord\Media; -use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\TOC; use PhpOffice\PhpWord\Container\Footer; use PhpOffice\PhpWord\Container\Header; -use PhpOffice\PhpWord\Element\Text; -use PhpOffice\PhpWord\Element\TextRun; -use PhpOffice\PhpWord\Element\Title; -use PhpOffice\PhpWord\Element\Link; -use PhpOffice\PhpWord\Element\TextBreak; +use PhpOffice\PhpWord\Container\Settings; use PhpOffice\PhpWord\Element\PageBreak; -use PhpOffice\PhpWord\Element\ListItem; -use PhpOffice\PhpWord\Element\Table; -use PhpOffice\PhpWord\Element\Image; -use PhpOffice\PhpWord\Element\Object; -use PhpOffice\PhpWord\Element\CheckBox; -use PhpOffice\PhpWord\Shared\String; /** * Section */ -class Section +class Section extends Container { - /** - * Section count - * - * @var int - */ - private $_sectionCount; - /** * Section settings * - * @var \PhpOffice\PhpWord\Container\Settings + * @var Settings */ - private $_settings; + private $settings; /** - * Section Element Collection + * Section headers * - * @var array + * @var Header[] */ - private $_elementCollection = array(); + private $headers = array(); /** - * Section Headers + * Section footer * - * @var array + * @var Footer */ - private $_headers = array(); - - /** - * Section Footer - * - * @var \PhpOffice\PhpWord\Container\Footer - */ - private $_footer = null; + private $footer = null; /** - * Create a new Section + * Create new instance * * @param int $sectionCount * @param mixed $settings */ public function __construct($sectionCount, $settings = null) { - $this->_sectionCount = $sectionCount; - $this->_settings = new \PhpOffice\PhpWord\Container\Settings(); + $this->containerType = 'section'; + $this->sectionId = $sectionCount; + $this->settings = new Settings(); $this->setSettings($settings); } /** - * Set Section Settings + * Set section settings * - * @param array $settings + * @param array $settings */ public function setSettings($settings = null) { @@ -96,7 +68,7 @@ class Section if (substr($key, 0, 1) != '_') { $key = '_' . $key; } - $this->_settings->setSettingValue($key, $value); + $this->settings->setSettingValue($key, $value); } } } @@ -104,71 +76,11 @@ class Section /** * Get Section Settings * - * @return \PhpOffice\PhpWord\Container\Settings + * @return Settings */ public function getSettings() { - return $this->_settings; - } - - /** - * Add a Text Element - * - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\Text - */ - public function addText($text, $styleFont = null, $styleParagraph = null) - { - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $text = new Text($text, $styleFont, $styleParagraph); - $this->_elementCollection[] = $text; - return $text; - } - - /** - * Add a Link Element - * - * @param string $linkSrc - * @param string $linkName - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\Link - */ - public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null) - { - if (!String::isUTF8($linkSrc)) { - $linkSrc = utf8_encode($linkSrc); - } - if (!is_null($linkName)) { - if (!String::isUTF8($linkName)) { - $linkName = utf8_encode($linkName); - } - } - - $link = new Link($linkSrc, $linkName, $styleFont, $styleParagraph); - $rID = Media::addSectionLinkElement($linkSrc); - $link->setRelationId($rID); - - $this->_elementCollection[] = $link; - return $link; - } - - /** - * Add a TextBreak Element - * - * @param int $count - * @param null|string|array|\PhpOffice\PhpWord\Style\Font $fontStyle - * @param null|string|array|\PhpOffice\PhpWord\Style\Paragraph $paragraphStyle - */ - public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null) - { - for ($i = 1; $i <= $count; $i++) { - $this->_elementCollection[] = new TextBreak($fontStyle, $paragraphStyle); - } + return $this->settings; } /** @@ -176,104 +88,7 @@ class Section */ public function addPageBreak() { - $this->_elementCollection[] = new PageBreak(); - } - - /** - * Add a Table Element - * - * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Table - */ - public function addTable($style = null) - { - $table = new Table('section', $this->_sectionCount, $style); - $this->_elementCollection[] = $table; - return $table; - } - - /** - * Add a ListItem Element - * - * @param string $text - * @param int $depth - * @param mixed $styleFont - * @param mixed $styleList - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\ListItem - */ - public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null) - { - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $listItem = new ListItem($text, $depth, $styleFont, $styleList, $styleParagraph); - $this->_elementCollection[] = $listItem; - return $listItem; - } - - /** - * Add a OLE-Object Element - * - * All exceptions should be handled by PhpOffice\PhpWord\Element\Object - * - * @param string $src - * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Object - */ - public function addObject($src, $style = null) - { - $object = new Object($src, $style); - if (!is_null($object->getSource())) { - $inf = pathinfo($src); - $ext = $inf['extension']; - if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') { - $ext = substr($ext, 0, -1); - } - $icon = __DIR__ . "/../_staticDocParts/_{$ext}.png"; - $rIDimg = Media::addSectionMediaElement($icon, 'image', new Image($icon)); - $data = Media::addSectionMediaElement($src, 'oleObject'); - $rID = $data[0]; - $objectId = $data[1]; - $object->setRelationId($rID); - $object->setObjectId($objectId); - $object->setImageRelationId($rIDimg); - $this->_elementCollection[] = $object; - return $object; - } else { - throw new InvalidObjectException(); - } - } - - /** - * Add image element - * - * All exceptions should be handled by PhpOffice\PhpWord\Element\Image - * - * @param string $src - * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Image - */ - public function addImage($src, $style = null) - { - $image = new Image($src, $style); - $rID = Media::addSectionMediaElement($src, 'image', $image); - $image->setRelationId($rID); - $this->_elementCollection[] = $image; - return $image; - } - - /** - * Add memory image element - * - * @deprecated - * - * @param string $src - * @param mixed $style - */ - public function addMemoryImage($src, $style = null) - { - return $this->addImage($src, $style); + $this->elements[] = new PageBreak(); } /** @@ -281,82 +96,39 @@ class Section * * @param mixed $styleFont * @param mixed $styleTOC - * @return \PhpOffice\PhpWord\TOC + * @return TOC */ public function addTOC($styleFont = null, $styleTOC = null) { $toc = new TOC($styleFont, $styleTOC); - $this->_elementCollection[] = $toc; + $this->elements[] = $toc; return $toc; } /** - * Add a Title Element + * Add header * - * @param string $text - * @param int $depth - * @return \PhpOffice\PhpWord\Element\Title + * @return Header */ - public function addTitle($text, $depth = 1) + public function addHeader() { - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $styles = Style::getStyles(); - if (array_key_exists('Heading_' . $depth, $styles)) { - $style = 'Heading' . $depth; - } else { - $style = null; - } - - $title = new Title($text, $depth, $style); - - $data = TOC::addTitle($text, $depth); - $anchor = $data[0]; - $bookmarkId = $data[1]; - - $title->setAnchor($anchor); - $title->setBookmarkId($bookmarkId); - - $this->_elementCollection[] = $title; - return $title; - } - - /** - * Create a new TextRun - * - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\TextRun - */ - public function createTextRun($styleParagraph = null) - { - $textRun = new TextRun($styleParagraph); - $this->_elementCollection[] = $textRun; - return $textRun; - } - - /** - * Get all Elements - * - * @return array - */ - public function getElements() - { - return $this->_elementCollection; - } - - /** - * Create a new Header - * - * @return \PhpOffice\PhpWord\Container\Header - */ - public function createHeader() - { - $header = new Header($this->_sectionCount); - $this->_headers[] = $header; + $header = new Header($this->sectionId); + $this->headers[] = $header; return $header; } + /** + * Add footer + * + * @return Footer + */ + public function addFooter() + { + $footer = new Footer($this->sectionId); + $this->footer = $footer; + return $footer; + } + /** * Get Headers * @@ -364,7 +136,17 @@ class Section */ public function getHeaders() { - return $this->_headers; + return $this->headers; + } + + /** + * Get footer element + * + * @return Footer + */ + public function getFooter() + { + return $this->footer; } /** @@ -373,73 +155,33 @@ class Section * If any of the Header instances have a type of Header::FIRST then this method returns true. * False otherwise. * - * @return Boolean + * @return boolean */ public function hasDifferentFirstPage() { - $value = array_filter($this->_headers, function (Header &$header) { + $value = array_filter($this->headers, function (Header &$header) { return $header->getType() == Header::FIRST; }); return count($value) > 0; } /** - * Create a new Footer + * Create header * - * @return \PhpOffice\PhpWord\Container\Footer + * @deprecated 0.9.2 + */ + public function createHeader() + { + return $this->addHeader(); + } + + /** + * Create footer + * + * @deprecated 0.9.2 */ public function createFooter() { - $footer = new Footer($this->_sectionCount); - $this->_footer = $footer; - return $footer; - } - - /** - * Get footer element - * - * @return \PhpOffice\PhpWord\Container\Footer - */ - public function getFooter() - { - return $this->_footer; - } - - /** - * Create a new Footnote Element - * - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\Footnote - */ - public function createFootnote($styleParagraph = null) - { - $footnote = new \PhpOffice\PhpWord\Element\Footnote($styleParagraph); - $refID = Footnote::addFootnoteElement($footnote); - $footnote->setReferenceId($refID); - $this->_elementCollection[] = $footnote; - return $footnote; - } - - /** - * Add a CheckBox Element - * - * @param string $name - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\CheckBox - */ - public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null) - { - if (!String::isUTF8($name)) { - $name = utf8_encode($name); - } - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $element = new CheckBox($name, $text, $styleFont, $styleParagraph); - $this->_elementCollection[] = $element; - - return $element; + return $this->addFooter(); } } diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index 5f247707..0781e2da 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -158,7 +158,7 @@ class Word2007 extends Writer implements IWriter $footers[++$_cFtrs] = $_footer; if (!is_null($_footer)) { $_footer->setRelationId(++$rID); - $_footerCount = $_footer->getFooterCount(); + $_footerCount = $_footer->getSectionId(); $_footerFile = 'footer' . $_footerCount . '.xml'; $sectionElements[] = array('target' => $_footerFile, 'type' => 'footer', 'rID' => $rID); $objZip->addFromString('word/' . $_footerFile, $this->getWriterPart('footer')->writeFooter($_footer)); diff --git a/tests/PhpWord/Tests/Container/FooterTest.php b/tests/PhpWord/Tests/Container/FooterTest.php index d9988ac5..74a09a51 100644 --- a/tests/PhpWord/Tests/Container/FooterTest.php +++ b/tests/PhpWord/Tests/Container/FooterTest.php @@ -27,7 +27,7 @@ class FooterTest extends \PHPUnit_Framework_TestCase $oFooter = new Footer($iVal); $this->assertInstanceOf('PhpOffice\\PhpWord\\Container\\Footer', $oFooter); - $this->assertEquals($oFooter->getFooterCount(), $iVal); + $this->assertEquals($oFooter->getSectionId(), $iVal); } /** diff --git a/tests/PhpWord/Tests/Container/HeaderTest.php b/tests/PhpWord/Tests/Container/HeaderTest.php index df1435de..9d479ade 100644 --- a/tests/PhpWord/Tests/Container/HeaderTest.php +++ b/tests/PhpWord/Tests/Container/HeaderTest.php @@ -27,7 +27,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $oHeader = new Header($iVal); $this->assertInstanceOf('PhpOffice\\PhpWord\\Container\\Header', $oHeader); - $this->assertEquals($oHeader->getHeaderCount(), $iVal); + $this->assertEquals($oHeader->getSectionId(), $iVal); $this->assertEquals($oHeader->getType(), Header::AUTO); } diff --git a/tests/PhpWord/Tests/Container/SectionTest.php b/tests/PhpWord/Tests/Container/SectionTest.php index a61498ea..0517399c 100644 --- a/tests/PhpWord/Tests/Container/SectionTest.php +++ b/tests/PhpWord/Tests/Container/SectionTest.php @@ -25,7 +25,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase public function testGetSettings() { $oSection = new Section(0); - $this->assertAttributeEquals($oSection->getSettings(), '_settings', new Section(0)); + $this->assertAttributeEquals($oSection->getSettings(), 'settings', new Section(0)); } /** @@ -34,7 +34,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase public function testGetElements() { $oSection = new Section(0); - $this->assertAttributeEquals($oSection->getElements(), '_elementCollection', new Section(0)); + $this->assertAttributeEquals($oSection->getElements(), 'elements', new Section(0)); } /** @@ -43,7 +43,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase public function testGetFooter() { $oSection = new Section(0); - $this->assertAttributeEquals($oSection->getFooter(), '_footer', new Section(0)); + $this->assertAttributeEquals($oSection->getFooter(), 'footer', new Section(0)); } /** @@ -52,7 +52,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase public function testGetHeaders() { $oSection = new Section(0); - $this->assertAttributeEquals($oSection->getHeaders(), '_headers', new Section(0)); + $this->assertAttributeEquals($oSection->getHeaders(), 'headers', new Section(0)); } /** From faba46cc059e0ba4e8df297051f0d9e746fecb89 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Mon, 31 Mar 2014 23:52:42 +0700 Subject: [PATCH 03/27] Extends `Container` abstract class into `Footnote`, `TextRun`, and `Cell` element class. --- src/PhpWord/Container/Container.php | 153 ++++++--- src/PhpWord/Element/Footnote.php | 102 +----- src/PhpWord/Element/Table/Cell.php | 313 ++---------------- src/PhpWord/Element/TextRun.php | 140 +------- .../PhpWord/Tests/Element/Table/CellTest.php | 4 +- 5 files changed, 166 insertions(+), 546 deletions(-) diff --git a/src/PhpWord/Container/Container.php b/src/PhpWord/Container/Container.php index 7ca3ebb1..379f7b14 100644 --- a/src/PhpWord/Container/Container.php +++ b/src/PhpWord/Container/Container.php @@ -57,6 +57,20 @@ abstract class Container */ protected $elements = array(); + /** + * Parent container type: section|header|footer + * + * @var string + */ + protected $parentContainer = null; + + /** + * Parent container Id + * + * @var int + */ + protected $parentContainerId; + /** * Relation Id * @@ -68,19 +82,22 @@ abstract class Container * Add text element * * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph + * @param mixed $fontStyle + * @param mixed $paragraphStyle * @return Text */ - public function addText($text, $styleFont = null, $styleParagraph = null) + public function addText($text, $fontStyle = null, $paragraphStyle = null) { + if (in_array($this->containerType, array('footnote', 'textrun'))) { + $paragraphStyle = null; + } if (!String::isUTF8($text)) { $text = utf8_encode($text); } - $text = new Text($text, $styleFont, $styleParagraph); - $this->elements[] = $text; + $element = new Text($text, $fontStyle, $paragraphStyle); + $this->elements[] = $element; - return $text; + return $element; } /** @@ -100,12 +117,16 @@ abstract class Container /** * Add textrun element * - * @param mixed $styleParagraph + * @param mixed $paragraphStyle * @return TextRun */ - public function addTextRun($styleParagraph = null) + public function addTextRun($paragraphStyle = null) { - $textRun = new TextRun($styleParagraph); + if (!in_array($this->containerType, array('section', 'header', 'footer', 'cell'))) { + throw new \BadMethodCallException(); + } + + $textRun = new TextRun($paragraphStyle); $this->elements[] = $textRun; return $textRun; @@ -116,14 +137,17 @@ abstract class Container * * @param string $linkSrc * @param string $linkName - * @param mixed $styleFont - * @param mixed $styleParagraph + * @param mixed $fontStyle + * @param mixed $paragraphStyle * @return Link * @todo Enable link element in header and footer */ - public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null) + public function addLink($linkSrc, $linkName = null, $fontStyle = null, $paragraphStyle = null) { - if ($this->containerType != 'section') { + if (!in_array($this->containerType, array('section', 'footnote', 'textrun', 'cell'))) { + throw new \BadMethodCallException(); + } + if ($this->containerType == 'cell' && $this->parentContainer != 'section') { throw new \BadMethodCallException(); } @@ -135,10 +159,15 @@ abstract class Container $linkName = utf8_encode($linkName); } } - $link = new Link($linkSrc, $linkName, $styleFont, $styleParagraph); - $rID = Media::addSectionLinkElement($linkSrc); + $link = new Link($linkSrc, $linkName, $fontStyle, $paragraphStyle); + if ($this->containerType == 'footnote') { + $rID = Footnote::addFootnoteLinkElement($linkSrc); + } else { + $rID = Media::addSectionLinkElement($linkSrc); + } $link->setRelationId($rID); $this->elements[] = $link; + return $link; } @@ -148,7 +177,7 @@ abstract class Container * @param string $text * @param int $depth * @return Title - * @todo Enable title element in header and footer + * @todo Enable title element in header, footer, footnote, textrun */ public function addTitle($text, $depth = 1) { @@ -172,6 +201,7 @@ abstract class Container $title->setAnchor($anchor); $title->setBookmarkId($bookmarkId); $this->elements[] = $title; + return $title; } @@ -179,20 +209,23 @@ abstract class Container * Add preserve text element * * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph + * @param mixed $fontStyle + * @param mixed $paragraphStyle * @return PreserveText */ - public function addPreserveText($text, $styleFont = null, $styleParagraph = null) + public function addPreserveText($text, $fontStyle = null, $paragraphStyle = null) { - if ($this->containerType == 'section') { + if (!in_array($this->containerType, array('header', 'footer', 'cell'))) { + throw new \BadMethodCallException(); + } + if ($this->containerType == 'cell' && $this->parentContainer == 'section') { throw new \BadMethodCallException(); } if (!String::isUTF8($text)) { $text = utf8_encode($text); } - $ptext = new PreserveText($text, $styleFont, $styleParagraph); + $ptext = new PreserveText($text, $fontStyle, $paragraphStyle); $this->elements[] = $ptext; return $ptext; @@ -203,23 +236,27 @@ abstract class Container * * @param string $text * @param int $depth - * @param mixed $styleFont + * @param mixed $fontStyle * @param mixed $styleList - * @param mixed $styleParagraph + * @param mixed $paragraphStyle * @return ListItem * @todo Enable list item element in header and footer */ - public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null) + public function addListItem($text, $depth = 0, $fontStyle = null, $styleList = null, $paragraphStyle = null) { - if ($this->containerType != 'section') { + if (!in_array($this->containerType, array('section', 'cell'))) { + throw new \BadMethodCallException(); + } + if ($this->containerType == 'cell' && $this->parentContainer != 'section') { throw new \BadMethodCallException(); } if (!String::isUTF8($text)) { $text = utf8_encode($text); } - $listItem = new ListItem($text, $depth, $styleFont, $styleList, $styleParagraph); + $listItem = new ListItem($text, $depth, $fontStyle, $styleList, $paragraphStyle); $this->elements[] = $listItem; + return $listItem; } @@ -231,6 +268,10 @@ abstract class Container */ public function addTable($style = null) { + if (!in_array($this->containerType, array('section', 'header', 'footer'))) { + throw new \BadMethodCallException(); + } + $table = new Table($this->containerType, $this->sectionId, $style); $this->elements[] = $table; @@ -247,17 +288,29 @@ abstract class Container */ public function addImage($src, $style = null, $isWatermark = false) { + if ($this->containerType == 'footnote') { + throw new \BadMethodCallException(); + } + if (!is_null($this->parentContainer)) { + $imageContainerType = $this->parentContainer; + $imageContainerId = $this->parentContainerId; + } else { + $imageContainerType = $this->containerType; + $imageContainerId = $this->sectionId; + } + $image = new Image($src, $style, $isWatermark); if (!is_null($image->getSource())) { - switch ($this->containerType) { + switch ($imageContainerType) { + case 'textrun': case 'section': $rID = Media::addSectionMediaElement($src, 'image', $image); break; case 'header': - $rID = Media::addHeaderMediaElement($this->sectionId, $src, $image); + $rID = Media::addHeaderMediaElement($imageContainerId, $src, $image); break; case 'footer': - $rID = Media::addFooterMediaElement($this->sectionId, $src, $image); + $rID = Media::addFooterMediaElement($imageContainerId, $src, $image); break; } $image->setRelationId($rID); @@ -280,7 +333,10 @@ abstract class Container */ public function addObject($src, $style = null) { - if ($this->containerType != 'section') { + if (!in_array($this->containerType, array('section', 'cell'))) { + throw new \BadMethodCallException(); + } + if ($this->containerType == 'cell' && $this->parentContainer != 'section') { throw new \BadMethodCallException(); } @@ -309,17 +365,17 @@ abstract class Container /** * Add footnote element * - * @param mixed $styleParagraph + * @param mixed $paragraphStyle * @return FootnoteElement * @todo Enable footnote element in header and footer */ - public function addFootnote($styleParagraph = null) + public function addFootnote($paragraphStyle = null) { - if ($this->containerType != 'section') { + if (!in_array($this->containerType, array('section', 'textrun'))) { throw new \BadMethodCallException(); } - $footnote = new FootnoteElement($styleParagraph); + $footnote = new FootnoteElement($paragraphStyle); $refID = Footnote::addFootnoteElement($footnote); $footnote->setReferenceId($refID); $this->elements[] = $footnote; @@ -331,14 +387,17 @@ abstract class Container * * @param string $name * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph + * @param mixed $fontStyle + * @param mixed $paragraphStyle * @return CheckBox * @todo Enable checkbox element in header and footer */ - public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null) + public function addCheckBox($name, $text, $fontStyle = null, $paragraphStyle = null) { - if ($this->containerType != 'section') { + if (!in_array($this->containerType, array('section', 'cell'))) { + throw new \BadMethodCallException(); + } + if ($this->containerType == 'cell' && $this->parentContainer != 'section') { throw new \BadMethodCallException(); } @@ -348,7 +407,7 @@ abstract class Container if (!String::isUTF8($text)) { $text = utf8_encode($text); } - $element = new CheckBox($name, $text, $styleFont, $styleParagraph); + $element = new CheckBox($name, $text, $fontStyle, $paragraphStyle); $this->elements[] = $element; return $element; @@ -380,7 +439,7 @@ abstract class Container */ public function getRelationId() { - if ($this->containerType == 'section') { + if (!in_array($this->containerType, array('header', 'footer'))) { throw new \BadMethodCallException(); } @@ -394,7 +453,7 @@ abstract class Container */ public function setRelationId($rId) { - if ($this->containerType == 'section') { + if (!in_array($this->containerType, array('header', 'footer'))) { throw new \BadMethodCallException(); } @@ -416,22 +475,22 @@ abstract class Container /** * Create textrun element * - * @param mixed $styleParagraph + * @param mixed $paragraphStyle * @deprecated 0.9.2 */ - public function createTextRun($styleParagraph = null) + public function createTextRun($paragraphStyle = null) { - return $this->addTextRun($styleParagraph); + return $this->addTextRun($paragraphStyle); } /** * Create footnote element * - * @param mixed $styleParagraph + * @param mixed $paragraphStyle * @deprecated 0.9.2 */ - public function createFootnote($styleParagraph = null) + public function createFootnote($paragraphStyle = null) { - return $this->addFootnote($styleParagraph); + return $this->addFootnote($paragraphStyle); } } diff --git a/src/PhpWord/Element/Footnote.php b/src/PhpWord/Element/Footnote.php index d1cb61cf..a683b3b0 100644 --- a/src/PhpWord/Element/Footnote.php +++ b/src/PhpWord/Element/Footnote.php @@ -9,125 +9,59 @@ namespace PhpOffice\PhpWord\Element; +use PhpOffice\PhpWord\Container\Container; use PhpOffice\PhpWord\Style\Paragraph; /** * Footnote element */ -class Footnote +class Footnote extends Container { /** * Paragraph style * - * @var \PhpOffice\PhpWord\Style\Paragraph + * @var string|Paragraph */ - private $_styleParagraph; + private $paragraphStyle; /** * Footnote Reference ID * * @var string */ - private $_refId; + private $referenceId; /** - * Text collection + * Create new instance * - * @var array + * @param string|array|Paragraph $paragraphStyle */ - private $_elementCollection; - - /** - * Create a new Footnote Element - * - * @param mixed $styleParagraph - */ - public function __construct($styleParagraph = null) + public function __construct($paragraphStyle = null) { - $this->_elementCollection = array(); + $this->containerType = 'footnote'; // Set paragraph style - if (is_array($styleParagraph)) { - $this->_styleParagraph = new Paragraph(); - - foreach ($styleParagraph as $key => $value) { + if (is_array($paragraphStyle)) { + $this->paragraphStyle = new Paragraph(); + foreach ($paragraphStyle as $key => $value) { if (substr($key, 0, 1) != '_') { $key = '_' . $key; } - $this->_styleParagraph->setStyleValue($key, $value); + $this->paragraphStyle->setStyleValue($key, $value); } } else { - $this->_styleParagraph = $styleParagraph; + $this->paragraphStyle = $paragraphStyle; } } - - /** - * Add a Text Element - * - * @param string $text - * @param mixed $styleFont - * @return \PhpOffice\PhpWord\Element\Text - */ - public function addText($text = null, $styleFont = null) - { - $givenText = $text; - $text = new Text($givenText, $styleFont); - $this->_elementCollection[] = $text; - return $text; - } - - /** - * Add TextBreak - * - * @param int $count - * @param mixed $fontStyle - * @param mixed $paragraphStyle - */ - public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null) - { - for ($i = 1; $i <= $count; $i++) { - $this->_elementCollection[] = new TextBreak($fontStyle, $paragraphStyle); - } - } - - /** - * Add a Link Element - * - * @param string $linkSrc - * @param string $linkName - * @param mixed $styleFont - * @return \PhpOffice\PhpWord\Element\Link - */ - public function addLink($linkSrc, $linkName = null, $styleFont = null) - { - - $link = new Link($linkSrc, $linkName, $styleFont); - $rID = \PhpOffice\PhpWord\Footnote::addFootnoteLinkElement($linkSrc); - $link->setRelationId($rID); - - $this->_elementCollection[] = $link; - return $link; - } - - /** - * Get Footnote content - * - * @return array - */ - public function getElements() - { - return $this->_elementCollection; - } - /** * Get paragraph style * - * @return \PhpOffice\PhpWord\Style\Paragraph + * @return string|Paragraph */ public function getParagraphStyle() { - return $this->_styleParagraph; + return $this->paragraphStyle; } /** @@ -137,7 +71,7 @@ class Footnote */ public function getReferenceId() { - return $this->_refId; + return $this->referenceId; } /** @@ -147,6 +81,6 @@ class Footnote */ public function setReferenceId($refId) { - $this->_refId = $refId; + $this->referenceId = $refId; } } diff --git a/src/PhpWord/Element/Table/Cell.php b/src/PhpWord/Element/Table/Cell.php index 0e0d688e..0a14400c 100755 --- a/src/PhpWord/Element/Table/Cell.php +++ b/src/PhpWord/Element/Table/Cell.php @@ -9,76 +9,44 @@ namespace PhpOffice\PhpWord\Element\Table; -use PhpOffice\PhpWord\Exception\Exception; -use PhpOffice\PhpWord\Exception\InvalidObjectException; -use PhpOffice\PhpWord\Exception\InvalidImageException; -use PhpOffice\PhpWord\Media; -use PhpOffice\PhpWord\Element\PreserveText; -use PhpOffice\PhpWord\Element\Image; -use PhpOffice\PhpWord\Element\Link; -use PhpOffice\PhpWord\Element\ListItem; -use PhpOffice\PhpWord\Element\Object; -use PhpOffice\PhpWord\Element\Text; -use PhpOffice\PhpWord\Element\TextBreak; -use PhpOffice\PhpWord\Element\TextRun; -use PhpOffice\PhpWord\Element\CheckBox; -use PhpOffice\PhpWord\Shared\String; +use PhpOffice\PhpWord\Container\Container; +use PhpOffice\PhpWord\Style\Cell as CellStyle; /** * Table cell element */ -class Cell +class Cell extends Container { /** - * Cell Width + * Cell width * * @var int */ - private $_width = null; + private $width = null; /** - * Cell Style + * Cell style * - * @var \PhpOffice\PhpWord\Style\Cell + * @var CellStyle */ - private $_style; + private $cellStyle; /** - * Cell Element Collection + * Create new instance * - * @var array - */ - private $_elementCollection = array(); - - /** - * Table holder - * - * @var string - */ - private $_insideOf; - - /** - * Section/Header/Footer count - * - * @var int - */ - private $_pCount; - - - /** - * Create a new Table Cell - * - * @param string $insideOf - * @param int $pCount + * @param string $parentType section|header|footer + * @param int $parentId * @param int $width - * @param mixed $style + * @param array|CellStyle $style */ - public function __construct($insideOf, $pCount, $width = null, $style = null) + public function __construct($parentType, $parentId, $width = null, $style = null) { - $this->_insideOf = $insideOf; - $this->_pCount = $pCount; - $this->_width = $width; - $this->_style = new \PhpOffice\PhpWord\Style\Cell(); + $this->containerType = 'cell'; + + $this->parentContainer = $parentType; + $this->parentContainerId = $parentId; + $this->width = $width; + $this->cellStyle = new CellStyle(); if (!is_null($style)) { if (is_array($style)) { @@ -86,260 +54,31 @@ class Cell if (substr($key, 0, 1) != '_') { $key = '_' . $key; } - $this->_style->setStyleValue($key, $value); + $this->cellStyle->setStyleValue($key, $value); } } else { - $this->_style = $style; + $this->cellStyle = $style; } } } /** - * Add a Text Element + * Get cell style * - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\Text - */ - public function addText($text, $styleFont = null, $styleParagraph = null) - { - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $text = new Text($text, $styleFont, $styleParagraph); - $this->_elementCollection[] = $text; - return $text; - } - - /** - * Add a Link Element - * - * @param string $linkSrc - * @param string $linkName - * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Link - */ - public function addLink($linkSrc, $linkName = null, $style = null) - { - if ($this->_insideOf == 'section') { - if (!String::isUTF8($linkSrc)) { - $linkSrc = utf8_encode($linkSrc); - } - if (!is_null($linkName)) { - if (!String::isUTF8($linkName)) { - $linkName = utf8_encode($linkName); - } - } - - $link = new Link($linkSrc, $linkName, $style); - $rID = Media::addSectionLinkElement($linkSrc); - $link->setRelationId($rID); - - $this->_elementCollection[] = $link; - return $link; - } else { - throw new Exception('Unsupported Link header / footer reference'); - return false; - } - } - - /** - * Add TextBreak - * - * @param int $count - * @param null|string|array|\PhpOffice\PhpWord\Style\Font $fontStyle - * @param null|string|array|\PhpOffice\PhpWord\Style\Paragraph $paragraphStyle - */ - public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null) - { - for ($i = 1; $i <= $count; $i++) { - $this->_elementCollection[] = new TextBreak($fontStyle, $paragraphStyle); - } - } - - /** - * Add a ListItem Element - * - * @param string $text - * @param int $depth - * @param mixed $styleText - * @param mixed $styleList - * @return \PhpOffice\PhpWord\Element\ListItem - */ - public function addListItem($text, $depth = 0, $styleText = null, $styleList = null) - { - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $listItem = new ListItem($text, $depth, $styleText, $styleList); - $this->_elementCollection[] = $listItem; - return $listItem; - } - - /** - * Add a Image Element - * - * @param string $src - * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Image - */ - public function addImage($src, $style = null) - { - $image = new Image($src, $style); - if (!is_null($image->getSource())) { - if ($this->_insideOf == 'section') { - $rID = Media::addSectionMediaElement($src, 'image', $image); - } elseif ($this->_insideOf == 'header') { - $rID = Media::addHeaderMediaElement($this->_pCount, $src, $image); - } elseif ($this->_insideOf == 'footer') { - $rID = Media::addFooterMediaElement($this->_pCount, $src, $image); - } - $image->setRelationId($rID); - $this->_elementCollection[] = $image; - return $image; - } else { - throw new InvalidImageException; - } - } - - /** - * Add a by PHP created Image Element - * - * @param string $src - * @param mixed $style - * @deprecated - */ - public function addMemoryImage($src, $style = null) - { - return $this->addImage($src, $style); - } - - /** - * Add a OLE-Object Element - * - * @param string $src - * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Object - */ - public function addObject($src, $style = null) - { - $object = new Object($src, $style); - - if (!is_null($object->getSource())) { - $inf = pathinfo($src); - $ext = $inf['extension']; - if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') { - $ext = substr($ext, 0, -1); - } - - $iconSrc = __DIR__ . '/../../_staticDocParts/'; - if (!\file_exists($iconSrc . '_' . $ext . '.png')) { - $iconSrc = $iconSrc . '_default.png'; - } else { - $iconSrc .= '_' . $ext . '.png'; - } - - $rIDimg = Media::addSectionMediaElement($iconSrc, 'image', new Image($iconSrc)); - $data = Media::addSectionMediaElement($src, 'oleObject'); - $rID = $data[0]; - $objectId = $data[1]; - - $object->setRelationId($rID); - $object->setObjectId($objectId); - $object->setImageRelationId($rIDimg); - - $this->_elementCollection[] = $object; - return $object; - } else { - throw new InvalidObjectException; - } - } - - /** - * Add a PreserveText Element - * - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\PreserveText - */ - public function addPreserveText($text, $styleFont = null, $styleParagraph = null) - { - if ($this->_insideOf == 'footer' || $this->_insideOf == 'header') { - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $ptext = new PreserveText($text, $styleFont, $styleParagraph); - $this->_elementCollection[] = $ptext; - return $ptext; - } else { - throw new Exception('addPreserveText only supported in footer/header.'); - } - } - - /** - * Create a new TextRun - * - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\TextRun - */ - public function createTextRun($styleParagraph = null) - { - $textRun = new TextRun($styleParagraph); - $this->_elementCollection[] = $textRun; - return $textRun; - } - - /** - * Add a CheckBox Element - * - * @param string $name - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\CheckBox - */ - public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null) - { - if (!String::isUTF8($name)) { - $name = utf8_encode($name); - } - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $text = new CheckBox($name, $text, $styleFont, $styleParagraph); - $this->_elementCollection[] = $text; - return $text; - } - - /** - * Get all Elements - * - * @return array - */ - public function getElements() - { - return $this->_elementCollection; - } - - /** - * Get Cell Style - * - * @return \PhpOffice\PhpWord\Style\Cell + * @return CellStyle */ public function getStyle() { - return $this->_style; + return $this->cellStyle; } /** - * Get Cell width + * Get cell width * * @return int */ public function getWidth() { - return $this->_width; + return $this->width; } } diff --git a/src/PhpWord/Element/TextRun.php b/src/PhpWord/Element/TextRun.php index a890bb04..63401d33 100755 --- a/src/PhpWord/Element/TextRun.php +++ b/src/PhpWord/Element/TextRun.php @@ -9,156 +9,44 @@ namespace PhpOffice\PhpWord\Element; -use PhpOffice\PhpWord\Exception\InvalidImageException; -use PhpOffice\PhpWord\Media; -use PhpOffice\PhpWord\Shared\String; -use PhpOffice\PhpWord\Style\Font; +use PhpOffice\PhpWord\Container\Container; use PhpOffice\PhpWord\Style\Paragraph; /** * Textrun/paragraph element */ -class TextRun +class TextRun extends Container { /** * Paragraph style * - * @var Paragraph + * @var string|Paragraph */ - private $_styleParagraph; + private $paragraphStyle; /** - * Text collection + * Create new instance * - * @var array + * @param string|array|Paragraph $paragraphStyle */ - private $_elementCollection; - - - /** - * Create a new TextRun Element - * - * @param mixed $styleParagraph - */ - public function __construct($styleParagraph = null) + public function __construct($paragraphStyle = null) { - $this->_elementCollection = array(); + $this->containerType = 'textrun'; // Set paragraph style - if (is_array($styleParagraph)) { - $this->_styleParagraph = new Paragraph(); - - foreach ($styleParagraph as $key => $value) { + if (is_array($paragraphStyle)) { + $this->paragraphStyle = new Paragraph(); + foreach ($paragraphStyle as $key => $value) { if (substr($key, 0, 1) != '_') { $key = '_' . $key; } - $this->_styleParagraph->setStyleValue($key, $value); + $this->paragraphStyle->setStyleValue($key, $value); } } else { - $this->_styleParagraph = $styleParagraph; + $this->paragraphStyle = $paragraphStyle; } } - - /** - * Add a Text Element - * - * @param string $text - * @param mixed $styleFont - * @return \PhpOffice\PhpWord\Element\Text - */ - public function addText($text = null, $styleFont = null) - { - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } - $text = new Text($text, $styleFont); - $this->_elementCollection[] = $text; - return $text; - } - - /** - * Add a Link Element - * - * @param string $linkSrc - * @param string $linkName - * @param mixed $styleFont - * @return \PhpOffice\PhpWord\Element\Link - */ - public function addLink($linkSrc, $linkName = null, $styleFont = null) - { - $linkSrc = utf8_encode($linkSrc); - if (!is_null($linkName)) { - $linkName = utf8_encode($linkName); - } - - $link = new Link($linkSrc, $linkName, $styleFont); - $rID = Media::addSectionLinkElement($linkSrc); - $link->setRelationId($rID); - - $this->_elementCollection[] = $link; - return $link; - } - - /** - * Add a Image Element - * - * @param string $imageSrc - * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Image - */ - public function addImage($imageSrc, $style = null) - { - $image = new Image($imageSrc, $style); - if (!is_null($image->getSource())) { - $rID = Media::addSectionMediaElement($imageSrc, 'image', $image); - $image->setRelationId($rID); - $this->_elementCollection[] = $image; - return $image; - } else { - throw new InvalidImageException; - } - } - - /** - * Add TextBreak - * - * @param int $count - * @param mixed $fontStyle - * @param mixed $paragraphStyle - */ - public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null) - { - for ($i = 1; $i <= $count; $i++) { - $this->_elementCollection[] = new TextBreak($fontStyle, $paragraphStyle); - } - } - - /** - * Create a new Footnote Element - * - * @param mixed $styleParagraph - * @return \PhpOffice\PhpWord\Element\Footnote - */ - public function createFootnote($styleParagraph = null) - { - $footnote = new \PhpOffice\PhpWord\Element\Footnote($styleParagraph); - $refID = \PhpOffice\PhpWord\Footnote::addFootnoteElement($footnote); - $footnote->setReferenceId($refID); - $this->_elementCollection[] = $footnote; - return $footnote; - } - - /** - * Get TextRun content - * - * @return string - */ - public function getElements() - { - return $this->_elementCollection; - } - /** * Get Paragraph style * @@ -166,6 +54,6 @@ class TextRun */ public function getParagraphStyle() { - return $this->_styleParagraph; + return $this->paragraphStyle; } } diff --git a/tests/PhpWord/Tests/Element/Table/CellTest.php b/tests/PhpWord/Tests/Element/Table/CellTest.php index 661120d4..b8daca61 100644 --- a/tests/PhpWord/Tests/Element/Table/CellTest.php +++ b/tests/PhpWord/Tests/Element/Table/CellTest.php @@ -92,7 +92,7 @@ class CellTest extends \PHPUnit_Framework_TestCase /** * Add link exception - * @expectedException \PhpOffice\PhpWord\Exception\Exception + * @expectedException \BadMethodCallException */ public function testAddLinkException() { @@ -272,7 +272,7 @@ class CellTest extends \PHPUnit_Framework_TestCase /** * Add preserve text exception * - * @expectedException \PhpOffice\PhpWord\Exception\Exception + * @expectedException \BadMethodCallException */ public function testAddPreserveTextException() { From 07be5eaea326a43fe0c68b6231c4a74e9639dd99 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Tue, 1 Apr 2014 15:01:30 +0700 Subject: [PATCH 04/27] Adding more functionalities to containers: - Table: Ability to add footnote in table cell - Footnote: Ability to add image in footnote - ListItem: Ability to add list item in header/footer - CheckBox: Ability to add checkbox in header/footer - Link: Ability to add link in header/footer --- CHANGELOG.md | 9 +- samples/Sample_04_Textrun.php | 2 +- samples/Sample_06_Footnote.php | 19 +- samples/Sample_09_Tables.php | 15 +- samples/Sample_12_HeaderFooter.php | 6 +- src/PhpWord/Container/Container.php | 113 ++++----- src/PhpWord/Element/Footnote.php | 1 - src/PhpWord/Element/Table/Cell.php | 11 +- src/PhpWord/Element/TextRun.php | 7 +- src/PhpWord/Footnote.php | 53 ++-- src/PhpWord/Media.php | 240 +++++++++++------- src/PhpWord/Reader/Word2007.php | 2 +- src/PhpWord/Shared/String.php | 15 ++ src/PhpWord/Shared/XMLWriter.php | 2 + src/PhpWord/Writer/Word2007.php | 94 ++++--- src/PhpWord/Writer/Word2007/Base.php | 10 +- src/PhpWord/Writer/Word2007/Document.php | 22 +- src/PhpWord/Writer/Word2007/DocumentRels.php | 4 +- src/PhpWord/Writer/Word2007/Footer.php | 25 +- src/PhpWord/Writer/Word2007/Footnotes.php | 28 +- src/PhpWord/Writer/Word2007/Header.php | 26 +- .../PhpWord/Tests/Element/Table/CellTest.php | 10 - tests/PhpWord/Tests/FootnoteTest.php | 3 +- tests/PhpWord/Tests/MediaTest.php | 4 +- 24 files changed, 419 insertions(+), 302 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9c5d39a..24f6ac85 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,11 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - Table: Add `exactHeight` to row style to define whether row height should be exact or atLeast - @jcarignan GH-168 - Element: New `CheckBox` element for sections and table cells - @ozilion GH-156 - Settings: Ability to use PCLZip as alternative to ZipArchive - @bskrtich @ivanlanin GH-106 GH-140 GH-185 +- Table: Ability to add footnote in table cell - @ivanlanin GH-187 +- Footnote: Ability to add image in footnote - @ivanlanin GH-187 +- ListItem: Ability to add list item in header/footer - @ivanlanin GH-187 +- CheckBox: Ability to add checkbox in header/footer - @ivanlanin GH-187 +- Link: Ability to add link in header/footer - @ivanlanin GH-187 ### Bugfixes @@ -24,8 +29,8 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - Documentation: Simplify page level docblock - @ivanlanin GH-179 - Writer: Refactor writer classes and make a new Writer abstract class - @ivanlanin GH-160 - Reader: Rename AbstractReader > Reader - @ivanlanin -- General: Refactor folders: Element, Container, and Exception - @ivanlanin -- Container: Create new Container abstract class - @ivanlanin +- General: Refactor folders: Element, Container, and Exception - @ivanlanin GH-187 +- Container: Create new Container abstract class - @ivanlanin GH-187 ## 0.9.1 - 27 Mar 2014 diff --git a/samples/Sample_04_Textrun.php b/samples/Sample_04_Textrun.php index 94bc3d5b..d289457d 100644 --- a/samples/Sample_04_Textrun.php +++ b/samples/Sample_04_Textrun.php @@ -28,7 +28,7 @@ $textrun->addText(' All elements are placed inside a paragraph with the optional $textrun->addText(' Sample Link: '); $textrun->addLink('http://www.google.com', null, 'NLink'); $textrun->addText(' Sample Image: '); -$textrun->addImage('resources/_earth.jpg', array('width'=>18, 'height'=>18)); +$textrun->addImage('resources/_earth.jpg', array('width' => 18, 'height' => 18)); $textrun->addText(' Here is some more text. '); // Save file diff --git a/samples/Sample_06_Footnote.php b/samples/Sample_06_Footnote.php index 081d5918..e35f5d6b 100755 --- a/samples/Sample_06_Footnote.php +++ b/samples/Sample_06_Footnote.php @@ -4,6 +4,7 @@ include_once 'Sample_Header.php'; // New Word Document echo date('H:i:s') , " Create new PhpWord object" , \EOL; $phpWord = new \PhpOffice\PhpWord\PhpWord(); +\PhpOffice\PhpWord\Settings::setCompatibility(false); // New portrait section $section = $phpWord->createSection(); @@ -19,16 +20,16 @@ $textrun = $section->createTextRun('pStyle'); $textrun->addText('This is some lead text in a paragraph with a following footnote. ','pStyle'); $footnote = $textrun->createFootnote(); -$footnote->addText('Just like a textrun a footnote can contain native text and link elements.'); -$footnote->addText(' No break is placed after adding an element.', 'BoldText'); -$footnote->addText(' All elements are placed inside a paragraph.', 'ColoredText'); -$footnote->addText(' The best search engine: '); -$footnote->addLink('http://www.google.com', null, 'NLink'); -$footnote->addText('. Also not bad:'); +$footnote->addText('Just like a textrun, a footnote can contain native texts. '); +$footnote->addText('No break is placed after adding an element. ', 'BoldText'); +$footnote->addText('All elements are placed inside a paragraph. ', 'ColoredText'); $footnote->addTextBreak(); -$footnote->addLink('http://www.bing.com', null, 'NLink'); - -$textrun->addText('The trailing text in the paragraph.'); +$footnote->addText('But you can insert a manual text break like above, '); +$footnote->addText('links like '); +$footnote->addLink('http://www.google.com', null, 'NLink'); +$footnote->addText(', or image like '); +$footnote->addImage('resources/_earth.jpg', array('width' => 18, 'height' => 18)); +$footnote->addText('But you can only put footnote in section, not in header or footer.'); $section->addText('You can also create the footnote directly from the section making it wrap in a paragraph like the footnote below this paragraph. But is is best used from within a textrun.'); $footnote = $section->createFootnote(); diff --git a/samples/Sample_09_Tables.php b/samples/Sample_09_Tables.php index ea10eec9..0c9fbeb5 100644 --- a/samples/Sample_09_Tables.php +++ b/samples/Sample_09_Tables.php @@ -63,10 +63,21 @@ $cellVCentered = array('valign' => 'center'); $phpWord->addTableStyle('Colspan Rowspan', $styleTable); $table = $section->addTable('Colspan Rowspan'); + $table->addRow(); -$table->addCell(2000, $cellRowSpan)->addText('A', null, $cellHCentered); -$table->addCell(4000, $cellColSpan)->addText('B', null, $cellHCentered); + +$cell1 = $table->addCell(2000, $cellRowSpan); +$textrun1 = $cell1->addTextRun($cellHCentered); +$textrun1->addText('A'); +$textrun1->addFootnote()->addText('Row span'); + +$cell2 = $table->addCell(4000, $cellColSpan); +$textrun2 = $cell2->addTextRun($cellHCentered); +$textrun2->addText('B'); +$textrun2->addFootnote()->addText('Colspan span'); + $table->addCell(2000, $cellRowSpan)->addText('E', null, $cellHCentered); + $table->addRow(); $table->addCell(null, $cellRowContinue); $table->addCell(2000, $cellVCentered)->addText('C', null, $cellHCentered); diff --git a/samples/Sample_12_HeaderFooter.php b/samples/Sample_12_HeaderFooter.php index 3c5775ef..19139ab2 100644 --- a/samples/Sample_12_HeaderFooter.php +++ b/samples/Sample_12_HeaderFooter.php @@ -13,7 +13,10 @@ $header = $section->createHeader(); $header->firstPage(); $table = $header->addTable(); $table->addRow(); -$table->addCell(4500)->addText('This is the header.'); +$cell = $table->addCell(4500); +$textrun = $cell->addTextRun(); +$textrun->addText('This is the header with '); +$textrun->addLink('http://google.com', 'link to Google'); $table->addCell(4500)->addImage( 'resources/PhpWord.png', array('width' => 80, 'height' => 80, 'align' => 'right') @@ -26,6 +29,7 @@ $subsequent->addText("Subsequent pages in Section 1 will Have this!"); // Add footer $footer = $section->createFooter(); $footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', array('align' => 'center')); +$footer->addLink('http://google.com', 'Direct Google'); // Write some text $section->addTextBreak(); diff --git a/src/PhpWord/Container/Container.php b/src/PhpWord/Container/Container.php index 379f7b14..a29e393c 100644 --- a/src/PhpWord/Container/Container.php +++ b/src/PhpWord/Container/Container.php @@ -14,7 +14,7 @@ use PhpOffice\PhpWord\Exception\InvalidObjectException; use PhpOffice\PhpWord\Media; use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\TOC; -use PhpOffice\PhpWord\Footnote; +use PhpOffice\PhpWord\Footnote as FootnoteCollection; use PhpOffice\PhpWord\Shared\String; use PhpOffice\PhpWord\Element\Text; use PhpOffice\PhpWord\Element\TextRun; @@ -58,18 +58,22 @@ abstract class Container protected $elements = array(); /** - * Parent container type: section|header|footer + * Document part type: section|header|footer + * + * Used by textrun and cell to determine where the element is located + * because it will affect the availability of other element, e.g. footnote + * will not be available when $docPartType is header or footer. * * @var string */ - protected $parentContainer = null; + protected $docPartType = null; /** - * Parent container Id + * Document part Id * * @var int */ - protected $parentContainerId; + protected $docPartId; /** * Relation Id @@ -91,9 +95,7 @@ abstract class Container if (in_array($this->containerType, array('footnote', 'textrun'))) { $paragraphStyle = null; } - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } + $text = String::toUTF8($text); $element = new Text($text, $fontStyle, $paragraphStyle); $this->elements[] = $element; @@ -125,8 +127,15 @@ abstract class Container if (!in_array($this->containerType, array('section', 'header', 'footer', 'cell'))) { throw new \BadMethodCallException(); } + if ($this->containerType == 'cell') { + $docPartType = $this->docPartType; + $docPartId = $this->docPartId; + } else { + $docPartType = $this->containerType; + $docPartId = $this->sectionId; + } - $textRun = new TextRun($paragraphStyle); + $textRun = new TextRun($paragraphStyle, $docPartType, $docPartId); $this->elements[] = $textRun; return $textRun; @@ -140,30 +149,27 @@ abstract class Container * @param mixed $fontStyle * @param mixed $paragraphStyle * @return Link - * @todo Enable link element in header and footer */ public function addLink($linkSrc, $linkName = null, $fontStyle = null, $paragraphStyle = null) { - if (!in_array($this->containerType, array('section', 'footnote', 'textrun', 'cell'))) { - throw new \BadMethodCallException(); + if (!is_null($this->docPartType)) { + $linkContainer = $this->docPartType; + $linkContainerId = $this->docPartId; + } else { + $linkContainer = $this->containerType; + $linkContainerId = $this->sectionId; } - if ($this->containerType == 'cell' && $this->parentContainer != 'section') { - throw new \BadMethodCallException(); + if ($linkContainer == 'header' || $linkContainer == 'footer') { + $linkContainer .= $linkContainerId; } - if (!String::isUTF8($linkSrc)) { - $linkSrc = utf8_encode($linkSrc); - } - if (!is_null($linkName)) { - if (!String::isUTF8($linkName)) { - $linkName = utf8_encode($linkName); - } - } + $linkSrc = String::toUTF8($linkSrc); + $linkName = String::toUTF8($linkName); $link = new Link($linkSrc, $linkName, $fontStyle, $paragraphStyle); - if ($this->containerType == 'footnote') { - $rID = Footnote::addFootnoteLinkElement($linkSrc); - } else { + if ($linkContainer == 'section') { $rID = Media::addSectionLinkElement($linkSrc); + } else { + $rID = Media::addMediaElement($linkContainer, 'hyperlink', $linkSrc); } $link->setRelationId($rID); $this->elements[] = $link; @@ -181,13 +187,11 @@ abstract class Container */ public function addTitle($text, $depth = 1) { - if ($this->containerType != 'section') { + if (!in_array($this->containerType, array('section'))) { throw new \BadMethodCallException(); } - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } + $text = String::toUTF8($text); $styles = Style::getStyles(); if (array_key_exists('Heading_' . $depth, $styles)) { $style = 'Heading' . $depth; @@ -218,13 +222,11 @@ abstract class Container if (!in_array($this->containerType, array('header', 'footer', 'cell'))) { throw new \BadMethodCallException(); } - if ($this->containerType == 'cell' && $this->parentContainer == 'section') { + if ($this->containerType == 'cell' && $this->docPartType == 'section') { throw new \BadMethodCallException(); } - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } + $text = String::toUTF8($text); $ptext = new PreserveText($text, $fontStyle, $paragraphStyle); $this->elements[] = $ptext; @@ -244,16 +246,11 @@ abstract class Container */ public function addListItem($text, $depth = 0, $fontStyle = null, $styleList = null, $paragraphStyle = null) { - if (!in_array($this->containerType, array('section', 'cell'))) { - throw new \BadMethodCallException(); - } - if ($this->containerType == 'cell' && $this->parentContainer != 'section') { + if (!in_array($this->containerType, array('section', 'header', 'footer', 'cell'))) { throw new \BadMethodCallException(); } - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } + $text = String::toUTF8($text); $listItem = new ListItem($text, $depth, $fontStyle, $styleList, $paragraphStyle); $this->elements[] = $listItem; @@ -288,12 +285,9 @@ abstract class Container */ public function addImage($src, $style = null, $isWatermark = false) { - if ($this->containerType == 'footnote') { - throw new \BadMethodCallException(); - } - if (!is_null($this->parentContainer)) { - $imageContainerType = $this->parentContainer; - $imageContainerId = $this->parentContainerId; + if ($this->containerType == 'cell') { + $imageContainerType = $this->docPartType; + $imageContainerId = $this->docPartId; } else { $imageContainerType = $this->containerType; $imageContainerId = $this->sectionId; @@ -301,6 +295,7 @@ abstract class Container $image = new Image($src, $style, $isWatermark); if (!is_null($image->getSource())) { + $rID = null; switch ($imageContainerType) { case 'textrun': case 'section': @@ -312,6 +307,9 @@ abstract class Container case 'footer': $rID = Media::addFooterMediaElement($imageContainerId, $src, $image); break; + case 'footnote': + $rID = Media::addMediaElement('footnotes', 'image', $src, $image); + break; } $image->setRelationId($rID); $this->elements[] = $image; @@ -336,7 +334,7 @@ abstract class Container if (!in_array($this->containerType, array('section', 'cell'))) { throw new \BadMethodCallException(); } - if ($this->containerType == 'cell' && $this->parentContainer != 'section') { + if ($this->containerType == 'cell' && $this->docPartType != 'section') { throw new \BadMethodCallException(); } @@ -367,18 +365,21 @@ abstract class Container * * @param mixed $paragraphStyle * @return FootnoteElement - * @todo Enable footnote element in header and footer */ public function addFootnote($paragraphStyle = null) { - if (!in_array($this->containerType, array('section', 'textrun'))) { + if (!in_array($this->containerType, array('section', 'textrun', 'cell'))) { + throw new \BadMethodCallException(); + } + if (!is_null($this->docPartType) && $this->docPartType != 'section') { throw new \BadMethodCallException(); } $footnote = new FootnoteElement($paragraphStyle); - $refID = Footnote::addFootnoteElement($footnote); + $refID = FootnoteCollection::addFootnoteElement($footnote); $footnote->setReferenceId($refID); $this->elements[] = $footnote; + return $footnote; } @@ -394,19 +395,15 @@ abstract class Container */ public function addCheckBox($name, $text, $fontStyle = null, $paragraphStyle = null) { - if (!in_array($this->containerType, array('section', 'cell'))) { + if (!in_array($this->containerType, array('section', 'header', 'footer', 'cell'))) { throw new \BadMethodCallException(); } - if ($this->containerType == 'cell' && $this->parentContainer != 'section') { + if ($this->containerType == 'cell' && $this->docPartType != 'section') { throw new \BadMethodCallException(); } - if (!String::isUTF8($name)) { - $name = utf8_encode($name); - } - if (!String::isUTF8($text)) { - $text = utf8_encode($text); - } + $name = String::toUTF8($name); + $text = String::toUTF8($text); $element = new CheckBox($name, $text, $fontStyle, $paragraphStyle); $this->elements[] = $element; diff --git a/src/PhpWord/Element/Footnote.php b/src/PhpWord/Element/Footnote.php index a683b3b0..aa6af010 100644 --- a/src/PhpWord/Element/Footnote.php +++ b/src/PhpWord/Element/Footnote.php @@ -39,7 +39,6 @@ class Footnote extends Container public function __construct($paragraphStyle = null) { $this->containerType = 'footnote'; - // Set paragraph style if (is_array($paragraphStyle)) { $this->paragraphStyle = new Paragraph(); diff --git a/src/PhpWord/Element/Table/Cell.php b/src/PhpWord/Element/Table/Cell.php index 0a14400c..3f795a28 100755 --- a/src/PhpWord/Element/Table/Cell.php +++ b/src/PhpWord/Element/Table/Cell.php @@ -34,17 +34,16 @@ class Cell extends Container /** * Create new instance * - * @param string $parentType section|header|footer - * @param int $parentId + * @param string $docPartType section|header|footer + * @param int $docPartId * @param int $width * @param array|CellStyle $style */ - public function __construct($parentType, $parentId, $width = null, $style = null) + public function __construct($docPartType, $docPartId, $width = null, $style = null) { $this->containerType = 'cell'; - - $this->parentContainer = $parentType; - $this->parentContainerId = $parentId; + $this->docPartType = $docPartType; + $this->docPartId = $docPartId; $this->width = $width; $this->cellStyle = new CellStyle(); diff --git a/src/PhpWord/Element/TextRun.php b/src/PhpWord/Element/TextRun.php index 63401d33..35701d45 100755 --- a/src/PhpWord/Element/TextRun.php +++ b/src/PhpWord/Element/TextRun.php @@ -28,11 +28,14 @@ class TextRun extends Container * Create new instance * * @param string|array|Paragraph $paragraphStyle + * @param string $docPartType section|header|footer + * @param int $docPartId */ - public function __construct($paragraphStyle = null) + public function __construct($paragraphStyle = null, $docPartType = 'section', $docPartId = 1) { $this->containerType = 'textrun'; - + $this->docPartType = $docPartType; + $this->docPartId = $docPartId; // Set paragraph style if (is_array($paragraphStyle)) { $this->paragraphStyle = new Paragraph(); diff --git a/src/PhpWord/Footnote.php b/src/PhpWord/Footnote.php index af104bc8..2ec3d0b3 100644 --- a/src/PhpWord/Footnote.php +++ b/src/PhpWord/Footnote.php @@ -9,35 +9,32 @@ namespace PhpOffice\PhpWord; +use PhpOffice\PhpWord\Media; +use PhpOffice\PhpWord\Element\Footnote as FootnoteElement; + /** * Footnote */ class Footnote { /** - * Footnote Elements + * Footnote elements * * @var array */ - private static $_footnoteCollection = array(); + private static $elements = array(); /** - * Footnote Link Elements - * - * @var array - */ - private static $_footnoteLink = array(); - - /** - * Add new Footnote Element + * Add new footnote * + * @param FootnoteElement $footnote * @return int Reference ID */ - public static function addFootnoteElement(\PhpOffice\PhpWord\Element\Footnote $footnote) + public static function addFootnoteElement(FootnoteElement $footnote) { - $refID = self::countFootnoteElements() + 2; + $refID = self::countFootnoteElements() + 1; - self::$_footnoteCollection[] = $footnote; + self::$elements[] = $footnote; return $refID; } @@ -49,7 +46,7 @@ class Footnote */ public static function getFootnoteElements() { - return self::$_footnoteCollection; + return self::$elements; } /** @@ -59,47 +56,29 @@ class Footnote */ public static function countFootnoteElements() { - return count(self::$_footnoteCollection); + return count(self::$elements); } /** * Add new Footnote Link Element * * @param string $linkSrc - * * @return int Reference ID + * @deprecated 0.9.2 */ public static function addFootnoteLinkElement($linkSrc) { - $rID = self::countFootnoteLinkElements() + 1; - - $link = array(); - $link['target'] = $linkSrc; - $link['rID'] = $rID; - $link['type'] = 'hyperlink'; - - self::$_footnoteLink[] = $link; - - return $rID; + return Media::addMediaElement('footnotes', 'hyperlink', $linkSrc); } /** * Get Footnote Link Elements * * @return array + * @deprecated 0.9.2 */ public static function getFootnoteLinkElements() { - return self::$_footnoteLink; - } - - /** - * Get Footnote Link Elements Count - * - * @return int - */ - public static function countFootnoteLinkElements() - { - return count(self::$_footnoteLink); + return Media::getMediaElements('footnotes', 'hyperlink'); } } diff --git a/src/PhpWord/Media.php b/src/PhpWord/Media.php index 35800f92..c8b33301 100755 --- a/src/PhpWord/Media.php +++ b/src/PhpWord/Media.php @@ -21,7 +21,7 @@ class Media * * @var array */ - private static $_sectionMedia = array( + private static $sectionMedia = array( 'images' => array(), 'embeddings' => array(), 'links' => array() @@ -32,35 +32,42 @@ class Media * * @var array */ - private static $_headerMedia = array(); + private static $headerMedia = array(); /** * Footer Media Elements * * @var array */ - private static $_footerMedia = array(); + private static $footerMedia = array(); + + /** + * Media elements + * + * @var array + */ + private static $media = array(); /** * ObjectID Counter * * @var int */ - private static $_objectId = 1325353440; + private static $objectId = 1325353440; /** * Add new Section Media Element * * @param string $src * @param string $type - * @param \PhpOffice\PhpWord\Element\Image $image + * @param Image $image * @return mixed */ public static function addSectionMediaElement($src, $type, Image $image = null) { $mediaId = md5($src); $key = ($type === 'image') ? 'images' : 'embeddings'; - if (!array_key_exists($mediaId, self::$_sectionMedia[$key])) { + if (!array_key_exists($mediaId, self::$sectionMedia[$key])) { $cImg = self::countSectionMediaElements('images'); $cObj = self::countSectionMediaElements('embeddings'); $rID = self::countSectionMediaElements() + 7; @@ -90,17 +97,17 @@ class Media $media['target'] = "$folder/section_$file"; $media['type'] = $type; $media['rID'] = $rID; - self::$_sectionMedia[$key][$mediaId] = $media; + self::$sectionMedia[$key][$mediaId] = $media; if ($type === 'oleObject') { - return array($rID, ++self::$_objectId); + return array($rID, ++self::$objectId); } return $rID; } else { if ($type === 'oleObject') { - $rID = self::$_sectionMedia[$key][$mediaId]['rID']; - return array($rID, ++self::$_objectId); + $rID = self::$sectionMedia[$key][$mediaId]['rID']; + return array($rID, ++self::$objectId); } - return self::$_sectionMedia[$key][$mediaId]['rID']; + return self::$sectionMedia[$key][$mediaId]['rID']; } } @@ -119,7 +126,7 @@ class Media $link['rID'] = $rID; $link['type'] = 'hyperlink'; - self::$_sectionMedia['links'][] = $link; + self::$sectionMedia['links'][] = $link; return $rID; } @@ -133,12 +140,12 @@ class Media public static function getSectionMediaElements($key = null) { if (!is_null($key)) { - return self::$_sectionMedia[$key]; + return self::$sectionMedia[$key]; } - $arrImages = self::$_sectionMedia['images']; - $arrObjects = self::$_sectionMedia['embeddings']; - $arrLinks = self::$_sectionMedia['links']; + $arrImages = self::$sectionMedia['images']; + $arrObjects = self::$sectionMedia['embeddings']; + $arrLinks = self::$sectionMedia['links']; return array_merge($arrImages, $arrObjects, $arrLinks); } @@ -151,12 +158,12 @@ class Media public static function countSectionMediaElements($key = null) { if (!is_null($key)) { - return count(self::$_sectionMedia[$key]); + return count(self::$sectionMedia[$key]); } - $cImages = count(self::$_sectionMedia['images']); - $cObjects = count(self::$_sectionMedia['embeddings']); - $cLinks = count(self::$_sectionMedia['links']); + $cImages = count(self::$sectionMedia['images']); + $cObjects = count(self::$sectionMedia['embeddings']); + $cLinks = count(self::$sectionMedia['links']); return ($cImages + $cObjects + $cLinks); } @@ -165,41 +172,12 @@ class Media * * @param int $headerCount * @param string $src - * @param \PhpOffice\PhpWord\Element\Image $image + * @param Image $image * @return int */ public static function addHeaderMediaElement($headerCount, $src, Image $image = null) { - $mediaId = md5($src); - $key = 'header' . $headerCount; - if (!array_key_exists($key, self::$_headerMedia)) { - self::$_headerMedia[$key] = array(); - } - if (!array_key_exists($mediaId, self::$_headerMedia[$key])) { - $cImg = self::countHeaderMediaElements($key); - $rID = $cImg + 1; - $cImg++; - $media = array(); - $isMemImage = false; - if (!is_null($image)) { - $isMemImage = $image->getIsMemImage(); - $extension = $image->getImageExtension(); - } - if ($isMemImage) { - $media['isMemImage'] = true; - $media['createfunction'] = $image->getImageCreateFunction(); - $media['imagefunction'] = $image->getImageFunction(); - } - $file = 'image' . $cImg . '.' . strtolower($extension); - $media['source'] = $src; - $media['target'] = 'media/' . $key . '_' . $file; - $media['type'] = 'image'; - $media['rID'] = $rID; - self::$_headerMedia[$key][$mediaId] = $media; - return $rID; - } else { - return self::$_headerMedia[$key][$mediaId]['rID']; - } + return self::addMediaElement("header{$headerCount}", 'image', $src, $image); } /** @@ -210,17 +188,26 @@ class Media */ public static function countHeaderMediaElements($key) { - return count(self::$_headerMedia[$key]); + return self::countMediaElements($key); } /** * Get Header Media Elements * - * @return int + * @return array */ - public static function getHeaderMediaElements() + public static function getHeaderMediaElements($prefix = 'header') { - return self::$_headerMedia; + $mediaCollection = array(); + if (!empty(self::$media)) { + foreach (self::$media as $key => $val) { + if (substr($key, 0, 6) == $prefix) { + $mediaCollection[$key] = $val; + } + } + } + + return $mediaCollection; } /** @@ -228,41 +215,12 @@ class Media * * @param int $footerCount * @param string $src - * @param \PhpOffice\PhpWord\Element\Image $image + * @param Image $image * @return int */ public static function addFooterMediaElement($footerCount, $src, Image $image = null) { - $mediaId = md5($src); - $key = 'footer' . $footerCount; - if (!array_key_exists($key, self::$_footerMedia)) { - self::$_footerMedia[$key] = array(); - } - if (!array_key_exists($mediaId, self::$_footerMedia[$key])) { - $cImg = self::countFooterMediaElements($key); - $rID = $cImg + 1; - $cImg++; - $media = array(); - $isMemImage = false; - if (!is_null($image)) { - $isMemImage = $image->getIsMemImage(); - $extension = $image->getImageExtension(); - } - if ($isMemImage) { - $media['isMemImage'] = true; - $media['createfunction'] = $image->getImageCreateFunction(); - $media['imagefunction'] = $image->getImageFunction(); - } - $file = 'image' . $cImg . '.' . strtolower($extension); - $media['source'] = $src; - $media['target'] = 'media/' . $key . '_' . $file; - $media['type'] = 'image'; - $media['rID'] = $rID; - self::$_footerMedia[$key][$mediaId] = $media; - return $rID; - } else { - return self::$_footerMedia[$key][$mediaId]['rID']; - } + return self::addMediaElement("footer{$footerCount}", 'image', $src, $image); } /** @@ -273,16 +231,120 @@ class Media */ public static function countFooterMediaElements($key) { - return count(self::$_footerMedia[$key]); + return self::countMediaElements($key); } /** * Get Footer Media Elements * - * @return int + * @return array */ public static function getFooterMediaElements() { - return self::$_footerMedia; + return self::getHeaderMediaElements('footer'); + } + + /** + * Add new media element + * + * @param string $container section|header|footer|footnotes + * @param string $mediaType image|embedding|hyperlink + * @param string $source + * @param Image $image + * @return int + */ + public static function addMediaElement($container, $mediaType, $source, Image $image = null) + { + // Assign media Id and initiate media container if none exists + $mediaId = md5($source); + if (!array_key_exists($container, self::$media)) { + self::$media[$container]= array(); + } + + // Add media if not exists or point to existing media + if (!array_key_exists($mediaId, self::$media[$container])) { + $mediaCount = self::countMediaElements($container); + $mediaTypeCount = self::countMediaElements($container, $mediaType); + $mediaData = array(); + $relId = $mediaCount + 1; + $mediaTypeCount++; + if ($mediaType == 'image') { + $isMemImage = false; + if (!is_null($image)) { + $isMemImage = $image->getIsMemImage(); + $extension = $image->getImageExtension(); + } + if ($isMemImage) { + $mediaData['isMemImage'] = true; + $mediaData['createfunction'] = $image->getImageCreateFunction(); + $mediaData['imagefunction'] = $image->getImageFunction(); + } + $file = 'image' . $mediaTypeCount . '.' . strtolower($extension); + if ($container != 'footnotes') { + $file = $container . '_' . $file; + } + $target = 'media/' . $file; + } elseif ($mediaType == 'hyperlink') { + $target = $source; + } + $mediaData['source'] = $source; + $mediaData['target'] = $target; + $mediaData['type'] = $mediaType; + $mediaData['rID'] = $relId; + self::$media[$container][$mediaId] = $mediaData; + + return $relId; + } else { + return self::$media[$container][$mediaId]['rID']; + } + } + + /** + * Get media elements count + * + * @param string $container + * @param string $mediaType + * @return int + */ + public static function countMediaElements($container, $mediaType = null) + { + $mediaCount = 0; + foreach (self::$media[$container] as $mediaKey => $mediaData) { + if (!is_null($mediaType)) { + if ($mediaType == $mediaData['type']) { + $mediaCount++; + } + } else { + $mediaCount++; + } + } + + return $mediaCount; + } + + /** + * Get media elements + * + * @param string $container + * @return int + */ + public static function getMediaElements($container, $mediaType = null) + { + if (!array_key_exists($container, self::$media)) { + return false; + } + + $mediaElements = array(); + foreach (self::$media[$container] as $mediaKey => $mediaData) { + if (!is_null($mediaType)) { + if ($mediaType == $mediaData['type']) { + $mediaElements[$mediaKey] = $mediaData; + } + } else { + $mediaElements[$mediaKey] = $mediaData; + } + } + + return $mediaElements; } } diff --git a/src/PhpWord/Reader/Word2007.php b/src/PhpWord/Reader/Word2007.php index 12cf4e6b..36ff79e3 100644 --- a/src/PhpWord/Reader/Word2007.php +++ b/src/PhpWord/Reader/Word2007.php @@ -194,7 +194,7 @@ class Word2007 extends Reader implements IReader ); // w:r more than 1? It's a textrun } else { - $textRun = $section->createTextRun(); + $textRun = $section->addTextRun(); foreach ($elm->r as $r) { $textRun->addText( $r->t, diff --git a/src/PhpWord/Shared/String.php b/src/PhpWord/Shared/String.php index 262d3e42..9d298a66 100644 --- a/src/PhpWord/Shared/String.php +++ b/src/PhpWord/Shared/String.php @@ -91,4 +91,19 @@ class String { return $value === '' || preg_match('/^./su', $value) === 1; } + + /** + * Return UTF8 encoded value + * + * @param string $value + * @return string + */ + public static function toUTF8($value = '') + { + if (!is_null($value) && !self::isUTF8($value)) { + $value = utf8_encode($value); + } + + return $value; + } } diff --git a/src/PhpWord/Shared/XMLWriter.php b/src/PhpWord/Shared/XMLWriter.php index e27013d5..16bb8334 100644 --- a/src/PhpWord/Shared/XMLWriter.php +++ b/src/PhpWord/Shared/XMLWriter.php @@ -24,6 +24,8 @@ if (!defined('DATE_W3C')) { * @method bool startElement(string $name) * @method bool writeAttribute(string $name, string $value) * @method bool endElement() + * @method bool startDocument(string $version = 1.0, string $encoding = null, string $standalone = null) + * @method bool text(string $content) */ class XMLWriter { diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index 0781e2da..dd144da1 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -102,6 +102,7 @@ class Word2007 extends Writer implements IWriter } } + // Add section elements $sectionElements = array(); $_secElements = Media::getSectionMediaElements(); foreach ($_secElements as $element) { // loop through section media elements @@ -111,67 +112,94 @@ class Word2007 extends Writer implements IWriter $sectionElements[] = $element; } - $_hdrElements = Media::getHeaderMediaElements(); - foreach ($_hdrElements as $_headerFile => $_hdrMedia) { // loop through headers - if (count($_hdrMedia) > 0) { - $objZip->addFromString('word/_rels/' . $_headerFile . '.xml.rels', $this->getWriterPart('documentrels')->writeHeaderFooterRels($_hdrMedia)); - foreach ($_hdrMedia as $element) { // loop through header media elements - $this->addFileToPackage($objZip, $element); + // Add header relations & elements + $hdrElements = Media::getHeaderMediaElements(); + foreach ($hdrElements as $hdrFile => $hdrMedia) { + if (count($hdrMedia) > 0) { + $objZip->addFromString( + 'word/_rels/' . $hdrFile . '.xml.rels', + $this->getWriterPart('documentrels')->writeHeaderFooterRels($hdrMedia) + ); + foreach ($hdrMedia as $element) { + if ($element['type'] == 'image') { + $this->addFileToPackage($objZip, $element); + } } } } - $_ftrElements = Media::getFooterMediaElements(); - foreach ($_ftrElements as $_footerFile => $_ftrMedia) { // loop through footers - if (count($_ftrMedia) > 0) { - $objZip->addFromString('word/_rels/' . $_footerFile . '.xml.rels', $this->getWriterPart('documentrels')->writeHeaderFooterRels($_ftrMedia)); - foreach ($_ftrMedia as $element) { // loop through footers media elements - $this->addFileToPackage($objZip, $element); + // Add footer relations & elements + $ftrElements = Media::getFooterMediaElements(); + foreach ($ftrElements as $ftrFile => $ftrMedia) { + if (count($ftrMedia) > 0) { + $objZip->addFromString( + 'word/_rels/' . $ftrFile . '.xml.rels', + $this->getWriterPart('documentrels')->writeHeaderFooterRels($ftrMedia) + ); + foreach ($ftrMedia as $element) { + if ($element['type'] == 'image') { + $this->addFileToPackage($objZip, $element); + } } } } - $footnoteLinks = array(); - $_footnoteElements = Footnote::getFootnoteLinkElements(); - // loop through footnote link elements - foreach ($_footnoteElements as $element) { - $footnoteLinks[] = $element; - } - + // Process header/footer xml files $_cHdrs = 0; $_cFtrs = 0; $rID = Media::countSectionMediaElements() + 6; $_sections = $this->phpWord->getSections(); - $footers = array(); foreach ($_sections as $section) { $_headers = $section->getHeaders(); foreach ($_headers as $index => &$_header) { $_cHdrs++; $_header->setRelationId(++$rID); - $_headerFile = 'header' . $_cHdrs . '.xml'; - $sectionElements[] = array('target' => $_headerFile, 'type' => 'header', 'rID' => $rID); - $objZip->addFromString('word/' . $_headerFile, $this->getWriterPart('header')->writeHeader($_header)); + $hdrFile = 'header' . $_cHdrs . '.xml'; + $sectionElements[] = array('target' => $hdrFile, 'type' => 'header', 'rID' => $rID); + $objZip->addFromString( + 'word/' . $hdrFile, + $this->getWriterPart('header')->writeHeader($_header) + ); } - $_footer = $section->getFooter(); $footers[++$_cFtrs] = $_footer; if (!is_null($_footer)) { $_footer->setRelationId(++$rID); $_footerCount = $_footer->getSectionId(); - $_footerFile = 'footer' . $_footerCount . '.xml'; - $sectionElements[] = array('target' => $_footerFile, 'type' => 'footer', 'rID' => $rID); - $objZip->addFromString('word/' . $_footerFile, $this->getWriterPart('footer')->writeFooter($_footer)); + $ftrFile = 'footer' . $_footerCount . '.xml'; + $sectionElements[] = array('target' => $ftrFile, 'type' => 'footer', 'rID' => $rID); + $objZip->addFromString( + 'word/' . $ftrFile, + $this->getWriterPart('footer')->writeFooter($_footer) + ); } } + // Process footnotes if (Footnote::countFootnoteElements() > 0) { - $_allFootnotesCollection = Footnote::getFootnoteElements(); - $_footnoteFile = 'footnotes.xml'; - $sectionElements[] = array('target'=>$_footnoteFile, 'type'=>'footnotes', 'rID'=>++$rID); - $objZip->addFromString('word/'.$_footnoteFile, $this->getWriterPart('footnotes')->writeFootnotes($_allFootnotesCollection)); - if (count($footnoteLinks) > 0) { - $objZip->addFromString('word/_rels/footnotes.xml.rels', $this->getWriterPart('footnotesrels')->writeFootnotesRels($footnoteLinks)); + // Push to document.xml.rels + $sectionElements[] = array('target' => 'footnotes.xml', 'type' => 'footnotes', 'rID' => ++$rID); + // Add footnote media to package + $footnotesMedia = Media::getMediaElements('footnotes'); + if (!empty($footnotesMedia)) { + foreach ($footnotesMedia as $media) { + if ($media['type'] == 'image') { + $this->addFileToPackage($objZip, $media); + } + } + } + // Write footnotes.xml + $objZip->addFromString( + "word/footnotes.xml", + $this->getWriterPart('footnotes')->writeFootnotes(Footnote::getFootnoteElements()) + ); + // Write footnotes.xml.rels + if (!empty($footnotesMedia)) { + $objZip->addFromString( + 'word/_rels/footnotes.xml.rels', + $this->getWriterPart('footnotesrels')->writeFootnotesRels($footnotesMedia) + ); } } diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index b6e4c32c..7af96b03 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -90,12 +90,12 @@ class Base extends WriterPart $this->writeText($xmlWriter, $element, true); } elseif ($element instanceof Link) { $this->writeLink($xmlWriter, $element, true); + } elseif ($element instanceof TextBreak) { + $xmlWriter->writeElement('w:br'); } elseif ($element instanceof Image) { $this->writeImage($xmlWriter, $element, true); } elseif ($element instanceof Footnote) { $this->writeFootnote($xmlWriter, $element, true); - } elseif ($element instanceof TextBreak) { - $xmlWriter->writeElement('w:br'); } } } @@ -434,6 +434,8 @@ class Base extends WriterPart $this->writeTextRun($xmlWriter, $element); } elseif ($element instanceof Link) { $this->writeLink($xmlWriter, $element); + } elseif ($element instanceof PreserveText) { + $this->writePreserveText($xmlWriter, $element); } elseif ($element instanceof TextBreak) { $this->writeTextBreak($xmlWriter, $element); } elseif ($element instanceof ListItem) { @@ -442,8 +444,6 @@ class Base extends WriterPart $this->writeImage($xmlWriter, $element); } elseif ($element instanceof Object) { $this->writeObject($xmlWriter, $element); - } elseif ($element instanceof PreserveText) { - $this->writePreserveText($xmlWriter, $element); } elseif ($element instanceof CheckBox) { $this->writeCheckBox($xmlWriter, $element); } @@ -493,7 +493,6 @@ class Base extends WriterPart $xmlWriter->endElement(); } } - $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:pict'); @@ -995,7 +994,6 @@ class Base extends WriterPart if ($margins) { $xmlWriter->startElement('w:tblCellMar'); if ($mTop) { - echo $margins[0]; $xmlWriter->startElement('w:top'); $xmlWriter->writeAttribute('w:w', $cellMargin[0]); $xmlWriter->writeAttribute('w:type', 'dxa'); diff --git a/src/PhpWord/Writer/Word2007/Document.php b/src/PhpWord/Writer/Word2007/Document.php index c4f8278f..b6e63628 100644 --- a/src/PhpWord/Writer/Word2007/Document.php +++ b/src/PhpWord/Writer/Word2007/Document.php @@ -10,23 +10,23 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\TOC; use PhpOffice\PhpWord\Container\Section; -use PhpOffice\PhpWord\Element\Footnote; -use PhpOffice\PhpWord\Element\Image; -use PhpOffice\PhpWord\Element\Link; -use PhpOffice\PhpWord\Element\ListItem; -use PhpOffice\PhpWord\Element\Object; -use PhpOffice\PhpWord\Element\PageBreak; -use PhpOffice\PhpWord\Element\Table; use PhpOffice\PhpWord\Element\Text; -use PhpOffice\PhpWord\Element\TextBreak; use PhpOffice\PhpWord\Element\TextRun; +use PhpOffice\PhpWord\Element\Link; use PhpOffice\PhpWord\Element\Title; +use PhpOffice\PhpWord\Element\TextBreak; +use PhpOffice\PhpWord\Element\PageBreak; +use PhpOffice\PhpWord\Element\ListItem; +use PhpOffice\PhpWord\Element\Table; +use PhpOffice\PhpWord\Element\Image; +use PhpOffice\PhpWord\Element\Object; +use PhpOffice\PhpWord\Element\Footnote; use PhpOffice\PhpWord\Element\CheckBox; use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; -use PhpOffice\PhpWord\TOC; /** * Word2007 document part writer @@ -83,10 +83,10 @@ class Document extends Base $this->writeTextBreak($xmlWriter, $element); } elseif ($element instanceof PageBreak) { $this->writePageBreak($xmlWriter); - } elseif ($element instanceof Table) { - $this->writeTable($xmlWriter, $element); } elseif ($element instanceof ListItem) { $this->writeListItem($xmlWriter, $element); + } elseif ($element instanceof Table) { + $this->writeTable($xmlWriter, $element); } elseif ($element instanceof Image) { $this->writeImage($xmlWriter, $element); } elseif ($element instanceof Object) { diff --git a/src/PhpWord/Writer/Word2007/DocumentRels.php b/src/PhpWord/Writer/Word2007/DocumentRels.php index 648d0864..0650d46a 100755 --- a/src/PhpWord/Writer/Word2007/DocumentRels.php +++ b/src/PhpWord/Writer/Word2007/DocumentRels.php @@ -127,12 +127,14 @@ class DocumentRels extends Base $relationType = $relation['type']; $relationName = $relation['target']; $relationId = $relation['rID']; + $targetMode = ($relationType == 'hyperlink') ? 'External' : ''; $this->writeRelationship( $xmlWriter, $relationId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/' . $relationType, - $relationName + $relationName, + $targetMode ); } diff --git a/src/PhpWord/Writer/Word2007/Footer.php b/src/PhpWord/Writer/Word2007/Footer.php index de2bf44d..cbcbcb26 100644 --- a/src/PhpWord/Writer/Word2007/Footer.php +++ b/src/PhpWord/Writer/Word2007/Footer.php @@ -9,13 +9,16 @@ namespace PhpOffice\PhpWord\Writer\Word2007; -use PhpOffice\PhpWord\Element\PreserveText; -use PhpOffice\PhpWord\Element\Image; -use PhpOffice\PhpWord\Element\Table; -use PhpOffice\PhpWord\Element\Text; -use PhpOffice\PhpWord\Element\TextBreak; -use PhpOffice\PhpWord\Element\TextRun; use PhpOffice\PhpWord\Container\Footer as FooterElement; +use PhpOffice\PhpWord\Element\Text; +use PhpOffice\PhpWord\Element\TextRun; +use PhpOffice\PhpWord\Element\Link; +use PhpOffice\PhpWord\Element\PreserveText; +use PhpOffice\PhpWord\Element\TextBreak; +use PhpOffice\PhpWord\Element\ListItem; +use PhpOffice\PhpWord\Element\Table; +use PhpOffice\PhpWord\Element\Image; +use PhpOffice\PhpWord\Element\CheckBox; use PhpOffice\PhpWord\Shared\XMLWriter; /** @@ -54,14 +57,20 @@ class Footer extends Base $this->writeText($xmlWriter, $element); } elseif ($element instanceof TextRun) { $this->writeTextRun($xmlWriter, $element); + } elseif ($element instanceof Link) { + $this->writeLink($xmlWriter, $element); + } elseif ($element instanceof PreserveText) { + $this->writePreserveText($xmlWriter, $element); } elseif ($element instanceof TextBreak) { $this->writeTextBreak($xmlWriter, $element); + } elseif ($element instanceof ListItem) { + $this->writeListItem($xmlWriter, $element); } elseif ($element instanceof Table) { $this->writeTable($xmlWriter, $element); } elseif ($element instanceof Image) { $this->writeImage($xmlWriter, $element); - } elseif ($element instanceof PreserveText) { - $this->writePreserveText($xmlWriter, $element); + } elseif ($element instanceof CheckBox) { + $this->writeCheckBox($xmlWriter, $element); } } diff --git a/src/PhpWord/Writer/Word2007/Footnotes.php b/src/PhpWord/Writer/Word2007/Footnotes.php index d8ba4352..628c1e35 100644 --- a/src/PhpWord/Writer/Word2007/Footnotes.php +++ b/src/PhpWord/Writer/Word2007/Footnotes.php @@ -12,6 +12,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\Element\Footnote; use PhpOffice\PhpWord\Element\Text; use PhpOffice\PhpWord\Element\Link; +use PhpOffice\PhpWord\Element\Image; use PhpOffice\PhpWord\Element\TextBreak; use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Shared\XMLWriter; @@ -34,17 +35,20 @@ class Footnotes extends Base // XML header $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); $xmlWriter->startElement('w:footnotes'); - $xmlWriter->writeAttribute( - 'xmlns:r', - 'http://schemas.openxmlformats.org/officeDocument/2006/relationships' - ); - $xmlWriter->writeAttribute( - 'xmlns:w', - 'http://schemas.openxmlformats.org/wordprocessingml/2006/main' - ); + + $xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006'); + $xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office'); + $xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'); + $xmlWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math'); + $xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml'); + $xmlWriter->writeAttribute('xmlns:wp', 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing'); + $xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word'); + $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); + $xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml'); + // Separator and continuation separator $xmlWriter->startElement('w:footnote'); - $xmlWriter->writeAttribute('w:id', 0); + $xmlWriter->writeAttribute('w:id', -1); $xmlWriter->writeAttribute('w:type', 'separator'); $xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:r'); @@ -53,9 +57,8 @@ class Footnotes extends Base $xmlWriter->endElement(); // w:r $xmlWriter->endElement(); // w:p $xmlWriter->endElement(); // w:footnote - // Content $xmlWriter->startElement('w:footnote'); - $xmlWriter->writeAttribute('w:id', 1); + $xmlWriter->writeAttribute('w:id', 0); $xmlWriter->writeAttribute('w:type', 'continuationSeparator'); $xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:r'); @@ -64,6 +67,7 @@ class Footnotes extends Base $xmlWriter->endElement(); // w:r $xmlWriter->endElement(); // w:p $xmlWriter->endElement(); // w:footnote + // Content foreach ($allFootnotesCollection as $footnote) { if ($footnote instanceof Footnote) { $this->writeFootnote($xmlWriter, $footnote); @@ -113,6 +117,8 @@ class Footnotes extends Base $this->writeText($xmlWriter, $element, true); } elseif ($element instanceof Link) { $this->writeLink($xmlWriter, $element, true); + } elseif ($element instanceof Image) { + $this->writeImage($xmlWriter, $element, true); } elseif ($element instanceof TextBreak) { $xmlWriter->writeElement('w:br'); } diff --git a/src/PhpWord/Writer/Word2007/Header.php b/src/PhpWord/Writer/Word2007/Header.php index 07a16ec7..7bd95ed6 100644 --- a/src/PhpWord/Writer/Word2007/Header.php +++ b/src/PhpWord/Writer/Word2007/Header.php @@ -9,13 +9,16 @@ namespace PhpOffice\PhpWord\Writer\Word2007; -use PhpOffice\PhpWord\Element\PreserveText; -use PhpOffice\PhpWord\Element\Image; -use PhpOffice\PhpWord\Element\Table; -use PhpOffice\PhpWord\Element\Text; -use PhpOffice\PhpWord\Element\TextBreak; -use PhpOffice\PhpWord\Element\TextRun; use PhpOffice\PhpWord\Container\Header as HeaderElement; +use PhpOffice\PhpWord\Element\Text; +use PhpOffice\PhpWord\Element\TextRun; +use PhpOffice\PhpWord\Element\Link; +use PhpOffice\PhpWord\Element\PreserveText; +use PhpOffice\PhpWord\Element\TextBreak; +use PhpOffice\PhpWord\Element\ListItem; +use PhpOffice\PhpWord\Element\Table; +use PhpOffice\PhpWord\Element\Image; +use PhpOffice\PhpWord\Element\CheckBox; use PhpOffice\PhpWord\Shared\XMLWriter; /** @@ -47,7 +50,6 @@ class Header extends Base $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); $xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml'); - $_elements = $header->getElements(); foreach ($_elements as $element) { @@ -55,8 +57,14 @@ class Header extends Base $this->writeText($xmlWriter, $element); } elseif ($element instanceof TextRun) { $this->writeTextRun($xmlWriter, $element); + } elseif ($element instanceof Link) { + $this->writeLink($xmlWriter, $element); + } elseif ($element instanceof PreserveText) { + $this->writePreserveText($xmlWriter, $element); } elseif ($element instanceof TextBreak) { $this->writeTextBreak($xmlWriter, $element); + } elseif ($element instanceof ListItem) { + $this->writeListItem($xmlWriter, $element); } elseif ($element instanceof Table) { $this->writeTable($xmlWriter, $element); } elseif ($element instanceof Image) { @@ -65,8 +73,8 @@ class Header extends Base } else { $this->writeWatermark($xmlWriter, $element); } - } elseif ($element instanceof PreserveText) { - $this->writePreserveText($xmlWriter, $element); + } elseif ($element instanceof CheckBox) { + $this->writeCheckBox($xmlWriter, $element); } } diff --git a/tests/PhpWord/Tests/Element/Table/CellTest.php b/tests/PhpWord/Tests/Element/Table/CellTest.php index b8daca61..d16f275a 100644 --- a/tests/PhpWord/Tests/Element/Table/CellTest.php +++ b/tests/PhpWord/Tests/Element/Table/CellTest.php @@ -90,16 +90,6 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element); } - /** - * Add link exception - * @expectedException \BadMethodCallException - */ - public function testAddLinkException() - { - $oCell = new Cell('header', 1); - $element = $oCell->addLink('http://google.com', 'Google'); - } - /** * Add text break */ diff --git a/tests/PhpWord/Tests/FootnoteTest.php b/tests/PhpWord/Tests/FootnoteTest.php index 5ea7ed80..ff74e992 100644 --- a/tests/PhpWord/Tests/FootnoteTest.php +++ b/tests/PhpWord/Tests/FootnoteTest.php @@ -27,9 +27,8 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase $rIdFootnote = Footnote::addFootnoteElement($footnoteElement); $rIdLink = Footnote::addFootnoteLinkElement('http://test.com'); - $this->assertEquals(2, $rIdFootnote); + $this->assertEquals(1, $rIdFootnote); $this->assertEquals(1, $rIdLink); $this->assertEquals(1, count(Footnote::getFootnoteElements())); - $this->assertEquals(1, count(Footnote::getFootnoteLinkElements())); } } diff --git a/tests/PhpWord/Tests/MediaTest.php b/tests/PhpWord/Tests/MediaTest.php index 852cedb7..2d1434b3 100644 --- a/tests/PhpWord/Tests/MediaTest.php +++ b/tests/PhpWord/Tests/MediaTest.php @@ -43,7 +43,7 @@ class MediaTest extends \PHPUnit_Framework_TestCase { $this->assertAttributeEquals( Media::getHeaderMediaElements(), - '_headerMedia', + 'headerMedia', 'PhpOffice\\PhpWord\\Media' ); } @@ -55,7 +55,7 @@ class MediaTest extends \PHPUnit_Framework_TestCase { $this->assertAttributeEquals( Media::getFooterMediaElements(), - '_footerMedia', + 'footerMedia', 'PhpOffice\\PhpWord\\Media' ); } From 6f7b97e3ab562c1dbabc05d5a06bff4a1e426e20 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Tue, 1 Apr 2014 19:05:07 +0700 Subject: [PATCH 05/27] Add unit tests for refactoring results --- src/PhpWord/DocumentProperties.php | 12 ++++++------ src/PhpWord/Media.php | 2 +- tests/PhpWord/Tests/FootnoteTest.php | 1 + tests/PhpWord/Tests/MediaTest.php | 2 ++ tests/PhpWord/Tests/SettingsTest.php | 12 +++++++++++- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/PhpWord/DocumentProperties.php b/src/PhpWord/DocumentProperties.php index 63ff0275..14ed3150 100644 --- a/src/PhpWord/DocumentProperties.php +++ b/src/PhpWord/DocumentProperties.php @@ -39,14 +39,14 @@ class DocumentProperties /** * Created * - * @var datetime|int + * @var int */ private $_created; /** * Modified * - * @var datetime|int + * @var int */ private $_modified; @@ -171,7 +171,7 @@ class DocumentProperties /** * Get Created * - * @return datetime + * @return int */ public function getCreated() { @@ -181,7 +181,7 @@ class DocumentProperties /** * Set Created * - * @param datetime $pValue + * @param int $pValue * @return \PhpOffice\PhpWord\DocumentProperties */ public function setCreated($pValue = null) @@ -196,7 +196,7 @@ class DocumentProperties /** * Get Modified * - * @return datetime + * @return int */ public function getModified() { @@ -206,7 +206,7 @@ class DocumentProperties /** * Set Modified * - * @param datetime $pValue + * @param int $pValue * @return \PhpOffice\PhpWord\DocumentProperties */ public function setModified($pValue = null) diff --git a/src/PhpWord/Media.php b/src/PhpWord/Media.php index c8b33301..dea9e1f3 100755 --- a/src/PhpWord/Media.php +++ b/src/PhpWord/Media.php @@ -202,7 +202,7 @@ class Media if (!empty(self::$media)) { foreach (self::$media as $key => $val) { if (substr($key, 0, 6) == $prefix) { - $mediaCollection[$key] = $val; + $mediaCollection[$key] = $val; } } } diff --git a/tests/PhpWord/Tests/FootnoteTest.php b/tests/PhpWord/Tests/FootnoteTest.php index ff74e992..955d02e5 100644 --- a/tests/PhpWord/Tests/FootnoteTest.php +++ b/tests/PhpWord/Tests/FootnoteTest.php @@ -30,5 +30,6 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase $this->assertEquals(1, $rIdFootnote); $this->assertEquals(1, $rIdLink); $this->assertEquals(1, count(Footnote::getFootnoteElements())); + $this->assertEquals(1, count(Footnote::getFootnoteLinkElements())); } } diff --git a/tests/PhpWord/Tests/MediaTest.php b/tests/PhpWord/Tests/MediaTest.php index 2d1434b3..a3e6ead5 100644 --- a/tests/PhpWord/Tests/MediaTest.php +++ b/tests/PhpWord/Tests/MediaTest.php @@ -102,6 +102,8 @@ class MediaTest extends \PHPUnit_Framework_TestCase Media::addHeaderMediaElement(1, $remote, new Image($remote)); $this->assertEquals(2, Media::countHeaderMediaElements('header1')); + $this->assertEquals(2, count(Media::getMediaElements('header1'))); + $this->assertFalse(Media::getMediaElements('header2')); } /** diff --git a/tests/PhpWord/Tests/SettingsTest.php b/tests/PhpWord/Tests/SettingsTest.php index 543da143..fa78489e 100644 --- a/tests/PhpWord/Tests/SettingsTest.php +++ b/tests/PhpWord/Tests/SettingsTest.php @@ -19,7 +19,7 @@ use PhpOffice\PhpWord\Settings; class SettingsTest extends \PHPUnit_Framework_TestCase { /** - * Get and set compatibity option + * Get/set compatibity option */ public function testGetSetCompatibility() { @@ -28,4 +28,14 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertFalse(Settings::getCompatibility()); $this->assertFalse(Settings::setCompatibility('Non boolean')); } + + /** + * Get/set zip class + */ + public function testGetSetZipClass() + { + $this->assertEquals(Settings::ZIPARCHIVE, Settings::getZipClass()); + $this->assertTrue(Settings::setZipClass(Settings::PCLZIP)); + $this->assertFalse(Settings::setZipClass('foo')); + } } From 495930be9d5abcacc86dcf9d81d709c0f310c9e1 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Wed, 2 Apr 2014 09:01:44 +0700 Subject: [PATCH 06/27] Add element availability matrix and enable `addObject` in footnote and textrun --- CHANGELOG.md | 8 + docs/elements.rst | 45 ++++ samples/Sample_04_Textrun.php | 2 + samples/Sample_06_Footnote.php | 4 +- src/PhpWord/Container/Container.php | 236 +++++++++++------- src/PhpWord/Container/Footer.php | 4 +- src/PhpWord/Container/Header.php | 4 +- src/PhpWord/Container/Section.php | 8 +- src/PhpWord/Element/Footnote.php | 2 +- src/PhpWord/Element/Table/Cell.php | 8 +- src/PhpWord/Element/TextRun.php | 6 +- src/PhpWord/Media.php | 42 ++-- src/PhpWord/Writer/Word2007.php | 26 +- src/PhpWord/Writer/Word2007/Base.php | 128 +++++----- src/PhpWord/Writer/Word2007/ContentTypes.php | 1 - src/PhpWord/Writer/Word2007/DocumentRels.php | 59 +---- src/PhpWord/Writer/Word2007/Footnotes.php | 3 + src/PhpWord/Writer/Word2007/FootnotesRels.php | 18 +- src/PhpWord/Writer/Word2007/Rels.php | 6 +- 19 files changed, 330 insertions(+), 280 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24f6ac85..8b81a016 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,11 +19,19 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - ListItem: Ability to add list item in header/footer - @ivanlanin GH-187 - CheckBox: Ability to add checkbox in header/footer - @ivanlanin GH-187 - Link: Ability to add link in header/footer - @ivanlanin GH-187 +- Object: Ability to add object in textrun and footnote - @ivanlanin GH-187 ### Bugfixes - Footnote: Footnote content doesn't show footnote reference number - @ivanlanin GH-170 +### Deprecated + +- `createTextRun` replaced by `addTextRun` +- `createFootnote` replaced by `addFootnote` +- `createHeader` replaced by `addHeader` +- `createFooter` replaced by `addFooter` + ### Miscellaneous - Documentation: Simplify page level docblock - @ivanlanin GH-179 diff --git a/docs/elements.rst b/docs/elements.rst index bded72b8..d1634bf4 100644 --- a/docs/elements.rst +++ b/docs/elements.rst @@ -3,6 +3,51 @@ Elements ======== +Below are the matrix of element availability in each container. The column shows +the containers while the rows lists the elements. + ++-----+---------------+---------+--------+--------+------+----------+----------+ +| Num | Element | Section | Header | Footer | Cell | Text Run | Footnote | ++=====+===============+=========+========+========+======+==========+==========+ +| 1 | Text | v | v | v | v | v | v | ++-----+---------------+---------+--------+--------+------+----------+----------+ +| 2 | Text Run | v | v | v | v | \- | \- | ++-----+---------------+---------+--------+--------+------+----------+----------+ +| 3 | Link | v | v | v | v | v | v | ++-----+---------------+---------+--------+--------+------+----------+----------+ +| 4 | Title | v | ? | ? | ? | ? | ? | ++-----+---------------+---------+--------+--------+------+----------+----------+ +| 5 | Preserve Text | ? | v | v | v\* | ? | ? | ++-----+---------------+---------+--------+--------+------+----------+----------+ +| 6 | Text Break | v | v | v | v | v | v | ++-----+---------------+---------+--------+--------+------+----------+----------+ +| 7 | Page Break | v | \- | \- | \- | \- | \- | ++-----+---------------+---------+--------+--------+------+----------+----------+ +| 8 | List | v | v | v | v | \- | \- | ++-----+---------------+---------+--------+--------+------+----------+----------+ +| 9 | Table | v | v | v | ? | \- | \- | ++-----+---------------+---------+--------+--------+------+----------+----------+ +| 10 | Image | v | v | v | v | v | v | ++-----+---------------+---------+--------+--------+------+----------+----------+ +| 11 | Watermark | \- | v | \- | \- | \- | \- | ++-----+---------------+---------+--------+--------+------+----------+----------+ +| 12 | Object | v | ? | ? | v | v | v | ++-----+---------------+---------+--------+--------+------+----------+----------+ +| 13 | TOC | v | \- | \- | \- | \- | \- | ++-----+---------------+---------+--------+--------+------+----------+----------+ +| 14 | Footnote | v | \- | \- | v\*\*| v\*\* | \- | ++-----+---------------+---------+--------+--------+------+----------+----------+ +| 15 | CheckBox | v | v | v | v | ? | ? | ++-----+---------------+---------+--------+--------+------+----------+----------+ + +Legend: + +- ``v`` Available +- ``v*`` Available only when inside header/footer +- ``v**`` Available only when inside section +- ``-`` Not available +- ``?`` Should be available + Texts ----- diff --git a/samples/Sample_04_Textrun.php b/samples/Sample_04_Textrun.php index d289457d..d95297b8 100644 --- a/samples/Sample_04_Textrun.php +++ b/samples/Sample_04_Textrun.php @@ -29,6 +29,8 @@ $textrun->addText(' Sample Link: '); $textrun->addLink('http://www.google.com', null, 'NLink'); $textrun->addText(' Sample Image: '); $textrun->addImage('resources/_earth.jpg', array('width' => 18, 'height' => 18)); +$textrun->addText(' Sample Object: '); +$textrun->addObject('resources/_sheet.xls'); $textrun->addText(' Here is some more text. '); // Save file diff --git a/samples/Sample_06_Footnote.php b/samples/Sample_06_Footnote.php index e35f5d6b..57421eff 100755 --- a/samples/Sample_06_Footnote.php +++ b/samples/Sample_06_Footnote.php @@ -27,8 +27,10 @@ $footnote->addTextBreak(); $footnote->addText('But you can insert a manual text break like above, '); $footnote->addText('links like '); $footnote->addLink('http://www.google.com', null, 'NLink'); -$footnote->addText(', or image like '); +$footnote->addText(', image like '); $footnote->addImage('resources/_earth.jpg', array('width' => 18, 'height' => 18)); +$footnote->addText(', or object like '); +$footnote->addObject('resources/_sheet.xls'); $footnote->addText('But you can only put footnote in section, not in header or footer.'); $section->addText('You can also create the footnote directly from the section making it wrap in a paragraph like the footnote below this paragraph. But is is best used from within a textrun.'); diff --git a/src/PhpWord/Container/Container.php b/src/PhpWord/Container/Container.php index a29e393c..fd88955e 100644 --- a/src/PhpWord/Container/Container.php +++ b/src/PhpWord/Container/Container.php @@ -37,21 +37,21 @@ use PhpOffice\PhpWord\Element\CheckBox; abstract class Container { /** - * Container type section|header|footer + * Container type section|header|footer|cell|textrun|footnote * * @var string */ - protected $containerType; + protected $container; /** * Section Id * * @var int */ - protected $sectionId; + protected $containerId; /** - * Footer Element Collection + * Elements collection * * @var int */ @@ -62,11 +62,11 @@ abstract class Container * * Used by textrun and cell to determine where the element is located * because it will affect the availability of other element, e.g. footnote - * will not be available when $docPartType is header or footer. + * will not be available when $docPart is header or footer. * * @var string */ - protected $docPartType = null; + protected $docPart = null; /** * Document part Id @@ -92,9 +92,13 @@ abstract class Container */ public function addText($text, $fontStyle = null, $paragraphStyle = null) { - if (in_array($this->containerType, array('footnote', 'textrun'))) { + $this->checkValidity('text'); + + // Reset paragraph style for footnote and textrun. They have their own + if (in_array($this->container, array('footnote', 'textrun'))) { $paragraphStyle = null; } + $text = String::toUTF8($text); $element = new Text($text, $fontStyle, $paragraphStyle); $this->elements[] = $element; @@ -102,20 +106,6 @@ abstract class Container return $element; } - /** - * Add text break element - * - * @param int $count - * @param mixed $fontStyle - * @param mixed $paragraphStyle - */ - public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null) - { - for ($i = 1; $i <= $count; $i++) { - $this->elements[] = new TextBreak($fontStyle, $paragraphStyle); - } - } - /** * Add textrun element * @@ -124,18 +114,17 @@ abstract class Container */ public function addTextRun($paragraphStyle = null) { - if (!in_array($this->containerType, array('section', 'header', 'footer', 'cell'))) { - throw new \BadMethodCallException(); - } - if ($this->containerType == 'cell') { - $docPartType = $this->docPartType; + $this->checkValidity('textrun'); + + if ($this->container == 'cell') { + $docPart = $this->docPart; $docPartId = $this->docPartId; } else { - $docPartType = $this->containerType; - $docPartId = $this->sectionId; + $docPart = $this->container; + $docPartId = $this->containerId; } - $textRun = new TextRun($paragraphStyle, $docPartType, $docPartId); + $textRun = new TextRun($paragraphStyle, $docPart, $docPartId); $this->elements[] = $textRun; return $textRun; @@ -152,24 +141,30 @@ abstract class Container */ public function addLink($linkSrc, $linkName = null, $fontStyle = null, $paragraphStyle = null) { - if (!is_null($this->docPartType)) { - $linkContainer = $this->docPartType; - $linkContainerId = $this->docPartId; + $this->checkValidity('link'); + + $inSection = true; + if (!is_null($this->docPart)) { + $container = $this->docPart; + $containerId = $this->docPartId; } else { - $linkContainer = $this->containerType; - $linkContainerId = $this->sectionId; + $container = $this->container; + $containerId = $this->containerId; } - if ($linkContainer == 'header' || $linkContainer == 'footer') { - $linkContainer .= $linkContainerId; + if ($container == 'header' || $container == 'footer') { + $container .= $containerId; + $inSection = false; + } elseif ($container == 'footnote') { + $inSection = false; } $linkSrc = String::toUTF8($linkSrc); $linkName = String::toUTF8($linkName); $link = new Link($linkSrc, $linkName, $fontStyle, $paragraphStyle); - if ($linkContainer == 'section') { + if ($inSection) { $rID = Media::addSectionLinkElement($linkSrc); } else { - $rID = Media::addMediaElement($linkContainer, 'hyperlink', $linkSrc); + $rID = Media::addMediaElement($container, 'hyperlink', $linkSrc); } $link->setRelationId($rID); $this->elements[] = $link; @@ -183,13 +178,11 @@ abstract class Container * @param string $text * @param int $depth * @return Title - * @todo Enable title element in header, footer, footnote, textrun + * @todo Enable title element in other containers */ public function addTitle($text, $depth = 1) { - if (!in_array($this->containerType, array('section'))) { - throw new \BadMethodCallException(); - } + $this->checkValidity('title'); $text = String::toUTF8($text); $styles = Style::getStyles(); @@ -219,12 +212,7 @@ abstract class Container */ public function addPreserveText($text, $fontStyle = null, $paragraphStyle = null) { - if (!in_array($this->containerType, array('header', 'footer', 'cell'))) { - throw new \BadMethodCallException(); - } - if ($this->containerType == 'cell' && $this->docPartType == 'section') { - throw new \BadMethodCallException(); - } + $this->checkValidity('preservetext'); $text = String::toUTF8($text); $ptext = new PreserveText($text, $fontStyle, $paragraphStyle); @@ -233,6 +221,22 @@ abstract class Container return $ptext; } + /** + * Add text break element + * + * @param int $count + * @param mixed $fontStyle + * @param mixed $paragraphStyle + */ + public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null) + { + $this->checkValidity('textbreak'); + + for ($i = 1; $i <= $count; $i++) { + $this->elements[] = new TextBreak($fontStyle, $paragraphStyle); + } + } + /** * Add listitem element * @@ -242,13 +246,10 @@ abstract class Container * @param mixed $styleList * @param mixed $paragraphStyle * @return ListItem - * @todo Enable list item element in header and footer */ public function addListItem($text, $depth = 0, $fontStyle = null, $styleList = null, $paragraphStyle = null) { - if (!in_array($this->containerType, array('section', 'header', 'footer', 'cell'))) { - throw new \BadMethodCallException(); - } + $this->checkValidity('listitem'); $text = String::toUTF8($text); $listItem = new ListItem($text, $depth, $fontStyle, $styleList, $paragraphStyle); @@ -265,11 +266,9 @@ abstract class Container */ public function addTable($style = null) { - if (!in_array($this->containerType, array('section', 'header', 'footer'))) { - throw new \BadMethodCallException(); - } + $this->checkValidity('table'); - $table = new Table($this->containerType, $this->sectionId, $style); + $table = new Table($this->container, $this->containerId, $style); $this->elements[] = $table; return $table; @@ -285,30 +284,31 @@ abstract class Container */ public function addImage($src, $style = null, $isWatermark = false) { - if ($this->containerType == 'cell') { - $imageContainerType = $this->docPartType; - $imageContainerId = $this->docPartId; + $this->checkValidity('image'); + if ($this->container == 'cell' || $this->container == 'textrun') { + $container = $this->docPart; + $containerId = $this->docPartId; } else { - $imageContainerType = $this->containerType; - $imageContainerId = $this->sectionId; + $container = $this->container; + $containerId = $this->containerId; } $image = new Image($src, $style, $isWatermark); if (!is_null($image->getSource())) { $rID = null; - switch ($imageContainerType) { + switch ($container) { case 'textrun': case 'section': $rID = Media::addSectionMediaElement($src, 'image', $image); break; case 'header': - $rID = Media::addHeaderMediaElement($imageContainerId, $src, $image); + $rID = Media::addHeaderMediaElement($containerId, $src, $image); break; case 'footer': - $rID = Media::addFooterMediaElement($imageContainerId, $src, $image); + $rID = Media::addFooterMediaElement($containerId, $src, $image); break; case 'footnote': - $rID = Media::addMediaElement('footnotes', 'image', $src, $image); + $rID = Media::addMediaElement('footnote', 'image', $src, $image); break; } $image->setRelationId($rID); @@ -331,11 +331,19 @@ abstract class Container */ public function addObject($src, $style = null) { - if (!in_array($this->containerType, array('section', 'cell'))) { - throw new \BadMethodCallException(); + $inSection = true; + if (!is_null($this->docPart)) { + $container = $this->docPart; + $containerId = $this->docPartId; + } else { + $container = $this->container; + $containerId = $this->containerId; } - if ($this->containerType == 'cell' && $this->docPartType != 'section') { - throw new \BadMethodCallException(); + if ($container == 'header' || $container == 'footer') { + $container .= $containerId; + $inSection = false; + } elseif ($container == 'footnote') { + $inSection = false; } $object = new Object($src, $style); @@ -345,9 +353,14 @@ abstract class Container if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') { $ext = substr($ext, 0, -1); } - $icon = __DIR__ . "/../_staticDocParts/_{$ext}.png"; - $rIDimg = Media::addSectionMediaElement($icon, 'image', new Image($icon)); - $data = Media::addSectionMediaElement($src, 'oleObject'); + $icon = realpath(__DIR__ . "/../_staticDocParts/_{$ext}.png"); + if ($inSection) { + $rIDimg = Media::addSectionMediaElement($icon, 'image', new Image($icon)); + $data = Media::addSectionMediaElement($src, 'oleObject'); + } else { + $rIDimg = Media::addMediaElement($container, 'image', $icon, new Image($icon)); + $data = Media::addMediaElement($container, 'embeddings', $src); + } $rID = $data[0]; $objectId = $data[1]; $object->setRelationId($rID); @@ -368,12 +381,7 @@ abstract class Container */ public function addFootnote($paragraphStyle = null) { - if (!in_array($this->containerType, array('section', 'textrun', 'cell'))) { - throw new \BadMethodCallException(); - } - if (!is_null($this->docPartType) && $this->docPartType != 'section') { - throw new \BadMethodCallException(); - } + $this->checkValidity('footnote'); $footnote = new FootnoteElement($paragraphStyle); $refID = FootnoteCollection::addFootnoteElement($footnote); @@ -391,16 +399,10 @@ abstract class Container * @param mixed $fontStyle * @param mixed $paragraphStyle * @return CheckBox - * @todo Enable checkbox element in header and footer */ public function addCheckBox($name, $text, $fontStyle = null, $paragraphStyle = null) { - if (!in_array($this->containerType, array('section', 'header', 'footer', 'cell'))) { - throw new \BadMethodCallException(); - } - if ($this->containerType == 'cell' && $this->docPartType != 'section') { - throw new \BadMethodCallException(); - } + $this->checkValidity('checkbox'); $name = String::toUTF8($name); $text = String::toUTF8($text); @@ -416,7 +418,7 @@ abstract class Container */ public function getSectionId() { - return $this->sectionId; + return $this->containerId; } /** @@ -436,10 +438,7 @@ abstract class Container */ public function getRelationId() { - if (!in_array($this->containerType, array('header', 'footer'))) { - throw new \BadMethodCallException(); - } - + $this->checkValidity('relationid'); return $this->relationId; } @@ -450,10 +449,7 @@ abstract class Container */ public function setRelationId($rId) { - if (!in_array($this->containerType, array('header', 'footer'))) { - throw new \BadMethodCallException(); - } - + $this->checkValidity('relationid'); $this->relationId = $rId; } @@ -490,4 +486,56 @@ abstract class Container { return $this->addFootnote($paragraphStyle); } + + /** + * Check if a method is allowed for the current container + * + * @param string $element + * @return boolean + */ + private function checkValidity($method) + { + $validContainers = array( + 'text' => 'all', + 'link' => 'all', + 'textbreak' => 'all', + 'image' => 'all', + 'textrun' => array('section', 'header', 'footer', 'cell'), + 'listitem' => array('section', 'header', 'footer', 'cell'), + 'checkbox' => array('section', 'header', 'footer', 'cell'), + 'table' => array('section', 'header', 'footer'), + 'object' => array('section', 'textrun', 'cell', 'footnote'), + 'footnote' => array('section', 'textrun', 'cell'), + 'preservetext' => array('header', 'footer', 'cell'), + 'relationid' => array('header', 'footer'), + 'title' => array('section'), + ); + $validContainerInContainers = array( + 'preservetext' => array(array('cell'), array('header', 'footer')), + 'object' => array(array('cell', 'textrun'), array('section')), + 'footnote' => array(array('cell', 'textrun'), array('section')), + ); + + // Check if a method is valid for current container + if (array_key_exists($method, $validContainers)) { + if (is_array($validContainers[$method])) { + if (!in_array($this->container, $validContainers[$method])) { + throw new \BadMethodCallException(); + } + } + } + // Check if a method is valid for current container, located in other container + if (array_key_exists($method, $validContainerInContainers)) { + $rules = $validContainerInContainers[$method]; + $containers = $rules[0]; + $allowedDocParts = $rules[1]; + foreach ($containers as $container) { + if ($this->container == $container && !in_array($this->docPart, $allowedDocParts)) { + throw new \BadMethodCallException(); + } + } + } + + return true; + } } diff --git a/src/PhpWord/Container/Footer.php b/src/PhpWord/Container/Footer.php index 37884b86..56f06383 100755 --- a/src/PhpWord/Container/Footer.php +++ b/src/PhpWord/Container/Footer.php @@ -21,7 +21,7 @@ class Footer extends Container */ public function __construct($sectionId) { - $this->containerType = 'footer'; - $this->sectionId = $sectionId; + $this->container = 'footer'; + $this->containerId = $sectionId; } } diff --git a/src/PhpWord/Container/Header.php b/src/PhpWord/Container/Header.php index 3c702406..126f9198 100755 --- a/src/PhpWord/Container/Header.php +++ b/src/PhpWord/Container/Header.php @@ -40,8 +40,8 @@ class Header extends Container */ public function __construct($sectionId) { - $this->containerType = 'header'; - $this->sectionId = $sectionId; + $this->container = 'header'; + $this->containerId = $sectionId; } /** diff --git a/src/PhpWord/Container/Section.php b/src/PhpWord/Container/Section.php index dcef4707..b549280d 100644 --- a/src/PhpWord/Container/Section.php +++ b/src/PhpWord/Container/Section.php @@ -50,8 +50,8 @@ class Section extends Container */ public function __construct($sectionCount, $settings = null) { - $this->containerType = 'section'; - $this->sectionId = $sectionCount; + $this->container = 'section'; + $this->containerId = $sectionCount; $this->settings = new Settings(); $this->setSettings($settings); } @@ -112,7 +112,7 @@ class Section extends Container */ public function addHeader() { - $header = new Header($this->sectionId); + $header = new Header($this->containerId); $this->headers[] = $header; return $header; } @@ -124,7 +124,7 @@ class Section extends Container */ public function addFooter() { - $footer = new Footer($this->sectionId); + $footer = new Footer($this->containerId); $this->footer = $footer; return $footer; } diff --git a/src/PhpWord/Element/Footnote.php b/src/PhpWord/Element/Footnote.php index aa6af010..3d65a256 100644 --- a/src/PhpWord/Element/Footnote.php +++ b/src/PhpWord/Element/Footnote.php @@ -38,7 +38,7 @@ class Footnote extends Container */ public function __construct($paragraphStyle = null) { - $this->containerType = 'footnote'; + $this->container = 'footnote'; // Set paragraph style if (is_array($paragraphStyle)) { $this->paragraphStyle = new Paragraph(); diff --git a/src/PhpWord/Element/Table/Cell.php b/src/PhpWord/Element/Table/Cell.php index 3f795a28..2a15a263 100755 --- a/src/PhpWord/Element/Table/Cell.php +++ b/src/PhpWord/Element/Table/Cell.php @@ -34,15 +34,15 @@ class Cell extends Container /** * Create new instance * - * @param string $docPartType section|header|footer + * @param string $docPart section|header|footer * @param int $docPartId * @param int $width * @param array|CellStyle $style */ - public function __construct($docPartType, $docPartId, $width = null, $style = null) + public function __construct($docPart, $docPartId, $width = null, $style = null) { - $this->containerType = 'cell'; - $this->docPartType = $docPartType; + $this->container = 'cell'; + $this->docPart = $docPart; $this->docPartId = $docPartId; $this->width = $width; $this->cellStyle = new CellStyle(); diff --git a/src/PhpWord/Element/TextRun.php b/src/PhpWord/Element/TextRun.php index 35701d45..5cf1a0c6 100755 --- a/src/PhpWord/Element/TextRun.php +++ b/src/PhpWord/Element/TextRun.php @@ -31,10 +31,10 @@ class TextRun extends Container * @param string $docPartType section|header|footer * @param int $docPartId */ - public function __construct($paragraphStyle = null, $docPartType = 'section', $docPartId = 1) + public function __construct($paragraphStyle = null, $docPart = 'section', $docPartId = 1) { - $this->containerType = 'textrun'; - $this->docPartType = $docPartType; + $this->container = 'textrun'; + $this->docPart = $docPart; $this->docPartId = $docPartId; // Set paragraph style if (is_array($paragraphStyle)) { diff --git a/src/PhpWord/Media.php b/src/PhpWord/Media.php index dea9e1f3..ccfa1649 100755 --- a/src/PhpWord/Media.php +++ b/src/PhpWord/Media.php @@ -79,7 +79,7 @@ class Media $isMemImage = false; if (!is_null($image)) { $isMemImage = $image->getIsMemImage(); - $extension = $image->getImageExtension(); + $ext = $image->getImageExtension(); } if ($isMemImage) { $media['isMemImage'] = true; @@ -87,10 +87,10 @@ class Media $media['imagefunction'] = $image->getImageFunction(); } $folder = 'media'; - $file = $type . $cImg . '.' . strtolower($extension); + $file = $type . $cImg . '.' . strtolower($ext); } elseif ($type === 'oleObject') { $cObj++; - $folder = 'embedding'; + $folder = 'embeddings'; $file = $type . $cObj . '.bin'; } $media['source'] = $src; @@ -247,8 +247,8 @@ class Media /** * Add new media element * - * @param string $container section|header|footer|footnotes - * @param string $mediaType image|embedding|hyperlink + * @param string $container section|headerx|footerx|footnote + * @param string $mediaType image|embeddings|hyperlink * @param string $source * @param Image $image * @return int @@ -267,35 +267,49 @@ class Media $mediaTypeCount = self::countMediaElements($container, $mediaType); $mediaData = array(); $relId = $mediaCount + 1; + $target = null; $mediaTypeCount++; + + // Images if ($mediaType == 'image') { $isMemImage = false; if (!is_null($image)) { $isMemImage = $image->getIsMemImage(); - $extension = $image->getImageExtension(); + $ext = $image->getImageExtension(); + $ext = strtolower($ext); } if ($isMemImage) { $mediaData['isMemImage'] = true; $mediaData['createfunction'] = $image->getImageCreateFunction(); $mediaData['imagefunction'] = $image->getImageFunction(); } - $file = 'image' . $mediaTypeCount . '.' . strtolower($extension); - if ($container != 'footnotes') { - $file = $container . '_' . $file; - } - $target = 'media/' . $file; + $target = "media/{$container}_image{$mediaTypeCount}.{$ext}"; + // Objects + } elseif ($mediaType == 'embeddings') { + $file = "oleObject{$mediaTypeCount}.bin"; + $target = "embeddings/{$container}_oleObject{$mediaTypeCount}.bin"; + // Links } elseif ($mediaType == 'hyperlink') { $target = $source; } + $mediaData['source'] = $source; $mediaData['target'] = $target; $mediaData['type'] = $mediaType; $mediaData['rID'] = $relId; self::$media[$container][$mediaId] = $mediaData; - - return $relId; + if ($mediaType === 'embeddings') { + return array($relId, ++self::$objectId); + } else { + return $relId; + } } else { - return self::$media[$container][$mediaId]['rID']; + if ($mediaType === 'embeddings') { + $relId = self::$media[$container][$mediaId]['rID']; + return array($relId, ++self::$objectId); + } else { + return self::$media[$container][$mediaId]['rID']; + } } } diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index dd144da1..298b93cc 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -121,7 +121,7 @@ class Word2007 extends Writer implements IWriter $this->getWriterPart('documentrels')->writeHeaderFooterRels($hdrMedia) ); foreach ($hdrMedia as $element) { - if ($element['type'] == 'image') { + if ($element['type'] != 'hyperlink') { $this->addFileToPackage($objZip, $element); } } @@ -137,7 +137,7 @@ class Word2007 extends Writer implements IWriter $this->getWriterPart('documentrels')->writeHeaderFooterRels($ftrMedia) ); foreach ($ftrMedia as $element) { - if ($element['type'] == 'image') { + if ($element['type'] != 'hyperlink') { $this->addFileToPackage($objZip, $element); } } @@ -155,10 +155,10 @@ class Word2007 extends Writer implements IWriter foreach ($_headers as $index => &$_header) { $_cHdrs++; $_header->setRelationId(++$rID); - $hdrFile = 'header' . $_cHdrs . '.xml'; + $hdrFile = "header{$_cHdrs}.xml"; $sectionElements[] = array('target' => $hdrFile, 'type' => 'header', 'rID' => $rID); $objZip->addFromString( - 'word/' . $hdrFile, + "word/{$hdrFile}", $this->getWriterPart('header')->writeHeader($_header) ); } @@ -167,10 +167,10 @@ class Word2007 extends Writer implements IWriter if (!is_null($_footer)) { $_footer->setRelationId(++$rID); $_footerCount = $_footer->getSectionId(); - $ftrFile = 'footer' . $_footerCount . '.xml'; + $ftrFile = "footer{$_footerCount}.xml"; $sectionElements[] = array('target' => $ftrFile, 'type' => 'footer', 'rID' => $rID); $objZip->addFromString( - 'word/' . $ftrFile, + "word/{$ftrFile}", $this->getWriterPart('footer')->writeFooter($_footer) ); } @@ -181,24 +181,24 @@ class Word2007 extends Writer implements IWriter // Push to document.xml.rels $sectionElements[] = array('target' => 'footnotes.xml', 'type' => 'footnotes', 'rID' => ++$rID); // Add footnote media to package - $footnotesMedia = Media::getMediaElements('footnotes'); - if (!empty($footnotesMedia)) { - foreach ($footnotesMedia as $media) { - if ($media['type'] == 'image') { + $footnoteMedia = Media::getMediaElements('footnote'); + if (!empty($footnoteMedia)) { + foreach ($footnoteMedia as $media) { + if ($media['type'] != 'hyperlink') { $this->addFileToPackage($objZip, $media); } } } // Write footnotes.xml $objZip->addFromString( - "word/footnotes.xml", + 'word/footnotes.xml', $this->getWriterPart('footnotes')->writeFootnotes(Footnote::getFootnoteElements()) ); // Write footnotes.xml.rels - if (!empty($footnotesMedia)) { + if (!empty($footnoteMedia)) { $objZip->addFromString( 'word/_rels/footnotes.xml.rels', - $this->getWriterPart('footnotesrels')->writeFootnotesRels($footnotesMedia) + $this->getWriterPart('footnotesrels')->writeFootnotesRels($footnoteMedia) ); } } diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index 7af96b03..d5c0160a 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -44,11 +44,8 @@ class Base extends WriterPart * @param Text $text * @param boolean $withoutP */ - protected function writeText( - XMLWriter $xmlWriter, - Text $text, - $withoutP = false - ) { + protected function writeText(XMLWriter $xmlWriter, Text $text, $withoutP = false) + { $styleFont = $text->getFontStyle(); $styleParagraph = $text->getParagraphStyle(); $strText = htmlspecialchars($text->getText()); @@ -76,10 +73,8 @@ class Base extends WriterPart * @param XMLWriter $xmlWriter * @param TextRun $textrun */ - protected function writeTextRun( - XMLWriter $xmlWriter, - TextRun $textrun - ) { + protected function writeTextRun(XMLWriter $xmlWriter, TextRun $textrun) + { $elements = $textrun->getElements(); $styleParagraph = $textrun->getParagraphStyle(); $xmlWriter->startElement('w:p'); @@ -94,6 +89,8 @@ class Base extends WriterPart $xmlWriter->writeElement('w:br'); } elseif ($element instanceof Image) { $this->writeImage($xmlWriter, $element, true); + } elseif ($element instanceof Object) { + $this->writeObject($xmlWriter, $element, true); } elseif ($element instanceof Footnote) { $this->writeFootnote($xmlWriter, $element, true); } @@ -109,11 +106,8 @@ class Base extends WriterPart * @param Link $link * @param boolean $withoutP */ - protected function writeLink( - XMLWriter $xmlWriter, - Link $link, - $withoutP = false - ) { + protected function writeLink(XMLWriter $xmlWriter, Link $link, $withoutP = false) + { $rID = $link->getRelationId(); $linkName = $link->getLinkName(); if (is_null($linkName)) { @@ -196,10 +190,8 @@ class Base extends WriterPart * @param XMLWriter $xmlWriter * @param PreserveText $textrun */ - protected function writePreserveText( - XMLWriter $xmlWriter, - PreserveText $textrun - ) { + protected function writePreserveText(XMLWriter $xmlWriter, PreserveText $textrun) + { $styleFont = $textrun->getFontStyle(); $styleParagraph = $textrun->getParagraphStyle(); @@ -467,11 +459,8 @@ class Base extends WriterPart * @param Image $image * @param boolean $withoutP */ - protected function writeImage( - XMLWriter $xmlWriter, - Image $image, - $withoutP = false - ) { + protected function writeImage(XMLWriter $xmlWriter, Image $image, $withoutP = false) + { $rId = $image->getRelationId(); $style = $image->getStyle(); @@ -601,8 +590,9 @@ class Base extends WriterPart * * @param XMLWriter $xmlWriter * @param Object $object + * @param boolean $withoutP */ - protected function writeObject(XMLWriter $xmlWriter, Object $object) + protected function writeObject(XMLWriter $xmlWriter, Object $object, $withoutP = false) { $rIdObject = $object->getRelationId(); $rIdImage = $object->getImageRelationId(); @@ -611,7 +601,9 @@ class Base extends WriterPart $style = $object->getStyle(); $align = $style->getAlign(); - $xmlWriter->startElement('w:p'); + if (!$withoutP) { + $xmlWriter->startElement('w:p'); + } if (!is_null($align)) { $xmlWriter->startElement('w:pPr'); $xmlWriter->startElement('w:jc'); @@ -643,7 +635,9 @@ class Base extends WriterPart $xmlWriter->endElement(); // o:OLEObject $xmlWriter->endElement(); // w:object $xmlWriter->endElement(); // w:r - $xmlWriter->endElement(); // w:p + if (!$withoutP) { + $xmlWriter->endElement(); // w:p + } } /** @@ -653,11 +647,8 @@ class Base extends WriterPart * @param Footnote $footnote * @param boolean $withoutP */ - protected function writeFootnote( - XMLWriter $xmlWriter, - Footnote $footnote, - $withoutP = false - ) { + protected function writeFootnote(XMLWriter $xmlWriter, Footnote $footnote, $withoutP = false) + { if (!$withoutP) { $xmlWriter->startElement('w:p'); } @@ -682,12 +673,8 @@ class Base extends WriterPart * @param boolean $withoutP * @param boolean $checkState */ - protected function writeCheckBox( - XMLWriter $xmlWriter, - CheckBox $checkbox, - $withoutP = false, - $checkState = false - ) { + protected function writeCheckBox(XMLWriter $xmlWriter, CheckBox $checkbox, $withoutP = false, $checkState = false) + { $name = htmlspecialchars($checkbox->getName()); $name = String::controlCharacterPHP2OOXML($name); $text = htmlspecialchars($checkbox->getText()); @@ -758,11 +745,8 @@ class Base extends WriterPart * @param Paragraph $style * @param bool $withoutPPR */ - protected function writeParagraphStyle( - XMLWriter $xmlWriter, - Paragraph $style, - $withoutPPR = false - ) { + protected function writeParagraphStyle(XMLWriter $xmlWriter, Paragraph $style, $withoutPPR = false) + { $align = $style->getAlign(); $spacing = $style->getSpacing(); @@ -965,11 +949,8 @@ class Base extends WriterPart * @param TableStyle $style * @param boolean $isFullStyle */ - protected function writeTableStyle( - XMLWriter $xmlWriter, - TableStyle $style, - $isFullStyle = true - ) { + protected function writeTableStyle(XMLWriter $xmlWriter, TableStyle $style, $isFullStyle = true) + { $bgColor = $style->getBgColor(); $brdCol = $style->getBorderColor(); @@ -1094,11 +1075,8 @@ class Base extends WriterPart * @param string $type * @param TableStyle $style */ - protected function writeRowStyle( - XMLWriter $xmlWriter, - $type, - TableStyle $style - ) { + protected function writeRowStyle(XMLWriter $xmlWriter, $type, TableStyle $style) + { $brdSz = $style->getBorderSize(); $brdCol = $style->getBorderColor(); $bgColor = $style->getBgColor(); @@ -1265,6 +1243,26 @@ class Base extends WriterPart } } + /** + * Write media rels (image, embeddings, hyperlink) + * + * @param XMLWriter $xmlWriter + * @param array mediaRels + */ + protected function writeMediaRels(XMLWriter $xmlWriter, $mediaRels) + { + $rTypePrefix = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/'; + foreach ($mediaRels as $mediaRel) { + $rId = $mediaRel['rID']; + $rType = $mediaRel['type']; + $rName = $mediaRel['target']; // file name + $targetMode = ($rType == 'hyperlink') ? 'External' : ''; + $rType = $rTypePrefix . ($rType == 'embeddings' ? 'oleObject' : $rType); + $this->writeRel($xmlWriter, $rId, $rType, $rName, $targetMode); + } + + } + /** * Write individual rels entry * @@ -1274,28 +1272,19 @@ class Base extends WriterPart * @param string $pTarget Relationship target * @param string $pTargetMode Relationship target mode */ - protected function writeRelationship( - XMLWriter $xmlWriter, - $pId = 1, - $pType = '', - $pTarget = '', - $pTargetMode = '' - ) { + protected function writeRel(XMLWriter $xmlWriter, $pId, $pType, $pTarget, $pTargetMode = '') + { if ($pType != '' && $pTarget != '') { if (strpos($pId, 'rId') === false) { $pId = 'rId' . $pId; } - - // Write relationship $xmlWriter->startElement('Relationship'); $xmlWriter->writeAttribute('Id', $pId); $xmlWriter->writeAttribute('Type', $pType); $xmlWriter->writeAttribute('Target', $pTarget); - if ($pTargetMode != '') { $xmlWriter->writeAttribute('TargetMode', $pTargetMode); } - $xmlWriter->endElement(); } else { throw new Exception("Invalid parameters passed."); @@ -1309,11 +1298,8 @@ class Base extends WriterPart * @param Paragraph|string $styleParagraph * @param boolean $withoutPPR */ - protected function writeInlineParagraphStyle( - XMLWriter $xmlWriter, - $styleParagraph = null, - $withoutPPR = false - ) { + protected function writeInlineParagraphStyle(XMLWriter $xmlWriter, $styleParagraph = null, $withoutPPR = false) + { if ($styleParagraph instanceof Paragraph) { $this->writeParagraphStyle($xmlWriter, $styleParagraph, $withoutPPR); } else { @@ -1337,10 +1323,8 @@ class Base extends WriterPart * @param XMLWriter $xmlWriter * @param Font|string $styleFont */ - protected function writeInlineFontStyle( - XMLWriter $xmlWriter, - $styleFont = null - ) { + protected function writeInlineFontStyle(XMLWriter $xmlWriter, $styleFont = null) + { if ($styleFont instanceof Font) { $this->writeFontStyle($xmlWriter, $styleFont); } else { diff --git a/src/PhpWord/Writer/Word2007/ContentTypes.php b/src/PhpWord/Writer/Word2007/ContentTypes.php index 98121133..86689823 100755 --- a/src/PhpWord/Writer/Word2007/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/ContentTypes.php @@ -151,7 +151,6 @@ class ContentTypes extends WriterPart } } - $xmlWriter->endElement(); // Return diff --git a/src/PhpWord/Writer/Word2007/DocumentRels.php b/src/PhpWord/Writer/Word2007/DocumentRels.php index 0650d46a..d39527a9 100755 --- a/src/PhpWord/Writer/Word2007/DocumentRels.php +++ b/src/PhpWord/Writer/Word2007/DocumentRels.php @@ -35,7 +35,7 @@ class DocumentRels extends Base $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); // Relationship word/document.xml - $this->writeRelationship( + $this->writeRel( $xmlWriter, 1, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles', @@ -43,7 +43,7 @@ class DocumentRels extends Base ); // Relationship word/numbering.xml - $this->writeRelationship( + $this->writeRel( $xmlWriter, 2, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering', @@ -51,7 +51,7 @@ class DocumentRels extends Base ); // Relationship word/settings.xml - $this->writeRelationship( + $this->writeRel( $xmlWriter, 3, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings', @@ -59,7 +59,7 @@ class DocumentRels extends Base ); // Relationship word/settings.xml - $this->writeRelationship( + $this->writeRel( $xmlWriter, 4, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme', @@ -67,7 +67,7 @@ class DocumentRels extends Base ); // Relationship word/settings.xml - $this->writeRelationship( + $this->writeRel( $xmlWriter, 5, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings', @@ -75,73 +75,34 @@ class DocumentRels extends Base ); // Relationship word/settings.xml - $this->writeRelationship( + $this->writeRel( $xmlWriter, 6, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable', 'fontTable.xml' ); - // Relationships to Images / Embeddings / Headers / Footers - foreach ($_relsCollection as $relation) { - $relationType = $relation['type']; - $relationName = $relation['target']; - $relationId = $relation['rID']; - $targetMode = ($relationType == 'hyperlink') ? 'External' : ''; - - $this->writeRelationship( - $xmlWriter, - $relationId, - 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/' . $relationType, - $relationName, - $targetMode - ); - } - - - $xmlWriter->endElement(); + $this->writeMediaRels($xmlWriter, $_relsCollection); + $xmlWriter->endElement(); // Relationships // Return return $xmlWriter->getData(); } /** - * Write header footer rels + * Write header footer rels word/_rels/*.xml.rels * * @param array $_relsCollection */ public function writeHeaderFooterRels($_relsCollection) { - // Create XML writer $xmlWriter = $this->getXmlWriter(); - - // XML header $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); - - // Relationships $xmlWriter->startElement('Relationships'); $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); - - // Relationships to Images / Embeddings / Headers / Footers - foreach ($_relsCollection as $relation) { - $relationType = $relation['type']; - $relationName = $relation['target']; - $relationId = $relation['rID']; - $targetMode = ($relationType == 'hyperlink') ? 'External' : ''; - - $this->writeRelationship( - $xmlWriter, - $relationId, - 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/' . $relationType, - $relationName, - $targetMode - ); - } - - + $this->writeMediaRels($xmlWriter, $_relsCollection); $xmlWriter->endElement(); - // Return return $xmlWriter->getData(); } } diff --git a/src/PhpWord/Writer/Word2007/Footnotes.php b/src/PhpWord/Writer/Word2007/Footnotes.php index 628c1e35..9ae7473a 100644 --- a/src/PhpWord/Writer/Word2007/Footnotes.php +++ b/src/PhpWord/Writer/Word2007/Footnotes.php @@ -13,6 +13,7 @@ use PhpOffice\PhpWord\Element\Footnote; use PhpOffice\PhpWord\Element\Text; use PhpOffice\PhpWord\Element\Link; use PhpOffice\PhpWord\Element\Image; +use PhpOffice\PhpWord\Element\Object; use PhpOffice\PhpWord\Element\TextBreak; use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Shared\XMLWriter; @@ -119,6 +120,8 @@ class Footnotes extends Base $this->writeLink($xmlWriter, $element, true); } elseif ($element instanceof Image) { $this->writeImage($xmlWriter, $element, true); + } elseif ($element instanceof Object) { + $this->writeObject($xmlWriter, $element, true); } elseif ($element instanceof TextBreak) { $xmlWriter->writeElement('w:br'); } diff --git a/src/PhpWord/Writer/Word2007/FootnotesRels.php b/src/PhpWord/Writer/Word2007/FootnotesRels.php index 405df69f..49ca2181 100644 --- a/src/PhpWord/Writer/Word2007/FootnotesRels.php +++ b/src/PhpWord/Writer/Word2007/FootnotesRels.php @@ -24,29 +24,13 @@ class FootnotesRels extends Base */ public function writeFootnotesRels($_relsCollection) { - // Create XML writer $xmlWriter = $this->getXmlWriter(); - - // XML header $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); - - // Relationships $xmlWriter->startElement('Relationships'); $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); - - // Relationships to Links - foreach ($_relsCollection as $relation) { - $relationType = $relation['type']; - $relationName = $relation['target']; - $relationId = $relation['rID']; - $targetMode = ($relationType == 'hyperlink') ? 'External' : ''; - - $this->writeRelationship($xmlWriter, $relationId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/' . $relationType, $relationName, $targetMode); - } - + $this->writeMediaRels($xmlWriter, $_relsCollection); $xmlWriter->endElement(); - // Return return $xmlWriter->getData(); } } diff --git a/src/PhpWord/Writer/Word2007/Rels.php b/src/PhpWord/Writer/Word2007/Rels.php index a675057f..5619d978 100755 --- a/src/PhpWord/Writer/Word2007/Rels.php +++ b/src/PhpWord/Writer/Word2007/Rels.php @@ -38,7 +38,7 @@ class Rels extends Base $relationId = 1; // Relationship word/document.xml - $this->writeRelationship( + $this->writeRel( $xmlWriter, $relationId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument', @@ -46,7 +46,7 @@ class Rels extends Base ); // Relationship docProps/core.xml - $this->writeRelationship( + $this->writeRel( $xmlWriter, ++$relationId, 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties', @@ -54,7 +54,7 @@ class Rels extends Base ); // Relationship docProps/app.xml - $this->writeRelationship( + $this->writeRel( $xmlWriter, ++$relationId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties', From a2a00393c18f781fdc1ace92871ed0b2a2d74e34 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Wed, 2 Apr 2014 10:37:27 +0700 Subject: [PATCH 07/27] Remove HashTable and all related properties/methods. PHPWord doesn't (yet) need it. --- CHANGELOG.md | 1 + src/PhpWord/HashTable.php | 203 ---------------------- src/PhpWord/Writer/ODText.php | 56 ------ src/PhpWord/Writer/ODText/Manifest.php | 28 +-- src/PhpWord/Writer/RTF.php | 21 --- tests/PhpWord/Tests/Writer/ODTextTest.php | 1 - tests/PhpWord/Tests/Writer/RTFTest.php | 1 - 7 files changed, 2 insertions(+), 309 deletions(-) delete mode 100644 src/PhpWord/HashTable.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b81a016..84952a3c 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - Reader: Rename AbstractReader > Reader - @ivanlanin - General: Refactor folders: Element, Container, and Exception - @ivanlanin GH-187 - Container: Create new Container abstract class - @ivanlanin GH-187 +- General: Remove legacy HashTable and all related properties/methods - @ivanlanin GH-187 ## 0.9.1 - 27 Mar 2014 diff --git a/src/PhpWord/HashTable.php b/src/PhpWord/HashTable.php deleted file mode 100644 index 93bcb689..00000000 --- a/src/PhpWord/HashTable.php +++ /dev/null @@ -1,203 +0,0 @@ -addFromSource($pSource); - } - } - - /** - * Add HashTable items from source - * - * @param \PhpOffice\PhpWord\IComparable[] $pSource Source array to create HashTable from - * @throws \PhpOffice\PhpWord\Exception\Exception - */ - public function addFromSource($pSource = null) - { - // Check if an array was passed - if ($pSource == null) { - return; - } elseif (!is_array($pSource)) { - throw new Exception('Invalid array parameter passed.'); - } - - foreach ($pSource as $item) { - $this->add($item); - } - } - - /** - * Add HashTable item - * - * @param \PhpOffice\PhpWord\IComparable $pSource Item to add - */ - public function add(IComparable $pSource = null) - { - // Determine hashcode - $hashCode = null; - $hashIndex = $pSource->getHashIndex(); - if (is_null($hashIndex)) { - $hashCode = $pSource->getHashCode(); - } elseif (isset ($this->_keyMap[$hashIndex])) { - $hashCode = $this->_keyMap[$hashIndex]; - } else { - $hashCode = $pSource->getHashCode(); - } - - // Add value - if (!isset($this->_items[$hashCode])) { - $this->_items[$hashCode] = $pSource; - $index = count($this->_items) - 1; - $this->_keyMap[$index] = $hashCode; - $pSource->setHashIndex($index); - } else { - $pSource->setHashIndex($this->_items[$hashCode]->getHashIndex()); - } - } - - /** - * Remove HashTable item - * - * @param \PhpOffice\PhpWord\IComparable $pSource Item to remove - */ - public function remove(IComparable $pSource = null) - { - if (isset($this->_items[$pSource->getHashCode()])) { - unset($this->_items[$pSource->getHashCode()]); - - $deleteKey = -1; - foreach ($this->_keyMap as $key => $value) { - if ($deleteKey >= 0) { - $this->_keyMap[$key - 1] = $value; - } - - if ($value == $pSource->getHashCode()) { - $deleteKey = $key; - } - } - unset($this->_keyMap[count($this->_keyMap) - 1]); - } - } - - /** - * Clear HashTable - * - */ - public function clear() - { - $this->_items = array(); - $this->_keyMap = array(); - } - - /** - * Get item count - * - * @return int - */ - public function count() - { - return count($this->_items); - } - - /** - * Get hash code index - * - * @param string $pHashCode - * @return int Index - */ - public function getIndexForHashCode($pHashCode = '') - { - return array_search($pHashCode, $this->_keyMap); - } - - /** - * Get by index - * - * @param int $pIndex - * @return \PhpOffice\PhpWord\IComparable - */ - public function getByIndex($pIndex = 0) - { - if (isset($this->_keyMap[$pIndex])) { - return $this->getByHashCode($this->_keyMap[$pIndex]); - } - - return null; - } - - /** - * Get by hashcode - * @param string $pHashCode - * @return \PhpOffice\PhpWord\IComparable - * - */ - public function getByHashCode($pHashCode = '') - { - if (isset($this->_items[$pHashCode])) { - return $this->_items[$pHashCode]; - } - - return null; - } - - /** - * Convert to array - * - * @return \PhpOffice\PhpWord\IComparable[] - */ - public function toArray() - { - return $this->_items; - } - - /** - * Implement PHP __clone to create a deep clone, not just a shallow copy. - */ - public function __clone() - { - $vars = get_object_vars($this); - foreach ($vars as $key => $value) { - if (is_object($value)) { - $this->$key = clone $value; - } - } - } -} diff --git a/src/PhpWord/Writer/ODText.php b/src/PhpWord/Writer/ODText.php index 72955ef1..04ec0bec 100755 --- a/src/PhpWord/Writer/ODText.php +++ b/src/PhpWord/Writer/ODText.php @@ -11,7 +11,6 @@ namespace PhpOffice\PhpWord\Writer; use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\HashTable; use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Writer\ODText\Content; use PhpOffice\PhpWord\Writer\ODText\Manifest; @@ -24,12 +23,6 @@ use PhpOffice\PhpWord\Writer\ODText\Styles; */ class ODText extends Writer implements IWriter { - /** - * Private unique PHPWord_Worksheet_BaseDrawing HashTable - * - * @var HashTable - */ - private $drawingHashTable; /** * Create new ODText writer @@ -49,9 +42,6 @@ class ODText extends Writer implements IWriter foreach ($this->writerParts as $writer) { $writer->setParentWriter($this); } - - // Set HashTable variables - $this->drawingHashTable = new HashTable(); } /** @@ -103,42 +93,6 @@ class ODText extends Writer implements IWriter // Add META-INF/manifest.xml $objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('manifest')->writeManifest($this->phpWord)); - // Add media. Has not used yet. Legacy from PHPExcel. - // @codeCoverageIgnoreStart - for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) { - if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_Drawing) { - $imageContents = null; - $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath(); - - if (strpos($imagePath, 'zip://') !== false) { - $imagePath = substr($imagePath, 6); - $imagePathSplitted = explode('#', $imagePath); - - $zipClass = Settings::getZipClass(); - $imageZip = new $zipClass(); - $imageZip->open($imagePathSplitted[0]); - $imageContents = $imageZip->getFromName($imagePathSplitted[1]); - $imageZip->close(); - unset($imageZip); - } else { - $imageContents = file_get_contents($imagePath); - } - - $objZip->addFromString('Pictures/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents); - } elseif ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_MemoryDrawing) { - ob_start(); - call_user_func( - $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(), - $this->getDrawingHashTable()->getByIndex($i)->getImageResource() - ); - $imageContents = ob_get_contents(); - ob_end_clean(); - - $objZip->addFromString('Pictures/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents); - } - } - // @codeCoverageIgnoreEnd - // Close file if ($objZip->close() === false) { throw new Exception("Could not close zip file $pFilename."); @@ -149,14 +103,4 @@ class ODText extends Writer implements IWriter throw new Exception("PhpWord object unassigned."); } } - - /** - * Get PHPWord_Worksheet_BaseDrawing HashTable - * - * @return HashTable - */ - public function getDrawingHashTable() - { - return $this->drawingHashTable; - } } diff --git a/src/PhpWord/Writer/ODText/Manifest.php b/src/PhpWord/Writer/ODText/Manifest.php index 69949c73..d7ab6cdc 100755 --- a/src/PhpWord/Writer/ODText/Manifest.php +++ b/src/PhpWord/Writer/ODText/Manifest.php @@ -59,33 +59,7 @@ class Manifest extends WriterPart $xmlWriter->writeAttribute('manifest:full-path', 'styles.xml'); $xmlWriter->endElement(); - // Not used yet. Legacy from PHPExcel - // @codeCoverageIgnoreStart - for ($i = 0; $i < $this->getParentWriter()->getDrawingHashTable()->count(); ++$i) { - if ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_Drawing) { - $extension = strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getExtension()); - $mimeType = $this->getImageMimeType($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getPath()); - - $xmlWriter->startElement('manifest:file-entry'); - $xmlWriter->writeAttribute('manifest:media-type', $mimeType); - $xmlWriter->writeAttribute('manifest:full-path', 'Pictures/' . str_replace(' ', '_', $this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getIndexedFilename())); - $xmlWriter->endElement(); - } elseif ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_MemoryDrawing) { - $extension = strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType()); - $extension = explode('/', $extension); - $extension = $extension[1]; - - $mimeType = $this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType(); - - $xmlWriter->startElement('manifest:file-entry'); - $xmlWriter->writeAttribute('manifest:media-type', $mimeType); - $xmlWriter->writeAttribute('manifest:full-path', 'Pictures/' . str_replace(' ', '_', $this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getIndexedFilename())); - $xmlWriter->endElement(); - } - } - // @codeCoverageIgnoreEnd - - $xmlWriter->endElement(); + $xmlWriter->endElement(); // manifest:manifest // Return return $xmlWriter->getData(); diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index b5ce20a7..6d693f17 100755 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -11,7 +11,6 @@ namespace PhpOffice\PhpWord\Writer; use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\HashTable; use PhpOffice\PhpWord\Element\Image; use PhpOffice\PhpWord\Element\Link; use PhpOffice\PhpWord\Element\ListItem; @@ -33,13 +32,6 @@ use PhpOffice\PhpWord\TOC; */ class RTF extends Writer implements IWriter { - /** - * Private unique PHPWord_Worksheet_BaseDrawing HashTable - * - * @var HashTable - */ - private $drawingHashTable; - /** * Color register * @@ -69,9 +61,6 @@ class RTF extends Writer implements IWriter { // Assign PhpWord $this->setPhpWord($phpWord); - - // Set HashTable variables - $this->drawingHashTable = new HashTable(); } /** @@ -95,16 +84,6 @@ class RTF extends Writer implements IWriter } } - /** - * Get PHPWord_Worksheet_BaseDrawing HashTable - * - * @return HashTable - */ - public function getDrawingHashTable() - { - return $this->drawingHashTable; - } - /** * Get all data * diff --git a/tests/PhpWord/Tests/Writer/ODTextTest.php b/tests/PhpWord/Tests/Writer/ODTextTest.php index 9ee31432..82263c0f 100644 --- a/tests/PhpWord/Tests/Writer/ODTextTest.php +++ b/tests/PhpWord/Tests/Writer/ODTextTest.php @@ -26,7 +26,6 @@ class ODTextTest extends \PHPUnit_Framework_TestCase $object = new ODText(new PhpWord()); $this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $object->getPhpWord()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\HashTable', $object->getDrawingHashTable()); $this->assertEquals('./', $object->getDiskCachingDirectory()); foreach (array('Content', 'Manifest', 'Meta', 'Mimetype', 'Styles') as $part) { diff --git a/tests/PhpWord/Tests/Writer/RTFTest.php b/tests/PhpWord/Tests/Writer/RTFTest.php index 4016cb85..4872ef39 100644 --- a/tests/PhpWord/Tests/Writer/RTFTest.php +++ b/tests/PhpWord/Tests/Writer/RTFTest.php @@ -26,7 +26,6 @@ class RTFTest extends \PHPUnit_Framework_TestCase $object = new RTF(new PhpWord); $this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $object->getPhpWord()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\HashTable', $object->getDrawingHashTable()); } /** From f0ee25f3437fd8101d55c2afe8372ab62133fc99 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Wed, 2 Apr 2014 11:02:56 +0700 Subject: [PATCH 08/27] Deprecate `createSection` in favor of `addSection` --- CHANGELOG.md | 1 + README.md | 2 +- docs/containers.rst | 18 ++++----- docs/elements.rst | 24 +++++------ docs/general.rst | 4 +- samples/Sample_01_SimpleText.php | 2 +- samples/Sample_02_TabStops.php | 2 +- samples/Sample_03_Sections.php | 12 +++--- samples/Sample_04_Textrun.php | 4 +- samples/Sample_05_Multicolumn.php | 10 ++--- samples/Sample_06_Footnote.php | 8 ++-- samples/Sample_08_ParagraphPagination.php | 2 +- samples/Sample_09_Tables.php | 2 +- samples/Sample_10_EastAsianFontStyle.php | 2 +- samples/Sample_12_HeaderFooter.php | 12 +++--- samples/Sample_13_Images.php | 2 +- samples/Sample_14_ListItem.php | 2 +- samples/Sample_15_Link.php | 2 +- samples/Sample_16_Object.php | 2 +- samples/Sample_17_TitleTOC.php | 2 +- samples/Sample_18_Watermark.php | 4 +- samples/Sample_19_TextBreak.php | 2 +- samples/Sample_20_BGColor.php | 2 +- samples/Sample_21_TableRowRules.php | 2 +- samples/Sample_22_CheckBox.php | 2 +- src/PhpWord/PhpWord.php | 27 +++++++++---- src/PhpWord/Reader/Word2007.php | 4 +- tests/PhpWord/Tests/Container/FooterTest.php | 2 +- tests/PhpWord/Tests/Container/HeaderTest.php | 2 +- tests/PhpWord/Tests/Container/SectionTest.php | 4 +- .../PhpWord/Tests/Element/Table/CellTest.php | 2 +- tests/PhpWord/Tests/Element/TextRunTest.php | 2 +- tests/PhpWord/Tests/PhpWordTest.php | 4 +- tests/PhpWord/Tests/Style/FontTest.php | 2 +- tests/PhpWord/Tests/Style/ParagraphTest.php | 2 +- .../Tests/Writer/ODText/ContentTest.php | 4 +- tests/PhpWord/Tests/Writer/ODTextTest.php | 8 ++-- tests/PhpWord/Tests/Writer/RTFTest.php | 8 ++-- .../Tests/Writer/Word2007/BaseTest.php | 40 +++++++++---------- .../Tests/Writer/Word2007/DocumentTest.php | 8 ++-- .../Tests/Writer/Word2007/FooterTest.php | 2 +- .../Tests/Writer/Word2007/FootnotesTest.php | 6 +-- .../Tests/Writer/Word2007/HeaderTest.php | 2 +- tests/PhpWord/Tests/Writer/Word2007Test.php | 20 +++++----- 44 files changed, 145 insertions(+), 131 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84952a3c..28b6326b 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - `createFootnote` replaced by `addFootnote` - `createHeader` replaced by `addHeader` - `createFooter` replaced by `addFooter` +- `createSection` replaced by `addSection` ### Miscellaneous diff --git a/README.md b/README.md index 8d5370eb..95d5947e 100755 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ $phpWord = new \PhpOffice\PhpWord\PhpWord(); // Every element you want to append to the word document is placed in a section. // To create a basic section: -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); // After creating a section, you can append elements: $section->addText('Hello world!'); diff --git a/docs/containers.rst b/docs/containers.rst index dc8f2f9a..53579537 100644 --- a/docs/containers.rst +++ b/docs/containers.rst @@ -16,7 +16,7 @@ section, use the following code: .. code-block:: php - $section = $phpWord->createSection($sectionSettings); + $section = $phpWord->addSection($sectionSettings); The ``$sectionSettings`` is an optional associative array that sets the section. Example: @@ -70,10 +70,10 @@ property of the section. .. code-block:: php // Method 1 - $section = $phpWord->createSection(array('pageNumberingStart' => 1)); + $section = $phpWord->addSection(array('pageNumberingStart' => 1)); // Method 2 - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->getSettings()->setPageNumberingStart(1); Multicolumn @@ -85,10 +85,10 @@ using the ``breakType`` and ``colsNum`` property of the section. .. code-block:: php // Method 1 - $section = $phpWord->createSection(array('breakType' => 'continuous', 'colsNum' => 2)); + $section = $phpWord->addSection(array('breakType' => 'continuous', 'colsNum' => 2)); // Method 2 - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->getSettings()->setBreakType('continuous'); $section->getSettings()->setColsNum(2); @@ -96,11 +96,11 @@ Headers ------- Each section can have its own header reference. To create a header use -the ``createHeader`` method: +the ``addHeader`` method: .. code-block:: php - $header = $section->createHeader(); + $header = $section->addHeader(); Be sure to save the result in a local object. You can use all elements that are available for the footer. See "Footer" section for detail. @@ -111,11 +111,11 @@ Footers ------- Each section can have its own footer reference. To create a footer, use -the ``createFooter`` method: +the ``addFooter`` method: .. code-block:: php - $footer = $section->createFooter(); + $footer = $section->addFooter(); Be sure to save the result in a local object to add elements to a footer. You can add the following elements to footers: diff --git a/docs/elements.rst b/docs/elements.rst index d1634bf4..da2038e5 100644 --- a/docs/elements.rst +++ b/docs/elements.rst @@ -51,9 +51,9 @@ Legend: Texts ----- -Text can be added by using ``addText`` and ``createTextRun`` method. +Text can be added by using ``addText`` and ``addTextRun`` method. ``addText`` is used for creating simple paragraphs that only contain -texts with the same style. ``createTextRun`` is used for creating +texts with the same style. ``addTextRun`` is used for creating complex paragraphs that contain text with different style (some bold, other italics, etc) or other elements, e.g. images or links. The syntaxes are as follow: @@ -61,7 +61,7 @@ syntaxes are as follow: .. code-block:: php $section->addText($text, [$fontStyle], [$paragraphStyle]); - $textrun = $section->createTextRun([$paragraphStyle]); + $textrun = $section->addTextRun([$paragraphStyle]); Text styles ~~~~~~~~~~~ @@ -79,7 +79,7 @@ Inline style examples: $paragraphStyle = array('align' => 'both'); $section->addText('I am simple paragraph', $fontStyle, $paragraphStyle); - $textrun = $section->createTextRun(); + $textrun = $section->addTextRun(); $textrun->addText('I am bold', array('bold' => true)); $textrun->addText('I am italic', array('italic' => true)); $textrun->addText('I am colored, array('color' => 'AACC00')); @@ -300,7 +300,7 @@ Examples: .. code-block:: php - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addImage( 'mars.jpg', array( @@ -311,9 +311,9 @@ Examples: 'wrappingStyle' => 'behind' ) ); - $footer = $section->createFooter(); + $footer = $section->addFooter(); $footer->addImage('http://example.com/image.php'); - $textrun = $section->createTextRun(); + $textrun = $section->addTextRun(); $textrun->addImage('http://php.net/logo.jpg'); Image styles @@ -338,8 +338,8 @@ header reference. After creating a header, you can use the .. code-block:: php - $section = $phpWord->createSection(); - $header = $section->createHeader(); + $section = $phpWord->addSection(); + $header = $section->addHeader(); $header->addWatermark('resources/_earth.jpg', array('marginTop' => 200, 'marginLeft' => 55)); Objects @@ -380,9 +380,9 @@ On textrun: .. code-block:: php - $textrun = $section->createTextRun(); + $textrun = $section->addTextRun(); $textrun->addText('Lead text.'); - $footnote = $textrun->createFootnote(); + $footnote = $textrun->addFootnote(); $footnote->addText('Footnote text can have '); $footnote->addLink('http://test.com', 'links'); $footnote->addText('.'); @@ -395,7 +395,7 @@ On text: .. code-block:: php $section->addText('Lead text.'); - $footnote = $section->createFootnote(); + $footnote = $section->addFootnote(); $footnote->addText('Footnote text.'); The footnote reference number will be displayed with decimal number starting diff --git a/docs/general.rst b/docs/general.rst index 1c810722..e1545a04 100644 --- a/docs/general.rst +++ b/docs/general.rst @@ -19,7 +19,7 @@ folder `__. // Every element you want to append to the word document is placed in a section. // To create a basic section: - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); // After creating a section, you can append elements: $section->addText('Hello world!'); @@ -136,7 +136,7 @@ points to twips. 'spaceAfter' => \PhpOffice\PhpWord\Shared\Font::pointSizeToTwips(6)) ); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $sectionStyle = $section->getSettings(); // half inch left margin $sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Font::inchSizeToTwips(.5)); diff --git a/samples/Sample_01_SimpleText.php b/samples/Sample_01_SimpleText.php index 7ada399a..4405eaeb 100755 --- a/samples/Sample_01_SimpleText.php +++ b/samples/Sample_01_SimpleText.php @@ -9,7 +9,7 @@ $phpWord->addParagraphStyle('pStyle', array('align' => 'center', 'spaceAfter' => $phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240)); // New portrait section -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); // Simple text $section->addTitle('Welcome to PhpWord', 1); diff --git a/samples/Sample_02_TabStops.php b/samples/Sample_02_TabStops.php index f08f1e15..01dc7617 100755 --- a/samples/Sample_02_TabStops.php +++ b/samples/Sample_02_TabStops.php @@ -25,7 +25,7 @@ $phpWord->addParagraphStyle('centerTab', array( )); // New portrait section -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); // Add listitem elements $section->addText("Multiple Tabs:\tOne\tTwo\tThree", NULL, 'multipleTab'); diff --git a/samples/Sample_03_Sections.php b/samples/Sample_03_Sections.php index c16b73db..7289fef0 100755 --- a/samples/Sample_03_Sections.php +++ b/samples/Sample_03_Sections.php @@ -6,24 +6,24 @@ echo date('H:i:s') , ' Create new PhpWord object' , \EOL; $phpWord = new \PhpOffice\PhpWord\PhpWord(); // New portrait section -$section = $phpWord->createSection(array('borderColor' => '00FF00', 'borderSize' => 12)); +$section = $phpWord->addSection(array('borderColor' => '00FF00', 'borderSize' => 12)); $section->addText('I am placed on a default section.'); // New landscape section -$section = $phpWord->createSection(array('orientation' => 'landscape')); +$section = $phpWord->addSection(array('orientation' => 'landscape')); $section->addText('I am placed on a landscape section. Every page starting from this section will be landscape style.'); $section->addPageBreak(); $section->addPageBreak(); // New portrait section -$section = $phpWord->createSection(array('marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600)); +$section = $phpWord->addSection(array('marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600)); $section->addText('This section uses other margins.'); // New portrait section with Header & Footer -$section = $phpWord->createSection(array('marginLeft' => 200, 'marginRight' => 200, 'marginTop' => 200, 'marginBottom' => 200, 'headerHeight' => 50, 'footerHeight' => 50,)); +$section = $phpWord->addSection(array('marginLeft' => 200, 'marginRight' => 200, 'marginTop' => 200, 'marginBottom' => 200, 'headerHeight' => 50, 'footerHeight' => 50,)); $section->addText('This section and we play with header/footer height.'); -$section->createHeader()->addText('Header'); -$section->createFooter()->addText('Footer'); +$section->addHeader()->addText('Header'); +$section->addFooter()->addText('Footer'); // Save file $name = basename(__FILE__, '.php'); diff --git a/samples/Sample_04_Textrun.php b/samples/Sample_04_Textrun.php index d95297b8..6c13a84f 100644 --- a/samples/Sample_04_Textrun.php +++ b/samples/Sample_04_Textrun.php @@ -12,10 +12,10 @@ $phpWord->addFontStyle('ColoredText', array('color'=>'FF8080')); $phpWord->addLinkStyle('NLink', array('color'=>'0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE)); // New portrait section -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); // Add text run -$textrun = $section->createTextRun('pStyle'); +$textrun = $section->addTextRun('pStyle'); $textrun->addText('Each textrun can contain native text, link elements or an image.'); $textrun->addText(' No break is placed after adding an element.', 'BoldText'); diff --git a/samples/Sample_05_Multicolumn.php b/samples/Sample_05_Multicolumn.php index 541e2e8a..4d81766e 100644 --- a/samples/Sample_05_Multicolumn.php +++ b/samples/Sample_05_Multicolumn.php @@ -10,29 +10,29 @@ $filler = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ' . 'Suspendisse congue congue leo sed pellentesque.'; // Normal -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); $section->addText('Normal paragraph. ' . $filler); // Two columns -$section = $phpWord->createSection(array( +$section = $phpWord->addSection(array( 'colsNum' => 2, 'colsSpace' => 1440, 'breakType' => 'continuous')); $section->addText('Three columns, one inch (1440 twips) spacing. ' . $filler); // Normal -$section = $phpWord->createSection(array('breakType' => 'continuous')); +$section = $phpWord->addSection(array('breakType' => 'continuous')); $section->addText('Normal paragraph again. ' . $filler); // Three columns -$section = $phpWord->createSection(array( +$section = $phpWord->addSection(array( 'colsNum' => 3, 'colsSpace' => 720, 'breakType' => 'continuous')); $section->addText('Three columns, half inch (720 twips) spacing. ' . $filler); // Normal -$section = $phpWord->createSection(array('breakType' => 'continuous')); +$section = $phpWord->addSection(array('breakType' => 'continuous')); $section->addText('Normal paragraph again.'); // Save file diff --git a/samples/Sample_06_Footnote.php b/samples/Sample_06_Footnote.php index 57421eff..270573d9 100755 --- a/samples/Sample_06_Footnote.php +++ b/samples/Sample_06_Footnote.php @@ -7,7 +7,7 @@ $phpWord = new \PhpOffice\PhpWord\PhpWord(); \PhpOffice\PhpWord\Settings::setCompatibility(false); // New portrait section -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); // Add style definitions $phpWord->addParagraphStyle('pStyle', array('spacing'=>100)); @@ -16,10 +16,10 @@ $phpWord->addFontStyle('ColoredText', array('color'=>'FF8080')); $phpWord->addLinkStyle('NLink', array('color'=>'0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE)); // Add text elements -$textrun = $section->createTextRun('pStyle'); +$textrun = $section->addTextRun('pStyle'); $textrun->addText('This is some lead text in a paragraph with a following footnote. ','pStyle'); -$footnote = $textrun->createFootnote(); +$footnote = $textrun->addFootnote(); $footnote->addText('Just like a textrun, a footnote can contain native texts. '); $footnote->addText('No break is placed after adding an element. ', 'BoldText'); $footnote->addText('All elements are placed inside a paragraph. ', 'ColoredText'); @@ -34,7 +34,7 @@ $footnote->addObject('resources/_sheet.xls'); $footnote->addText('But you can only put footnote in section, not in header or footer.'); $section->addText('You can also create the footnote directly from the section making it wrap in a paragraph like the footnote below this paragraph. But is is best used from within a textrun.'); -$footnote = $section->createFootnote(); +$footnote = $section->addFootnote(); $footnote->addText('The reference for this is wrapped in its own line'); // Save file diff --git a/samples/Sample_08_ParagraphPagination.php b/samples/Sample_08_ParagraphPagination.php index eeef6058..4b184cfd 100644 --- a/samples/Sample_08_ParagraphPagination.php +++ b/samples/Sample_08_ParagraphPagination.php @@ -11,7 +11,7 @@ $phpWord->setDefaultParagraphStyle(array( )); // Sample -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); $section->addText('Below are the samples on how to control your paragraph ' . 'pagination. See "Line and Page Break" tab on paragraph properties ' . diff --git a/samples/Sample_09_Tables.php b/samples/Sample_09_Tables.php index 0c9fbeb5..d841092c 100644 --- a/samples/Sample_09_Tables.php +++ b/samples/Sample_09_Tables.php @@ -4,7 +4,7 @@ include_once 'Sample_Header.php'; // New Word Document echo date('H:i:s') , ' Create new PhpWord object' , \EOL; $phpWord = new \PhpOffice\PhpWord\PhpWord(); -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); $header = array('size' => 16, 'bold' => true); // 1. Basic table diff --git a/samples/Sample_10_EastAsianFontStyle.php b/samples/Sample_10_EastAsianFontStyle.php index 6e8b26a0..d4ea2c4a 100644 --- a/samples/Sample_10_EastAsianFontStyle.php +++ b/samples/Sample_10_EastAsianFontStyle.php @@ -4,7 +4,7 @@ include_once 'Sample_Header.php'; // New Word Document echo date('H:i:s') , ' Create new PhpWord object' , \EOL; $phpWord = new \PhpOffice\PhpWord\PhpWord(); -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); $header = array('size' => 16, 'bold' => true); //1.Use EastAisa FontStyle $section->addText('中文楷体样式测试',array('name' => '楷体', 'size' => 16, 'color' => '1B2232')); diff --git a/samples/Sample_12_HeaderFooter.php b/samples/Sample_12_HeaderFooter.php index 19139ab2..a30358f2 100644 --- a/samples/Sample_12_HeaderFooter.php +++ b/samples/Sample_12_HeaderFooter.php @@ -6,10 +6,10 @@ echo date('H:i:s') , " Create new PhpWord object" , \EOL; $phpWord = new \PhpOffice\PhpWord\PhpWord(); // New portrait section -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); // Add first page header -$header = $section->createHeader(); +$header = $section->addHeader(); $header->firstPage(); $table = $header->addTable(); $table->addRow(); @@ -23,11 +23,11 @@ $table->addCell(4500)->addImage( ); // Add header for all other pages -$subsequent = $section->createHeader(); +$subsequent = $section->addHeader(); $subsequent->addText("Subsequent pages in Section 1 will Have this!"); // Add footer -$footer = $section->createFooter(); +$footer = $section->addFooter(); $footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', array('align' => 'center')); $footer->addLink('http://google.com', 'Direct Google'); @@ -50,9 +50,9 @@ $section->addTextBreak(); $section->addText('Some text...'); // New portrait section -$section2 = $phpWord->createSection(); +$section2 = $phpWord->addSection(); -$sec2Header = $section2->createHeader(); +$sec2Header = $section2->addHeader(); $sec2Header->addText("All pages in Section 2 will Have this!"); // Write some text diff --git a/samples/Sample_13_Images.php b/samples/Sample_13_Images.php index cf467464..405f653b 100644 --- a/samples/Sample_13_Images.php +++ b/samples/Sample_13_Images.php @@ -6,7 +6,7 @@ echo date('H:i:s'), " Create new PhpWord object", \EOL; $phpWord = new \PhpOffice\PhpWord\PhpWord(); // Begin code -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); $section->addText('Local image without any styles:'); $section->addImage('resources/_mars.jpg'); $section->addTextBreak(2); diff --git a/samples/Sample_14_ListItem.php b/samples/Sample_14_ListItem.php index 45cb73ab..a7dc48ff 100644 --- a/samples/Sample_14_ListItem.php +++ b/samples/Sample_14_ListItem.php @@ -6,7 +6,7 @@ echo date('H:i:s'), " Create new PhpWord object", \EOL; $phpWord = new \PhpOffice\PhpWord\PhpWord(); // Begin code -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); // Add listitem elements $section->addListItem('List Item 1', 0); diff --git a/samples/Sample_15_Link.php b/samples/Sample_15_Link.php index 06628de5..a2e4f9e1 100644 --- a/samples/Sample_15_Link.php +++ b/samples/Sample_15_Link.php @@ -6,7 +6,7 @@ echo date('H:i:s'), " Create new PhpWord object", \EOL; $phpWord = new \PhpOffice\PhpWord\PhpWord(); // Begin code -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); // Add hyperlink elements $section->addLink('http://www.google.com', 'Best search engine', array('color'=>'0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE)); diff --git a/samples/Sample_16_Object.php b/samples/Sample_16_Object.php index 1a7f557a..ec2dbce4 100644 --- a/samples/Sample_16_Object.php +++ b/samples/Sample_16_Object.php @@ -6,7 +6,7 @@ echo date('H:i:s'), " Create new PhpWord object", \EOL; $phpWord = new \PhpOffice\PhpWord\PhpWord(); // Begin code -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); $section->addText('You can open this OLE object by double clicking on the icon:'); $section->addTextBreak(2); $section->addObject('resources/_sheet.xls'); diff --git a/samples/Sample_17_TitleTOC.php b/samples/Sample_17_TitleTOC.php index 27982921..951632af 100644 --- a/samples/Sample_17_TitleTOC.php +++ b/samples/Sample_17_TitleTOC.php @@ -6,7 +6,7 @@ echo date('H:i:s'), " Create new PhpWord object", \EOL; $phpWord = new \PhpOffice\PhpWord\PhpWord(); // Begin code -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); // Define the TOC font style $fontStyle = array('spaceAfter'=>60, 'size'=>12); diff --git a/samples/Sample_18_Watermark.php b/samples/Sample_18_Watermark.php index ff299c97..96fe5c99 100644 --- a/samples/Sample_18_Watermark.php +++ b/samples/Sample_18_Watermark.php @@ -7,8 +7,8 @@ $phpWord = new \PhpOffice\PhpWord\PhpWord(); // Begin code -$section = $phpWord->createSection(); -$header = $section->createHeader(); +$section = $phpWord->addSection(); +$header = $section->addHeader(); $header->addWatermark('resources/_earth.jpg', array('marginTop' => 200, 'marginLeft' => 55)); $section->addText('The header reference to the current section includes a watermark image.'); diff --git a/samples/Sample_19_TextBreak.php b/samples/Sample_19_TextBreak.php index 9433222c..b594bebd 100644 --- a/samples/Sample_19_TextBreak.php +++ b/samples/Sample_19_TextBreak.php @@ -12,7 +12,7 @@ $phpWord->addFontStyle('fontStyle', array('size' => 9)); $phpWord->addParagraphStyle('paragraphStyle', array('spacing' => 480)); $fontStyle = array('size' => 24); -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); $section->addText('Text break with no style:'); $section->addTextBreak(); $section->addText('Text break with defined font style:'); diff --git a/samples/Sample_20_BGColor.php b/samples/Sample_20_BGColor.php index 46a232f7..ed6ec320 100644 --- a/samples/Sample_20_BGColor.php +++ b/samples/Sample_20_BGColor.php @@ -4,7 +4,7 @@ include_once 'Sample_Header.php'; // New Word document echo date('H:i:s'), " Create new PhpWord object", \EOL; $phpWord = new \PhpOffice\PhpWord\PhpWord(); -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); $section->addText("This is some text highlighted using fgColor (limited to 15 colors) ", array("fgColor" => \PhpOffice\PhpWord\Style\Font::FGCOLOR_YELLOW)); $section->addText("This one uses bgColor and is using hex value (0xfbbb10)", array("bgColor" => "fbbb10")); diff --git a/samples/Sample_21_TableRowRules.php b/samples/Sample_21_TableRowRules.php index c02ac2ba..6f3575cd 100644 --- a/samples/Sample_21_TableRowRules.php +++ b/samples/Sample_21_TableRowRules.php @@ -4,7 +4,7 @@ include_once 'Sample_Header.php'; // New Word document echo date('H:i:s'), " Create new PhpWord object", \EOL; $phpWord = new \PhpOffice\PhpWord\PhpWord(); -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); $section->addText("By default, when you insert an image, it adds a textbreak after its content."); $section->addText("If we want a simple border around an image, we wrap the image inside a table->row->cell"); diff --git a/samples/Sample_22_CheckBox.php b/samples/Sample_22_CheckBox.php index 505d3518..3c2b64e5 100644 --- a/samples/Sample_22_CheckBox.php +++ b/samples/Sample_22_CheckBox.php @@ -5,7 +5,7 @@ include_once 'Sample_Header.php'; echo date('H:i:s'), " Create new PhpWord object", \EOL; $phpWord = new \PhpOffice\PhpWord\PhpWord(); -$section = $phpWord->createSection(); +$section = $phpWord->addSection(); $section->addText('Check box in section'); $section->addCheckBox('chkBox1', 'Checkbox 1'); $section->addText('Check box in table cell'); diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index 894d7df7..89ee8d94 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -23,6 +23,7 @@ class PhpWord const DEFAULT_FONT_COLOR = '000000'; // HEX const DEFAULT_FONT_CONTENT_TYPE = 'default'; // default|eastAsia|cs const DEFAULT_FONT_NAME = 'Arial'; + /** * Default font size, in points. * @@ -34,7 +35,7 @@ class PhpWord /** * Document properties object * - * @var \PhpOffice\PhpWord\DocumentProperties + * @var DocumentProperties */ private $_documentProperties; @@ -54,7 +55,7 @@ class PhpWord /** * Collection of sections * - * @var \PhpOffice\PhpWord\Section[] + * @var Section[] */ private $_sections = array(); @@ -71,7 +72,7 @@ class PhpWord /** * Get document properties object * - * @return \PhpOffice\PhpWord\DocumentProperties + * @return DocumentProperties */ public function getDocumentProperties() { @@ -95,9 +96,9 @@ class PhpWord * Create new section * * @param \PhpOffice\PhpWord\Container\Settings $settings - * @return \PhpOffice\PhpWord\Container\Section + * @return Section */ - public function createSection($settings = null) + public function addSection($settings = null) { $section = new Section(\count($this->_sections) + 1, $settings); $this->_sections[] = $section; @@ -227,8 +228,8 @@ class PhpWord * Load template by filename * * @param string $filename Fully qualified filename. - * @return \PhpOffice\PhpWord\Template - * @throws \PhpOffice\PhpWord\Exception\Exception + * @return Template + * @throws Exception */ public function loadTemplate($filename) { @@ -238,4 +239,16 @@ class PhpWord throw new Exception("Template file {$filename} not found."); } } + + /** + * Create new section + * + * @param \PhpOffice\PhpWord\Container\Settings $settings + * @return Section + * @deprecated 0.9.2 + */ + public function createSection($settings = null) + { + return $this->addSection($settings); + } } diff --git a/src/PhpWord/Reader/Word2007.php b/src/PhpWord/Reader/Word2007.php index 36ff79e3..be84d636 100644 --- a/src/PhpWord/Reader/Word2007.php +++ b/src/PhpWord/Reader/Word2007.php @@ -173,7 +173,7 @@ class Word2007 extends Reader implements IReader ); $xmlDoc = simplexml_load_string($this->getFromZipArchive($zip, "{$rel['Target']}", true)); if (is_object($xmlDoc)) { - $section = $word->createSection(); + $section = $word->addSection(); foreach ($xmlDoc->body->children() as $elm) { $elmName = $elm->getName(); @@ -181,7 +181,7 @@ class Word2007 extends Reader implements IReader // Create new section if section setting found if ($elm->pPr->sectPr) { $section->setSettings($this->loadSectionSettings($elm->pPr)); - $section = $word->createSection(); + $section = $word->addSection(); continue; } // Has w:r? It's either text or textrun diff --git a/tests/PhpWord/Tests/Container/FooterTest.php b/tests/PhpWord/Tests/Container/FooterTest.php index 74a09a51..ba712903 100644 --- a/tests/PhpWord/Tests/Container/FooterTest.php +++ b/tests/PhpWord/Tests/Container/FooterTest.php @@ -73,7 +73,7 @@ class FooterTest extends \PHPUnit_Framework_TestCase public function testCreateTextRun() { $oFooter = new Footer(1); - $element = $oFooter->createTextRun(); + $element = $oFooter->addTextRun(); $this->assertCount(1, $oFooter->getElements()); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $element); diff --git a/tests/PhpWord/Tests/Container/HeaderTest.php b/tests/PhpWord/Tests/Container/HeaderTest.php index 9d479ade..dab67048 100644 --- a/tests/PhpWord/Tests/Container/HeaderTest.php +++ b/tests/PhpWord/Tests/Container/HeaderTest.php @@ -84,7 +84,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase public function testCreateTextRun() { $oHeader = new Header(1); - $element = $oHeader->createTextRun(); + $element = $oHeader->addTextRun(); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $element); $this->assertCount(1, $oHeader->getElements()); } diff --git a/tests/PhpWord/Tests/Container/SectionTest.php b/tests/PhpWord/Tests/Container/SectionTest.php index 0517399c..a289df49 100644 --- a/tests/PhpWord/Tests/Container/SectionTest.php +++ b/tests/PhpWord/Tests/Container/SectionTest.php @@ -86,8 +86,8 @@ class SectionTest extends \PHPUnit_Framework_TestCase $section->addImage($imageSource); $section->addMemoryImage($imageUrl); $section->addTitle(utf8_decode('ä'), 1); - $section->createTextRun(); - $section->createFootnote(); + $section->addTextRun(); + $section->addFootnote(); $section->addCheckBox(utf8_decode('chkä'), utf8_decode('Contentä')); $section->addTOC(); diff --git a/tests/PhpWord/Tests/Element/Table/CellTest.php b/tests/PhpWord/Tests/Element/Table/CellTest.php index d16f275a..bc0a8db1 100644 --- a/tests/PhpWord/Tests/Element/Table/CellTest.php +++ b/tests/PhpWord/Tests/Element/Table/CellTest.php @@ -276,7 +276,7 @@ class CellTest extends \PHPUnit_Framework_TestCase public function testCreateTextRun() { $oCell = new Cell('section', 1); - $element = $oCell->createTextRun(); + $element = $oCell->addTextRun(); $this->assertCount(1, $oCell->getElements()); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $element); diff --git a/tests/PhpWord/Tests/Element/TextRunTest.php b/tests/PhpWord/Tests/Element/TextRunTest.php index e71ce108..6f277e82 100644 --- a/tests/PhpWord/Tests/Element/TextRunTest.php +++ b/tests/PhpWord/Tests/Element/TextRunTest.php @@ -138,7 +138,7 @@ class TextRunTest extends \PHPUnit_Framework_TestCase public function testCreateFootnote() { $oTextRun = new TextRun(); - $element = $oTextRun->createFootnote(); + $element = $oTextRun->addFootnote(); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Footnote', $element); $this->assertCount(1, $oTextRun->getElements()); diff --git a/tests/PhpWord/Tests/PhpWordTest.php b/tests/PhpWord/Tests/PhpWordTest.php index 2cf5ae62..829ddfff 100644 --- a/tests/PhpWord/Tests/PhpWordTest.php +++ b/tests/PhpWord/Tests/PhpWordTest.php @@ -51,8 +51,8 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase public function testCreateGetSections() { $phpWord = new PhpWord(); - $this->assertEquals(new Section(1), $phpWord->createSection()); - $phpWord->createSection(); + $this->assertEquals(new Section(1), $phpWord->addSection()); + $phpWord->addSection(); $this->assertEquals(2, \count($phpWord->getSections())); } diff --git a/tests/PhpWord/Tests/Style/FontTest.php b/tests/PhpWord/Tests/Style/FontTest.php index 47ed57f4..d26b97fe 100644 --- a/tests/PhpWord/Tests/Style/FontTest.php +++ b/tests/PhpWord/Tests/Style/FontTest.php @@ -103,7 +103,7 @@ class FontTest extends \PHPUnit_Framework_TestCase public function testLineHeight() { $phpWord = new PhpWord(); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); // Test style array $text = $section->addText('This is a test', array( diff --git a/tests/PhpWord/Tests/Style/ParagraphTest.php b/tests/PhpWord/Tests/Style/ParagraphTest.php index 4c424a93..10a74dae 100644 --- a/tests/PhpWord/Tests/Style/ParagraphTest.php +++ b/tests/PhpWord/Tests/Style/ParagraphTest.php @@ -105,7 +105,7 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase public function testLineHeight() { $phpWord = new PhpWord(); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); // Test style array $text = $section->addText('This is a test', array(), array( diff --git a/tests/PhpWord/Tests/Writer/ODText/ContentTest.php b/tests/PhpWord/Tests/Writer/ODText/ContentTest.php index 377f07c2..4808de9e 100644 --- a/tests/PhpWord/Tests/Writer/ODText/ContentTest.php +++ b/tests/PhpWord/Tests/Writer/ODText/ContentTest.php @@ -41,7 +41,7 @@ class ContentTest extends \PHPUnit_Framework_TestCase $phpWord->setDefaultFontName('Verdana'); $phpWord->addFontStyle('Font', array('size' => 11)); $phpWord->addParagraphStyle('Paragraph', array('align' => 'center')); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addText($expected); $section->addText('Test font style', 'Font'); $section->addText('Test paragraph style', null, 'Paragraph'); @@ -54,7 +54,7 @@ class ContentTest extends \PHPUnit_Framework_TestCase $section->addImage($imageSrc); $section->addObject($objectSrc); $section->addTOC(); - $textrun = $section->createTextRun(); + $textrun = $section->addTextRun(); $textrun->addText('Test text run'); $doc = TestHelperDOCX::getDocument($phpWord, 'ODText'); diff --git a/tests/PhpWord/Tests/Writer/ODTextTest.php b/tests/PhpWord/Tests/Writer/ODTextTest.php index 82263c0f..743907be 100644 --- a/tests/PhpWord/Tests/Writer/ODTextTest.php +++ b/tests/PhpWord/Tests/Writer/ODTextTest.php @@ -64,7 +64,7 @@ class ODTextTest extends \PHPUnit_Framework_TestCase $phpWord = new PhpWord(); $phpWord->addFontStyle('Font', array('size' => 11)); $phpWord->addParagraphStyle('Paragraph', array('align' => 'center')); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addText('Test 1', 'Font'); $section->addTextBreak(); $section->addText('Test 2', null, 'Paragraph'); @@ -76,8 +76,8 @@ class ODTextTest extends \PHPUnit_Framework_TestCase $section->addImage($imageSrc); $section->addObject($objectSrc); $section->addTOC(); - $section = $phpWord->createSection(); - $textrun = $section->createTextRun(); + $section = $phpWord->addSection(); + $textrun = $section->addTextRun(); $textrun->addText('Test 3'); $writer = new ODText($phpWord); $writer->save($file); @@ -95,7 +95,7 @@ class ODTextTest extends \PHPUnit_Framework_TestCase public function testSavePhpOutput() { $phpWord = new PhpWord(); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addText('Test'); $writer = new ODText($phpWord); $writer->save('php://output'); diff --git a/tests/PhpWord/Tests/Writer/RTFTest.php b/tests/PhpWord/Tests/Writer/RTFTest.php index 4872ef39..1abeeefa 100644 --- a/tests/PhpWord/Tests/Writer/RTFTest.php +++ b/tests/PhpWord/Tests/Writer/RTFTest.php @@ -52,7 +52,7 @@ class RTFTest extends \PHPUnit_Framework_TestCase $phpWord = new PhpWord(); $phpWord->addFontStyle('Font', array('name' => 'Verdana', 'size' => 11, 'color' => 'FF0000', 'fgColor' => 'FF0000')); $phpWord->addParagraphStyle('Paragraph', array('align' => 'center')); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addText('Test 1', 'Font', 'Paragraph'); $section->addTextBreak(); $section->addText('Test 2', array('name' => 'Tahoma', 'bold' => true, 'italic' => true)); @@ -64,8 +64,8 @@ class RTFTest extends \PHPUnit_Framework_TestCase $section->addImage($imageSrc); $section->addObject($objectSrc); $section->addTOC(); - $section = $phpWord->createSection(); - $textrun = $section->createTextRun(); + $section = $phpWord->addSection(); + $textrun = $section->addTextRun(); $textrun->addText('Test 3'); $textrun->addTextBreak(); $writer = new RTF($phpWord); @@ -84,7 +84,7 @@ class RTFTest extends \PHPUnit_Framework_TestCase public function testSavePhpOutput() { $phpWord = new PhpWord(); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addText('Test'); $writer = new RTF($phpWord); $writer->save('php://output'); diff --git a/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php b/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php index 18524f58..d9d1b3c5 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php @@ -38,7 +38,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase $phpWord = new PhpWord(); $phpWord->addFontStyle($rStyle, array('bold' => true)); $phpWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120)); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addText('Test', $rStyle, $pStyle); $doc = TestHelperDOCX::getDocument($phpWord); @@ -59,14 +59,14 @@ class BaseTest extends \PHPUnit_Framework_TestCase $phpWord = new PhpWord(); $phpWord->addParagraphStyle($pStyle, $aStyle); - $section = $phpWord->createSection('Test'); - $textrun = $section->createTextRun($pStyle); + $section = $phpWord->addSection('Test'); + $textrun = $section->addTextRun($pStyle); $textrun->addText('Test'); $textrun->addTextBreak(); - $textrun = $section->createTextRun($aStyle); + $textrun = $section->addTextRun($aStyle); $textrun->addLink('http://test.com'); $textrun->addImage($imageSrc, array('align' => 'top')); - $textrun->createFootnote(); + $textrun->addFootnote(); $doc = TestHelperDOCX::getDocument($phpWord); $parent = "/w:document/w:body/w:p"; @@ -79,7 +79,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase public function testWriteLink() { $phpWord = new PhpWord(); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $fontStyleArray = array('bold' => true); $fontStyleName = 'Font Style'; $paragraphStyleArray = array('align' => 'center'); @@ -102,8 +102,8 @@ class BaseTest extends \PHPUnit_Framework_TestCase public function testWritePreserveText() { $phpWord = new PhpWord(); - $section = $phpWord->createSection(); - $footer = $section->createFooter(); + $section = $phpWord->addSection(); + $footer = $section->addFooter(); $fontStyleArray = array('bold' => true); $fontStyleName = 'Font'; $paragraphStyleArray = array('align' => 'right'); @@ -133,7 +133,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase $phpWord = new PhpWord(); $phpWord->addFontStyle($fName, $fArray); $phpWord->addParagraphStyle($pName, $pArray); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addTextBreak(); $section->addTextBreak(1, $fArray, $pArray); $section->addTextBreak(1, $fName, $pName); @@ -151,7 +151,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase public function testWriteParagraphStyleAlign() { $phpWord = new PhpWord(); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addText('This is my text', null, array('align' => 'right')); @@ -168,7 +168,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase { // Create the doc $phpWord = new PhpWord(); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $attributes = array( 'widowControl' => false, 'keepNext' => true, @@ -209,7 +209,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase $styles['bgColor'] = 'FFFF00'; $styles['hint'] = 'eastAsia'; - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addText('Test', $styles); $doc = TestHelperDOCX::getDocument($phpWord); @@ -257,7 +257,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase $cStyles["borderRightColor"] = 'FF0000'; $cStyles["vMerge"] = 'restart'; - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $table = $section->addTable($tStyles); $table->setWidth = 100; $table->addRow($rHeight, $rStyles); @@ -268,7 +268,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase $cell->addListItem('Test'); $cell->addImage($imageSrc); $cell->addObject($objectSrc); - $textrun = $cell->createTextRun(); + $textrun = $cell->addTextRun(); $textrun->addText('Test'); $doc = TestHelperDOCX::getDocument($phpWord); @@ -296,7 +296,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase public function testWriteCellStyleCellGridSpan() { $phpWord = new PhpWord(); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $table = $section->addTable(); @@ -323,7 +323,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase public function testWriteImagePosition() { $phpWord = new PhpWord(); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addImage( __DIR__ . "/../../_files/images/earth.jpg", array( @@ -350,8 +350,8 @@ class BaseTest extends \PHPUnit_Framework_TestCase $imageSrc = __DIR__ . "/../../_files/images/earth.jpg"; $phpWord = new PhpWord(); - $section = $phpWord->createSection(); - $header = $section->createHeader(); + $section = $phpWord->addSection(); + $header = $section->addHeader(); $header->addWatermark($imageSrc); $doc = TestHelperDOCX::getDocument($phpWord); @@ -366,7 +366,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase { $phpWord = new PhpWord(); $phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240)); - $phpWord->createSection()->addTitle('Test', 1); + $phpWord->addSection()->addTitle('Test', 1); $doc = TestHelperDOCX::getDocument($phpWord); $element = "/w:document/w:body/w:p/w:pPr/w:pStyle"; @@ -386,7 +386,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase $phpWord = new PhpWord(); $phpWord->addFontStyle($rStyle, array('bold' => true)); $phpWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120)); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addCheckbox('Check1', 'Test', $rStyle, $pStyle); $doc = TestHelperDOCX::getDocument($phpWord); diff --git a/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php b/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php index 5318ac81..8cceecb3 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php @@ -33,7 +33,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase public function testWriteEndSectionPageNumbering() { $phpWord = new PhpWord(); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $settings = $section->getSettings(); $settings->setLandscape(); $settings->setPageNumberingStart(2); @@ -56,14 +56,14 @@ class DocumentTest extends \PHPUnit_Framework_TestCase $phpWord = new PhpWord(); $phpWord->addTitleStyle(1, array('color' => '333333', 'bold'=>true)); $phpWord->addTitleStyle(2, array('color'=>'666666')); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addTOC(); $section->addPageBreak(); $section->addTitle('Title 1', 1); $section->addListItem('List Item 1', 0); $section->addListItem('List Item 2', 0); $section->addListItem('List Item 3', 0); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addTitle('Title 2', 2); $section->addObject($objectSrc); $doc = TestHelperDOCX::getDocument($phpWord); @@ -103,7 +103,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase $phpWord->addFontStyle('fStyle', array('size' => '20')); $phpWord->addTitleStyle(1, array('color' => '333333', 'bold' => true)); $fontStyle = new Font('text', array('align' => 'center')); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addListItem('List Item', 0, null, null, 'pStyle'); $section->addObject($objectSrc, array('align' => 'center')); $section->addTOC($fontStyle); diff --git a/tests/PhpWord/Tests/Writer/Word2007/FooterTest.php b/tests/PhpWord/Tests/Writer/Word2007/FooterTest.php index c0f94de6..04e86832 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/FooterTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/FooterTest.php @@ -32,7 +32,7 @@ class FooterTest extends \PHPUnit_Framework_TestCase $container->addText(''); $container->addPreserveText(''); $container->addTextBreak(); - $container->createTextRun(); + $container->addTextRun(); $container->addTable()->addRow()->addCell()->addText(''); $container->addImage($imageSrc); diff --git a/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php b/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php index 6baba0ac..11817962 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php @@ -34,13 +34,13 @@ class FootnotesTest extends \PHPUnit_Framework_TestCase { $phpWord = new PhpWord(); $phpWord->addParagraphStyle('pStyle', array('align' => 'left')); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addText('Text'); - $footnote1 = $section->createFootnote('pStyle'); + $footnote1 = $section->addFootnote('pStyle'); $footnote1->addText('Footnote'); $footnote1->addTextBreak(); $footnote1->addLink('http://google.com'); - $footnote2 = $section->createFootnote(array('align' => 'left')); + $footnote2 = $section->addFootnote(array('align' => 'left')); $footnote2->addText('Footnote'); $doc = TestHelperDOCX::getDocument($phpWord); diff --git a/tests/PhpWord/Tests/Writer/Word2007/HeaderTest.php b/tests/PhpWord/Tests/Writer/Word2007/HeaderTest.php index 253f663e..c468d7a7 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/HeaderTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/HeaderTest.php @@ -30,7 +30,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $container->addText('Test'); $container->addPreserveText(''); $container->addTextBreak(); - $container->createTextRun(); + $container->addTextRun(); $container->addTable()->addRow()->addCell()->addText(''); $container->addImage($imageSrc); $container->addWatermark($imageSrc); diff --git a/tests/PhpWord/Tests/Writer/Word2007Test.php b/tests/PhpWord/Tests/Writer/Word2007Test.php index f486d18d..7b722a9c 100644 --- a/tests/PhpWord/Tests/Writer/Word2007Test.php +++ b/tests/PhpWord/Tests/Writer/Word2007Test.php @@ -68,18 +68,18 @@ class Word2007Test extends \PHPUnit_Framework_TestCase $phpWord = new PhpWord(); $phpWord->addFontStyle('Font', array('size' => 11)); $phpWord->addParagraphStyle('Paragraph', array('align' => 'center')); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addText('Test 1', 'Font', 'Paragraph'); $section->addTextBreak(); $section->addText('Test 2'); - $section = $phpWord->createSection(); - $textrun = $section->createTextRun(); + $section = $phpWord->addSection(); + $textrun = $section->addTextRun(); $textrun->addText('Test 3'); - $footnote = $textrun->createFootnote(); + $footnote = $textrun->addFootnote(); $footnote->addLink('http://test.com'); - $header = $section->createHeader(); + $header = $section->addHeader(); $header->addImage($localImage); - $footer = $section->createFooter(); + $footer = $section->addFooter(); $footer->addImage($remoteImage); $writer = new Word2007($phpWord); @@ -97,9 +97,9 @@ class Word2007Test extends \PHPUnit_Framework_TestCase public function testSaveUseDiskCaching() { $phpWord = new PhpWord(); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $section->addText('Test'); - $footnote = $section->createFootnote(); + $footnote = $section->addFootnote(); $footnote->addText('Test'); $writer = new Word2007($phpWord); @@ -138,7 +138,7 @@ class Word2007Test extends \PHPUnit_Framework_TestCase 'angela_merkel.tif' => '6.tif', ); $phpWord = new PhpWord(); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); foreach ($images as $source => $target) { $section->addImage(__DIR__ . "/../_files/images/{$source}"); } @@ -169,7 +169,7 @@ class Word2007Test extends \PHPUnit_Framework_TestCase public function testSetGetUseDiskCaching() { $phpWord = new PhpWord(); - $section = $phpWord->createSection(); + $section = $phpWord->addSection(); $object = new Word2007($phpWord); $object->setUseDiskCaching(true, \PHPWORD_TESTS_BASE_DIR); $writer = new Word2007($phpWord); From 06ba9e70930aabac16b4e1aa8ddc8e58ab92b17a Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Wed, 2 Apr 2014 11:11:59 +0700 Subject: [PATCH 09/27] Make one level Element folder structure (move Cell and Row up one level) --- src/PhpWord/Element/{Table => }/Cell.php | 2 +- src/PhpWord/Element/{Table => }/Row.php | 4 ++-- src/PhpWord/Element/Table.php | 4 ++-- .../Tests/Element/{Table => }/CellTest.php | 18 +++++++++--------- .../Tests/Element/{Table => }/RowTest.php | 12 ++++++------ tests/PhpWord/Tests/Element/TableTest.php | 4 ++-- 6 files changed, 22 insertions(+), 22 deletions(-) rename src/PhpWord/Element/{Table => }/Cell.php (97%) rename src/PhpWord/Element/{Table => }/Row.php (95%) rename tests/PhpWord/Tests/Element/{Table => }/CellTest.php (93%) rename tests/PhpWord/Tests/Element/{Table => }/RowTest.php (79%) diff --git a/src/PhpWord/Element/Table/Cell.php b/src/PhpWord/Element/Cell.php similarity index 97% rename from src/PhpWord/Element/Table/Cell.php rename to src/PhpWord/Element/Cell.php index 2a15a263..25e9fb71 100755 --- a/src/PhpWord/Element/Table/Cell.php +++ b/src/PhpWord/Element/Cell.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Element\Table; +namespace PhpOffice\PhpWord\Element; use PhpOffice\PhpWord\Container\Container; use PhpOffice\PhpWord\Style\Cell as CellStyle; diff --git a/src/PhpWord/Element/Table/Row.php b/src/PhpWord/Element/Row.php similarity index 95% rename from src/PhpWord/Element/Table/Row.php rename to src/PhpWord/Element/Row.php index c027d373..eefeca57 100644 --- a/src/PhpWord/Element/Table/Row.php +++ b/src/PhpWord/Element/Row.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Element\Table; +namespace PhpOffice\PhpWord\Element; /** * Table row element @@ -83,7 +83,7 @@ class Row * * @param int $width * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Table\Cell + * @return \PhpOffice\PhpWord\Element\Cell */ public function addCell($width = null, $style = null) { diff --git a/src/PhpWord/Element/Table.php b/src/PhpWord/Element/Table.php index fc168349..083ad5fc 100644 --- a/src/PhpWord/Element/Table.php +++ b/src/PhpWord/Element/Table.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord\Element; -use PhpOffice\PhpWord\Element\Table\Row; +use PhpOffice\PhpWord\Element\Row; /** * Table element @@ -98,7 +98,7 @@ class Table * * @param int $width * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Table\Cell + * @return \PhpOffice\PhpWord\Element\Cell */ public function addCell($width = null, $style = null) { diff --git a/tests/PhpWord/Tests/Element/Table/CellTest.php b/tests/PhpWord/Tests/Element/CellTest.php similarity index 93% rename from tests/PhpWord/Tests/Element/Table/CellTest.php rename to tests/PhpWord/Tests/Element/CellTest.php index bc0a8db1..be541935 100644 --- a/tests/PhpWord/Tests/Element/Table/CellTest.php +++ b/tests/PhpWord/Tests/Element/CellTest.php @@ -7,12 +7,12 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Element\Table; +namespace PhpOffice\PhpWord\Tests\Element; -use PhpOffice\PhpWord\Element\Table\Cell; +use PhpOffice\PhpWord\Element\Cell; /** - * Test class for PhpOffice\PhpWord\Element\Table\Cell + * Test class for PhpOffice\PhpWord\Element\Cell * * @runTestsInSeparateProcesses */ @@ -26,7 +26,7 @@ class CellTest extends \PHPUnit_Framework_TestCase $iVal = rand(1, 1000); $oCell = new Cell('section', $iVal); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Table\\Cell', $oCell); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Cell', $oCell); $this->assertEquals($oCell->getWidth(), null); } @@ -132,7 +132,7 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testAddImageSection() { - $src = __DIR__ . "/../../_files/images/earth.jpg"; + $src = __DIR__ . "/../_files/images/earth.jpg"; $oCell = new Cell('section', 1); $element1 = $oCell->addImage($src); $element2 = $oCell->addMemoryImage($src); // @deprecated @@ -146,7 +146,7 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testAddImageHeader() { - $src = __DIR__ . "/../../_files/images/earth.jpg"; + $src = __DIR__ . "/../_files/images/earth.jpg"; $oCell = new Cell('header', 1); $element = $oCell->addImage($src); @@ -159,7 +159,7 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testAddImageFooter() { - $src = __DIR__ . "/../../_files/images/earth.jpg"; + $src = __DIR__ . "/../_files/images/earth.jpg"; $oCell = new Cell('footer', 1); $element = $oCell->addImage($src); @@ -214,7 +214,7 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testAddObjectXLS() { - $src = __DIR__ . "/../../_files/documents/sheet.xls"; + $src = __DIR__ . "/../_files/documents/sheet.xls"; $oCell = new Cell('section', 1); $element = $oCell->addObject($src); @@ -229,7 +229,7 @@ class CellTest extends \PHPUnit_Framework_TestCase */ public function testAddObjectException() { - $src = __DIR__ . "/_files/xsl/passthrough.xsl"; + $src = __DIR__ . "/../_files/xsl/passthrough.xsl"; $oCell = new Cell('section', 1); $element = $oCell->addObject($src); } diff --git a/tests/PhpWord/Tests/Element/Table/RowTest.php b/tests/PhpWord/Tests/Element/RowTest.php similarity index 79% rename from tests/PhpWord/Tests/Element/Table/RowTest.php rename to tests/PhpWord/Tests/Element/RowTest.php index f64c0cb4..e232afa8 100644 --- a/tests/PhpWord/Tests/Element/Table/RowTest.php +++ b/tests/PhpWord/Tests/Element/RowTest.php @@ -7,14 +7,14 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Element\Table; +namespace PhpOffice\PhpWord\Tests\Element; -use PhpOffice\PhpWord\Element\Table\Row; +use PhpOffice\PhpWord\Element\Row; /** - * Test class for PhpOffice\PhpWord\Element\Table\Row + * Test class for PhpOffice\PhpWord\Element\Row * - * @coversDefaultClass \PhpOffice\PhpWord\Element\Table\Row + * @coversDefaultClass \PhpOffice\PhpWord\Element\Row * @runTestsInSeparateProcesses */ class RowTest extends \PHPUnit_Framework_TestCase @@ -27,7 +27,7 @@ class RowTest extends \PHPUnit_Framework_TestCase $iVal = rand(1, 1000); $oRow = new Row('section', $iVal); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Table\\Row', $oRow); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Row', $oRow); $this->assertEquals($oRow->getHeight(), null); $this->assertInternalType('array', $oRow->getCells()); $this->assertCount(0, $oRow->getCells()); @@ -60,7 +60,7 @@ class RowTest extends \PHPUnit_Framework_TestCase $oRow = new Row('section', 1); $element = $oRow->addCell(); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Table\\Cell', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Cell', $element); $this->assertCount(1, $oRow->getCells()); } } diff --git a/tests/PhpWord/Tests/Element/TableTest.php b/tests/PhpWord/Tests/Element/TableTest.php index 1b337fd6..0d3a74b9 100644 --- a/tests/PhpWord/Tests/Element/TableTest.php +++ b/tests/PhpWord/Tests/Element/TableTest.php @@ -75,7 +75,7 @@ class TableTest extends \PHPUnit_Framework_TestCase { $oTable = new Table('section', 1); $element = $oTable->addRow(); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Table\\Row', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Row', $element); $this->assertCount(1, $oTable->getRows()); } @@ -87,6 +87,6 @@ class TableTest extends \PHPUnit_Framework_TestCase $oTable = new Table('section', 1); $oTable->addRow(); $element = $oTable->addCell(); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Table\\Cell', $element); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Cell', $element); } } From 04e6dbc86a39c8df00e03a512e7d0863806efcb2 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 3 Apr 2014 06:54:45 +0700 Subject: [PATCH 10/27] Remove some duplication on Word2007 writers --- src/PhpWord/Writer/Word2007/Base.php | 442 +++++++----------- src/PhpWord/Writer/Word2007/Document.php | 145 ++---- src/PhpWord/Writer/Word2007/DocumentRels.php | 73 +-- src/PhpWord/Writer/Word2007/Footer.php | 33 +- src/PhpWord/Writer/Word2007/Footnotes.php | 20 +- src/PhpWord/Writer/Word2007/FootnotesRels.php | 6 +- src/PhpWord/Writer/Word2007/Header.php | 37 +- 7 files changed, 247 insertions(+), 509 deletions(-) diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index d5c0160a..db8bd851 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -9,7 +9,9 @@ namespace PhpOffice\PhpWord\Writer\Word2007; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\Container\Container; use PhpOffice\PhpWord\Element\Text; use PhpOffice\PhpWord\Element\TextRun; use PhpOffice\PhpWord\Element\Link; @@ -75,27 +77,10 @@ class Base extends WriterPart */ protected function writeTextRun(XMLWriter $xmlWriter, TextRun $textrun) { - $elements = $textrun->getElements(); $styleParagraph = $textrun->getParagraphStyle(); $xmlWriter->startElement('w:p'); $this->writeInlineParagraphStyle($xmlWriter, $styleParagraph); - if (count($elements) > 0) { - foreach ($elements as $element) { - if ($element instanceof Text) { - $this->writeText($xmlWriter, $element, true); - } elseif ($element instanceof Link) { - $this->writeLink($xmlWriter, $element, true); - } elseif ($element instanceof TextBreak) { - $xmlWriter->writeElement('w:br'); - } elseif ($element instanceof Image) { - $this->writeImage($xmlWriter, $element, true); - } elseif ($element instanceof Object) { - $this->writeObject($xmlWriter, $element, true); - } elseif ($element instanceof Footnote) { - $this->writeFootnote($xmlWriter, $element, true); - } - } - } + $this->writeContainerElements($xmlWriter, $textrun); $xmlWriter->endElement(); // w:p } @@ -254,28 +239,31 @@ class Base extends WriterPart * @param XMLWriter $xmlWriter * @param TextBreak $element */ - protected function writeTextBreak($xmlWriter, TextBreak $element = null) + protected function writeTextBreak(XMLWriter $xmlWriter, TextBreak $element = null, $withoutP = false) { - $hasStyle = false; - $styleFont = null; - $styleParagraph = null; - if (!is_null($element)) { - $styleFont = $element->getFontStyle(); - $styleParagraph = $element->getParagraphStyle(); - $hasStyle = !is_null($styleFont) || !is_null($styleParagraph); - } - if ($hasStyle) { - $xmlWriter->startElement('w:p'); - $this->writeInlineParagraphStyle($xmlWriter, $styleParagraph); - if (!is_null($styleFont)) { - $xmlWriter->startElement('w:pPr'); - $this->writeInlineFontStyle($xmlWriter, $styleFont); - $xmlWriter->endElement(); // w:pPr + if (!$withoutP) { + $hasStyle = false; + $styleFont = null; + $styleParagraph = null; + if (!is_null($element)) { + $styleFont = $element->getFontStyle(); + $styleParagraph = $element->getParagraphStyle(); + $hasStyle = !is_null($styleFont) || !is_null($styleParagraph); + } + if ($hasStyle) { + $xmlWriter->startElement('w:p'); + $this->writeInlineParagraphStyle($xmlWriter, $styleParagraph); + if (!is_null($styleFont)) { + $xmlWriter->startElement('w:pPr'); + $this->writeInlineFontStyle($xmlWriter, $styleFont); + $xmlWriter->endElement(); // w:pPr + } + $xmlWriter->endElement(); // w:p + } else { + $xmlWriter->writeElement('w:p'); } - $xmlWriter->endElement(); // w:p } else { - // Null element. No paragraph nor font style - $xmlWriter->writeElement('w:p', null); + $xmlWriter->writeElement('w:br'); } } @@ -316,16 +304,16 @@ class Base extends WriterPart */ protected function writeTable(XMLWriter $xmlWriter, Table $table) { - $_rows = $table->getRows(); - $_cRows = count($_rows); + $rows = $table->getRows(); + $cRows = count($rows); - if ($_cRows > 0) { + if ($cRows > 0) { $xmlWriter->startElement('w:tbl'); // Table grid $cellWidths = array(); - for ($i = 0; $i < $_cRows; $i++) { - $row = $_rows[$i]; + for ($i = 0; $i < $cRows; $i++) { + $row = $rows[$i]; $cells = $row->getCells(); if (count($cells) <= count($cellWidths)) { continue; @@ -368,8 +356,8 @@ class Base extends WriterPart } // Table rows - for ($i = 0; $i < $_cRows; $i++) { - $row = $_rows[$i]; + for ($i = 0; $i < $cRows; $i++) { + $row = $rows[$i]; $height = $row->getHeight(); $rowStyle = $row->getStyle(); $tblHeader = $rowStyle->getTblHeader(); @@ -377,7 +365,6 @@ class Base extends WriterPart $exactHeight = $rowStyle->getExactHeight(); $xmlWriter->startElement('w:tr'); - if (!is_null($height) || !is_null($tblHeader) || !is_null($cantSplit)) { $xmlWriter->startElement('w:trPr'); if (!is_null($height)) { @@ -398,55 +385,23 @@ class Base extends WriterPart } $xmlWriter->endElement(); } - foreach ($row->getCells() as $cell) { - $xmlWriter->startElement('w:tc'); - $cellStyle = $cell->getStyle(); $width = $cell->getWidth(); - + $xmlWriter->startElement('w:tc'); $xmlWriter->startElement('w:tcPr'); $xmlWriter->startElement('w:tcW'); $xmlWriter->writeAttribute('w:w', $width); $xmlWriter->writeAttribute('w:type', 'dxa'); - $xmlWriter->endElement(); - + $xmlWriter->endElement(); // w:tcW if ($cellStyle instanceof Cell) { $this->writeCellStyle($xmlWriter, $cellStyle); } - - $xmlWriter->endElement(); - - $_elements = $cell->getElements(); - if (count($_elements) > 0) { - foreach ($_elements as $element) { - if ($element instanceof Text) { - $this->writeText($xmlWriter, $element); - } elseif ($element instanceof TextRun) { - $this->writeTextRun($xmlWriter, $element); - } elseif ($element instanceof Link) { - $this->writeLink($xmlWriter, $element); - } elseif ($element instanceof PreserveText) { - $this->writePreserveText($xmlWriter, $element); - } elseif ($element instanceof TextBreak) { - $this->writeTextBreak($xmlWriter, $element); - } elseif ($element instanceof ListItem) { - $this->writeListItem($xmlWriter, $element); - } elseif ($element instanceof Image) { - $this->writeImage($xmlWriter, $element); - } elseif ($element instanceof Object) { - $this->writeObject($xmlWriter, $element); - } elseif ($element instanceof CheckBox) { - $this->writeCheckBox($xmlWriter, $element); - } - } - } else { - $this->writeTextBreak($xmlWriter); - } - - $xmlWriter->endElement(); + $xmlWriter->endElement(); // w:tcPr + $this->writeContainerElements($xmlWriter, $cell); + $xmlWriter->endElement(); // w:tc } - $xmlWriter->endElement(); + $xmlWriter->endElement(); // w:tr } $xmlWriter->endElement(); } @@ -953,98 +908,35 @@ class Base extends WriterPart { $bgColor = $style->getBgColor(); $brdCol = $style->getBorderColor(); - $brdSz = $style->getBorderSize(); - $bTop = (!is_null($brdSz[0])) ? true : false; - $bLeft = (!is_null($brdSz[1])) ? true : false; - $bRight = (!is_null($brdSz[2])) ? true : false; - $bBottom = (!is_null($brdSz[3])) ? true : false; - $bInsH = (!is_null($brdSz[4])) ? true : false; - $bInsV = (!is_null($brdSz[5])) ? true : false; - $borders = ($bTop || $bLeft || $bRight || $bBottom || $bInsH || $bInsV) ? true : false; - $cellMargin = $style->getCellMargin(); - $mTop = (!is_null($cellMargin[0])) ? true : false; - $mLeft = (!is_null($cellMargin[1])) ? true : false; - $mRight = (!is_null($cellMargin[2])) ? true : false; - $mBottom = (!is_null($cellMargin[3])) ? true : false; - $margins = ($mTop || $mLeft || $mRight || $mBottom) ? true : false; - if ($margins || $borders) { - $xmlWriter->startElement('w:tblPr'); - if ($margins) { - $xmlWriter->startElement('w:tblCellMar'); - if ($mTop) { - $xmlWriter->startElement('w:top'); - $xmlWriter->writeAttribute('w:w', $cellMargin[0]); - $xmlWriter->writeAttribute('w:type', 'dxa'); - $xmlWriter->endElement(); - } - if ($mLeft) { - $xmlWriter->startElement('w:left'); - $xmlWriter->writeAttribute('w:w', $cellMargin[1]); - $xmlWriter->writeAttribute('w:type', 'dxa'); - $xmlWriter->endElement(); - } - if ($mRight) { - $xmlWriter->startElement('w:right'); - $xmlWriter->writeAttribute('w:w', $cellMargin[2]); - $xmlWriter->writeAttribute('w:type', 'dxa'); - $xmlWriter->endElement(); - } - if ($mBottom) { - $xmlWriter->startElement('w:bottom'); - $xmlWriter->writeAttribute('w:w', $cellMargin[3]); - $xmlWriter->writeAttribute('w:type', 'dxa'); - $xmlWriter->endElement(); - } - $xmlWriter->endElement(); + // If any of the borders/margins is set, process them + $hasBorders = false; + for ($i = 0; $i < 6; $i++) { + if (!is_null($brdSz[$i])) { + $hasBorders = true; + break; } - if ($borders) { + } + $hasMargins = false; + for ($i = 0; $i < 4; $i++) { + if (!is_null($cellMargin[$i])) { + $hasMargins = true; + break; + } + } + if ($hasMargins || $hasBorders) { + $xmlWriter->startElement('w:tblPr'); + if ($hasMargins) { + $xmlWriter->startElement('w:tblCellMar'); + $this->writeMarginBorder($xmlWriter, $cellMargin); + $xmlWriter->endElement(); // w:tblCellMar + } + if ($hasBorders) { $xmlWriter->startElement('w:tblBorders'); - if ($bTop) { - $xmlWriter->startElement('w:top'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $brdSz[0]); - $xmlWriter->writeAttribute('w:color', $brdCol[0]); - $xmlWriter->endElement(); - } - if ($bLeft) { - $xmlWriter->startElement('w:left'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $brdSz[1]); - $xmlWriter->writeAttribute('w:color', $brdCol[1]); - $xmlWriter->endElement(); - } - if ($bRight) { - $xmlWriter->startElement('w:right'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $brdSz[2]); - $xmlWriter->writeAttribute('w:color', $brdCol[2]); - $xmlWriter->endElement(); - } - if ($bBottom) { - $xmlWriter->startElement('w:bottom'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $brdSz[3]); - $xmlWriter->writeAttribute('w:color', $brdCol[3]); - $xmlWriter->endElement(); - } - if ($bInsH) { - $xmlWriter->startElement('w:insideH'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $brdSz[4]); - $xmlWriter->writeAttribute('w:color', $brdCol[4]); - $xmlWriter->endElement(); - } - if ($bInsV) { - $xmlWriter->startElement('w:insideV'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $brdSz[5]); - $xmlWriter->writeAttribute('w:color', $brdCol[5]); - $xmlWriter->endElement(); - } - $xmlWriter->endElement(); + $this->writeMarginBorder($xmlWriter, $brdSz, $brdCol); + $xmlWriter->endElement(); // w:tblBorders } $xmlWriter->endElement(); // w:tblPr } @@ -1077,61 +969,37 @@ class Base extends WriterPart */ protected function writeRowStyle(XMLWriter $xmlWriter, $type, TableStyle $style) { - $brdSz = $style->getBorderSize(); - $brdCol = $style->getBorderColor(); $bgColor = $style->getBgColor(); - $bTop = (!is_null($brdSz[0])) ? true : false; - $bLeft = (!is_null($brdSz[1])) ? true : false; - $bRight = (!is_null($brdSz[2])) ? true : false; - $bBottom = (!is_null($brdSz[3])) ? true : false; - $xmlWriter->startElement('w:tblStylePr'); $xmlWriter->writeAttribute('w:type', $type); - $xmlWriter->startElement('w:tcPr'); if (!is_null($bgColor)) { $xmlWriter->startElement('w:shd'); $xmlWriter->writeAttribute('w:val', 'clear'); $xmlWriter->writeAttribute('w:color', 'auto'); $xmlWriter->writeAttribute('w:fill', $bgColor); - $xmlWriter->endElement(); + $xmlWriter->endElement(); // w:shd } - $xmlWriter->startElement('w:tcBorders'); - if ($bTop) { - $xmlWriter->startElement('w:top'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $brdSz[0]); - $xmlWriter->writeAttribute('w:color', $brdCol[0]); - $xmlWriter->endElement(); + // Borders + $brdSz = $style->getBorderSize(); + $brdCol = $style->getBorderColor(); + $hasBorders = false; + for ($i = 0; $i < 6; $i++) { + if (!is_null($brdSz[$i])) { + $hasBorders = true; + break; + } } - if ($bLeft) { - $xmlWriter->startElement('w:left'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $brdSz[1]); - $xmlWriter->writeAttribute('w:color', $brdCol[1]); - $xmlWriter->endElement(); + if ($hasBorders) { + $xmlWriter->startElement('w:tcBorders'); + $this->writeMarginBorder($xmlWriter, $brdSz, $brdCol); + $xmlWriter->endElement(); // w:tcBorders } - if ($bRight) { - $xmlWriter->startElement('w:right'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $brdSz[2]); - $xmlWriter->writeAttribute('w:color', $brdCol[2]); - $xmlWriter->endElement(); - } - if ($bBottom) { - $xmlWriter->startElement('w:bottom'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $brdSz[3]); - $xmlWriter->writeAttribute('w:color', $brdCol[3]); - $xmlWriter->endElement(); - } - $xmlWriter->endElement(); - $xmlWriter->endElement(); - - $xmlWriter->endElement(); + $xmlWriter->endElement(); // w:tcPr + $xmlWriter->endElement(); // w:tblStylePr } /** @@ -1147,14 +1015,15 @@ class Base extends WriterPart $textDir = $style->getTextDirection(); $brdSz = $style->getBorderSize(); $brdCol = $style->getBorderColor(); + $hasBorders = false; + for ($i = 0; $i < 4; $i++) { + if (!is_null($brdSz[$i])) { + $hasBorders = true; + break; + } + } - $bTop = (!is_null($brdSz[0])) ? true : false; - $bLeft = (!is_null($brdSz[1])) ? true : false; - $bRight = (!is_null($brdSz[2])) ? true : false; - $bBottom = (!is_null($brdSz[3])) ? true : false; - $borders = ($bTop || $bLeft || $bRight || $bBottom) ? true : false; - - $styles = (!is_null($bgColor) || !is_null($valign) || !is_null($textDir) || $borders) ? true : false; + $styles = (!is_null($bgColor) || !is_null($valign) || !is_null($textDir) || $hasBorders) ? true : false; if ($styles) { if (!is_null($textDir)) { @@ -1177,54 +1046,11 @@ class Base extends WriterPart $xmlWriter->endElement(); } - if ($borders) { - $_defaultColor = $style->getDefaultBorderColor(); + if ($hasBorders) { + $defaultColor = $style->getDefaultBorderColor(); $xmlWriter->startElement('w:tcBorders'); - if ($bTop) { - if (is_null($brdCol[0])) { - $brdCol[0] = $_defaultColor; - } - $xmlWriter->startElement('w:top'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $brdSz[0]); - $xmlWriter->writeAttribute('w:color', $brdCol[0]); - $xmlWriter->endElement(); - } - - if ($bLeft) { - if (is_null($brdCol[1])) { - $brdCol[1] = $_defaultColor; - } - $xmlWriter->startElement('w:left'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $brdSz[1]); - $xmlWriter->writeAttribute('w:color', $brdCol[1]); - $xmlWriter->endElement(); - } - - if ($bRight) { - if (is_null($brdCol[2])) { - $brdCol[2] = $_defaultColor; - } - $xmlWriter->startElement('w:right'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $brdSz[2]); - $xmlWriter->writeAttribute('w:color', $brdCol[2]); - $xmlWriter->endElement(); - } - - if ($bBottom) { - if (is_null($brdCol[3])) { - $brdCol[3] = $_defaultColor; - } - $xmlWriter->startElement('w:bottom'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $brdSz[3]); - $xmlWriter->writeAttribute('w:color', $brdCol[3]); - $xmlWriter->endElement(); - } - + $this->writeMarginBorder($xmlWriter, $brdSz, $brdCol, array('defaultColor' => $defaultColor)); $xmlWriter->endElement(); } } @@ -1337,4 +1163,94 @@ class Base extends WriterPart } } } + + /** + * Write container elements + * + * @param XMLWriter $xmlWriter + * @param Container $container + * @param Container $textBreak Add text break when no element found + */ + protected function writeContainerElements(XMLWriter $xmlWriter, Container $container) + { + $allowedElements = array( + 'Section' => array('Text', 'TextRun', 'Link', 'Title', 'TextBreak', 'ListItem', 'Table', 'Image', 'Object', 'CheckBox', 'Footnote', 'TOC'), + 'Header' => array('Text', 'TextRun', 'Link', 'PreserveText', 'TextBreak', 'ListItem', 'Image', 'CheckBox'), + 'Footer' => array('Text', 'TextRun', 'Link', 'PreserveText', 'TextBreak', 'ListItem', 'Image', 'CheckBox'), + 'Cell' => array('Text', 'TextRun', 'Link', 'PreserveText', 'TextBreak', 'ListItem', 'Image', 'Object', 'CheckBox', 'Footnote'), + 'TextRun' => array('Text', 'Link', 'TextBreak', 'Image', 'Object', 'Footnote'), + 'Footnote' => array('Text', 'Link', 'TextBreak', 'Image', 'Object'), + ); + $containerName = get_class($container); + $containerName = substr($containerName, strrpos($containerName, '\\') + 1); + if (array_key_exists($containerName, $allowedElements)) { + $containerElements = $allowedElements[$containerName]; + } else { + throw new Exception('Invalid container.'); + } + + $elements = $container->getElements(); + if (count($elements) > 0) { + foreach ($elements as $element) { + $elmName = get_class($element); + $elmName = substr($elmName, strrpos($elmName, '\\') + 1); + if (in_array($elmName, $containerElements)) { + $method = "write{$elmName}"; + // Image on Header could be watermark + if ($containerName == 'Header' && $elmName == 'Image') { + if ($element->getIsWatermark()) { + $method = "writeWatermark"; + } + } + if (in_array($containerName, array('TextRun', 'Footnote'))) { + $this->$method($xmlWriter, $element, true); + } else { + $this->$method($xmlWriter, $element); + } + } + } + } else { + if ($containerName == 'Cell') { + $this->writeTextBreak($xmlWriter); + } + } + } + + /** + * Write margin or border + * + * @param XMLWriter $xmlWriter + * @param boolean $isBorder + * @param array $sizes + * @param array $colors + */ + protected function writeMarginBorder(XMLWriter $xmlWriter, $sizes, $colors = array(), $attributes = array()) + { + $sides = array('top', 'left', 'right', 'bottom', 'insideH', 'insideV'); + $sizeCount = count($sizes) - 1; + for ($i = 0; $i < $sizeCount; $i++) { + if (!is_null($sizes[$i])) { + $xmlWriter->startElement('w:' . $sides[$i]); + if (!empty($colors)) { + if (is_null($colors[$i]) && !empty($attributes)) { + if (array_key_exists('defaultColor', $attributes)) + $colors[$i] = $attributes['defaultColor']; + + } + $xmlWriter->writeAttribute('w:val', 'single'); + $xmlWriter->writeAttribute('w:sz', $sizes[$i]); + $xmlWriter->writeAttribute('w:color', $colors[$i]); + if (!empty($attributes)) { + if (array_key_exists('space', $attributes)) { + $xmlWriter->writeAttribute('w:space', '24'); + } + } + } else { + $xmlWriter->writeAttribute('w:w', $sizes[$i]); + $xmlWriter->writeAttribute('w:type', 'dxa'); + } + $xmlWriter->endElement(); + } + } + } } diff --git a/src/PhpWord/Writer/Word2007/Document.php b/src/PhpWord/Writer/Word2007/Document.php index b6e63628..60837794 100644 --- a/src/PhpWord/Writer/Word2007/Document.php +++ b/src/PhpWord/Writer/Word2007/Document.php @@ -12,18 +12,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\TOC; use PhpOffice\PhpWord\Container\Section; -use PhpOffice\PhpWord\Element\Text; -use PhpOffice\PhpWord\Element\TextRun; -use PhpOffice\PhpWord\Element\Link; -use PhpOffice\PhpWord\Element\Title; -use PhpOffice\PhpWord\Element\TextBreak; use PhpOffice\PhpWord\Element\PageBreak; -use PhpOffice\PhpWord\Element\ListItem; -use PhpOffice\PhpWord\Element\Table; -use PhpOffice\PhpWord\Element\Image; -use PhpOffice\PhpWord\Element\Object; -use PhpOffice\PhpWord\Element\Footnote; -use PhpOffice\PhpWord\Element\CheckBox; use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; @@ -61,44 +50,15 @@ class Document extends Base $xmlWriter->startElement('w:body'); - $_sections = $phpWord->getSections(); - $countSections = count($_sections); + $sections = $phpWord->getSections(); + $countSections = count($sections); $pSection = 0; if ($countSections > 0) { - foreach ($_sections as $section) { + foreach ($sections as $section) { $pSection++; - $_elements = $section->getElements(); - foreach ($_elements as $element) { - if ($element instanceof Text) { - $this->writeText($xmlWriter, $element); - } elseif ($element instanceof TextRun) { - $this->writeTextRun($xmlWriter, $element); - } elseif ($element instanceof Link) { - $this->writeLink($xmlWriter, $element); - } elseif ($element instanceof Title) { - $this->writeTitle($xmlWriter, $element); - } elseif ($element instanceof TextBreak) { - $this->writeTextBreak($xmlWriter, $element); - } elseif ($element instanceof PageBreak) { - $this->writePageBreak($xmlWriter); - } elseif ($element instanceof ListItem) { - $this->writeListItem($xmlWriter, $element); - } elseif ($element instanceof Table) { - $this->writeTable($xmlWriter, $element); - } elseif ($element instanceof Image) { - $this->writeImage($xmlWriter, $element); - } elseif ($element instanceof Object) { - $this->writeObject($xmlWriter, $element); - } elseif ($element instanceof TOC) { - $this->writeTOC($xmlWriter); - } elseif ($element instanceof Footnote) { - $this->writeFootnote($xmlWriter, $element); - } elseif ($element instanceof CheckBox) { - $this->writeCheckBox($xmlWriter, $element); - } - } + $this->writeContainerElements($xmlWriter, $section); if ($pSection == $countSections) { $this->writeEndSection($xmlWriter, $section); @@ -139,8 +99,8 @@ class Document extends Base private function writeEndSection(XMLWriter $xmlWriter, Section $section) { $settings = $section->getSettings(); - $_headers = $section->getHeaders(); - $_footer = $section->getFooter(); + $headers = $section->getHeaders(); + $footer = $section->getFooter(); $pgSzW = $settings->getPageSizeW(); $pgSzH = $settings->getPageSizeH(); $orientation = $settings->getOrientation(); @@ -161,43 +121,45 @@ class Document extends Base $xmlWriter->startElement('w:sectPr'); - foreach ($_headers as &$_header) { - $rId = $_header->getRelationId(); - $xmlWriter->startElement('w:headerReference'); - $xmlWriter->writeAttribute('w:type', $_header->getType()); - $xmlWriter->writeAttribute('r:id', 'rId' . $rId); - $xmlWriter->endElement(); - } - - if ($section->hasDifferentFirstPage()) { - $xmlWriter->startElement('w:titlePg'); - $xmlWriter->endElement(); - } - + // Section break if (!is_null($breakType)) { $xmlWriter->startElement('w:type'); $xmlWriter->writeAttribute('w:val', $breakType); $xmlWriter->endElement(); } - if (!is_null($_footer)) { - $rId = $_footer->getRelationId(); + // Header reference + foreach ($headers as &$header) { + $rId = $header->getRelationId(); + $xmlWriter->startElement('w:headerReference'); + $xmlWriter->writeAttribute('w:type', $header->getType()); + $xmlWriter->writeAttribute('r:id', 'rId' . $rId); + $xmlWriter->endElement(); + } + if ($section->hasDifferentFirstPage()) { + $xmlWriter->startElement('w:titlePg'); + $xmlWriter->endElement(); + } + + // Footer reference + if (!is_null($footer)) { + $rId = $footer->getRelationId(); $xmlWriter->startElement('w:footerReference'); $xmlWriter->writeAttribute('w:type', 'default'); $xmlWriter->writeAttribute('r:id', 'rId' . $rId); $xmlWriter->endElement(); } + // Page size & orientation $xmlWriter->startElement('w:pgSz'); $xmlWriter->writeAttribute('w:w', $pgSzW); $xmlWriter->writeAttribute('w:h', $pgSzH); - if (!is_null($orientation) && strtolower($orientation) != 'portrait') { $xmlWriter->writeAttribute('w:orient', $orientation); } + $xmlWriter->endElement(); // w:pgSz - $xmlWriter->endElement(); - + // Margins $xmlWriter->startElement('w:pgMar'); $xmlWriter->writeAttribute('w:top', $marginTop); $xmlWriter->writeAttribute('w:right', $marginRight); @@ -208,48 +170,19 @@ class Document extends Base $xmlWriter->writeAttribute('w:gutter', '0'); $xmlWriter->endElement(); - - if (!is_null($borders[0]) || !is_null($borders[1]) || !is_null($borders[2]) || !is_null($borders[3])) { + // Borders + $hasBorders = false; + for ($i = 0; $i < 4; $i++) { + if (!is_null($borders[$i])) { + $hasBorders = true; + break; + } + } + if ($hasBorders) { $borderColor = $settings->getBorderColor(); - $xmlWriter->startElement('w:pgBorders'); $xmlWriter->writeAttribute('w:offsetFrom', 'page'); - - if (!is_null($borders[0])) { - $xmlWriter->startElement('w:top'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $borders[0]); - $xmlWriter->writeAttribute('w:space', '24'); - $xmlWriter->writeAttribute('w:color', $borderColor[0]); - $xmlWriter->endElement(); - } - - if (!is_null($borders[1])) { - $xmlWriter->startElement('w:left'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $borders[1]); - $xmlWriter->writeAttribute('w:space', '24'); - $xmlWriter->writeAttribute('w:color', $borderColor[1]); - $xmlWriter->endElement(); - } - - if (!is_null($borders[2])) { - $xmlWriter->startElement('w:right'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $borders[2]); - $xmlWriter->writeAttribute('w:space', '24'); - $xmlWriter->writeAttribute('w:color', $borderColor[2]); - $xmlWriter->endElement(); - } - - if (!is_null($borders[3])) { - $xmlWriter->startElement('w:bottom'); - $xmlWriter->writeAttribute('w:val', 'single'); - $xmlWriter->writeAttribute('w:sz', $borders[3]); - $xmlWriter->writeAttribute('w:space', '24'); - $xmlWriter->writeAttribute('w:color', $borderColor[3]); - $xmlWriter->endElement(); - } + $this->writeMarginBorder($xmlWriter, $borders, $borderColor, array('space' => '24')); $xmlWriter->endElement(); } @@ -260,12 +193,12 @@ class Document extends Base $xmlWriter->endElement(); } + // Columns $xmlWriter->startElement('w:cols'); $xmlWriter->writeAttribute('w:num', $colsNum); $xmlWriter->writeAttribute('w:space', $colsSpace); $xmlWriter->endElement(); - $xmlWriter->endElement(); } @@ -274,7 +207,7 @@ class Document extends Base * * @param XMLWriter $xmlWriter */ - private function writePageBreak(XMLWriter $xmlWriter) + protected function writePageBreak(XMLWriter $xmlWriter, PageBreak $pagebreak) { $xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:r'); @@ -290,7 +223,7 @@ class Document extends Base * * @param XMLWriter $xmlWriter */ - private function writeTOC(XMLWriter $xmlWriter) + protected function writeTOC(XMLWriter $xmlWriter, TOC $toc) { $titles = TOC::getTitles(); $styleFont = TOC::getStyleFont(); diff --git a/src/PhpWord/Writer/Word2007/DocumentRels.php b/src/PhpWord/Writer/Word2007/DocumentRels.php index d39527a9..2129ea38 100755 --- a/src/PhpWord/Writer/Word2007/DocumentRels.php +++ b/src/PhpWord/Writer/Word2007/DocumentRels.php @@ -20,9 +20,9 @@ class DocumentRels extends Base /** * Write word/_rels/document.xml.rels * - * @param array $_relsCollection + * @param array $relsCollection */ - public function writeDocumentRels($_relsCollection) + public function writeDocumentRels($relsCollection) { // Create XML writer $xmlWriter = $this->getXmlWriter(); @@ -34,73 +34,42 @@ class DocumentRels extends Base $xmlWriter->startElement('Relationships'); $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); - // Relationship word/document.xml - $this->writeRel( - $xmlWriter, - 1, - 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles', - 'styles.xml' + // Write static files + $staticFiles = array( + 'styles' => 'styles.xml', + 'numbering' => 'numbering.xml', + 'settings' => 'settings.xml', + 'theme' => 'theme/theme1.xml', + 'webSettings' => 'webSettings.xml', + 'fontTable' => 'fontTable.xml', ); + $i = 0; + foreach ($staticFiles as $type => $file) { + $i++; + $schema = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/' . $type; + $this->writeRel($xmlWriter, $i, $schema, $file); + } - // Relationship word/numbering.xml - $this->writeRel( - $xmlWriter, - 2, - 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering', - 'numbering.xml' - ); + // Write media relationship (image, oleObject, hyperlink) + $this->writeMediaRels($xmlWriter, $relsCollection); - // Relationship word/settings.xml - $this->writeRel( - $xmlWriter, - 3, - 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings', - 'settings.xml' - ); - - // Relationship word/settings.xml - $this->writeRel( - $xmlWriter, - 4, - 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme', - 'theme/theme1.xml' - ); - - // Relationship word/settings.xml - $this->writeRel( - $xmlWriter, - 5, - 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings', - 'webSettings.xml' - ); - - // Relationship word/settings.xml - $this->writeRel( - $xmlWriter, - 6, - 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable', - 'fontTable.xml' - ); - - $this->writeMediaRels($xmlWriter, $_relsCollection); $xmlWriter->endElement(); // Relationships - // Return return $xmlWriter->getData(); } /** * Write header footer rels word/_rels/*.xml.rels * - * @param array $_relsCollection + * @param array $relsCollection */ - public function writeHeaderFooterRels($_relsCollection) + public function writeHeaderFooterRels($relsCollection) { $xmlWriter = $this->getXmlWriter(); $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); $xmlWriter->startElement('Relationships'); $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); - $this->writeMediaRels($xmlWriter, $_relsCollection); + $this->writeMediaRels($xmlWriter, $relsCollection); $xmlWriter->endElement(); return $xmlWriter->getData(); diff --git a/src/PhpWord/Writer/Word2007/Footer.php b/src/PhpWord/Writer/Word2007/Footer.php index cbcbcb26..d451bccd 100644 --- a/src/PhpWord/Writer/Word2007/Footer.php +++ b/src/PhpWord/Writer/Word2007/Footer.php @@ -10,15 +10,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\Container\Footer as FooterElement; -use PhpOffice\PhpWord\Element\Text; -use PhpOffice\PhpWord\Element\TextRun; -use PhpOffice\PhpWord\Element\Link; -use PhpOffice\PhpWord\Element\PreserveText; -use PhpOffice\PhpWord\Element\TextBreak; -use PhpOffice\PhpWord\Element\ListItem; -use PhpOffice\PhpWord\Element\Table; -use PhpOffice\PhpWord\Element\Image; -use PhpOffice\PhpWord\Element\CheckBox; use PhpOffice\PhpWord\Shared\XMLWriter; /** @@ -50,29 +41,7 @@ class Footer extends Base $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); $xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml'); - $_elements = $footer->getElements(); - - foreach ($_elements as $element) { - if ($element instanceof Text) { - $this->writeText($xmlWriter, $element); - } elseif ($element instanceof TextRun) { - $this->writeTextRun($xmlWriter, $element); - } elseif ($element instanceof Link) { - $this->writeLink($xmlWriter, $element); - } elseif ($element instanceof PreserveText) { - $this->writePreserveText($xmlWriter, $element); - } elseif ($element instanceof TextBreak) { - $this->writeTextBreak($xmlWriter, $element); - } elseif ($element instanceof ListItem) { - $this->writeListItem($xmlWriter, $element); - } elseif ($element instanceof Table) { - $this->writeTable($xmlWriter, $element); - } elseif ($element instanceof Image) { - $this->writeImage($xmlWriter, $element); - } elseif ($element instanceof CheckBox) { - $this->writeCheckBox($xmlWriter, $element); - } - } + $this->writeContainerElements($xmlWriter, $footer); $xmlWriter->endElement(); diff --git a/src/PhpWord/Writer/Word2007/Footnotes.php b/src/PhpWord/Writer/Word2007/Footnotes.php index 9ae7473a..406b1559 100644 --- a/src/PhpWord/Writer/Word2007/Footnotes.php +++ b/src/PhpWord/Writer/Word2007/Footnotes.php @@ -110,23 +110,9 @@ class Footnotes extends Base $xmlWriter->writeRaw(' '); $xmlWriter->endElement(); // w:t $xmlWriter->endElement(); // w:r - // Actual footnote contents - $elements = $footnote->getElements(); - if (count($elements) > 0) { - foreach ($elements as $element) { - if ($element instanceof Text) { - $this->writeText($xmlWriter, $element, true); - } elseif ($element instanceof Link) { - $this->writeLink($xmlWriter, $element, true); - } elseif ($element instanceof Image) { - $this->writeImage($xmlWriter, $element, true); - } elseif ($element instanceof Object) { - $this->writeObject($xmlWriter, $element, true); - } elseif ($element instanceof TextBreak) { - $xmlWriter->writeElement('w:br'); - } - } - } + + $this->writeContainerElements($xmlWriter, $footnote); + $xmlWriter->endElement(); // w:p $xmlWriter->endElement(); // w:footnote } diff --git a/src/PhpWord/Writer/Word2007/FootnotesRels.php b/src/PhpWord/Writer/Word2007/FootnotesRels.php index 49ca2181..012062e1 100644 --- a/src/PhpWord/Writer/Word2007/FootnotesRels.php +++ b/src/PhpWord/Writer/Word2007/FootnotesRels.php @@ -20,15 +20,15 @@ class FootnotesRels extends Base /** * Write word/_rels/footnotes.xml.rels * - * @param mixed $_relsCollection + * @param mixed $relsCollection */ - public function writeFootnotesRels($_relsCollection) + public function writeFootnotesRels($relsCollection) { $xmlWriter = $this->getXmlWriter(); $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); $xmlWriter->startElement('Relationships'); $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); - $this->writeMediaRels($xmlWriter, $_relsCollection); + $this->writeMediaRels($xmlWriter, $relsCollection); $xmlWriter->endElement(); return $xmlWriter->getData(); diff --git a/src/PhpWord/Writer/Word2007/Header.php b/src/PhpWord/Writer/Word2007/Header.php index 7bd95ed6..6b58c99f 100644 --- a/src/PhpWord/Writer/Word2007/Header.php +++ b/src/PhpWord/Writer/Word2007/Header.php @@ -10,15 +10,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\Container\Header as HeaderElement; -use PhpOffice\PhpWord\Element\Text; -use PhpOffice\PhpWord\Element\TextRun; -use PhpOffice\PhpWord\Element\Link; -use PhpOffice\PhpWord\Element\PreserveText; -use PhpOffice\PhpWord\Element\TextBreak; -use PhpOffice\PhpWord\Element\ListItem; -use PhpOffice\PhpWord\Element\Table; -use PhpOffice\PhpWord\Element\Image; -use PhpOffice\PhpWord\Element\CheckBox; use PhpOffice\PhpWord\Shared\XMLWriter; /** @@ -50,33 +41,7 @@ class Header extends Base $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); $xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml'); - $_elements = $header->getElements(); - - foreach ($_elements as $element) { - if ($element instanceof Text) { - $this->writeText($xmlWriter, $element); - } elseif ($element instanceof TextRun) { - $this->writeTextRun($xmlWriter, $element); - } elseif ($element instanceof Link) { - $this->writeLink($xmlWriter, $element); - } elseif ($element instanceof PreserveText) { - $this->writePreserveText($xmlWriter, $element); - } elseif ($element instanceof TextBreak) { - $this->writeTextBreak($xmlWriter, $element); - } elseif ($element instanceof ListItem) { - $this->writeListItem($xmlWriter, $element); - } elseif ($element instanceof Table) { - $this->writeTable($xmlWriter, $element); - } elseif ($element instanceof Image) { - if (!$element->getIsWatermark()) { - $this->writeImage($xmlWriter, $element); - } else { - $this->writeWatermark($xmlWriter, $element); - } - } elseif ($element instanceof CheckBox) { - $this->writeCheckBox($xmlWriter, $element); - } - } + $this->writeContainerElements($xmlWriter, $header); $xmlWriter->endElement(); From 637c9fce6f6a18989288c8b535b5341c1b85fe20 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 3 Apr 2014 08:44:41 +0700 Subject: [PATCH 11/27] Create new Element abstract class --- CHANGELOG.md | 5 +- src/PhpWord/Container/Container.php | 31 ++++++++- src/PhpWord/Element/Cell.php | 15 +---- src/PhpWord/Element/CheckBox.php | 2 +- src/PhpWord/Element/Element.php | 42 +++++++++++++ src/PhpWord/Element/Footnote.php | 26 ++------ src/PhpWord/Element/Image.php | 17 ++--- src/PhpWord/Element/Link.php | 66 ++++++-------------- src/PhpWord/Element/ListItem.php | 33 ++++------ src/PhpWord/Element/Object.php | 48 ++++++-------- src/PhpWord/Element/PageBreak.php | 2 +- src/PhpWord/Element/PreserveText.php | 45 +++---------- src/PhpWord/Element/Row.php | 55 +++++++--------- src/PhpWord/Element/Table.php | 60 +++++++----------- src/PhpWord/Element/Text.php | 2 +- src/PhpWord/Element/TextBreak.php | 2 +- src/PhpWord/Element/TextRun.php | 13 +--- src/PhpWord/Element/Title.php | 30 ++++----- src/PhpWord/Writer/Word2007/Base.php | 18 +++--- src/PhpWord/Writer/Word2007/Footnotes.php | 2 +- tests/PhpWord/Tests/Element/FootnoteTest.php | 4 +- 21 files changed, 228 insertions(+), 290 deletions(-) create mode 100644 src/PhpWord/Element/Element.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ddd2e89..f1430f0a 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,8 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - `createHeader` replaced by `addHeader` - `createFooter` replaced by `addFooter` - `createSection` replaced by `addSection` +- `Element\Footnote::getReferenceId` replaced by `Container\Container::getRelationId` +- `Element\Footnote::setReferenceId` replaced by `Container\Container::setRelationId` ### Miscellaneous @@ -40,8 +42,9 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - Writer: Refactor writer classes and make a new Writer abstract class - @ivanlanin GH-160 - Reader: Rename AbstractReader > Reader - @ivanlanin - General: Refactor folders: Element, Container, and Exception - @ivanlanin GH-187 -- Container: Create new Container abstract class - @ivanlanin GH-187 - General: Remove legacy HashTable and all related properties/methods - @ivanlanin GH-187 +- Container: Create new Container abstract class - @ivanlanin GH-187 +- Element: Create new Element abstract class - @ivanlanin GH-187 ## 0.9.1 - 27 Mar 2014 diff --git a/src/PhpWord/Container/Container.php b/src/PhpWord/Container/Container.php index fd88955e..332f0c64 100644 --- a/src/PhpWord/Container/Container.php +++ b/src/PhpWord/Container/Container.php @@ -385,7 +385,7 @@ abstract class Container $footnote = new FootnoteElement($paragraphStyle); $refID = FootnoteCollection::addFootnoteElement($footnote); - $footnote->setReferenceId($refID); + $footnote->setRelationId($refID); $this->elements[] = $footnote; return $footnote; @@ -487,6 +487,33 @@ abstract class Container return $this->addFootnote($paragraphStyle); } + /** + * Set style value + * + * Used by Footnote + * + * @param mixed $styleObject Style object, could be Font, Paragraph, Cell, Image + * @param mixed $styleValue + * @param boolean $returnObject Always return object + * @todo Remove duplicate with ..\Element\Element + */ + protected function setStyle($styleObject, $styleValue = null, $returnObject = false) + { + if (!is_null($styleValue) && is_array($styleValue)) { + foreach ($styleValue as $key => $value) { + if (substr($key, 0, 1) != '_') { + $key = '_' . $key; + } + $styleObject->setStyleValue($key, $value); + } + $style = $styleObject; + } else { + $style = $returnObject ? $styleObject : $styleValue; + } + + return $style; + } + /** * Check if a method is allowed for the current container * @@ -507,7 +534,7 @@ abstract class Container 'object' => array('section', 'textrun', 'cell', 'footnote'), 'footnote' => array('section', 'textrun', 'cell'), 'preservetext' => array('header', 'footer', 'cell'), - 'relationid' => array('header', 'footer'), + 'relationid' => array('header', 'footer', 'footnote'), 'title' => array('section'), ); $validContainerInContainers = array( diff --git a/src/PhpWord/Element/Cell.php b/src/PhpWord/Element/Cell.php index 25e9fb71..61e2fa4c 100755 --- a/src/PhpWord/Element/Cell.php +++ b/src/PhpWord/Element/Cell.php @@ -45,20 +45,7 @@ class Cell extends Container $this->docPart = $docPart; $this->docPartId = $docPartId; $this->width = $width; - $this->cellStyle = new CellStyle(); - - if (!is_null($style)) { - if (is_array($style)) { - foreach ($style as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $this->cellStyle->setStyleValue($key, $value); - } - } else { - $this->cellStyle = $style; - } - } + $this->cellStyle = $this->setStyle(new CellStyle(), $style, true); } /** diff --git a/src/PhpWord/Element/CheckBox.php b/src/PhpWord/Element/CheckBox.php index 9a3fcc79..187bd132 100644 --- a/src/PhpWord/Element/CheckBox.php +++ b/src/PhpWord/Element/CheckBox.php @@ -15,7 +15,7 @@ use PhpOffice\PhpWord\Style\Paragraph; /** * Check box element */ -class CheckBox +class CheckBox extends Element { /** * Name content diff --git a/src/PhpWord/Element/Element.php b/src/PhpWord/Element/Element.php new file mode 100644 index 00000000..b94a62c3 --- /dev/null +++ b/src/PhpWord/Element/Element.php @@ -0,0 +1,42 @@ + $value) { + if (substr($key, 0, 1) != '_') { + $key = '_' . $key; + } + $styleObject->setStyleValue($key, $value); + } + $style = $styleObject; + } else { + $style = $returnObject ? $styleObject : $styleValue; + } + + return $style; + } +} diff --git a/src/PhpWord/Element/Footnote.php b/src/PhpWord/Element/Footnote.php index 3d65a256..67cd5d9a 100644 --- a/src/PhpWord/Element/Footnote.php +++ b/src/PhpWord/Element/Footnote.php @@ -24,13 +24,6 @@ class Footnote extends Container */ private $paragraphStyle; - /** - * Footnote Reference ID - * - * @var string - */ - private $referenceId; - /** * Create new instance * @@ -39,18 +32,7 @@ class Footnote extends Container public function __construct($paragraphStyle = null) { $this->container = 'footnote'; - // Set paragraph style - if (is_array($paragraphStyle)) { - $this->paragraphStyle = new Paragraph(); - foreach ($paragraphStyle as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $this->paragraphStyle->setStyleValue($key, $value); - } - } else { - $this->paragraphStyle = $paragraphStyle; - } + $this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle); } /** @@ -67,19 +49,21 @@ class Footnote extends Container * Get Footnote Reference ID * * @return int + * @deprecated 0.9.2 */ public function getReferenceId() { - return $this->referenceId; + return $this->getRelationId(); } /** * Set Footnote Reference ID * * @param int $refId + * @deprecated 0.9.2 */ public function setReferenceId($refId) { - $this->referenceId = $refId; + $this->setRelationId($refId); } } diff --git a/src/PhpWord/Element/Image.php b/src/PhpWord/Element/Image.php index 89ee6f37..2631ec16 100755 --- a/src/PhpWord/Element/Image.php +++ b/src/PhpWord/Element/Image.php @@ -11,11 +11,12 @@ namespace PhpOffice\PhpWord\Element; use PhpOffice\PhpWord\Exception\InvalidImageException; use PhpOffice\PhpWord\Exception\UnsupportedImageTypeException; +use PhpOffice\PhpWord\Style\Image as ImageStyle; /** * Image element */ -class Image +class Image extends Element { /** * Image source @@ -27,7 +28,7 @@ class Image /** * Image style * - * @var \PhpOffice\PhpWord\Style\Image + * @var ImageStyle */ private $style; @@ -131,15 +132,7 @@ class Image // Set private properties $this->source = $source; $this->isWatermark = $isWatermark; - $this->style = new \PhpOffice\PhpWord\Style\Image(); - if (!is_null($style) && is_array($style)) { - foreach ($style as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $this->style->setStyleValue($key, $value); - } - } + $this->style = $this->setStyle(new ImageStyle(), $style, true); if (isset($style['wrappingStyle'])) { $this->style->setWrappingStyle($style['wrappingStyle']); } @@ -153,7 +146,7 @@ class Image /** * Get Image style * - * @return \PhpOffice\PhpWord\Style\Image + * @return ImageStyle */ public function getStyle() { diff --git a/src/PhpWord/Element/Link.php b/src/PhpWord/Element/Link.php index 1b6cf22a..ee1af203 100644 --- a/src/PhpWord/Element/Link.php +++ b/src/PhpWord/Element/Link.php @@ -15,42 +15,42 @@ use PhpOffice\PhpWord\Style\Paragraph; /** * Link element */ -class Link +class Link extends Element { /** * Link source * * @var string */ - private $_linkSrc; + private $source; /** * Link name * * @var string */ - private $_linkName; + private $name; /** * Link Relation ID * * @var string */ - private $_rId; + private $relationId; /** - * Link style + * Font style * * @var string|Font */ - private $_styleFont; + private $fontStyle; /** * Paragraph style * * @var string|Paragraph */ - private $_styleParagraph; + private $paragraphStyle; /** @@ -58,41 +58,15 @@ class Link * * @param string $linkSrc * @param string $linkName - * @param mixed $styleFont - * @param mixed $styleParagraph + * @param mixed $fontStyle + * @param mixed $paragraphStyle */ - public function __construct($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null) + public function __construct($linkSrc, $linkName = null, $fontStyle = null, $paragraphStyle = null) { - $this->_linkSrc = $linkSrc; - $this->_linkName = $linkName; - - // Set font style - if (is_array($styleFont)) { - $this->_styleFont = new Font('text'); - - foreach ($styleFont as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $this->_styleFont->setStyleValue($key, $value); - } - } else { - $this->_styleFont = $styleFont; - } - - // Set paragraph style - if (is_array($styleParagraph)) { - $this->_styleParagraph = new Paragraph(); - - foreach ($styleParagraph as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $this->_styleParagraph->setStyleValue($key, $value); - } - } else { - $this->_styleParagraph = $styleParagraph; - } + $this->source = $linkSrc; + $this->name = $linkName; + $this->fontStyle = $this->setStyle(new Font('text'), $fontStyle); + $this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle); return $this; } @@ -104,7 +78,7 @@ class Link */ public function getRelationId() { - return $this->_rId; + return $this->relationId; } /** @@ -114,7 +88,7 @@ class Link */ public function setRelationId($rId) { - $this->_rId = $rId; + $this->relationId = $rId; } /** @@ -124,7 +98,7 @@ class Link */ public function getLinkSrc() { - return $this->_linkSrc; + return $this->source; } /** @@ -134,7 +108,7 @@ class Link */ public function getLinkName() { - return $this->_linkName; + return $this->name; } /** @@ -144,7 +118,7 @@ class Link */ public function getFontStyle() { - return $this->_styleFont; + return $this->fontStyle; } /** @@ -154,6 +128,6 @@ class Link */ public function getParagraphStyle() { - return $this->_styleParagraph; + return $this->paragraphStyle; } } diff --git a/src/PhpWord/Element/ListItem.php b/src/PhpWord/Element/ListItem.php index e920d4b8..6cf5d1d6 100644 --- a/src/PhpWord/Element/ListItem.php +++ b/src/PhpWord/Element/ListItem.php @@ -9,31 +9,33 @@ namespace PhpOffice\PhpWord\Element; +use PhpOffice\PhpWord\Style\ListItem as ListItemStyle; + /** * List item element */ -class ListItem +class ListItem extends Element { /** * ListItem Style * - * @var \PhpOffice\PhpWord\Style\ListItem + * @var ListItemStyle */ - private $_style; + private $style; /** * Textrun * * @var \PhpOffice\PhpWord\Element\Text */ - private $_textObject; + private $textObject; /** * ListItem Depth * * @var int */ - private $_depth; + private $depth; /** @@ -47,18 +49,9 @@ class ListItem */ public function __construct($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null) { - $this->_style = new \PhpOffice\PhpWord\Style\ListItem(); - $this->_textObject = new Text($text, $styleFont, $styleParagraph); - $this->_depth = $depth; - - if (!is_null($styleList) && is_array($styleList)) { - foreach ($styleList as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $this->_style->setStyleValue($key, $value); - } - } + $this->textObject = new Text($text, $styleFont, $styleParagraph); + $this->depth = $depth; + $this->style = $this->setStyle(new ListItemStyle(), $styleList, true); } /** @@ -66,7 +59,7 @@ class ListItem */ public function getStyle() { - return $this->_style; + return $this->style; } /** @@ -74,7 +67,7 @@ class ListItem */ public function getTextObject() { - return $this->_textObject; + return $this->textObject; } /** @@ -82,6 +75,6 @@ class ListItem */ public function getDepth() { - return $this->_depth; + return $this->depth; } } diff --git a/src/PhpWord/Element/Object.php b/src/PhpWord/Element/Object.php index 61bf6f8e..26207787 100644 --- a/src/PhpWord/Element/Object.php +++ b/src/PhpWord/Element/Object.php @@ -9,45 +9,47 @@ namespace PhpOffice\PhpWord\Element; +use PhpOffice\PhpWord\Style\Image as ImageStyle; + /** * Object element */ -class Object +class Object extends Element { /** * Ole-Object Src * * @var string */ - private $_src; + private $source; /** * Image Style * * @var \PhpOffice\PhpWord\Style\Image */ - private $_style; + private $style; /** * Object Relation ID * * @var int */ - private $_rId; + private $relationId; /** * Image Relation ID * * @var int */ - private $_rIdImg; + private $imageRelationId; /** * Object ID * * @var int */ - private $_objId; + private $objectId; /** @@ -58,22 +60,12 @@ class Object */ public function __construct($src, $style = null) { - $_supportedObjectTypes = array('xls', 'doc', 'ppt', 'xlsx', 'docx', 'pptx'); + $supportedTypes = array('xls', 'doc', 'ppt', 'xlsx', 'docx', 'pptx'); $inf = pathinfo($src); - if (\file_exists($src) && in_array($inf['extension'], $_supportedObjectTypes)) { - $this->_src = $src; - $this->_style = new \PhpOffice\PhpWord\Style\Image(); - - if (!is_null($style) && is_array($style)) { - foreach ($style as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $this->_style->setStyleValue($key, $value); - } - } - + if (\file_exists($src) && in_array($inf['extension'], $supportedTypes)) { + $this->source = $src; + $this->style = $this->setStyle(new ImageStyle(), $style, true); return $this; } else { return false; @@ -87,7 +79,7 @@ class Object */ public function getStyle() { - return $this->_style; + return $this->style; } /** @@ -97,7 +89,7 @@ class Object */ public function getSource() { - return $this->_src; + return $this->source; } /** @@ -107,7 +99,7 @@ class Object */ public function getRelationId() { - return $this->_rId; + return $this->relationId; } /** @@ -117,7 +109,7 @@ class Object */ public function setRelationId($rId) { - $this->_rId = $rId; + $this->relationId = $rId; } /** @@ -127,7 +119,7 @@ class Object */ public function getImageRelationId() { - return $this->_rIdImg; + return $this->imageRelationId; } /** @@ -137,7 +129,7 @@ class Object */ public function setImageRelationId($rId) { - $this->_rIdImg = $rId; + $this->imageRelationId = $rId; } /** @@ -147,7 +139,7 @@ class Object */ public function getObjectId() { - return $this->_objId; + return $this->objectId; } /** @@ -157,6 +149,6 @@ class Object */ public function setObjectId($objId) { - $this->_objId = $objId; + $this->objectId = $objId; } } diff --git a/src/PhpWord/Element/PageBreak.php b/src/PhpWord/Element/PageBreak.php index 4e49582d..c297681d 100644 --- a/src/PhpWord/Element/PageBreak.php +++ b/src/PhpWord/Element/PageBreak.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Element; /** * Page break element */ -class PageBreak +class PageBreak extends Element { /** * Create new page break diff --git a/src/PhpWord/Element/PreserveText.php b/src/PhpWord/Element/PreserveText.php index 453e9b84..0fe77d9a 100644 --- a/src/PhpWord/Element/PreserveText.php +++ b/src/PhpWord/Element/PreserveText.php @@ -15,28 +15,28 @@ use PhpOffice\PhpWord\Style\Paragraph; /** * Preserve text/field element */ -class PreserveText +class PreserveText extends Element { /** * Text content * * @var string */ - private $_text; + private $text; /** * Text style * * @var string|Font */ - private $_styleFont; + private $fontStyle; /** * Paragraph style * * @var string|Paragraph */ - private $_styleParagraph; + private $paragraphStyle; /** @@ -49,37 +49,12 @@ class PreserveText */ public function __construct($text = null, $styleFont = null, $styleParagraph = null) { - // Set font style - if (is_array($styleFont)) { - $this->_styleFont = new Font('text'); - - foreach ($styleFont as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $this->_styleFont->setStyleValue($key, $value); - } - } else { - $this->_styleFont = $styleFont; - } - - // Set paragraph style - if (is_array($styleParagraph)) { - $this->_styleParagraph = new Paragraph(); - - foreach ($styleParagraph as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $this->_styleParagraph->setStyleValue($key, $value); - } - } else { - $this->_styleParagraph = $styleParagraph; - } + $this->fontStyle = $this->setStyle(new Font('text'), $styleFont); + $this->paragraphStyle = $this->setStyle(new Paragraph(), $styleParagraph); $matches = preg_split('/({.*?})/', $text, null, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY); if (isset($matches[0])) { - $this->_text = $matches; + $this->text = $matches; } return $this; @@ -92,7 +67,7 @@ class PreserveText */ public function getFontStyle() { - return $this->_styleFont; + return $this->fontStyle; } /** @@ -102,7 +77,7 @@ class PreserveText */ public function getParagraphStyle() { - return $this->_styleParagraph; + return $this->paragraphStyle; } /** @@ -112,6 +87,6 @@ class PreserveText */ public function getText() { - return $this->_text; + return $this->text; } } diff --git a/src/PhpWord/Element/Row.php b/src/PhpWord/Element/Row.php index eefeca57..25357062 100644 --- a/src/PhpWord/Element/Row.php +++ b/src/PhpWord/Element/Row.php @@ -9,73 +9,63 @@ namespace PhpOffice\PhpWord\Element; +use PhpOffice\PhpWord\Style\Row as RowStyle; + /** * Table row element */ -class Row +class Row extends Element { /** * Row height * * @var int */ - private $_height = null; + private $height = null; /** * Row style * - * @var \PhpOffice\PhpWord\Style\Row + * @var RowStyle */ - private $_style; + private $style; /** * Row cells * * @var array */ - private $_cells = array(); + private $cells = array(); /** * Table holder * * @var string */ - private $_insideOf; + private $docPart; /** * Section/Header/Footer count * * @var int */ - private $_pCount; + private $docPartId; /** * Create a new table row * - * @param string $insideOf - * @param int $pCount + * @param string $docPart + * @param int $docPartId * @param int $height * @param mixed $style */ - public function __construct($insideOf, $pCount, $height = null, $style = null) + public function __construct($docPart, $docPartId, $height = null, $style = null) { - $this->_insideOf = $insideOf; - $this->_pCount = $pCount; - $this->_height = $height; - $this->_style = new \PhpOffice\PhpWord\Style\Row(); - - if (!is_null($style)) { - if (is_array($style)) { - - foreach ($style as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $this->_style->setStyleValue($key, $value); - } - } - } + $this->docPart = $docPart; + $this->docPartId = $docPartId; + $this->height = $height; + $this->style = $this->setStyle(new RowStyle(), $style, true); } /** @@ -83,12 +73,11 @@ class Row * * @param int $width * @param mixed $style - * @return \PhpOffice\PhpWord\Element\Cell */ public function addCell($width = null, $style = null) { - $cell = new Cell($this->_insideOf, $this->_pCount, $width, $style); - $this->_cells[] = $cell; + $cell = new Cell($this->docPart, $this->docPartId, $width, $style); + $this->cells[] = $cell; return $cell; } @@ -99,17 +88,17 @@ class Row */ public function getCells() { - return $this->_cells; + return $this->cells; } /** * Get row style * - * @return \PhpOffice\PhpWord\Style\Row + * @return RowStyle */ public function getStyle() { - return $this->_style; + return $this->style; } /** @@ -119,6 +108,6 @@ class Row */ public function getHeight() { - return $this->_height; + return $this->height; } } diff --git a/src/PhpWord/Element/Table.php b/src/PhpWord/Element/Table.php index 083ad5fc..0035b897 100644 --- a/src/PhpWord/Element/Table.php +++ b/src/PhpWord/Element/Table.php @@ -10,74 +10,60 @@ namespace PhpOffice\PhpWord\Element; use PhpOffice\PhpWord\Element\Row; - +use PhpOffice\PhpWord\Style\Table as TableStyle; /** * Table element */ -class Table +class Table extends Element { /** * Table style * - * @var \PhpOffice\PhpWord\Style\Table + * @var TableStyle */ - private $_style; + private $style; /** * Table rows * * @var array */ - private $_rows = array(); + private $rows = array(); /** * Table holder * * @var string */ - private $_insideOf = null; + private $docPart = null; /** * Table holder count * * @var array */ - private $_pCount; + private $docPartId; /** * Table width * * @var int */ - private $_width = null; + private $width = null; /** * Create a new table * - * @param string $insideOf - * @param int $pCount + * @param string $docPart + * @param int $docPartId * @param mixed $style */ - public function __construct($insideOf, $pCount, $style = null) + public function __construct($docPart, $docPartId, $style = null) { - $this->_insideOf = $insideOf; - $this->_pCount = $pCount; - - if (!is_null($style)) { - if (is_array($style)) { - $this->_style = new \PhpOffice\PhpWord\Style\Table(); - - foreach ($style as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $this->_style->setStyleValue($key, $value); - } - } else { - $this->_style = $style; - } - } + $this->docPart = $docPart; + $this->docPartId = $docPartId; + $this->style = $this->setStyle(new TableStyle(), $style); } /** @@ -88,8 +74,8 @@ class Table */ public function addRow($height = null, $style = null) { - $row = new Row($this->_insideOf, $this->_pCount, $height, $style); - $this->_rows[] = $row; + $row = new Row($this->docPart, $this->docPartId, $height, $style); + $this->rows[] = $row; return $row; } @@ -102,8 +88,8 @@ class Table */ public function addCell($width = null, $style = null) { - $i = count($this->_rows) - 1; - $cell = $this->_rows[$i]->addCell($width, $style); + $i = count($this->rows) - 1; + $cell = $this->rows[$i]->addCell($width, $style); return $cell; } @@ -114,17 +100,17 @@ class Table */ public function getRows() { - return $this->_rows; + return $this->rows; } /** * Get table style * - * @return \PhpOffice\PhpWord\Style\Table + * @return TableStyle */ public function getStyle() { - return $this->_style; + return $this->style; } /** @@ -134,7 +120,7 @@ class Table */ public function setWidth($width) { - $this->_width = $width; + $this->width = $width; } /** @@ -144,6 +130,6 @@ class Table */ public function getWidth() { - return $this->_width; + return $this->width; } } diff --git a/src/PhpWord/Element/Text.php b/src/PhpWord/Element/Text.php index 75e1727e..620095b8 100644 --- a/src/PhpWord/Element/Text.php +++ b/src/PhpWord/Element/Text.php @@ -15,7 +15,7 @@ use PhpOffice\PhpWord\Style\Paragraph; /** * Text element */ -class Text +class Text extends Element { /** * Text content diff --git a/src/PhpWord/Element/TextBreak.php b/src/PhpWord/Element/TextBreak.php index 39f3ac16..adf144ba 100755 --- a/src/PhpWord/Element/TextBreak.php +++ b/src/PhpWord/Element/TextBreak.php @@ -15,7 +15,7 @@ use PhpOffice\PhpWord\Style\Paragraph; /** * Text break element */ -class TextBreak +class TextBreak extends Element { /** * Paragraph style diff --git a/src/PhpWord/Element/TextRun.php b/src/PhpWord/Element/TextRun.php index 5cf1a0c6..50b302de 100755 --- a/src/PhpWord/Element/TextRun.php +++ b/src/PhpWord/Element/TextRun.php @@ -36,18 +36,7 @@ class TextRun extends Container $this->container = 'textrun'; $this->docPart = $docPart; $this->docPartId = $docPartId; - // Set paragraph style - if (is_array($paragraphStyle)) { - $this->paragraphStyle = new Paragraph(); - foreach ($paragraphStyle as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $this->paragraphStyle->setStyleValue($key, $value); - } - } else { - $this->paragraphStyle = $paragraphStyle; - } + $this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle); } /** diff --git a/src/PhpWord/Element/Title.php b/src/PhpWord/Element/Title.php index 3921d66c..2e7bf035 100644 --- a/src/PhpWord/Element/Title.php +++ b/src/PhpWord/Element/Title.php @@ -12,42 +12,42 @@ namespace PhpOffice\PhpWord\Element; /** * Title element */ -class Title +class Title extends Element { /** * Title Text content * * @var string */ - private $_text; + private $text; /** * Title depth * * @var int */ - private $_depth; + private $depth; /** * Title anchor * * @var int */ - private $_anchor; + private $anchor; /** * Title Bookmark ID * * @var int */ - private $_bookmarkId; + private $bookmarkId; /** * Title style * * @var string */ - private $_style; + private $style; /** @@ -60,11 +60,11 @@ class Title public function __construct($text, $depth = 1, $style = null) { if (!is_null($style)) { - $this->_style = $style; + $this->style = $style; } - $this->_text = $text; - $this->_depth = $depth; + $this->text = $text; + $this->depth = $depth; return $this; } @@ -76,7 +76,7 @@ class Title */ public function setAnchor($anchor) { - $this->_anchor = $anchor; + $this->anchor = $anchor; } /** @@ -86,7 +86,7 @@ class Title */ public function getAnchor() { - return $this->_anchor; + return $this->anchor; } /** @@ -96,7 +96,7 @@ class Title */ public function setBookmarkId($bookmarkId) { - $this->_bookmarkId = $bookmarkId; + $this->bookmarkId = $bookmarkId; } /** @@ -106,7 +106,7 @@ class Title */ public function getBookmarkId() { - return $this->_bookmarkId; + return $this->bookmarkId; } /** @@ -116,7 +116,7 @@ class Title */ public function getText() { - return $this->_text; + return $this->text; } /** @@ -126,6 +126,6 @@ class Title */ public function getStyle() { - return $this->_style; + return $this->style; } } diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index db8bd851..aec01ee3 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -614,7 +614,7 @@ class Base extends WriterPart $xmlWriter->endElement(); // w:rStyle $xmlWriter->endElement(); // w:rPr $xmlWriter->startElement('w:footnoteReference'); - $xmlWriter->writeAttribute('w:id', $footnote->getReferenceId()); + $xmlWriter->writeAttribute('w:id', $footnote->getRelationId()); $xmlWriter->endElement(); // w:footnoteReference $xmlWriter->endElement(); // w:r if (!$withoutP) { @@ -1173,13 +1173,16 @@ class Base extends WriterPart */ protected function writeContainerElements(XMLWriter $xmlWriter, Container $container) { + // Check allowed elements + $elmCommon = array('Text', 'Link', 'TextBreak', 'Image'); + $elmMainCell = array_merge($elmCommon, array('TextRun', 'ListItem', 'CheckBox')); $allowedElements = array( - 'Section' => array('Text', 'TextRun', 'Link', 'Title', 'TextBreak', 'ListItem', 'Table', 'Image', 'Object', 'CheckBox', 'Footnote', 'TOC'), - 'Header' => array('Text', 'TextRun', 'Link', 'PreserveText', 'TextBreak', 'ListItem', 'Image', 'CheckBox'), - 'Footer' => array('Text', 'TextRun', 'Link', 'PreserveText', 'TextBreak', 'ListItem', 'Image', 'CheckBox'), - 'Cell' => array('Text', 'TextRun', 'Link', 'PreserveText', 'TextBreak', 'ListItem', 'Image', 'Object', 'CheckBox', 'Footnote'), - 'TextRun' => array('Text', 'Link', 'TextBreak', 'Image', 'Object', 'Footnote'), - 'Footnote' => array('Text', 'Link', 'TextBreak', 'Image', 'Object'), + 'Section' => array_merge($elmMainCell, array('Table', 'Footnote', 'Object', 'Title', 'PageBreak', 'TOC')), + 'Header' => array_merge($elmMainCell, array('Table', 'PreserveText')), + 'Footer' => array_merge($elmMainCell, array('Table', 'PreserveText')), + 'Cell' => array_merge($elmMainCell, array('Object', 'PreserveText', 'Footnote')), + 'TextRun' => array_merge($elmCommon, array('Object', 'Footnote')), + 'Footnote' => array_merge($elmCommon, array('Object')), ); $containerName = get_class($container); $containerName = substr($containerName, strrpos($containerName, '\\') + 1); @@ -1189,6 +1192,7 @@ class Base extends WriterPart throw new Exception('Invalid container.'); } + // Loop through elements $elements = $container->getElements(); if (count($elements) > 0) { foreach ($elements as $element) { diff --git a/src/PhpWord/Writer/Word2007/Footnotes.php b/src/PhpWord/Writer/Word2007/Footnotes.php index 406b1559..45c60eeb 100644 --- a/src/PhpWord/Writer/Word2007/Footnotes.php +++ b/src/PhpWord/Writer/Word2007/Footnotes.php @@ -89,7 +89,7 @@ class Footnotes extends Base protected function writeFootnote(XMLWriter $xmlWriter, Footnote $footnote, $withoutP = false) { $xmlWriter->startElement('w:footnote'); - $xmlWriter->writeAttribute('w:id', $footnote->getReferenceId()); + $xmlWriter->writeAttribute('w:id', $footnote->getRelationId()); $xmlWriter->startElement('w:p'); // Paragraph style $styleParagraph = $footnote->getParagraphStyle(); diff --git a/tests/PhpWord/Tests/Element/FootnoteTest.php b/tests/PhpWord/Tests/Element/FootnoteTest.php index 21a18a4f..c2571afe 100644 --- a/tests/PhpWord/Tests/Element/FootnoteTest.php +++ b/tests/PhpWord/Tests/Element/FootnoteTest.php @@ -96,8 +96,8 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase $oFootnote = new Footnote(); $iVal = rand(1, 1000); - $oFootnote->setReferenceId($iVal); - $this->assertEquals($oFootnote->getReferenceId(), $iVal); + $oFootnote->setRelationId($iVal); + $this->assertEquals($oFootnote->getRelationId(), $iVal); } /** From 1ebd26ddc6a228572689c97062fe0667f0a587d0 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 3 Apr 2014 09:20:21 +0700 Subject: [PATCH 12/27] Fix some test error --- src/PhpWord/Element/Table.php | 1 + src/PhpWord/Writer/Word2007/Base.php | 4 ++-- tests/PhpWord/Tests/Element/CellTest.php | 11 ----------- 3 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/PhpWord/Element/Table.php b/src/PhpWord/Element/Table.php index 0035b897..72362c07 100644 --- a/src/PhpWord/Element/Table.php +++ b/src/PhpWord/Element/Table.php @@ -11,6 +11,7 @@ namespace PhpOffice\PhpWord\Element; use PhpOffice\PhpWord\Element\Row; use PhpOffice\PhpWord\Style\Table as TableStyle; + /** * Table element */ diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index aec01ee3..0bf0056b 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -1237,9 +1237,9 @@ class Base extends WriterPart $xmlWriter->startElement('w:' . $sides[$i]); if (!empty($colors)) { if (is_null($colors[$i]) && !empty($attributes)) { - if (array_key_exists('defaultColor', $attributes)) + if (array_key_exists('defaultColor', $attributes)) { $colors[$i] = $attributes['defaultColor']; - + } } $xmlWriter->writeAttribute('w:val', 'single'); $xmlWriter->writeAttribute('w:sz', $sizes[$i]); diff --git a/tests/PhpWord/Tests/Element/CellTest.php b/tests/PhpWord/Tests/Element/CellTest.php index be541935..a3507e3f 100644 --- a/tests/PhpWord/Tests/Element/CellTest.php +++ b/tests/PhpWord/Tests/Element/CellTest.php @@ -42,17 +42,6 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oCell->getWidth(), null); } - /** - * New instance with string - */ - public function testConstructWithStyleString() - { - $iVal = rand(1, 1000); - $oCell = new Cell('section', $iVal, null, 'cellStyle'); - - $this->assertEquals($oCell->getStyle(), 'cellStyle'); - } - /** * Add text */ From 7c549f58020184f97b017c7a29b3746f3014cd29 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 3 Apr 2014 10:13:13 +0700 Subject: [PATCH 13/27] DocBlock and use fixes --- src/PhpWord/Container/Container.php | 34 +---- src/PhpWord/Container/Header.php | 2 +- src/PhpWord/Container/Section.php | 4 +- src/PhpWord/DocumentProperties.php | 116 +++++++++--------- src/PhpWord/Element/Image.php | 10 +- src/PhpWord/Element/TextRun.php | 2 +- src/PhpWord/Element/Title.php | 2 +- src/PhpWord/Media.php | 5 +- src/PhpWord/PhpWord.php | 40 +++--- src/PhpWord/Reader/Word2007.php | 2 +- src/PhpWord/Shared/Font.php | 16 +-- src/PhpWord/Style/Font.php | 4 +- src/PhpWord/Style/Paragraph.php | 4 +- src/PhpWord/Style/Table.php | 2 +- src/PhpWord/Writer/ODText/Manifest.php | 1 - src/PhpWord/Writer/ODText/Meta.php | 1 - src/PhpWord/Writer/ODText/Styles.php | 1 - src/PhpWord/Writer/Word2007/Base.php | 3 +- src/PhpWord/Writer/Word2007/DocProps.php | 1 - src/PhpWord/Writer/Word2007/Document.php | 1 - src/PhpWord/Writer/Word2007/DocumentRels.php | 3 - src/PhpWord/Writer/Word2007/Footer.php | 1 - src/PhpWord/Writer/Word2007/Footnotes.php | 6 - src/PhpWord/Writer/Word2007/FootnotesRels.php | 3 - src/PhpWord/Writer/Word2007/Header.php | 1 - src/PhpWord/Writer/Word2007/Rels.php | 2 - 26 files changed, 110 insertions(+), 157 deletions(-) diff --git a/src/PhpWord/Container/Container.php b/src/PhpWord/Container/Container.php index 332f0c64..95fa186f 100644 --- a/src/PhpWord/Container/Container.php +++ b/src/PhpWord/Container/Container.php @@ -16,6 +16,7 @@ use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\TOC; use PhpOffice\PhpWord\Footnote as FootnoteCollection; use PhpOffice\PhpWord\Shared\String; +use PhpOffice\PhpWord\Element\Element; use PhpOffice\PhpWord\Element\Text; use PhpOffice\PhpWord\Element\TextRun; use PhpOffice\PhpWord\Element\Link; @@ -34,7 +35,7 @@ use PhpOffice\PhpWord\Element\CheckBox; * * @since 0.9.2 */ -abstract class Container +abstract class Container extends Element { /** * Container type section|header|footer|cell|textrun|footnote @@ -53,7 +54,7 @@ abstract class Container /** * Elements collection * - * @var int + * @var array */ protected $elements = array(); @@ -487,37 +488,10 @@ abstract class Container return $this->addFootnote($paragraphStyle); } - /** - * Set style value - * - * Used by Footnote - * - * @param mixed $styleObject Style object, could be Font, Paragraph, Cell, Image - * @param mixed $styleValue - * @param boolean $returnObject Always return object - * @todo Remove duplicate with ..\Element\Element - */ - protected function setStyle($styleObject, $styleValue = null, $returnObject = false) - { - if (!is_null($styleValue) && is_array($styleValue)) { - foreach ($styleValue as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $styleObject->setStyleValue($key, $value); - } - $style = $styleObject; - } else { - $style = $returnObject ? $styleObject : $styleValue; - } - - return $style; - } - /** * Check if a method is allowed for the current container * - * @param string $element + * @param string $method * @return boolean */ private function checkValidity($method) diff --git a/src/PhpWord/Container/Header.php b/src/PhpWord/Container/Header.php index 126f9198..ba31994d 100755 --- a/src/PhpWord/Container/Header.php +++ b/src/PhpWord/Container/Header.php @@ -36,7 +36,7 @@ class Header extends Container /** * Create new instance * - * @param int $sectionCount + * @param int $sectionId */ public function __construct($sectionId) { diff --git a/src/PhpWord/Container/Section.php b/src/PhpWord/Container/Section.php index b549280d..dfbc9a72 100644 --- a/src/PhpWord/Container/Section.php +++ b/src/PhpWord/Container/Section.php @@ -46,7 +46,7 @@ class Section extends Container * Create new instance * * @param int $sectionCount - * @param mixed $settings + * @param array $settings */ public function __construct($sectionCount, $settings = null) { @@ -132,7 +132,7 @@ class Section extends Container /** * Get Headers * - * @return array + * @return Header[] */ public function getHeaders() { diff --git a/src/PhpWord/DocumentProperties.php b/src/PhpWord/DocumentProperties.php index 14ed3150..b1f02275 100644 --- a/src/PhpWord/DocumentProperties.php +++ b/src/PhpWord/DocumentProperties.php @@ -27,101 +27,101 @@ class DocumentProperties * * @var string */ - private $_creator; + private $creator; /** * LastModifiedBy * * @var string */ - private $_lastModifiedBy; + private $lastModifiedBy; /** * Created * * @var int */ - private $_created; + private $created; /** * Modified * * @var int */ - private $_modified; + private $modified; /** * Title * * @var string */ - private $_title; + private $title; /** * Description * * @var string */ - private $_description; + private $description; /** * Subject * * @var string */ - private $_subject; + private $subject; /** * Keywords * * @var string */ - private $_keywords; + private $keywords; /** * Category * * @var string */ - private $_category; + private $category; /** * Company * * @var string */ - private $_company; + private $company; /** * Manager * * @var string */ - private $_manager; + private $manager; /** * Custom Properties * * @var array */ - private $_customProperties = array(); + private $customProperties = array(); /** * Create new DocumentProperties */ public function __construct() { - $this->_creator = ''; - $this->_lastModifiedBy = $this->_creator; - $this->_created = time(); - $this->_modified = time(); - $this->_title = ''; - $this->_subject = ''; - $this->_description = ''; - $this->_keywords = ''; - $this->_category = ''; - $this->_company = ''; - $this->_manager = ''; + $this->creator = ''; + $this->lastModifiedBy = $this->creator; + $this->created = time(); + $this->modified = time(); + $this->title = ''; + $this->subject = ''; + $this->description = ''; + $this->keywords = ''; + $this->category = ''; + $this->company = ''; + $this->manager = ''; } /** @@ -131,7 +131,7 @@ class DocumentProperties */ public function getCreator() { - return $this->_creator; + return $this->creator; } /** @@ -142,7 +142,7 @@ class DocumentProperties */ public function setCreator($pValue = '') { - $this->_creator = $pValue; + $this->creator = $pValue; return $this; } @@ -153,7 +153,7 @@ class DocumentProperties */ public function getLastModifiedBy() { - return $this->_lastModifiedBy; + return $this->lastModifiedBy; } /** @@ -164,7 +164,7 @@ class DocumentProperties */ public function setLastModifiedBy($pValue = '') { - $this->_lastModifiedBy = $pValue; + $this->lastModifiedBy = $pValue; return $this; } @@ -175,7 +175,7 @@ class DocumentProperties */ public function getCreated() { - return $this->_created; + return $this->created; } /** @@ -189,7 +189,7 @@ class DocumentProperties if (is_null($pValue)) { $pValue = time(); } - $this->_created = $pValue; + $this->created = $pValue; return $this; } @@ -200,7 +200,7 @@ class DocumentProperties */ public function getModified() { - return $this->_modified; + return $this->modified; } /** @@ -214,7 +214,7 @@ class DocumentProperties if (is_null($pValue)) { $pValue = time(); } - $this->_modified = $pValue; + $this->modified = $pValue; return $this; } @@ -225,7 +225,7 @@ class DocumentProperties */ public function getTitle() { - return $this->_title; + return $this->title; } /** @@ -236,7 +236,7 @@ class DocumentProperties */ public function setTitle($pValue = '') { - $this->_title = $pValue; + $this->title = $pValue; return $this; } @@ -247,7 +247,7 @@ class DocumentProperties */ public function getDescription() { - return $this->_description; + return $this->description; } /** @@ -258,7 +258,7 @@ class DocumentProperties */ public function setDescription($pValue = '') { - $this->_description = $pValue; + $this->description = $pValue; return $this; } @@ -269,7 +269,7 @@ class DocumentProperties */ public function getSubject() { - return $this->_subject; + return $this->subject; } /** @@ -280,7 +280,7 @@ class DocumentProperties */ public function setSubject($pValue = '') { - $this->_subject = $pValue; + $this->subject = $pValue; return $this; } @@ -291,7 +291,7 @@ class DocumentProperties */ public function getKeywords() { - return $this->_keywords; + return $this->keywords; } /** @@ -302,7 +302,7 @@ class DocumentProperties */ public function setKeywords($pValue = '') { - $this->_keywords = $pValue; + $this->keywords = $pValue; return $this; } @@ -313,7 +313,7 @@ class DocumentProperties */ public function getCategory() { - return $this->_category; + return $this->category; } /** @@ -324,7 +324,7 @@ class DocumentProperties */ public function setCategory($pValue = '') { - $this->_category = $pValue; + $this->category = $pValue; return $this; } @@ -335,7 +335,7 @@ class DocumentProperties */ public function getCompany() { - return $this->_company; + return $this->company; } /** @@ -346,7 +346,7 @@ class DocumentProperties */ public function setCompany($pValue = '') { - $this->_company = $pValue; + $this->company = $pValue; return $this; } @@ -357,7 +357,7 @@ class DocumentProperties */ public function getManager() { - return $this->_manager; + return $this->manager; } /** @@ -368,7 +368,7 @@ class DocumentProperties */ public function setManager($pValue = '') { - $this->_manager = $pValue; + $this->manager = $pValue; return $this; } @@ -379,7 +379,7 @@ class DocumentProperties */ public function getCustomProperties() { - return array_keys($this->_customProperties); + return array_keys($this->customProperties); } /** @@ -390,7 +390,7 @@ class DocumentProperties */ public function isCustomPropertySet($propertyName) { - return isset($this->_customProperties[$propertyName]); + return isset($this->customProperties[$propertyName]); } /** @@ -401,8 +401,8 @@ class DocumentProperties */ public function getCustomPropertyValue($propertyName) { - if (isset($this->_customProperties[$propertyName])) { - return $this->_customProperties[$propertyName]['value']; + if (isset($this->customProperties[$propertyName])) { + return $this->customProperties[$propertyName]['value']; } } @@ -415,8 +415,8 @@ class DocumentProperties */ public function getCustomPropertyType($propertyName) { - if (isset($this->_customProperties[$propertyName])) { - return $this->_customProperties[$propertyName]['type']; + if (isset($this->customProperties[$propertyName])) { + return $this->customProperties[$propertyName]['type']; } } @@ -457,7 +457,7 @@ class DocumentProperties } } - $this->_customProperties[$propertyName] = array( + $this->customProperties[$propertyName] = array( 'value' => $propertyValue, 'type' => $propertyType ); @@ -465,11 +465,11 @@ class DocumentProperties } /** - * Convert document propery based on type + * Convert document property based on type * - * @param mixed $propertyValue - * @param string $propertyType - * @return mixed + * @param string $propertyValue + * @param string $propertyType + * @return mixed */ public static function convertProperty($propertyValue, $propertyType) { @@ -525,8 +525,8 @@ class DocumentProperties /** * Convert document property type * - * @param string $propertyType - * @return mixed + * @param string $propertyType + * @return string */ public static function convertPropertyType($propertyType) { diff --git a/src/PhpWord/Element/Image.php b/src/PhpWord/Element/Image.php index 2631ec16..1824cec1 100755 --- a/src/PhpWord/Element/Image.php +++ b/src/PhpWord/Element/Image.php @@ -42,7 +42,7 @@ class Image extends Element /** * Is watermark * - * @var bool + * @var boolean */ private $isWatermark; @@ -77,7 +77,7 @@ class Image extends Element /** * Is memory image * - * @var string + * @var boolean */ private $isMemImage; @@ -86,7 +86,7 @@ class Image extends Element * * @param string $source * @param mixed $style - * @param bool $isWatermark + * @param boolean $isWatermark * @throws \PhpOffice\PhpWord\Exception\InvalidImageException * @throws \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException */ @@ -196,7 +196,7 @@ class Image extends Element /** * Get is watermark * - * @return int + * @return boolean */ public function getIsWatermark() { @@ -206,7 +206,7 @@ class Image extends Element /** * Set is watermark * - * @param bool $pValue + * @param boolean $pValue */ public function setIsWatermark($pValue) { diff --git a/src/PhpWord/Element/TextRun.php b/src/PhpWord/Element/TextRun.php index 50b302de..4a980424 100755 --- a/src/PhpWord/Element/TextRun.php +++ b/src/PhpWord/Element/TextRun.php @@ -28,7 +28,7 @@ class TextRun extends Container * Create new instance * * @param string|array|Paragraph $paragraphStyle - * @param string $docPartType section|header|footer + * @param string $docPart section|header|footer * @param int $docPartId */ public function __construct($paragraphStyle = null, $docPart = 'section', $docPartId = 1) diff --git a/src/PhpWord/Element/Title.php b/src/PhpWord/Element/Title.php index 2e7bf035..c8c29a35 100644 --- a/src/PhpWord/Element/Title.php +++ b/src/PhpWord/Element/Title.php @@ -55,7 +55,7 @@ class Title extends Element * * @param string $text * @param int $depth - * @param mixed $style + * @param string $style Name of the heading style, e.g. 'Heading1' */ public function __construct($text, $depth = 1, $style = null) { diff --git a/src/PhpWord/Media.php b/src/PhpWord/Media.php index ccfa1649..99fd5c21 100755 --- a/src/PhpWord/Media.php +++ b/src/PhpWord/Media.php @@ -61,7 +61,7 @@ class Media * @param string $src * @param string $type * @param Image $image - * @return mixed + * @return integer|array */ public static function addSectionMediaElement($src, $type, Image $image = null) { @@ -115,7 +115,7 @@ class Media * Add new Section Link Element * * @param string $linkSrc - * @return mixed + * @return integer */ public static function addSectionLinkElement($linkSrc) { @@ -340,6 +340,7 @@ class Media * Get media elements * * @param string $container + * @param string $mediaType * @return int */ public static function getMediaElements($container, $mediaType = null) diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index 89ee8d94..12143a3f 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -37,36 +37,36 @@ class PhpWord * * @var DocumentProperties */ - private $_documentProperties; + private $documentProperties; /** * Default font name * * @var string */ - private $_defaultFontName; + private $defaultFontName; /** * Default font size * @var int */ - private $_defaultFontSize; + private $defaultFontSize; /** * Collection of sections * * @var Section[] */ - private $_sections = array(); + private $sections = array(); /** * Create new */ public function __construct() { - $this->_documentProperties = new DocumentProperties(); - $this->_defaultFontName = self::DEFAULT_FONT_NAME; - $this->_defaultFontSize = self::DEFAULT_FONT_SIZE; + $this->documentProperties = new DocumentProperties(); + $this->defaultFontName = self::DEFAULT_FONT_NAME; + $this->defaultFontSize = self::DEFAULT_FONT_SIZE; } /** @@ -76,7 +76,7 @@ class PhpWord */ public function getDocumentProperties() { - return $this->_documentProperties; + return $this->documentProperties; } /** @@ -87,7 +87,7 @@ class PhpWord */ public function setDocumentProperties(DocumentProperties $documentProperties) { - $this->_documentProperties = $documentProperties; + $this->documentProperties = $documentProperties; return $this; } @@ -100,8 +100,8 @@ class PhpWord */ public function addSection($settings = null) { - $section = new Section(\count($this->_sections) + 1, $settings); - $this->_sections[] = $section; + $section = new Section(\count($this->sections) + 1, $settings); + $this->sections[] = $section; return $section; } @@ -113,7 +113,7 @@ class PhpWord */ public function getDefaultFontName() { - return $this->_defaultFontName; + return $this->defaultFontName; } /** @@ -123,17 +123,17 @@ class PhpWord */ public function setDefaultFontName($fontName) { - $this->_defaultFontName = $fontName; + $this->defaultFontName = $fontName; } /** * Get default font size * - * @return string + * @return integer */ public function getDefaultFontSize() { - return $this->_defaultFontSize; + return $this->defaultFontSize; } /** @@ -143,7 +143,7 @@ class PhpWord */ public function setDefaultFontSize($fontSize) { - $this->_defaultFontSize = $fontSize; + $this->defaultFontSize = $fontSize; } /** @@ -159,8 +159,8 @@ class PhpWord /** * Adds a paragraph style definition to styles.xml * - * @param $styleName string - * @param $styles array + * @param string $styleName + * @param array $styles */ public function addParagraphStyle($styleName, $styles) { @@ -170,7 +170,7 @@ class PhpWord /** * Adds a font style definition to styles.xml * - * @param $styleName string + * @param string $styleName * @param mixed $styleFont * @param mixed $styleParagraph */ @@ -221,7 +221,7 @@ class PhpWord */ public function getSections() { - return $this->_sections; + return $this->sections; } /** diff --git a/src/PhpWord/Reader/Word2007.php b/src/PhpWord/Reader/Word2007.php index be84d636..41bfd85f 100644 --- a/src/PhpWord/Reader/Word2007.php +++ b/src/PhpWord/Reader/Word2007.php @@ -64,7 +64,7 @@ class Word2007 extends Reader implements IReader * @param mixed $archive * @param string $fileName * @param bool $removeNamespace - * @return mixed + * @return string */ public function getFromZipArchive($archive, $fileName = '', $removeNamespace = false) { diff --git a/src/PhpWord/Shared/Font.php b/src/PhpWord/Shared/Font.php index 23f368f6..f5317abb 100644 --- a/src/PhpWord/Shared/Font.php +++ b/src/PhpWord/Shared/Font.php @@ -17,8 +17,8 @@ class Font /** * Calculate an (approximate) pixel size, based on a font points size * - * @param int $fontSizeInPoints Font size (in points) - * @return int Font size (in pixels) + * @param int $fontSizeInPoints Font size (in points) + * @return int Font size (in pixels) */ public static function fontSizeToPixels($fontSizeInPoints = 12) { @@ -40,7 +40,7 @@ class Font * Calculate an (approximate) pixel size, based on centimeter size * * @param int $sizeInCm Font size (in centimeters) - * @return int Size (in pixels) + * @return double Size (in pixels) */ public static function centimeterSizeToPixels($sizeInCm = 1) { @@ -51,7 +51,7 @@ class Font * Convert centimeter to twip * * @param int $sizeInCm - * @return int + * @return double */ public static function centimeterSizeToTwips($sizeInCm = 1) { @@ -62,7 +62,7 @@ class Font * Convert inch to twip * * @param int $sizeInInch - * @return int + * @return double */ public static function inchSizeToTwips($sizeInInch = 1) { @@ -73,7 +73,7 @@ class Font * Convert pixel to twip * * @param int $sizeInPixel - * @return int + * @return double */ public static function pixelSizeToTwips($sizeInPixel = 1) { @@ -83,8 +83,8 @@ class Font /** * Calculate twip based on point size, used mainly for paragraph spacing * - * @param int|float $sizeInPoint Size in point - * @return int|float Size (in twips) + * @param integer $sizeInPoint Size in point + * @return integer Size (in twips) */ public static function pointSizeToTwips($sizeInPoint = 1) { diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index cc1f530c..69520804 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -447,7 +447,7 @@ class Font /** * Get foreground/highlight color * - * @return bool + * @return string */ public function getFgColor() { @@ -543,7 +543,7 @@ class Font /** * Get Font Content Type * - * @return bool + * @return string */ public function getHint() { diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index dbb04610..da1c8c05 100755 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -188,7 +188,7 @@ class Paragraph /** * Get Space before Paragraph * - * @return string + * @return integer */ public function getSpaceBefore() { @@ -210,7 +210,7 @@ class Paragraph /** * Get Space after Paragraph * - * @return string + * @return integer */ public function getSpaceAfter() { diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index b6de4a81..bf744a3e 100755 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -211,7 +211,7 @@ class Table /** * Get background * - * @return \PhpOffice\PhpWord\Style\Table + * @return string */ public function getBgColor() { diff --git a/src/PhpWord/Writer/ODText/Manifest.php b/src/PhpWord/Writer/ODText/Manifest.php index d7ab6cdc..3712cf66 100755 --- a/src/PhpWord/Writer/ODText/Manifest.php +++ b/src/PhpWord/Writer/ODText/Manifest.php @@ -11,7 +11,6 @@ namespace PhpOffice\PhpWord\Writer\ODText; use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Shared\XMLWriter; /** * ODText manifest part writer diff --git a/src/PhpWord/Writer/ODText/Meta.php b/src/PhpWord/Writer/ODText/Meta.php index 51647173..d0d593e2 100644 --- a/src/PhpWord/Writer/ODText/Meta.php +++ b/src/PhpWord/Writer/ODText/Meta.php @@ -10,7 +10,6 @@ namespace PhpOffice\PhpWord\Writer\ODText; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Shared\XMLWriter; /** * ODText meta part writer diff --git a/src/PhpWord/Writer/ODText/Styles.php b/src/PhpWord/Writer/ODText/Styles.php index 4ae937e0..fa81ea9e 100644 --- a/src/PhpWord/Writer/ODText/Styles.php +++ b/src/PhpWord/Writer/ODText/Styles.php @@ -10,7 +10,6 @@ namespace PhpOffice\PhpWord\Writer\ODText; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index 0bf0056b..6220b1f6 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -1169,7 +1169,6 @@ class Base extends WriterPart * * @param XMLWriter $xmlWriter * @param Container $container - * @param Container $textBreak Add text break when no element found */ protected function writeContainerElements(XMLWriter $xmlWriter, Container $container) { @@ -1224,9 +1223,9 @@ class Base extends WriterPart * Write margin or border * * @param XMLWriter $xmlWriter - * @param boolean $isBorder * @param array $sizes * @param array $colors + * @param array $attributes */ protected function writeMarginBorder(XMLWriter $xmlWriter, $sizes, $colors = array(), $attributes = array()) { diff --git a/src/PhpWord/Writer/Word2007/DocProps.php b/src/PhpWord/Writer/Word2007/DocProps.php index 92c2fab8..46a74688 100644 --- a/src/PhpWord/Writer/Word2007/DocProps.php +++ b/src/PhpWord/Writer/Word2007/DocProps.php @@ -10,7 +10,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Shared\XMLWriter; /** * Word2007 contenttypes part writer diff --git a/src/PhpWord/Writer/Word2007/Document.php b/src/PhpWord/Writer/Word2007/Document.php index 60837794..3c0d6483 100644 --- a/src/PhpWord/Writer/Word2007/Document.php +++ b/src/PhpWord/Writer/Word2007/Document.php @@ -15,7 +15,6 @@ use PhpOffice\PhpWord\Container\Section; use PhpOffice\PhpWord\Element\PageBreak; use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Style\Font; -use PhpOffice\PhpWord\Style\Paragraph; /** * Word2007 document part writer diff --git a/src/PhpWord/Writer/Word2007/DocumentRels.php b/src/PhpWord/Writer/Word2007/DocumentRels.php index 2129ea38..f5ca69fc 100755 --- a/src/PhpWord/Writer/Word2007/DocumentRels.php +++ b/src/PhpWord/Writer/Word2007/DocumentRels.php @@ -9,9 +9,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007; -use PhpOffice\PhpWord\Exception\Exception; -use PhpOffice\PhpWord\Shared\XMLWriter; - /** * Word2007 document rels part writer */ diff --git a/src/PhpWord/Writer/Word2007/Footer.php b/src/PhpWord/Writer/Word2007/Footer.php index d451bccd..4b075b97 100644 --- a/src/PhpWord/Writer/Word2007/Footer.php +++ b/src/PhpWord/Writer/Word2007/Footer.php @@ -10,7 +10,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\Container\Footer as FooterElement; -use PhpOffice\PhpWord\Shared\XMLWriter; /** * Word2007 footer part writer diff --git a/src/PhpWord/Writer/Word2007/Footnotes.php b/src/PhpWord/Writer/Word2007/Footnotes.php index 45c60eeb..ac85929d 100644 --- a/src/PhpWord/Writer/Word2007/Footnotes.php +++ b/src/PhpWord/Writer/Word2007/Footnotes.php @@ -10,12 +10,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\Element\Footnote; -use PhpOffice\PhpWord\Element\Text; -use PhpOffice\PhpWord\Element\Link; -use PhpOffice\PhpWord\Element\Image; -use PhpOffice\PhpWord\Element\Object; -use PhpOffice\PhpWord\Element\TextBreak; -use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Shared\XMLWriter; /** diff --git a/src/PhpWord/Writer/Word2007/FootnotesRels.php b/src/PhpWord/Writer/Word2007/FootnotesRels.php index 012062e1..e2cfd99f 100644 --- a/src/PhpWord/Writer/Word2007/FootnotesRels.php +++ b/src/PhpWord/Writer/Word2007/FootnotesRels.php @@ -9,9 +9,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007; -use PhpOffice\PhpWord\Exception\Exception; -use PhpOffice\PhpWord\Shared\XMLWriter; - /** * Word2007 footnotes rel part writer */ diff --git a/src/PhpWord/Writer/Word2007/Header.php b/src/PhpWord/Writer/Word2007/Header.php index 6b58c99f..3f649261 100644 --- a/src/PhpWord/Writer/Word2007/Header.php +++ b/src/PhpWord/Writer/Word2007/Header.php @@ -10,7 +10,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\Container\Header as HeaderElement; -use PhpOffice\PhpWord\Shared\XMLWriter; /** * Word2007 header part writer diff --git a/src/PhpWord/Writer/Word2007/Rels.php b/src/PhpWord/Writer/Word2007/Rels.php index 5619d978..d5476cf8 100755 --- a/src/PhpWord/Writer/Word2007/Rels.php +++ b/src/PhpWord/Writer/Word2007/Rels.php @@ -9,9 +9,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007; -use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Shared\XMLWriter; /** * Word2007 rels part writer From 1751ab09eec1de00f20cc64bdf5222b01a0589f0 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 3 Apr 2014 11:34:06 +0700 Subject: [PATCH 14/27] Remove unused ZipStreamWrapper.php and cleanup some scripts --- CHANGELOG.md | 2 +- src/PhpWord/Container/Container.php | 24 ++- src/PhpWord/Container/Section.php | 2 + src/PhpWord/Element/Footnote.php | 2 + src/PhpWord/Element/Table.php | 2 +- src/PhpWord/Footnote.php | 2 + src/PhpWord/IOFactory.php | 8 +- src/PhpWord/Media.php | 1 + src/PhpWord/PhpWord.php | 5 +- src/PhpWord/Reader/Word2007.php | 2 +- src/PhpWord/Shared/ZipArchive.php | 55 +++--- src/PhpWord/Shared/ZipStreamWrapper.php | 181 ------------------ src/PhpWord/Writer/ODText.php | 2 +- src/PhpWord/Writer/ODText/Content.php | 66 ++----- src/PhpWord/Writer/RTF.php | 36 ++-- src/PhpWord/Writer/Word2007.php | 40 ++-- src/PhpWord/Writer/Word2007/Base.php | 3 +- src/PhpWord/Writer/Word2007/ContentTypes.php | 10 +- src/PhpWord/Writer/Word2007/DocProps.php | 2 +- src/PhpWord/Writer/Word2007/Document.php | 2 +- tests/PhpWord/Tests/Container/FooterTest.php | 7 +- tests/PhpWord/Tests/Container/HeaderTest.php | 7 +- tests/PhpWord/Tests/Container/SectionTest.php | 3 +- tests/PhpWord/Tests/Element/CellTest.php | 7 +- 24 files changed, 135 insertions(+), 336 deletions(-) delete mode 100644 src/PhpWord/Shared/ZipStreamWrapper.php diff --git a/CHANGELOG.md b/CHANGELOG.md index f1430f0a..26c9f071 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,7 +42,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - Writer: Refactor writer classes and make a new Writer abstract class - @ivanlanin GH-160 - Reader: Rename AbstractReader > Reader - @ivanlanin - General: Refactor folders: Element, Container, and Exception - @ivanlanin GH-187 -- General: Remove legacy HashTable and all related properties/methods - @ivanlanin GH-187 +- General: Remove legacy HashTable and ZipStreamWrapper and all related properties/methods - @ivanlanin GH-187 - Container: Create new Container abstract class - @ivanlanin GH-187 - Element: Create new Element abstract class - @ivanlanin GH-187 diff --git a/src/PhpWord/Container/Container.php b/src/PhpWord/Container/Container.php index 95fa186f..0209b4c5 100644 --- a/src/PhpWord/Container/Container.php +++ b/src/PhpWord/Container/Container.php @@ -312,7 +312,9 @@ abstract class Container extends Element $rID = Media::addMediaElement('footnote', 'image', $src, $image); break; } - $image->setRelationId($rID); + if (is_int($rID)) { + $image->setRelationId($rID); + } $this->elements[] = $image; return $image; } else { @@ -366,7 +368,9 @@ abstract class Container extends Element $objectId = $data[1]; $object->setRelationId($rID); $object->setObjectId($objectId); - $object->setImageRelationId($rIDimg); + if (is_int($rIDimg)) { + $object->setImageRelationId($rIDimg); + } $this->elements[] = $object; return $object; } else { @@ -460,6 +464,7 @@ abstract class Container extends Element * @param string $src * @param mixed $style * @deprecated 0.9.0 + * @codeCoverageIgnore */ public function addMemoryImage($src, $style = null) { @@ -471,6 +476,7 @@ abstract class Container extends Element * * @param mixed $paragraphStyle * @deprecated 0.9.2 + * @codeCoverageIgnore */ public function createTextRun($paragraphStyle = null) { @@ -482,6 +488,7 @@ abstract class Container extends Element * * @param mixed $paragraphStyle * @deprecated 0.9.2 + * @codeCoverageIgnore */ public function createFootnote($paragraphStyle = null) { @@ -496,11 +503,12 @@ abstract class Container extends Element */ private function checkValidity($method) { + // Empty array means the element can be accepted by all containers $validContainers = array( - 'text' => 'all', - 'link' => 'all', - 'textbreak' => 'all', - 'image' => 'all', + 'text' => array(), + 'link' => array(), + 'textbreak' => array(), + 'image' => array(), 'textrun' => array('section', 'header', 'footer', 'cell'), 'listitem' => array('section', 'header', 'footer', 'cell'), 'checkbox' => array('section', 'header', 'footer', 'cell'), @@ -511,6 +519,8 @@ abstract class Container extends Element 'relationid' => array('header', 'footer', 'footnote'), 'title' => array('section'), ); + // Special condition, e.g. preservetext can only exists in cell when + // the cell is located in header or footer $validContainerInContainers = array( 'preservetext' => array(array('cell'), array('header', 'footer')), 'object' => array(array('cell', 'textrun'), array('section')), @@ -519,7 +529,7 @@ abstract class Container extends Element // Check if a method is valid for current container if (array_key_exists($method, $validContainers)) { - if (is_array($validContainers[$method])) { + if (!empty($validContainers[$method])) { if (!in_array($this->container, $validContainers[$method])) { throw new \BadMethodCallException(); } diff --git a/src/PhpWord/Container/Section.php b/src/PhpWord/Container/Section.php index dfbc9a72..5e39ee06 100644 --- a/src/PhpWord/Container/Section.php +++ b/src/PhpWord/Container/Section.php @@ -169,6 +169,7 @@ class Section extends Container * Create header * * @deprecated 0.9.2 + * @codeCoverageIgnore */ public function createHeader() { @@ -179,6 +180,7 @@ class Section extends Container * Create footer * * @deprecated 0.9.2 + * @codeCoverageIgnore */ public function createFooter() { diff --git a/src/PhpWord/Element/Footnote.php b/src/PhpWord/Element/Footnote.php index 67cd5d9a..ff0c9960 100644 --- a/src/PhpWord/Element/Footnote.php +++ b/src/PhpWord/Element/Footnote.php @@ -50,6 +50,7 @@ class Footnote extends Container * * @return int * @deprecated 0.9.2 + * @codeCoverageIgnore */ public function getReferenceId() { @@ -61,6 +62,7 @@ class Footnote extends Container * * @param int $refId * @deprecated 0.9.2 + * @codeCoverageIgnore */ public function setReferenceId($refId) { diff --git a/src/PhpWord/Element/Table.php b/src/PhpWord/Element/Table.php index 72362c07..fb50caf2 100644 --- a/src/PhpWord/Element/Table.php +++ b/src/PhpWord/Element/Table.php @@ -41,7 +41,7 @@ class Table extends Element /** * Table holder count * - * @var array + * @var int */ private $docPartId; diff --git a/src/PhpWord/Footnote.php b/src/PhpWord/Footnote.php index 2ec3d0b3..f656dc3a 100644 --- a/src/PhpWord/Footnote.php +++ b/src/PhpWord/Footnote.php @@ -65,6 +65,7 @@ class Footnote * @param string $linkSrc * @return int Reference ID * @deprecated 0.9.2 + * @codeCoverageIgnore */ public static function addFootnoteLinkElement($linkSrc) { @@ -76,6 +77,7 @@ class Footnote * * @return array * @deprecated 0.9.2 + * @codeCoverageIgnore */ public static function getFootnoteLinkElements() { diff --git a/src/PhpWord/IOFactory.php b/src/PhpWord/IOFactory.php index f99f7a00..eeafb057 100644 --- a/src/PhpWord/IOFactory.php +++ b/src/PhpWord/IOFactory.php @@ -22,9 +22,9 @@ abstract class IOFactory * @param \PhpOffice\PhpWord\PhpWord $phpWord * @param string $name * @return \PhpOffice\PhpWord\Writer\IWriter - * @throws \PhpOffice\PhpWord\Exception\Exception + * @throws Exception */ - public static function createWriter(PhpWord $phpWord, $name) + public static function createWriter(PhpWord $phpWord, $name = 'Word2007') { if ($name !== 'IWriter' && $name !== 'ODText' && $name !== 'RTF' && $name !== 'Word2007') { throw new Exception("\"{$name}\" is not a valid writer."); @@ -39,9 +39,9 @@ abstract class IOFactory * * @param string $name * @return \PhpOffice\PhpWord\Reader\IReader - * @throws \PhpOffice\PhpWord\Exception\Exception + * @throws Exception */ - public static function createReader($name) + public static function createReader($name = 'Word2007') { if ($name !== 'IReader' && $name !== 'Word2007') { throw new Exception("\"{$name}\" is not a valid reader."); diff --git a/src/PhpWord/Media.php b/src/PhpWord/Media.php index 99fd5c21..a026304d 100755 --- a/src/PhpWord/Media.php +++ b/src/PhpWord/Media.php @@ -194,6 +194,7 @@ class Media /** * Get Header Media Elements * + * @param string $prefix header|footer * @return array */ public static function getHeaderMediaElements($prefix = 'header') diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index 12143a3f..420b9d7e 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -95,7 +95,7 @@ class PhpWord /** * Create new section * - * @param \PhpOffice\PhpWord\Container\Settings $settings + * @param array $settings * @return Section */ public function addSection($settings = null) @@ -243,9 +243,10 @@ class PhpWord /** * Create new section * - * @param \PhpOffice\PhpWord\Container\Settings $settings + * @param array $settings * @return Section * @deprecated 0.9.2 + * @codeCoverageIgnore */ public function createSection($settings = null) { diff --git a/src/PhpWord/Reader/Word2007.php b/src/PhpWord/Reader/Word2007.php index 41bfd85f..175d7fd3 100644 --- a/src/PhpWord/Reader/Word2007.php +++ b/src/PhpWord/Reader/Word2007.php @@ -234,7 +234,7 @@ class Word2007 extends Reader implements IReader $styleName = (string)$elm->name['val']; if ($hasParagraphStyle) { $pStyle = $this->loadParagraphStyle($elm); - if (!$hasFontStyle) { + if (is_array($pStyle) && !$hasFontStyle) { $word->addParagraphStyle($styleName, $pStyle); } } diff --git a/src/PhpWord/Shared/ZipArchive.php b/src/PhpWord/Shared/ZipArchive.php index f207385c..7ef0889e 100644 --- a/src/PhpWord/Shared/ZipArchive.php +++ b/src/PhpWord/Shared/ZipArchive.php @@ -36,14 +36,14 @@ class ZipArchive * * @var string */ - private $_tempDir; + private $tempDir; /** * Zip Archive Stream Handle * * @var string */ - private $_zip; + private $zip; /** * Open a new zip archive @@ -53,8 +53,8 @@ class ZipArchive */ public function open($fileName) { - $this->_tempDir = sys_get_temp_dir(); - $this->_zip = new \PclZip($fileName); + $this->tempDir = sys_get_temp_dir(); + $this->zip = new \PclZip($fileName); return true; } @@ -83,13 +83,13 @@ class ZipArchive // To Rename the file while adding it to the zip we // need to create a temp file with the correct name if ($filenameParts['basename'] != $localnameParts['basename']) { - $temppath = $this->_tempDir . '/' . $localnameParts['basename']; + $temppath = $this->tempDir . '/' . $localnameParts['basename']; copy($filename, $temppath); $filename = $temppath; $filenameParts = pathinfo($temppath); } - $res = $this->_zip->add( + $res = $this->zip->add( $filename, PCLZIP_OPT_REMOVE_PATH, $filenameParts['dirname'], @@ -98,8 +98,7 @@ class ZipArchive ); if ($res == 0) { - throw new Exception("Error zipping files : " . $this->_zip->errorInfo(true)); - return false; + throw new Exception("Error zipping files : " . $this->zip->errorInfo(true)); } return true; @@ -116,25 +115,24 @@ class ZipArchive $filenameParts = pathinfo($localname); // Write $contents to a temp file - $handle = fopen($this->_tempDir . '/' . $filenameParts["basename"], "wb"); + $handle = fopen($this->tempDir . '/' . $filenameParts["basename"], "wb"); fwrite($handle, $contents); fclose($handle); // Add temp file to zip - $res = $this->_zip->add( - $this->_tempDir . '/' . $filenameParts["basename"], + $res = $this->zip->add( + $this->tempDir . '/' . $filenameParts["basename"], PCLZIP_OPT_REMOVE_PATH, - $this->_tempDir, + $this->tempDir, PCLZIP_OPT_ADD_PATH, $filenameParts["dirname"] ); if ($res == 0) { - throw new Exception("Error zipping files : " . $this->_zip->errorInfo(true)); - return false; + throw new Exception("Error zipping files : " . $this->zip->errorInfo(true)); } // Remove temp file - unlink($this->_tempDir . '/' . $filenameParts["basename"]); + unlink($this->tempDir . '/' . $filenameParts["basename"]); return true; } @@ -147,18 +145,18 @@ class ZipArchive */ public function locateName($fileName) { - $list = $this->_zip->listContent(); + $list = $this->zip->listContent(); $listCount = count($list); - $list_index = -1; + $listIndex = -1; for ($i = 0; $i < $listCount; ++$i) { if (strtolower($list[$i]["filename"]) == strtolower($fileName) || strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) { - $list_index = $i; + $listIndex = $i; break; } } - return ($list_index > -1); + return ($listIndex > -1); } /** @@ -169,31 +167,30 @@ class ZipArchive */ public function getFromName($fileName) { - $list = $this->_zip->listContent(); + $list = $this->zip->listContent(); $listCount = count($list); - $list_index = -1; + $listIndex = -1; for ($i = 0; $i < $listCount; ++$i) { if (strtolower($list[$i]["filename"]) == strtolower($fileName) || strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) { - $list_index = $i; + $listIndex = $i; break; } } - $extracted = ""; - if ($list_index != -1) { - $extracted = $this->_zip->extractByIndex($list_index, PCLZIP_OPT_EXTRACT_AS_STRING); + if ($listIndex != -1) { + $extracted = $this->zip->extractByIndex($listIndex, PCLZIP_OPT_EXTRACT_AS_STRING); } else { - $filename = substr($fileName, 1); - $list_index = -1; + $fileName = substr($fileName, 1); + $listIndex = -1; for ($i = 0; $i < $listCount; ++$i) { if (strtolower($list[$i]["filename"]) == strtolower($fileName) || strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) { - $list_index = $i; + $listIndex = $i; break; } } - $extracted = $this->_zip->extractByIndex($list_index, PCLZIP_OPT_EXTRACT_AS_STRING); + $extracted = $this->zip->extractByIndex($listIndex, PCLZIP_OPT_EXTRACT_AS_STRING); } if ((is_array($extracted)) && ($extracted != 0)) { $contents = $extracted[0]["content"]; diff --git a/src/PhpWord/Shared/ZipStreamWrapper.php b/src/PhpWord/Shared/ZipStreamWrapper.php deleted file mode 100644 index 23b69d84..00000000 --- a/src/PhpWord/Shared/ZipStreamWrapper.php +++ /dev/null @@ -1,181 +0,0 @@ -_archive = new $zipClass(); - $this->_archive->open($url['host']); - - $this->_fileNameInArchive = $url['fragment']; - $this->_position = 0; - $this->_data = $this->_archive->getFromName($this->_fileNameInArchive); - - return true; - } - - /** - * Stat stream - */ - public function streamStat() - { - return $this->_archive->statName($this->_fileNameInArchive); - } - - /** - * Read stream - * - * @param int $count - */ - public function streamRead($count) - { - $ret = substr($this->_data, $this->_position, $count); - $this->_position += strlen($ret); - return $ret; - } - - /** - * Tell stream - */ - public function streamTell() - { - return $this->_position; - } - - /** - * EOF stream - */ - public function streamEof() - { - return $this->_position >= strlen($this->_data); - } - - /** - * Seek stream - * - * @param int $offset - * @param mixed $whence - */ - public function streamSeek($offset, $whence) - { - switch ($whence) { - case \SEEK_SET: - if ($offset < strlen($this->_data) && $offset >= 0) { - $this->_position = $offset; - return true; - } else { - return false; - } - break; - - case \SEEK_CUR: - if ($offset >= 0) { - $this->_position += $offset; - return true; - } else { - return false; - } - break; - - case \SEEK_END: - if (strlen($this->_data) + $offset >= 0) { - $this->_position = strlen($this->_data) + $offset; - return true; - } else { - return false; - } - break; - - default: - return false; - } - } -} diff --git a/src/PhpWord/Writer/ODText.php b/src/PhpWord/Writer/ODText.php index 04ec0bec..10e90dd3 100755 --- a/src/PhpWord/Writer/ODText.php +++ b/src/PhpWord/Writer/ODText.php @@ -23,9 +23,9 @@ use PhpOffice\PhpWord\Writer\ODText\Styles; */ class ODText extends Writer implements IWriter { - /** * Create new ODText writer + * * @param PhpWord $phpWord */ public function __construct(PhpWord $phpWord = null) diff --git a/src/PhpWord/Writer/ODText/Content.php b/src/PhpWord/Writer/ODText/Content.php index ccbdcfce..2a96e103 100644 --- a/src/PhpWord/Writer/ODText/Content.php +++ b/src/PhpWord/Writer/ODText/Content.php @@ -82,18 +82,18 @@ class Content extends WriterPart $xmlWriter->writeAttribute('office:version', '1.2'); // We firstly search all fonts used - $_sections = $phpWord->getSections(); - $countSections = count($_sections); + $sections = $phpWord->getSections(); + $countSections = count($sections); if ($countSections > 0) { $pSection = 0; $numPStyles = 0; $numFStyles = 0; - foreach ($_sections as $section) { + foreach ($sections as $section) { $pSection++; - $_elements = $section->getElements(); + $elements = $section->getElements(); - foreach ($_elements as $element) { + foreach ($elements as $element) { if ($element instanceof Text) { $fStyle = $element->getFontStyle(); $pStyle = $element->getParagraphStyle(); @@ -232,17 +232,13 @@ class Content extends WriterPart $xmlWriter->endElement(); $xmlWriter->endElement(); - $_sections = $phpWord->getSections(); - $countSections = count($_sections); - $pSection = 0; - + $sections = $phpWord->getSections(); + $countSections = count($sections); if ($countSections > 0) { - foreach ($_sections as $section) { - $pSection++; + foreach ($sections as $section) { + $elements = $section->getElements(); - $_elements = $section->getElements(); - - foreach ($_elements as $element) { + foreach ($elements as $element) { if ($element instanceof Text) { $this->writeText($xmlWriter, $element); } elseif ($element instanceof TextRun) { @@ -269,12 +265,6 @@ class Content extends WriterPart $this->writeUnsupportedElement($xmlWriter, 'Element'); } } - - if ($pSection == $countSections) { - $this->writeEndSection($xmlWriter, $section); - } else { - $this->writeSection($xmlWriter, $section); - } } } $xmlWriter->endElement(); @@ -311,19 +301,21 @@ class Content extends WriterPart if (empty($styleFont)) { if (empty($styleParagraph)) { $xmlWriter->writeAttribute('text:style-name', 'P1'); - } else { - $xmlWriter->writeAttribute('text:style-name', $text->getParagraphStyle()); + } elseif (is_string($styleParagraph)) { + $xmlWriter->writeAttribute('text:style-name', $styleParagraph); } $xmlWriter->writeRaw($text->getText()); } else { if (empty($styleParagraph)) { $xmlWriter->writeAttribute('text:style-name', 'Standard'); - } else { - $xmlWriter->writeAttribute('text:style-name', $text->getParagraphStyle()); + } elseif (is_string($styleParagraph)) { + $xmlWriter->writeAttribute('text:style-name', $styleParagraph); } // text:span $xmlWriter->startElement('text:span'); - $xmlWriter->writeAttribute('text:style-name', $styleFont); + if (is_string($styleFont)) { + $xmlWriter->writeAttribute('text:style-name', $styleFont); + } $xmlWriter->writeRaw($text->getText()); $xmlWriter->endElement(); } @@ -359,35 +351,13 @@ class Content extends WriterPart * * @param XMLWriter $xmlWriter */ - protected function writeTextBreak(XMLWriter $xmlWriter = null) + protected function writeTextBreak(XMLWriter $xmlWriter) { $xmlWriter->startElement('text:p'); $xmlWriter->writeAttribute('text:style-name', 'Standard'); $xmlWriter->endElement(); } - // @codeCoverageIgnoreStart - /** - * Write end section - * - * @param XMLWriter $xmlWriter - * @param Section $section - */ - private function writeEndSection(XMLWriter $xmlWriter = null, Section $section = null) - { - } - - /** - * Write section - * - * @param XMLWriter $xmlWriter - * @param Section $section - */ - private function writeSection(XMLWriter $xmlWriter = null, Section $section = null) - { - } - // @codeCoverageIgnoreEnd - /** * Write unsupported element * diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index 6d693f17..a990575c 100755 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -170,16 +170,16 @@ class RTF extends Writer implements IWriter } // Search all fonts used - $_sections = $phpWord->getSections(); - $countSections = count($_sections); + $sections = $phpWord->getSections(); + $countSections = count($sections); if ($countSections > 0) { $pSection = 0; - foreach ($_sections as $section) { + foreach ($sections as $section) { $pSection++; - $_elements = $section->getElements(); + $elements = $section->getElements(); - foreach ($_elements as $element) { + foreach ($elements as $element) { if ($element instanceof Text) { $fStyle = $element->getFontStyle(); @@ -227,16 +227,16 @@ class RTF extends Writer implements IWriter } // Search all fonts used - $_sections = $phpWord->getSections(); - $countSections = count($_sections); + $sections = $phpWord->getSections(); + $countSections = count($sections); if ($countSections > 0) { $pSection = 0; - foreach ($_sections as $section) { + foreach ($sections as $section) { $pSection++; - $_elements = $section->getElements(); + $elements = $section->getElements(); - foreach ($_elements as $element) { + foreach ($elements as $element) { if ($element instanceof Text) { $fStyle = $element->getFontStyle(); @@ -266,15 +266,15 @@ class RTF extends Writer implements IWriter $phpWord = $this->phpWord; $sRTFBody = ''; - $_sections = $phpWord->getSections(); - $countSections = count($_sections); + $sections = $phpWord->getSections(); + $countSections = count($sections); $pSection = 0; if ($countSections > 0) { - foreach ($_sections as $section) { + foreach ($sections as $section) { $pSection++; - $_elements = $section->getElements(); - foreach ($_elements as $element) { + $elements = $section->getElements(); + foreach ($elements as $element) { if ($element instanceof Text) { $sRTFBody .= $this->getDataContentText($element); } elseif ($element instanceof TextBreak) { @@ -317,14 +317,12 @@ class RTF extends Writer implements IWriter $sRTFText = ''; $styleFont = $text->getFontStyle(); - $SfIsObject = ($styleFont instanceof Font) ? true : false; - if (!$SfIsObject) { + if (is_string($styleFont)) { $styleFont = Style::getStyle($styleFont); } $styleParagraph = $text->getParagraphStyle(); - $SpIsObject = ($styleParagraph instanceof Paragraph) ? true : false; - if (!$SpIsObject) { + if (is_string($styleParagraph)) { $styleParagraph = Style::getStyle($styleParagraph); } diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index 298b93cc..f86f30ac 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -104,8 +104,8 @@ class Word2007 extends Writer implements IWriter // Add section elements $sectionElements = array(); - $_secElements = Media::getSectionMediaElements(); - foreach ($_secElements as $element) { // loop through section media elements + $secElements = Media::getSectionMediaElements(); + foreach ($secElements as $element) { // loop through section media elements if ($element['type'] != 'hyperlink') { $this->addFileToPackage($objZip, $element); } @@ -145,33 +145,33 @@ class Word2007 extends Writer implements IWriter } // Process header/footer xml files - $_cHdrs = 0; - $_cFtrs = 0; + $cHdrs = 0; + $cFtrs = 0; $rID = Media::countSectionMediaElements() + 6; - $_sections = $this->phpWord->getSections(); + $sections = $this->phpWord->getSections(); $footers = array(); - foreach ($_sections as $section) { - $_headers = $section->getHeaders(); - foreach ($_headers as $index => &$_header) { - $_cHdrs++; - $_header->setRelationId(++$rID); - $hdrFile = "header{$_cHdrs}.xml"; + foreach ($sections as $section) { + $headers = $section->getHeaders(); + foreach ($headers as $index => &$header) { + $cHdrs++; + $header->setRelationId(++$rID); + $hdrFile = "header{$cHdrs}.xml"; $sectionElements[] = array('target' => $hdrFile, 'type' => 'header', 'rID' => $rID); $objZip->addFromString( "word/{$hdrFile}", - $this->getWriterPart('header')->writeHeader($_header) + $this->getWriterPart('header')->writeHeader($header) ); } - $_footer = $section->getFooter(); - $footers[++$_cFtrs] = $_footer; - if (!is_null($_footer)) { - $_footer->setRelationId(++$rID); - $_footerCount = $_footer->getSectionId(); - $ftrFile = "footer{$_footerCount}.xml"; + $footer = $section->getFooter(); + $footers[++$cFtrs] = $footer; + if (!is_null($footer)) { + $footer->setRelationId(++$rID); + $footerCount = $footer->getSectionId(); + $ftrFile = "footer{$footerCount}.xml"; $sectionElements[] = array('target' => $ftrFile, 'type' => 'footer', 'rID' => $rID); $objZip->addFromString( "word/{$ftrFile}", - $this->getWriterPart('footer')->writeFooter($_footer) + $this->getWriterPart('footer')->writeFooter($footer) ); } } @@ -210,7 +210,7 @@ class Word2007 extends Writer implements IWriter $this->getWriterPart('contenttypes')->writeContentTypes( $this->imageTypes, $this->objectTypes, - $_cHdrs, + $cHdrs, $footers ) ); diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index 6220b1f6..50bd6425 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -238,6 +238,7 @@ class Base extends WriterPart * * @param XMLWriter $xmlWriter * @param TextBreak $element + * @param boolean $withoutP */ protected function writeTextBreak(XMLWriter $xmlWriter, TextBreak $element = null, $withoutP = false) { @@ -1073,7 +1074,7 @@ class Base extends WriterPart * Write media rels (image, embeddings, hyperlink) * * @param XMLWriter $xmlWriter - * @param array mediaRels + * @param array $mediaRels */ protected function writeMediaRels(XMLWriter $xmlWriter, $mediaRels) { diff --git a/src/PhpWord/Writer/Word2007/ContentTypes.php b/src/PhpWord/Writer/Word2007/ContentTypes.php index 86689823..98bf4e08 100755 --- a/src/PhpWord/Writer/Word2007/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/ContentTypes.php @@ -21,10 +21,10 @@ class ContentTypes extends WriterPart * Write [Content_Types].xml * @param array $imageTypes * @param array $objectTypes - * @param int $_cHdrs + * @param int $cHdrs * @param array $footers */ - public function writeContentTypes($imageTypes, $objectTypes, $_cHdrs, $footers) + public function writeContentTypes($imageTypes, $objectTypes, $cHdrs, $footers) { // Create XML writer $xmlWriter = $this->getXmlWriter(); @@ -133,7 +133,7 @@ class ContentTypes extends WriterPart 'application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml' ); - for ($i = 1; $i <= $_cHdrs; $i++) { + for ($i = 1; $i <= $cHdrs; $i++) { $this->writeOverrideContentType( $xmlWriter, '/word/header' . $i . '.xml', @@ -165,7 +165,7 @@ class ContentTypes extends WriterPart * @param string $pContentType Content type * @throws Exception */ - private function writeDefaultContentType(XMLWriter $xmlWriter = null, $pPartname = '', $pContentType = '') + private function writeDefaultContentType(XMLWriter $xmlWriter, $pPartname = '', $pContentType = '') { if ($pPartname != '' && $pContentType != '') { // Write content type @@ -186,7 +186,7 @@ class ContentTypes extends WriterPart * @param string $pContentType Content type * @throws Exception */ - private function writeOverrideContentType(XMLWriter $xmlWriter = null, $pPartname = '', $pContentType = '') + private function writeOverrideContentType(XMLWriter $xmlWriter, $pPartname = '', $pContentType = '') { if ($pPartname != '' && $pContentType != '') { // Write content type diff --git a/src/PhpWord/Writer/Word2007/DocProps.php b/src/PhpWord/Writer/Word2007/DocProps.php index 46a74688..ceb5db04 100644 --- a/src/PhpWord/Writer/Word2007/DocProps.php +++ b/src/PhpWord/Writer/Word2007/DocProps.php @@ -111,7 +111,7 @@ class DocProps extends WriterPart * * @param PhpWord $phpWord */ - public function writeDocPropsCore(PhpWord $phpWord = null) + public function writeDocPropsCore(PhpWord $phpWord) { // Create XML writer $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/Word2007/Document.php b/src/PhpWord/Writer/Word2007/Document.php index 3c0d6483..8bf46a0b 100644 --- a/src/PhpWord/Writer/Word2007/Document.php +++ b/src/PhpWord/Writer/Word2007/Document.php @@ -84,7 +84,7 @@ class Document extends Base { $xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:pPr'); - $this->writeEndSection($xmlWriter, $section, 3); + $this->writeEndSection($xmlWriter, $section); $xmlWriter->endElement(); $xmlWriter->endElement(); } diff --git a/tests/PhpWord/Tests/Container/FooterTest.php b/tests/PhpWord/Tests/Container/FooterTest.php index ba712903..bcbf45a9 100644 --- a/tests/PhpWord/Tests/Container/FooterTest.php +++ b/tests/PhpWord/Tests/Container/FooterTest.php @@ -98,11 +98,10 @@ class FooterTest extends \PHPUnit_Framework_TestCase { $src = __DIR__ . "/../_files/images/earth.jpg"; $oFooter = new Footer(1); - $element1 = $oFooter->addImage($src); - $element2 = $oFooter->addMemoryImage($src); // @deprecated + $element = $oFooter->addImage($src); - $this->assertCount(2, $oFooter->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element1); + $this->assertCount(1, $oFooter->getElements()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); } /** diff --git a/tests/PhpWord/Tests/Container/HeaderTest.php b/tests/PhpWord/Tests/Container/HeaderTest.php index dab67048..d7425bdc 100644 --- a/tests/PhpWord/Tests/Container/HeaderTest.php +++ b/tests/PhpWord/Tests/Container/HeaderTest.php @@ -107,11 +107,10 @@ class HeaderTest extends \PHPUnit_Framework_TestCase { $src = __DIR__ . "/../_files/images/earth.jpg"; $oHeader = new Header(1); - $element1 = $oHeader->addImage($src); - $element2 = $oHeader->addMemoryImage($src); // @deprecated + $element = $oHeader->addImage($src); - $this->assertCount(2, $oHeader->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element1); + $this->assertCount(1, $oHeader->getElements()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); } /** diff --git a/tests/PhpWord/Tests/Container/SectionTest.php b/tests/PhpWord/Tests/Container/SectionTest.php index a289df49..268f66e8 100644 --- a/tests/PhpWord/Tests/Container/SectionTest.php +++ b/tests/PhpWord/Tests/Container/SectionTest.php @@ -84,7 +84,6 @@ class SectionTest extends \PHPUnit_Framework_TestCase $section->addListItem(utf8_decode('ä')); $section->addObject($objectSource); $section->addImage($imageSource); - $section->addMemoryImage($imageUrl); $section->addTitle(utf8_decode('ä'), 1); $section->addTextRun(); $section->addFootnote(); @@ -93,7 +92,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase $elementCollection = $section->getElements(); $elementTypes = array('Text', 'Link', 'TextBreak', 'PageBreak', - 'Table', 'ListItem', 'Object', 'Image', 'Image', + 'Table', 'ListItem', 'Object', 'Image', 'Title', 'TextRun', 'Footnote', 'CheckBox'); $i = 0; foreach ($elementTypes as $elementType) { diff --git a/tests/PhpWord/Tests/Element/CellTest.php b/tests/PhpWord/Tests/Element/CellTest.php index a3507e3f..3a2c3342 100644 --- a/tests/PhpWord/Tests/Element/CellTest.php +++ b/tests/PhpWord/Tests/Element/CellTest.php @@ -123,11 +123,10 @@ class CellTest extends \PHPUnit_Framework_TestCase { $src = __DIR__ . "/../_files/images/earth.jpg"; $oCell = new Cell('section', 1); - $element1 = $oCell->addImage($src); - $element2 = $oCell->addMemoryImage($src); // @deprecated + $element = $oCell->addImage($src); - $this->assertCount(2, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element1); + $this->assertCount(1, $oCell->getElements()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); } /** From 0fc1a0626735558fd0234ae23bde0653b6a41079 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 3 Apr 2014 21:03:10 +0700 Subject: [PATCH 15/27] Merge all Word2007 relationships writer --- samples/Sample_07_TemplateCloneRow.php | 2 +- src/PhpWord/Writer/Word2007.php | 16 +- src/PhpWord/Writer/Word2007/Base.php | 48 ------ src/PhpWord/Writer/Word2007/DocumentRels.php | 74 --------- src/PhpWord/Writer/Word2007/FootnotesRels.php | 33 ---- src/PhpWord/Writer/Word2007/Rels.php | 156 +++++++++++++----- tests/PhpWord/Tests/Writer/Word2007Test.php | 2 - 7 files changed, 123 insertions(+), 208 deletions(-) delete mode 100755 src/PhpWord/Writer/Word2007/DocumentRels.php delete mode 100644 src/PhpWord/Writer/Word2007/FootnotesRels.php diff --git a/samples/Sample_07_TemplateCloneRow.php b/samples/Sample_07_TemplateCloneRow.php index 72464d84..789d22f0 100755 --- a/samples/Sample_07_TemplateCloneRow.php +++ b/samples/Sample_07_TemplateCloneRow.php @@ -10,7 +10,7 @@ $document = $phpWord->loadTemplate('resources/Sample_07_TemplateCloneRow.docx'); // Variables on different parts of document $document->setValue('weekday', date('l')); // On section/content $document->setValue('time', date('H:i')); // On footer -$document->setValue('serverName', $_SERVER['SERVER_NAME']); // On header +$document->setValue('serverName', realpath(__DIR__)); // On header // Simple table $document->cloneRow('rowValue', 10); diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index f86f30ac..7c87c2d4 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -15,14 +15,12 @@ use PhpOffice\PhpWord\Footnote; use PhpOffice\PhpWord\Media; use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Writer\Word2007\ContentTypes; +use PhpOffice\PhpWord\Writer\Word2007\Rels; use PhpOffice\PhpWord\Writer\Word2007\DocProps; use PhpOffice\PhpWord\Writer\Word2007\Document; -use PhpOffice\PhpWord\Writer\Word2007\DocumentRels; use PhpOffice\PhpWord\Writer\Word2007\Footer; use PhpOffice\PhpWord\Writer\Word2007\Footnotes; -use PhpOffice\PhpWord\Writer\Word2007\FootnotesRels; use PhpOffice\PhpWord\Writer\Word2007\Header; -use PhpOffice\PhpWord\Writer\Word2007\Rels; use PhpOffice\PhpWord\Writer\Word2007\Styles; /** @@ -58,13 +56,11 @@ class Word2007 extends Writer implements IWriter $this->writerParts['contenttypes'] = new ContentTypes(); $this->writerParts['rels'] = new Rels(); $this->writerParts['docprops'] = new DocProps(); - $this->writerParts['documentrels'] = new DocumentRels(); $this->writerParts['document'] = new Document(); $this->writerParts['styles'] = new Styles(); $this->writerParts['header'] = new Header(); $this->writerParts['footer'] = new Footer(); $this->writerParts['footnotes'] = new Footnotes(); - $this->writerParts['footnotesrels'] = new FootnotesRels(); foreach ($this->writerParts as $writer) { $writer->setParentWriter($this); } @@ -118,7 +114,7 @@ class Word2007 extends Writer implements IWriter if (count($hdrMedia) > 0) { $objZip->addFromString( 'word/_rels/' . $hdrFile . '.xml.rels', - $this->getWriterPart('documentrels')->writeHeaderFooterRels($hdrMedia) + $this->getWriterPart('rels')->writeMediaRels($hdrMedia) ); foreach ($hdrMedia as $element) { if ($element['type'] != 'hyperlink') { @@ -134,7 +130,7 @@ class Word2007 extends Writer implements IWriter if (count($ftrMedia) > 0) { $objZip->addFromString( 'word/_rels/' . $ftrFile . '.xml.rels', - $this->getWriterPart('documentrels')->writeHeaderFooterRels($ftrMedia) + $this->getWriterPart('rels')->writeMediaRels($ftrMedia) ); foreach ($ftrMedia as $element) { if ($element['type'] != 'hyperlink') { @@ -198,7 +194,7 @@ class Word2007 extends Writer implements IWriter if (!empty($footnoteMedia)) { $objZip->addFromString( 'word/_rels/footnotes.xml.rels', - $this->getWriterPart('footnotesrels')->writeFootnotesRels($footnoteMedia) + $this->getWriterPart('rels')->writeMediaRels($footnoteMedia) ); } } @@ -214,11 +210,11 @@ class Word2007 extends Writer implements IWriter $footers ) ); - $objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeRelationships($this->phpWord)); + $objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeMainRels()); $objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->phpWord)); $objZip->addFromString('docProps/core.xml', $this->getWriterPart('docprops')->writeDocPropsCore($this->phpWord)); + $objZip->addFromString('word/_rels/document.xml.rels', $this->getWriterPart('rels')->writeDocRels($sectionElements)); $objZip->addFromString('word/document.xml', $this->getWriterPart('document')->writeDocument($this->phpWord)); - $objZip->addFromString('word/_rels/document.xml.rels', $this->getWriterPart('documentrels')->writeDocumentRels($sectionElements)); $objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->phpWord)); // Write static files diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index 50bd6425..f8873152 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -1070,54 +1070,6 @@ class Base extends WriterPart } } - /** - * Write media rels (image, embeddings, hyperlink) - * - * @param XMLWriter $xmlWriter - * @param array $mediaRels - */ - protected function writeMediaRels(XMLWriter $xmlWriter, $mediaRels) - { - $rTypePrefix = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/'; - foreach ($mediaRels as $mediaRel) { - $rId = $mediaRel['rID']; - $rType = $mediaRel['type']; - $rName = $mediaRel['target']; // file name - $targetMode = ($rType == 'hyperlink') ? 'External' : ''; - $rType = $rTypePrefix . ($rType == 'embeddings' ? 'oleObject' : $rType); - $this->writeRel($xmlWriter, $rId, $rType, $rName, $targetMode); - } - - } - - /** - * Write individual rels entry - * - * @param XMLWriter $xmlWriter - * @param int $pId Relationship ID - * @param string $pType Relationship type - * @param string $pTarget Relationship target - * @param string $pTargetMode Relationship target mode - */ - protected function writeRel(XMLWriter $xmlWriter, $pId, $pType, $pTarget, $pTargetMode = '') - { - if ($pType != '' && $pTarget != '') { - if (strpos($pId, 'rId') === false) { - $pId = 'rId' . $pId; - } - $xmlWriter->startElement('Relationship'); - $xmlWriter->writeAttribute('Id', $pId); - $xmlWriter->writeAttribute('Type', $pType); - $xmlWriter->writeAttribute('Target', $pTarget); - if ($pTargetMode != '') { - $xmlWriter->writeAttribute('TargetMode', $pTargetMode); - } - $xmlWriter->endElement(); - } else { - throw new Exception("Invalid parameters passed."); - } - } - /** * Write inline paragraph style * diff --git a/src/PhpWord/Writer/Word2007/DocumentRels.php b/src/PhpWord/Writer/Word2007/DocumentRels.php deleted file mode 100755 index f5ca69fc..00000000 --- a/src/PhpWord/Writer/Word2007/DocumentRels.php +++ /dev/null @@ -1,74 +0,0 @@ -getXmlWriter(); - - // XML header - $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); - - // Relationships - $xmlWriter->startElement('Relationships'); - $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); - - // Write static files - $staticFiles = array( - 'styles' => 'styles.xml', - 'numbering' => 'numbering.xml', - 'settings' => 'settings.xml', - 'theme' => 'theme/theme1.xml', - 'webSettings' => 'webSettings.xml', - 'fontTable' => 'fontTable.xml', - ); - $i = 0; - foreach ($staticFiles as $type => $file) { - $i++; - $schema = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/' . $type; - $this->writeRel($xmlWriter, $i, $schema, $file); - } - - // Write media relationship (image, oleObject, hyperlink) - $this->writeMediaRels($xmlWriter, $relsCollection); - - $xmlWriter->endElement(); // Relationships - - return $xmlWriter->getData(); - } - - /** - * Write header footer rels word/_rels/*.xml.rels - * - * @param array $relsCollection - */ - public function writeHeaderFooterRels($relsCollection) - { - $xmlWriter = $this->getXmlWriter(); - $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); - $xmlWriter->startElement('Relationships'); - $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); - $this->writeMediaRels($xmlWriter, $relsCollection); - $xmlWriter->endElement(); - - return $xmlWriter->getData(); - } -} diff --git a/src/PhpWord/Writer/Word2007/FootnotesRels.php b/src/PhpWord/Writer/Word2007/FootnotesRels.php deleted file mode 100644 index e2cfd99f..00000000 --- a/src/PhpWord/Writer/Word2007/FootnotesRels.php +++ /dev/null @@ -1,33 +0,0 @@ -getXmlWriter(); - $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); - $xmlWriter->startElement('Relationships'); - $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); - $this->writeMediaRels($xmlWriter, $relsCollection); - $xmlWriter->endElement(); - - return $xmlWriter->getData(); - } -} diff --git a/src/PhpWord/Writer/Word2007/Rels.php b/src/PhpWord/Writer/Word2007/Rels.php index d5476cf8..8f684c47 100755 --- a/src/PhpWord/Writer/Word2007/Rels.php +++ b/src/PhpWord/Writer/Word2007/Rels.php @@ -9,58 +9,134 @@ namespace PhpOffice\PhpWord\Writer\Word2007; -use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\Exception\Exception; +use PhpOffice\PhpWord\Shared\XMLWriter; /** - * Word2007 rels part writer + * Word2007 relationship writer + * + * @since 0.9.2 */ -class Rels extends Base +class Rels extends WriterPart { + /** + * Base relationship URL + */ + const RELS_BASE = 'http://schemas.openxmlformats.org/'; + /** * Write _rels/.rels * * @param PhpWord $phpWord */ - public function writeRelationships(PhpWord $phpWord = null) + public function writeMainRels() { - // Create XML writer + $rels = array( + 'word/document.xml' => 'officeDocument/2006/relationships/officeDocument', + 'docProps/core.xml' => 'package/2006/relationships/metadata/core-properties', + 'docProps/app.xml' => 'officeDocument/2006/relationships/extended-properties', + ); $xmlWriter = $this->getXmlWriter(); - - // XML header - $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); - - // Relationships - $xmlWriter->startElement('Relationships'); - $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); - - $relationId = 1; - - // Relationship word/document.xml - $this->writeRel( - $xmlWriter, - $relationId, - 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument', - 'word/document.xml' - ); - - // Relationship docProps/core.xml - $this->writeRel( - $xmlWriter, - ++$relationId, - 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties', - 'docProps/core.xml' - ); - - // Relationship docProps/app.xml - $this->writeRel( - $xmlWriter, - ++$relationId, - 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties', - 'docProps/app.xml' - ); - - $xmlWriter->endElement(); + $this->writeRels($xmlWriter, $rels); return $xmlWriter->getData(); } + + /** + * Write word/_rels/document.xml.rels + * + * @param array $mediaRels + */ + public function writeDocRels($mediaRels) + { + $rels = array( + 'styles.xml' => 'officeDocument/2006/relationships/styles', + 'numbering.xml' => 'officeDocument/2006/relationships/numbering', + 'settings.xml' => 'officeDocument/2006/relationships/settings', + 'theme/theme1.xml' => 'officeDocument/2006/relationships/theme', + 'webSettings.xml' => 'officeDocument/2006/relationships/webSettings', + 'fontTable.xml' => 'officeDocument/2006/relationships/fontTable', + ); + $xmlWriter = $this->getXmlWriter(); + $this->writeRels($xmlWriter, $rels, $mediaRels); + + return $xmlWriter->getData(); + } + + /** + * Write word/_rels/(header|footer|footnotes)*.xml.rels + * + * @param array $mediaRels + */ + public function writeMediaRels($mediaRels) + { + $xmlWriter = $this->getXmlWriter(); + $this->writeRels($xmlWriter, null, $mediaRels); + + return $xmlWriter->getData(); + } + + + /** + * Write relationships + * + * @param XMLWriter $xmlWriter + * @param null|array $rels + * @param null|array $mediaRels + * @param integer $id + */ + private function writeRels(XMLWriter $xmlWriter, $rels = null, $mediaRels = null, $id = 1) + { + $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); + $xmlWriter->startElement('Relationships'); + $xmlWriter->writeAttribute('xmlns', self::RELS_BASE . 'package/2006/relationships'); + if (is_array($rels)) { + foreach ($rels as $target => $type) { + $this->writeRel($xmlWriter, $id++, $type, $target); + } + } + if (is_array($mediaRels)) { + $typePrefix = 'officeDocument/2006/relationships/'; + foreach ($mediaRels as $mediaRel) { + $id = $mediaRel['rID']; + $type = $mediaRel['type']; + $target = $mediaRel['target']; // file name + $targetMode = ($type == 'hyperlink') ? 'External' : ''; + $type = $typePrefix . ($type == 'embeddings' ? 'oleObject' : $type); + $this->writeRel($xmlWriter, $id, $type, $target, $targetMode); + } + } + $xmlWriter->endElement(); + } + + /** + * Write individual rels entry + * + * Format: + * + * + * @param XMLWriter $xmlWriter + * @param int $id Relationship ID + * @param string $type Relationship type + * @param string $target Relationship target + * @param string $targetMode Relationship target mode + */ + private function writeRel(XMLWriter $xmlWriter, $id, $type, $target, $targetMode = '') + { + if ($type != '' && $target != '') { + if (strpos($id, 'rId') === false) { + $id = 'rId' . $id; + } + $xmlWriter->startElement('Relationship'); + $xmlWriter->writeAttribute('Id', $id); + $xmlWriter->writeAttribute('Type', self::RELS_BASE . $type); + $xmlWriter->writeAttribute('Target', $target); + if ($targetMode != '') { + $xmlWriter->writeAttribute('TargetMode', $targetMode); + } + $xmlWriter->endElement(); + } else { + throw new Exception("Invalid parameters passed."); + } + } } diff --git a/tests/PhpWord/Tests/Writer/Word2007Test.php b/tests/PhpWord/Tests/Writer/Word2007Test.php index 7b722a9c..fe80adb0 100644 --- a/tests/PhpWord/Tests/Writer/Word2007Test.php +++ b/tests/PhpWord/Tests/Writer/Word2007Test.php @@ -38,13 +38,11 @@ class Word2007Test extends \PHPUnit_Framework_TestCase 'ContentTypes', 'Rels', 'DocProps', - 'DocumentRels', 'Document', 'Styles', 'Header', 'Footer', 'Footnotes', - 'FootnotesRels', ); foreach ($writerParts as $part) { $this->assertInstanceOf( From b75403f9a11049be92c03b37b7ecf18dd3a692ba Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Fri, 4 Apr 2014 00:29:57 +0700 Subject: [PATCH 16/27] Refactor Media, addObject to header and footer --- CHANGELOG.md | 6 +- docs/elements.rst | 2 +- samples/index.php | 2 +- src/PhpWord/Container/Container.php | 88 ++---- src/PhpWord/Element/Object.php | 1 - src/PhpWord/Media.php | 422 +++++++++++--------------- src/PhpWord/Shared/Drawing.php | 38 +-- src/PhpWord/Shared/String.php | 66 ++-- src/PhpWord/Shared/XMLWriter.php | 66 ++-- src/PhpWord/Writer/ODText/Content.php | 1 - src/PhpWord/Writer/RTF.php | 1 - src/PhpWord/Writer/Word2007.php | 144 ++++----- src/PhpWord/Writer/Word2007/Base.php | 10 +- src/PhpWord/Writer/Word2007/Rels.php | 13 +- tests/PhpWord/Tests/MediaTest.php | 66 ++-- 15 files changed, 381 insertions(+), 545 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26c9f071..80a6253a 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - ListItem: Ability to add list item in header/footer - @ivanlanin GH-187 - CheckBox: Ability to add checkbox in header/footer - @ivanlanin GH-187 - Link: Ability to add link in header/footer - @ivanlanin GH-187 -- Object: Ability to add object in textrun and footnote - @ivanlanin GH-187 +- Object: Ability to add object in header, footer, textrun, and footnote - @ivanlanin GH-187 ### Bugfixes @@ -35,6 +35,9 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - `createSection` replaced by `addSection` - `Element\Footnote::getReferenceId` replaced by `Container\Container::getRelationId` - `Element\Footnote::setReferenceId` replaced by `Container\Container::setRelationId` +- `Footnote::addFootnoteLinkElement` replaced by `Media::addMediaElement` +- `Footnote::getFootnoteLinkElements` replaced by `Media::getMediaElements` +- All current methods on `Media` ### Miscellaneous @@ -45,6 +48,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - General: Remove legacy HashTable and ZipStreamWrapper and all related properties/methods - @ivanlanin GH-187 - Container: Create new Container abstract class - @ivanlanin GH-187 - Element: Create new Element abstract class - @ivanlanin GH-187 +- Media: Refactor media class to use one method for all docPart (section, header, footer, footnote) - @ivanlanin GH-187 ## 0.9.1 - 27 Mar 2014 diff --git a/docs/elements.rst b/docs/elements.rst index da2038e5..abe3f7ba 100644 --- a/docs/elements.rst +++ b/docs/elements.rst @@ -31,7 +31,7 @@ the containers while the rows lists the elements. +-----+---------------+---------+--------+--------+------+----------+----------+ | 11 | Watermark | \- | v | \- | \- | \- | \- | +-----+---------------+---------+--------+--------+------+----------+----------+ -| 12 | Object | v | ? | ? | v | v | v | +| 12 | Object | v | v | v | v | v | v | +-----+---------------+---------+--------+--------+------+----------+----------+ | 13 | TOC | v | \- | \- | \- | \- | \- | +-----+---------------+---------+--------+--------+------+----------+----------+ diff --git a/samples/index.php b/samples/index.php index f4446ad4..451cf381 100644 --- a/samples/index.php +++ b/samples/index.php @@ -3,7 +3,7 @@ include_once 'Sample_Header.php'; if (!CLI) { ?>
-

Welcome to PHPWord, a library written in pure PHP that provides a set of classes to write to and read from different document file formats, i.e. Word (.docx), WordPad (.rtf), and Libre/OpenOffice Writer (.odt).

+

Welcome to PHPWord, a pure PHP library for reading and writing word processing documents, i.e. Word (.docx), WordPad (.rtf), and Libre/OpenOffice Writer (.odt).

Please use the menu above to browse PHPWord samples.

Fork us on Github! diff --git a/src/PhpWord/Container/Container.php b/src/PhpWord/Container/Container.php index 0209b4c5..fd08785e 100644 --- a/src/PhpWord/Container/Container.php +++ b/src/PhpWord/Container/Container.php @@ -143,30 +143,12 @@ abstract class Container extends Element public function addLink($linkSrc, $linkName = null, $fontStyle = null, $paragraphStyle = null) { $this->checkValidity('link'); - - $inSection = true; - if (!is_null($this->docPart)) { - $container = $this->docPart; - $containerId = $this->docPartId; - } else { - $container = $this->container; - $containerId = $this->containerId; - } - if ($container == 'header' || $container == 'footer') { - $container .= $containerId; - $inSection = false; - } elseif ($container == 'footnote') { - $inSection = false; - } + $elementDocPart = $this->checkElementDocPart(); $linkSrc = String::toUTF8($linkSrc); $linkName = String::toUTF8($linkName); $link = new Link($linkSrc, $linkName, $fontStyle, $paragraphStyle); - if ($inSection) { - $rID = Media::addSectionLinkElement($linkSrc); - } else { - $rID = Media::addMediaElement($container, 'hyperlink', $linkSrc); - } + $rID = Media::addMediaElement($elementDocPart, 'link', $linkSrc); $link->setRelationId($rID); $this->elements[] = $link; @@ -286,32 +268,11 @@ abstract class Container extends Element public function addImage($src, $style = null, $isWatermark = false) { $this->checkValidity('image'); - if ($this->container == 'cell' || $this->container == 'textrun') { - $container = $this->docPart; - $containerId = $this->docPartId; - } else { - $container = $this->container; - $containerId = $this->containerId; - } + $elementDocPart = $this->checkElementDocPart(); $image = new Image($src, $style, $isWatermark); if (!is_null($image->getSource())) { - $rID = null; - switch ($container) { - case 'textrun': - case 'section': - $rID = Media::addSectionMediaElement($src, 'image', $image); - break; - case 'header': - $rID = Media::addHeaderMediaElement($containerId, $src, $image); - break; - case 'footer': - $rID = Media::addFooterMediaElement($containerId, $src, $image); - break; - case 'footnote': - $rID = Media::addMediaElement('footnote', 'image', $src, $image); - break; - } + $rID = Media::addMediaElement($elementDocPart, 'image', $src, $image); if (is_int($rID)) { $image->setRelationId($rID); } @@ -334,20 +295,8 @@ abstract class Container extends Element */ public function addObject($src, $style = null) { - $inSection = true; - if (!is_null($this->docPart)) { - $container = $this->docPart; - $containerId = $this->docPartId; - } else { - $container = $this->container; - $containerId = $this->containerId; - } - if ($container == 'header' || $container == 'footer') { - $container .= $containerId; - $inSection = false; - } elseif ($container == 'footnote') { - $inSection = false; - } + $this->checkValidity('object'); + $elementDocPart = $this->checkElementDocPart(); $object = new Object($src, $style); if (!is_null($object->getSource())) { @@ -357,13 +306,8 @@ abstract class Container extends Element $ext = substr($ext, 0, -1); } $icon = realpath(__DIR__ . "/../_staticDocParts/_{$ext}.png"); - if ($inSection) { - $rIDimg = Media::addSectionMediaElement($icon, 'image', new Image($icon)); - $data = Media::addSectionMediaElement($src, 'oleObject'); - } else { - $rIDimg = Media::addMediaElement($container, 'image', $icon, new Image($icon)); - $data = Media::addMediaElement($container, 'embeddings', $src); - } + $rIDimg = Media::addMediaElement($elementDocPart, 'image', $icon, new Image($icon)); + $data = Media::addMediaElement($elementDocPart, 'object', $src); $rID = $data[0]; $objectId = $data[1]; $object->setRelationId($rID); @@ -509,11 +453,11 @@ abstract class Container extends Element 'link' => array(), 'textbreak' => array(), 'image' => array(), + 'object' => array(), 'textrun' => array('section', 'header', 'footer', 'cell'), 'listitem' => array('section', 'header', 'footer', 'cell'), 'checkbox' => array('section', 'header', 'footer', 'cell'), 'table' => array('section', 'header', 'footer'), - 'object' => array('section', 'textrun', 'cell', 'footnote'), 'footnote' => array('section', 'textrun', 'cell'), 'preservetext' => array('header', 'footer', 'cell'), 'relationid' => array('header', 'footer', 'footnote'), @@ -523,7 +467,6 @@ abstract class Container extends Element // the cell is located in header or footer $validContainerInContainers = array( 'preservetext' => array(array('cell'), array('header', 'footer')), - 'object' => array(array('cell', 'textrun'), array('section')), 'footnote' => array(array('cell', 'textrun'), array('section')), ); @@ -549,4 +492,17 @@ abstract class Container extends Element return true; } + + /** + * Return element location in document: section, headerx, or footerx + */ + private function checkElementDocPart() + { + $isCellTextrun = in_array($this->container, array('cell', 'textrun')); + $docPart = $isCellTextrun ? $this->docPart : $this->container; + $docPartId = $isCellTextrun ? $this->docPartId : $this->containerId; + $inHeaderFooter = ($docPart == 'header' || $docPart == 'footer'); + + return $inHeaderFooter ? $docPart . $docPartId : $docPart; + } } diff --git a/src/PhpWord/Element/Object.php b/src/PhpWord/Element/Object.php index 26207787..d885bfed 100644 --- a/src/PhpWord/Element/Object.php +++ b/src/PhpWord/Element/Object.php @@ -51,7 +51,6 @@ class Object extends Element */ private $objectId; - /** * Create a new Ole-Object Element * diff --git a/src/PhpWord/Media.php b/src/PhpWord/Media.php index a026304d..0a24d12a 100755 --- a/src/PhpWord/Media.php +++ b/src/PhpWord/Media.php @@ -16,31 +16,6 @@ use PhpOffice\PhpWord\Element\Image; */ class Media { - /** - * Section Media Elements - * - * @var array - */ - private static $sectionMedia = array( - 'images' => array(), - 'embeddings' => array(), - 'links' => array() - ); - - /** - * Header Media Elements - * - * @var array - */ - private static $headerMedia = array(); - - /** - * Footer Media Elements - * - * @var array - */ - private static $footerMedia = array(); - /** * Media elements * @@ -51,213 +26,23 @@ class Media /** * ObjectID Counter * - * @var int + * @var integer */ private static $objectId = 1325353440; - /** - * Add new Section Media Element - * - * @param string $src - * @param string $type - * @param Image $image - * @return integer|array - */ - public static function addSectionMediaElement($src, $type, Image $image = null) - { - $mediaId = md5($src); - $key = ($type === 'image') ? 'images' : 'embeddings'; - if (!array_key_exists($mediaId, self::$sectionMedia[$key])) { - $cImg = self::countSectionMediaElements('images'); - $cObj = self::countSectionMediaElements('embeddings'); - $rID = self::countSectionMediaElements() + 7; - $media = array(); - $folder = null; - $file = null; - if ($type === 'image') { - $cImg++; - $isMemImage = false; - if (!is_null($image)) { - $isMemImage = $image->getIsMemImage(); - $ext = $image->getImageExtension(); - } - if ($isMemImage) { - $media['isMemImage'] = true; - $media['createfunction'] = $image->getImageCreateFunction(); - $media['imagefunction'] = $image->getImageFunction(); - } - $folder = 'media'; - $file = $type . $cImg . '.' . strtolower($ext); - } elseif ($type === 'oleObject') { - $cObj++; - $folder = 'embeddings'; - $file = $type . $cObj . '.bin'; - } - $media['source'] = $src; - $media['target'] = "$folder/section_$file"; - $media['type'] = $type; - $media['rID'] = $rID; - self::$sectionMedia[$key][$mediaId] = $media; - if ($type === 'oleObject') { - return array($rID, ++self::$objectId); - } - return $rID; - } else { - if ($type === 'oleObject') { - $rID = self::$sectionMedia[$key][$mediaId]['rID']; - return array($rID, ++self::$objectId); - } - return self::$sectionMedia[$key][$mediaId]['rID']; - } - } - - /** - * Add new Section Link Element - * - * @param string $linkSrc - * @return integer - */ - public static function addSectionLinkElement($linkSrc) - { - $rID = self::countSectionMediaElements() + 7; - - $link = array(); - $link['target'] = $linkSrc; - $link['rID'] = $rID; - $link['type'] = 'hyperlink'; - - self::$sectionMedia['links'][] = $link; - - return $rID; - } - - /** - * Get Section Media Elements - * - * @param string $key - * @return array - */ - public static function getSectionMediaElements($key = null) - { - if (!is_null($key)) { - return self::$sectionMedia[$key]; - } - - $arrImages = self::$sectionMedia['images']; - $arrObjects = self::$sectionMedia['embeddings']; - $arrLinks = self::$sectionMedia['links']; - return array_merge($arrImages, $arrObjects, $arrLinks); - } - - /** - * Get Section Media Elements Count - * - * @param string $key - * @return int - */ - public static function countSectionMediaElements($key = null) - { - if (!is_null($key)) { - return count(self::$sectionMedia[$key]); - } - - $cImages = count(self::$sectionMedia['images']); - $cObjects = count(self::$sectionMedia['embeddings']); - $cLinks = count(self::$sectionMedia['links']); - return ($cImages + $cObjects + $cLinks); - } - - /** - * Add new Header Media Element - * - * @param int $headerCount - * @param string $src - * @param Image $image - * @return int - */ - public static function addHeaderMediaElement($headerCount, $src, Image $image = null) - { - return self::addMediaElement("header{$headerCount}", 'image', $src, $image); - } - - /** - * Get Header Media Elements Count - * - * @param string $key - * @return int - */ - public static function countHeaderMediaElements($key) - { - return self::countMediaElements($key); - } - - /** - * Get Header Media Elements - * - * @param string $prefix header|footer - * @return array - */ - public static function getHeaderMediaElements($prefix = 'header') - { - $mediaCollection = array(); - if (!empty(self::$media)) { - foreach (self::$media as $key => $val) { - if (substr($key, 0, 6) == $prefix) { - $mediaCollection[$key] = $val; - } - } - } - - return $mediaCollection; - } - - /** - * Add new Footer Media Element - * - * @param int $footerCount - * @param string $src - * @param Image $image - * @return int - */ - public static function addFooterMediaElement($footerCount, $src, Image $image = null) - { - return self::addMediaElement("footer{$footerCount}", 'image', $src, $image); - } - - /** - * Get Footer Media Elements Count - * - * @param string $key - * @return int - */ - public static function countFooterMediaElements($key) - { - return self::countMediaElements($key); - } - - /** - * Get Footer Media Elements - * - * @return array - */ - public static function getFooterMediaElements() - { - return self::getHeaderMediaElements('footer'); - } - /** * Add new media element * * @param string $container section|headerx|footerx|footnote - * @param string $mediaType image|embeddings|hyperlink + * @param string $mediaType image|object|link * @param string $source * @param Image $image - * @return int + * @return integer|array */ public static function addMediaElement($container, $mediaType, $source, Image $image = null) { - // Assign media Id and initiate media container if none exists - $mediaId = md5($source); + // Assign unique media Id and initiate media container if none exists + $mediaId = md5($container . $source); if (!array_key_exists($container, self::$media)) { self::$media[$container]= array(); } @@ -267,7 +52,7 @@ class Media $mediaCount = self::countMediaElements($container); $mediaTypeCount = self::countMediaElements($container, $mediaType); $mediaData = array(); - $relId = $mediaCount + 1; + $relId = ++$mediaCount; $target = null; $mediaTypeCount++; @@ -286,11 +71,11 @@ class Media } $target = "media/{$container}_image{$mediaTypeCount}.{$ext}"; // Objects - } elseif ($mediaType == 'embeddings') { + } elseif ($mediaType == 'object') { $file = "oleObject{$mediaTypeCount}.bin"; $target = "embeddings/{$container}_oleObject{$mediaTypeCount}.bin"; // Links - } elseif ($mediaType == 'hyperlink') { + } elseif ($mediaType == 'link') { $target = $source; } @@ -299,13 +84,13 @@ class Media $mediaData['type'] = $mediaType; $mediaData['rID'] = $relId; self::$media[$container][$mediaId] = $mediaData; - if ($mediaType === 'embeddings') { + if ($mediaType === 'object') { return array($relId, ++self::$objectId); } else { return $relId; } } else { - if ($mediaType === 'embeddings') { + if ($mediaType === 'object') { $relId = self::$media[$container][$mediaId]['rID']; return array($relId, ++self::$objectId); } else { @@ -317,20 +102,23 @@ class Media /** * Get media elements count * - * @param string $container - * @param string $mediaType - * @return int + * @param string $container section|headerx|footerx|footnote + * @param string $mediaType image|object|link + * @return integer */ public static function countMediaElements($container, $mediaType = null) { $mediaCount = 0; - foreach (self::$media[$container] as $mediaKey => $mediaData) { - if (!is_null($mediaType)) { - if ($mediaType == $mediaData['type']) { + + if (array_key_exists($container, self::$media)) { + foreach (self::$media[$container] as $mediaKey => $mediaData) { + if (!is_null($mediaType)) { + if ($mediaType == $mediaData['type']) { + $mediaCount++; + } + } else { $mediaCount++; } - } else { - $mediaCount++; } } @@ -340,27 +128,171 @@ class Media /** * Get media elements * - * @param string $container - * @param string $mediaType - * @return int + * @param string $container section|headerx|footerx|footnote + * @param string $mediaType image|object|link + * @return array */ public static function getMediaElements($container, $mediaType = null) { - if (!array_key_exists($container, self::$media)) { - return false; - } - $mediaElements = array(); - foreach (self::$media[$container] as $mediaKey => $mediaData) { - if (!is_null($mediaType)) { - if ($mediaType == $mediaData['type']) { + + // If header/footer, search for headerx and footerx where x is number + if ($container == 'header' || $container == 'footer') { + foreach (self::$media as $key => $val) { + if (substr($key, 0, 6) == $container) { + $mediaElements[$key] = $val; + } + } + } else { + if (!array_key_exists($container, self::$media)) { + return $mediaElements; + } + foreach (self::$media[$container] as $mediaKey => $mediaData) { + if (!is_null($mediaType)) { + if ($mediaType == $mediaData['type']) { + $mediaElements[$mediaKey] = $mediaData; + } + } else { $mediaElements[$mediaKey] = $mediaData; } - } else { - $mediaElements[$mediaKey] = $mediaData; } } return $mediaElements; } + + /** + * Add new Section Media Element + * + * @param string $src + * @param string $type + * @param Image $image + * @return integer|array + * @deprecated 0.9.2 + * @codeCoverageIgnore + */ + public static function addSectionMediaElement($src, $type, Image $image = null) + { + return self::addMediaElement("section", $type, $src, $image); + } + + /** + * Add new Section Link Element + * + * @param string $linkSrc + * @return integer + * @deprecated 0.9.2 + * @codeCoverageIgnore + */ + public static function addSectionLinkElement($linkSrc) + { + return self::addMediaElement('section', 'link', $linkSrc); + } + + /** + * Get Section Media Elements + * + * @param string $key + * @return array + * @deprecated 0.9.2 + * @codeCoverageIgnore + */ + public static function getSectionMediaElements($key = null) + { + return self::getMediaElements('section', $key); + } + + /** + * Get Section Media Elements Count + * + * @param string $key + * @return integer + * @deprecated 0.9.2 + * @codeCoverageIgnore + */ + public static function countSectionMediaElements($key = null) + { + return self::countMediaElements('section', $key); + } + + /** + * Add new Header Media Element + * + * @param integer $headerCount + * @param string $src + * @param Image $image + * @return integer + * @deprecated 0.9.2 + * @codeCoverageIgnore + */ + public static function addHeaderMediaElement($headerCount, $src, Image $image = null) + { + return self::addMediaElement("header{$headerCount}", 'image', $src, $image); + } + + /** + * Get Header Media Elements Count + * + * @param string $key + * @return integer + * @deprecated 0.9.2 + * @codeCoverageIgnore + */ + public static function countHeaderMediaElements($key) + { + return self::countMediaElements($key); + } + + /** + * Get Header Media Elements + * + * @param string $prefix header|footer + * @return array + * @deprecated 0.9.2 + * @codeCoverageIgnore + */ + public static function getHeaderMediaElements() + { + return self::getMediaElements('header'); + } + + /** + * Add new Footer Media Element + * + * @param integer $footerCount + * @param string $src + * @param Image $image + * @return integer + * @deprecated 0.9.2 + * @codeCoverageIgnore + */ + public static function addFooterMediaElement($footerCount, $src, Image $image = null) + { + return self::addMediaElement("footer{$footerCount}", 'image', $src, $image); + } + + /** + * Get Footer Media Elements Count + * + * @param string $key + * @return integer + * @deprecated 0.9.2 + * @codeCoverageIgnore + */ + public static function countFooterMediaElements($key) + { + return self::countMediaElements($key); + } + + /** + * Get Footer Media Elements + * + * @return array + * @deprecated 0.9.2 + * @codeCoverageIgnore + */ + public static function getFooterMediaElements() + { + return self::getMediaElements('footer'); + } } diff --git a/src/PhpWord/Shared/Drawing.php b/src/PhpWord/Shared/Drawing.php index 4c04a5fc..58a6ee1a 100644 --- a/src/PhpWord/Shared/Drawing.php +++ b/src/PhpWord/Shared/Drawing.php @@ -17,8 +17,8 @@ class Drawing /** * Convert pixels to EMU * - * @param int $pValue Value in pixels - * @return int Value in EMU + * @param integer $pValue Value in pixels + * @return double Value in EMU */ public static function pixelsToEMU($pValue = 0) { @@ -28,8 +28,8 @@ class Drawing /** * Convert EMU to pixels * - * @param int $pValue Value in EMU - * @return int Value in pixels + * @param integer $pValue Value in EMU + * @return integer Value in pixels */ public static function EMUToPixels($pValue = 0) { @@ -43,8 +43,8 @@ class Drawing /** * Convert pixels to points * - * @param int $pValue Value in pixels - * @return int Value in points + * @param integer $pValue Value in pixels + * @return double Value in points */ public static function pixelsToPoints($pValue = 0) { @@ -54,8 +54,8 @@ class Drawing /** * Convert points width to pixels * - * @param int $pValue Value in points - * @return int Value in pixels + * @param integer $pValue Value in points + * @return integer Value in pixels */ public static function pointsToPixels($pValue = 0) { @@ -69,19 +69,19 @@ class Drawing /** * Convert degrees to angle * - * @param int $pValue Degrees - * @return int Angle + * @param integer $pValue Degrees + * @return integer Angle */ public static function degreesToAngle($pValue = 0) { - return (int)round($pValue * 60000); + return (integer)round($pValue * 60000); } /** * Convert angle to degrees * - * @param int $pValue Angle - * @return int Degrees + * @param integer $pValue Angle + * @return integer Degrees */ public static function angleToDegrees($pValue = 0) { @@ -95,8 +95,8 @@ class Drawing /** * Convert pixels to centimeters * - * @param int $pValue Value in pixels - * @return int Value in centimeters + * @param integer $pValue Value in pixels + * @return double Value in centimeters */ public static function pixelsToCentimeters($pValue = 0) { @@ -106,8 +106,8 @@ class Drawing /** * Convert centimeters width to pixels * - * @param int $pValue Value in centimeters - * @return int Value in pixels + * @param integer $pValue Value in centimeters + * @return integer Value in pixels */ public static function centimetersToPixels($pValue = 0) { @@ -121,8 +121,8 @@ class Drawing /** * Convert HTML hexadecimal to RGB * - * @param str $pValue HTML Color in hexadecimal - * @return array Value in RGB + * @param string $pValue HTML Color in hexadecimal + * @return array Value in RGB */ public static function htmlToRGB($pValue) { diff --git a/src/PhpWord/Shared/String.php b/src/PhpWord/Shared/String.php index 9d298a66..e603f034 100644 --- a/src/PhpWord/Shared/String.php +++ b/src/PhpWord/Shared/String.php @@ -19,66 +19,36 @@ class String * * @var string[] */ - private static $_controlCharacters = array(); - - /** - * Build control characters array - */ - private static function _buildControlCharacters() - { - for ($i = 0; $i <= 19; ++$i) { - if ($i != 9 && $i != 10 && $i != 13) { - $find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_'; - $replace = chr($i); - self::$_controlCharacters[$find] = $replace; - } - } - } + private static $controlCharacters = array(); /** * Convert from OpenXML escaped control character to PHP control character * - * Excel 2007 team: - * ---------------- - * That's correct, control characters are stored directly in the shared-strings table. - * We do encode characters that cannot be represented in XML using the following escape sequence: - * _xHHHH_ where H represents a hexadecimal character in the character's value... - * So you could end up with something like _x0008_ in a string (either in a cell value () - * element or in the shared string element. - * - * @param string $value Value to unescape - * @return string + * @param string $value Value to unescape + * @return string */ public static function controlCharacterOOXML2PHP($value = '') { - if (empty(self::$_controlCharacters)) { - self::_buildControlCharacters(); + if (empty(self::$controlCharacters)) { + self::buildControlCharacters(); } - return str_replace(array_keys(self::$_controlCharacters), array_values(self::$_controlCharacters), $value); + return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $value); } /** * Convert from PHP control character to OpenXML escaped control character * - * Excel 2007 team: - * ---------------- - * That's correct, control characters are stored directly in the shared-strings table. - * We do encode characters that cannot be represented in XML using the following escape sequence: - * _xHHHH_ where H represents a hexadecimal character in the character's value... - * So you could end up with something like _x0008_ in a string (either in a cell value () - * element or in the shared string element. - * - * @param string $value Value to escape - * @return string + * @param string $value Value to escape + * @return string */ public static function controlCharacterPHP2OOXML($value = '') { - if (empty(self::$_controlCharacters)) { - self::_buildControlCharacters(); + if (empty(self::$controlCharacters)) { + self::buildControlCharacters(); } - return str_replace(array_values(self::$_controlCharacters), array_keys(self::$_controlCharacters), $value); + return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $value); } /** @@ -106,4 +76,18 @@ class String return $value; } + + /** + * Build control characters array + */ + private static function buildControlCharacters() + { + for ($i = 0; $i <= 19; ++$i) { + if ($i != 9 && $i != 10 && $i != 13) { + $find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_'; + $replace = chr($i); + self::$controlCharacters[$find] = $replace; + } + } + } } diff --git a/src/PhpWord/Shared/XMLWriter.php b/src/PhpWord/Shared/XMLWriter.php index 16bb8334..d4e959bc 100644 --- a/src/PhpWord/Shared/XMLWriter.php +++ b/src/PhpWord/Shared/XMLWriter.php @@ -38,14 +38,14 @@ class XMLWriter * * @var \XMLWriter */ - private $_xmlWriter; + private $xmlWriter; /** * Temporary filename * * @var string */ - private $_tempFileName = ''; + private $tempFile = ''; /** * Create new XMLWriter @@ -56,30 +56,30 @@ class XMLWriter public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageFolder = './') { // Create internal XMLWriter - $this->_xmlWriter = new \XMLWriter(); + $this->xmlWriter = new \XMLWriter(); // Open temporary storage if ($pTemporaryStorage == self::STORAGE_MEMORY) { - $this->_xmlWriter->openMemory(); + $this->xmlWriter->openMemory(); } else { // Create temporary filename - $this->_tempFileName = @tempnam($pTemporaryStorageFolder, 'xml'); + $this->tempFile = @tempnam($pTemporaryStorageFolder, 'xml'); // Open storage - if ($this->_xmlWriter->openUri($this->_tempFileName) === false) { + if ($this->xmlWriter->openUri($this->tempFile) === false) { // Fallback to memory... - $this->_xmlWriter->openMemory(); + $this->xmlWriter->openMemory(); } } // Set xml Compatibility $compatibility = Settings::getCompatibility(); if ($compatibility) { - $this->_xmlWriter->setIndent(false); - $this->_xmlWriter->setIndentString(''); + $this->xmlWriter->setIndent(false); + $this->xmlWriter->setIndentString(''); } else { - $this->_xmlWriter->setIndent(true); - $this->_xmlWriter->setIndentString(' '); + $this->xmlWriter->setIndent(true); + $this->xmlWriter->setIndentString(' '); } } @@ -89,26 +89,11 @@ class XMLWriter public function __destruct() { // Desctruct XMLWriter - unset($this->_xmlWriter); + unset($this->xmlWriter); // Unlink temporary files - if ($this->_tempFileName != '') { - @unlink($this->_tempFileName); - } - } - - /** - * Get written data - * - * @return string XML data - */ - public function getData() - { - if ($this->_tempFileName == '') { - return $this->_xmlWriter->outputMemory(true); - } else { - $this->_xmlWriter->flush(); - return file_get_contents($this->_tempFileName); + if ($this->tempFile != '') { + @unlink($this->tempFile); } } @@ -121,22 +106,37 @@ class XMLWriter public function __call($function, $args) { try { - @call_user_func_array(array($this->_xmlWriter, $function), $args); + @call_user_func_array(array($this->xmlWriter, $function), $args); } catch (\Exception $ex) { // Do nothing! } } + /** + * Get written data + * + * @return string XML data + */ + public function getData() + { + if ($this->tempFile == '') { + return $this->xmlWriter->outputMemory(true); + } else { + $this->xmlWriter->flush(); + return file_get_contents($this->tempFile); + } + } + /** * Fallback method for writeRaw, introduced in PHP 5.2 * * @param string $text - * @return string + * @return bool */ public function writeRaw($text) { - if (isset($this->_xmlWriter) && is_object($this->_xmlWriter) && (method_exists($this->_xmlWriter, 'writeRaw'))) { - return $this->_xmlWriter->writeRaw($text); + if (isset($this->xmlWriter) && is_object($this->xmlWriter) && (method_exists($this->xmlWriter, 'writeRaw'))) { + return $this->xmlWriter->writeRaw($text); } return $this->text($text); diff --git a/src/PhpWord/Writer/ODText/Content.php b/src/PhpWord/Writer/ODText/Content.php index 2a96e103..0669bc41 100644 --- a/src/PhpWord/Writer/ODText/Content.php +++ b/src/PhpWord/Writer/ODText/Content.php @@ -10,7 +10,6 @@ namespace PhpOffice\PhpWord\Writer\ODText; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Container\Section; use PhpOffice\PhpWord\Element\Image; use PhpOffice\PhpWord\Element\Link; use PhpOffice\PhpWord\Element\ListItem; diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index a990575c..8698365f 100755 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -24,7 +24,6 @@ use PhpOffice\PhpWord\Element\Title; use PhpOffice\PhpWord\Shared\Drawing; use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Font; -use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\TOC; /** diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index 7c87c2d4..352bb7ca 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -98,65 +98,40 @@ class Word2007 extends Writer implements IWriter } } - // Add section elements + // Add section media files $sectionElements = array(); - $secElements = Media::getSectionMediaElements(); - foreach ($secElements as $element) { // loop through section media elements - if ($element['type'] != 'hyperlink') { - $this->addFileToPackage($objZip, $element); - } - $sectionElements[] = $element; - } - // Add header relations & elements - $hdrElements = Media::getHeaderMediaElements(); - foreach ($hdrElements as $hdrFile => $hdrMedia) { - if (count($hdrMedia) > 0) { - $objZip->addFromString( - 'word/_rels/' . $hdrFile . '.xml.rels', - $this->getWriterPart('rels')->writeMediaRels($hdrMedia) - ); - foreach ($hdrMedia as $element) { - if ($element['type'] != 'hyperlink') { - $this->addFileToPackage($objZip, $element); - } - } + $secElements = Media::getMediaElements('section'); + if (!empty($secElements)) { + $this->addFilesToPackage($objZip, $secElements); + foreach ($secElements as $element) { + $sectionElements[] = $element; } } - // Add footer relations & elements - $ftrElements = Media::getFooterMediaElements(); - foreach ($ftrElements as $ftrFile => $ftrMedia) { - if (count($ftrMedia) > 0) { - $objZip->addFromString( - 'word/_rels/' . $ftrFile . '.xml.rels', - $this->getWriterPart('rels')->writeMediaRels($ftrMedia) - ); - foreach ($ftrMedia as $element) { - if ($element['type'] != 'hyperlink') { - $this->addFileToPackage($objZip, $element); - } - } - } - } + // Add header/footer media files & relations + $this->addHeaderFooterMedia($objZip, 'header'); + $this->addHeaderFooterMedia($objZip, 'footer'); - // Process header/footer xml files + // Add header/footer contents $cHdrs = 0; $cFtrs = 0; - $rID = Media::countSectionMediaElements() + 6; + $rID = Media::countMediaElements('section') + 6; // @see Rels::writeDocRels for 6 first elements $sections = $this->phpWord->getSections(); $footers = array(); foreach ($sections as $section) { $headers = $section->getHeaders(); - foreach ($headers as $index => &$header) { - $cHdrs++; - $header->setRelationId(++$rID); - $hdrFile = "header{$cHdrs}.xml"; - $sectionElements[] = array('target' => $hdrFile, 'type' => 'header', 'rID' => $rID); - $objZip->addFromString( - "word/{$hdrFile}", - $this->getWriterPart('header')->writeHeader($header) - ); + if (!empty($headers)) { + foreach ($headers as $index => &$header) { + $cHdrs++; + $header->setRelationId(++$rID); + $hdrFile = "header{$cHdrs}.xml"; + $sectionElements[] = array('target' => $hdrFile, 'type' => 'header', 'rID' => $rID); + $objZip->addFromString( + "word/{$hdrFile}", + $this->getWriterPart('header')->writeHeader($header) + ); + } } $footer = $section->getFooter(); $footers[++$cFtrs] = $footer; @@ -172,34 +147,23 @@ class Word2007 extends Writer implements IWriter } } - // Process footnotes + // Add footnotes media files, relations, and contents if (Footnote::countFootnoteElements() > 0) { - // Push to document.xml.rels $sectionElements[] = array('target' => 'footnotes.xml', 'type' => 'footnotes', 'rID' => ++$rID); - // Add footnote media to package $footnoteMedia = Media::getMediaElements('footnote'); - if (!empty($footnoteMedia)) { - foreach ($footnoteMedia as $media) { - if ($media['type'] != 'hyperlink') { - $this->addFileToPackage($objZip, $media); - } - } - } - // Write footnotes.xml - $objZip->addFromString( - 'word/footnotes.xml', - $this->getWriterPart('footnotes')->writeFootnotes(Footnote::getFootnoteElements()) - ); - // Write footnotes.xml.rels + $this->addFilesToPackage($objZip, $footnoteMedia); if (!empty($footnoteMedia)) { $objZip->addFromString( 'word/_rels/footnotes.xml.rels', $this->getWriterPart('rels')->writeMediaRels($footnoteMedia) ); } + $objZip->addFromString( + 'word/footnotes.xml', + $this->getWriterPart('footnotes')->writeFootnotes(Footnote::getFootnoteElements()) + ); } - // build docx file // Write dynamic files $objZip->addFromString( '[Content_Types].xml', @@ -288,21 +252,47 @@ class Word2007 extends Writer implements IWriter * @param mixed $objZip * @param mixed $element */ - private function addFileToPackage($objZip, $element) + private function addFilesToPackage($objZip, $elements) { - if (isset($element['isMemImage']) && $element['isMemImage']) { - $image = call_user_func($element['createfunction'], $element['source']); - ob_start(); - call_user_func($element['imagefunction'], $image); - $imageContents = ob_get_contents(); - ob_end_clean(); - $objZip->addFromString('word/' . $element['target'], $imageContents); - imagedestroy($image); + foreach ($elements as $element) { + if ($element['type'] == 'link') { + continue; + } + if (isset($element['isMemImage']) && $element['isMemImage']) { + $image = call_user_func($element['createfunction'], $element['source']); + ob_start(); + call_user_func($element['imagefunction'], $image); + $imageContents = ob_get_contents(); + ob_end_clean(); + $objZip->addFromString('word/' . $element['target'], $imageContents); + imagedestroy($image); - $this->checkContentTypes($element['source']); - } else { - $objZip->addFile($element['source'], 'word/' . $element['target']); - $this->checkContentTypes($element['source']); + $this->checkContentTypes($element['source']); + } else { + $objZip->addFile($element['source'], 'word/' . $element['target']); + $this->checkContentTypes($element['source']); + } + } + } + + /** + * Add header/footer media elements + */ + private function addHeaderFooterMedia($objZip, $docPart) + { + $elements = Media::getMediaElements($docPart); + if (!empty($elements)) { + foreach ($elements as $file => $media) { + if (count($media) > 0) { + $objZip->addFromString( + 'word/_rels/' . $file . '.xml.rels', + $this->getWriterPart('rels')->writeMediaRels($media) + ); + if (!empty($media)) { + $this->addFilesToPackage($objZip, $media); + } + } + } } } } diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index f8873152..10cabe49 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -1126,15 +1126,15 @@ class Base extends WriterPart protected function writeContainerElements(XMLWriter $xmlWriter, Container $container) { // Check allowed elements - $elmCommon = array('Text', 'Link', 'TextBreak', 'Image'); + $elmCommon = array('Text', 'Link', 'TextBreak', 'Image', 'Object'); $elmMainCell = array_merge($elmCommon, array('TextRun', 'ListItem', 'CheckBox')); $allowedElements = array( - 'Section' => array_merge($elmMainCell, array('Table', 'Footnote', 'Object', 'Title', 'PageBreak', 'TOC')), + 'Section' => array_merge($elmMainCell, array('Table', 'Footnote', 'Title', 'PageBreak', 'TOC')), 'Header' => array_merge($elmMainCell, array('Table', 'PreserveText')), 'Footer' => array_merge($elmMainCell, array('Table', 'PreserveText')), - 'Cell' => array_merge($elmMainCell, array('Object', 'PreserveText', 'Footnote')), - 'TextRun' => array_merge($elmCommon, array('Object', 'Footnote')), - 'Footnote' => array_merge($elmCommon, array('Object')), + 'Cell' => array_merge($elmMainCell, array('PreserveText', 'Footnote')), + 'TextRun' => array_merge($elmCommon, array('Footnote')), + 'Footnote' => $elmCommon, ); $containerName = get_class($container); $containerName = substr($containerName, strrpos($containerName, '\\') + 1); diff --git a/src/PhpWord/Writer/Word2007/Rels.php b/src/PhpWord/Writer/Word2007/Rels.php index 8f684c47..bbf0634c 100755 --- a/src/PhpWord/Writer/Word2007/Rels.php +++ b/src/PhpWord/Writer/Word2007/Rels.php @@ -26,8 +26,6 @@ class Rels extends WriterPart /** * Write _rels/.rels - * - * @param PhpWord $phpWord */ public function writeMainRels() { @@ -95,15 +93,14 @@ class Rels extends WriterPart $this->writeRel($xmlWriter, $id++, $type, $target); } } - if (is_array($mediaRels)) { - $typePrefix = 'officeDocument/2006/relationships/'; + if (!is_null($mediaRels) && is_array($mediaRels)) { + $mapping = array('image' => 'image', 'object' => 'oleObject', 'link' => 'hyperlink'); foreach ($mediaRels as $mediaRel) { - $id = $mediaRel['rID']; $type = $mediaRel['type']; - $target = $mediaRel['target']; // file name + $type = array_key_exists($type, $mapping) ? $mapping[$type] : $type; + $target = $mediaRel['target']; $targetMode = ($type == 'hyperlink') ? 'External' : ''; - $type = $typePrefix . ($type == 'embeddings' ? 'oleObject' : $type); - $this->writeRel($xmlWriter, $id, $type, $target, $targetMode); + $this->writeRel($xmlWriter, $id++, "officeDocument/2006/relationships/{$type}", $target, $targetMode); } } $xmlWriter->endElement(); diff --git a/tests/PhpWord/Tests/MediaTest.php b/tests/PhpWord/Tests/MediaTest.php index a3e6ead5..b1a49059 100644 --- a/tests/PhpWord/Tests/MediaTest.php +++ b/tests/PhpWord/Tests/MediaTest.php @@ -25,7 +25,7 @@ class MediaTest extends \PHPUnit_Framework_TestCase */ public function testGetSectionMediaElementsWithNull() { - $this->assertEquals(Media::getSectionMediaElements(), array()); + $this->assertEquals(Media::getMediaElements('section'), array()); } /** @@ -33,31 +33,7 @@ class MediaTest extends \PHPUnit_Framework_TestCase */ public function testCountSectionMediaElementsWithNull() { - $this->assertEquals(Media::countSectionMediaElements(), 0); - } - - /** - * Get header media elements - */ - public function testGetHeaderMediaElements() - { - $this->assertAttributeEquals( - Media::getHeaderMediaElements(), - 'headerMedia', - 'PhpOffice\\PhpWord\\Media' - ); - } - - /** - * Get footer media elements - */ - public function testGetFooterMediaElements() - { - $this->assertAttributeEquals( - Media::getFooterMediaElements(), - 'footerMedia', - 'PhpOffice\\PhpWord\\Media' - ); + $this->assertEquals(Media::countMediaElements('section'), 0); } /** @@ -68,13 +44,13 @@ class MediaTest extends \PHPUnit_Framework_TestCase $local = __DIR__ . "/_files/images/mars.jpg"; $object = __DIR__ . "/_files/documents/sheet.xls"; $remote = 'http://php.net/images/logos/php-med-trans-light.gif'; - Media::addSectionMediaElement($local, 'image', new Image($local)); - Media::addSectionMediaElement($local, 'image', new Image($local)); - Media::addSectionMediaElement($remote, 'image', new Image($remote)); - Media::addSectionMediaElement($object, 'oleObject'); - Media::addSectionMediaElement($object, 'oleObject'); + Media::addMediaElement('section', 'image', $local, new Image($local)); + Media::addMediaElement('section', 'image', $local, new Image($local)); + Media::addMediaElement('section', 'image', $remote, new Image($local)); + Media::addMediaElement('section', 'object', $object); + Media::addMediaElement('section', 'object', $object); - $this->assertEquals(3, Media::countSectionMediaElements()); + $this->assertEquals(3, Media::countMediaElements('section')); } /** @@ -82,12 +58,12 @@ class MediaTest extends \PHPUnit_Framework_TestCase */ public function testAddSectionLinkElement() { - $expected = Media::countSectionMediaElements() + 7; - $actual = Media::addSectionLinkElement('http://test.com'); + $expected = Media::countMediaElements('section') + 1; + $actual = Media::addMediaElement('section', 'link', 'http://test.com'); $this->assertEquals($expected, $actual); - $this->assertEquals(1, Media::countSectionMediaElements('links')); - $this->assertEquals(1, count(Media::getSectionMediaElements('links'))); + $this->assertEquals(1, Media::countMediaElements('section', 'link')); + $this->assertEquals(1, count(Media::getMediaElements('section', 'link'))); } /** @@ -97,13 +73,13 @@ class MediaTest extends \PHPUnit_Framework_TestCase { $local = __DIR__ . "/_files/images/mars.jpg"; $remote = 'http://php.net/images/logos/php-med-trans-light.gif'; - Media::addHeaderMediaElement(1, $local, new Image($local)); - Media::addHeaderMediaElement(1, $local, new Image($local)); - Media::addHeaderMediaElement(1, $remote, new Image($remote)); + Media::addMediaElement('header1', 'image', $local, new Image($local)); + Media::addMediaElement('header1', 'image', $local, new Image($local)); + Media::addMediaElement('header1', 'image', $remote, new Image($remote)); - $this->assertEquals(2, Media::countHeaderMediaElements('header1')); + $this->assertEquals(2, Media::countMediaElements('header1')); $this->assertEquals(2, count(Media::getMediaElements('header1'))); - $this->assertFalse(Media::getMediaElements('header2')); + $this->assertEmpty(Media::getMediaElements('header2')); } /** @@ -113,10 +89,10 @@ class MediaTest extends \PHPUnit_Framework_TestCase { $local = __DIR__ . "/_files/images/mars.jpg"; $remote = 'http://php.net/images/logos/php-med-trans-light.gif'; - Media::addFooterMediaElement(1, $local, new Image($local)); - Media::addFooterMediaElement(1, $local, new Image($local)); - Media::addFooterMediaElement(1, $remote, new Image($remote)); + Media::addMediaElement('footer1', 'image', $local, new Image($local)); + Media::addMediaElement('footer1', 'image', $local, new Image($local)); + Media::addMediaElement('footer1', 'image', $remote, new Image($remote)); - $this->assertEquals(2, Media::countFooterMediaElements('footer1')); + $this->assertEquals(2, Media::countMediaElements('footer1')); } } From d7c18fe4b8457c47bbb5c97ed2abf20b71977205 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 5 Apr 2014 00:08:00 +0700 Subject: [PATCH 17/27] Move OOXML specific feature from Media to Word2007\Base --- src/PhpWord/Container/Container.php | 98 ++++----- src/PhpWord/Container/Footer.php | 1 + src/PhpWord/Container/Header.php | 1 + src/PhpWord/Container/Section.php | 3 + src/PhpWord/Element/Cell.php | 3 +- src/PhpWord/Element/Element.php | 60 ++++++ src/PhpWord/Element/Object.php | 15 +- src/PhpWord/Element/Row.php | 20 +- src/PhpWord/Element/Table.php | 19 +- src/PhpWord/Element/TextRun.php | 4 +- src/PhpWord/Media.php | 23 +- src/PhpWord/Reader/Word2007.php | 4 +- src/PhpWord/Writer/Word2007.php | 5 +- src/PhpWord/Writer/Word2007/Base.php | 10 +- src/PhpWord/Writer/Word2007/ContentTypes.php | 215 +++++++------------ tests/PhpWord/Tests/Element/ObjectTest.php | 13 -- 16 files changed, 200 insertions(+), 294 deletions(-) diff --git a/src/PhpWord/Container/Container.php b/src/PhpWord/Container/Container.php index fd08785e..ec66a5a0 100644 --- a/src/PhpWord/Container/Container.php +++ b/src/PhpWord/Container/Container.php @@ -58,24 +58,6 @@ abstract class Container extends Element */ protected $elements = array(); - /** - * Document part type: section|header|footer - * - * Used by textrun and cell to determine where the element is located - * because it will affect the availability of other element, e.g. footnote - * will not be available when $docPart is header or footer. - * - * @var string - */ - protected $docPart = null; - - /** - * Document part Id - * - * @var int - */ - protected $docPartId; - /** * Relation Id * @@ -101,10 +83,11 @@ abstract class Container extends Element } $text = String::toUTF8($text); - $element = new Text($text, $fontStyle, $paragraphStyle); - $this->elements[] = $element; + $textObject = new Text($text, $fontStyle, $paragraphStyle); + $textObject->setDocPart($this->getDocPart(), $this->getDocPartId()); + $this->elements[] = $textObject; - return $element; + return $textObject; } /** @@ -117,15 +100,8 @@ abstract class Container extends Element { $this->checkValidity('textrun'); - if ($this->container == 'cell') { - $docPart = $this->docPart; - $docPartId = $this->docPartId; - } else { - $docPart = $this->container; - $docPartId = $this->containerId; - } - - $textRun = new TextRun($paragraphStyle, $docPart, $docPartId); + $textRun = new TextRun($paragraphStyle); + $textRun->setDocPart($this->getDocPart(), $this->getDocPartId()); $this->elements[] = $textRun; return $textRun; @@ -145,9 +121,8 @@ abstract class Container extends Element $this->checkValidity('link'); $elementDocPart = $this->checkElementDocPart(); - $linkSrc = String::toUTF8($linkSrc); - $linkName = String::toUTF8($linkName); - $link = new Link($linkSrc, $linkName, $fontStyle, $paragraphStyle); + $link = new Link(String::toUTF8($linkSrc), String::toUTF8($linkName), $fontStyle, $paragraphStyle); + $link->setDocPart($this->getDocPart(), $this->getDocPartId()); $rID = Media::addMediaElement($elementDocPart, 'link', $linkSrc); $link->setRelationId($rID); $this->elements[] = $link; @@ -167,14 +142,15 @@ abstract class Container extends Element { $this->checkValidity('title'); - $text = String::toUTF8($text); $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 = TOC::addTitle($text, $depth); $anchor = $data[0]; $bookmarkId = $data[1]; @@ -197,11 +173,11 @@ abstract class Container extends Element { $this->checkValidity('preservetext'); - $text = String::toUTF8($text); - $ptext = new PreserveText($text, $fontStyle, $paragraphStyle); - $this->elements[] = $ptext; + $preserveText = new PreserveText(String::toUTF8($text), $fontStyle, $paragraphStyle); + $preserveText->setDocPart($this->getDocPart(), $this->getDocPartId()); + $this->elements[] = $preserveText; - return $ptext; + return $preserveText; } /** @@ -216,7 +192,9 @@ abstract class Container extends Element $this->checkValidity('textbreak'); for ($i = 1; $i <= $count; $i++) { - $this->elements[] = new TextBreak($fontStyle, $paragraphStyle); + $textBreak = new TextBreak($fontStyle, $paragraphStyle); + $textBreak->setDocPart($this->getDocPart(), $this->getDocPartId()); + $this->elements[] = $textBreak; } } @@ -234,8 +212,8 @@ abstract class Container extends Element { $this->checkValidity('listitem'); - $text = String::toUTF8($text); - $listItem = new ListItem($text, $depth, $fontStyle, $styleList, $paragraphStyle); + $listItem = new ListItem(String::toUTF8($text), $depth, $fontStyle, $styleList, $paragraphStyle); + $listItem->setDocPart($this->getDocPart(), $this->getDocPartId()); $this->elements[] = $listItem; return $listItem; @@ -251,7 +229,7 @@ abstract class Container extends Element { $this->checkValidity('table'); - $table = new Table($this->container, $this->containerId, $style); + $table = new Table($this->getDocPart(), $this->getDocPartId(), $style); $this->elements[] = $table; return $table; @@ -271,11 +249,10 @@ abstract class Container extends Element $elementDocPart = $this->checkElementDocPart(); $image = new Image($src, $style, $isWatermark); + $image->setDocPart($this->getDocPart(), $this->getDocPartId()); if (!is_null($image->getSource())) { $rID = Media::addMediaElement($elementDocPart, 'image', $src, $image); - if (is_int($rID)) { - $image->setRelationId($rID); - } + $image->setRelationId($rID); $this->elements[] = $image; return $image; } else { @@ -299,6 +276,7 @@ abstract class Container extends Element $elementDocPart = $this->checkElementDocPart(); $object = new Object($src, $style); + $object->setDocPart($this->getDocPart(), $this->getDocPartId()); if (!is_null($object->getSource())) { $inf = pathinfo($src); $ext = $inf['extension']; @@ -306,15 +284,10 @@ abstract class Container extends Element $ext = substr($ext, 0, -1); } $icon = realpath(__DIR__ . "/../_staticDocParts/_{$ext}.png"); - $rIDimg = Media::addMediaElement($elementDocPart, 'image', $icon, new Image($icon)); - $data = Media::addMediaElement($elementDocPart, 'object', $src); - $rID = $data[0]; - $objectId = $data[1]; + $rID = Media::addMediaElement($elementDocPart, 'object', $src); $object->setRelationId($rID); - $object->setObjectId($objectId); - if (is_int($rIDimg)) { - $object->setImageRelationId($rIDimg); - } + $rIDimg = Media::addMediaElement($elementDocPart, 'image', $icon, new Image($icon)); + $object->setImageRelationId($rIDimg); $this->elements[] = $object; return $object; } else { @@ -334,6 +307,7 @@ abstract class Container extends Element $footnote = new FootnoteElement($paragraphStyle); $refID = FootnoteCollection::addFootnoteElement($footnote); + $footnote->setDocPart($this->getDocPart(), $this->getDocPartId()); $footnote->setRelationId($refID); $this->elements[] = $footnote; @@ -353,17 +327,17 @@ abstract class Container extends Element { $this->checkValidity('checkbox'); - $name = String::toUTF8($name); - $text = String::toUTF8($text); - $element = new CheckBox($name, $text, $fontStyle, $paragraphStyle); - $this->elements[] = $element; + $checkBox = new CheckBox(String::toUTF8($name), String::toUTF8($text), $fontStyle, $paragraphStyle); + $checkBox->setDocPart($this->getDocPart(), $this->getDocPartId()); + $this->elements[] = $checkBox; - return $element; + return $checkBox; } /** * Get section number - * getFooterCount + * + * @return array */ public function getSectionId() { @@ -484,7 +458,7 @@ abstract class Container extends Element $containers = $rules[0]; $allowedDocParts = $rules[1]; foreach ($containers as $container) { - if ($this->container == $container && !in_array($this->docPart, $allowedDocParts)) { + if ($this->container == $container && !in_array($this->getDocPart(), $allowedDocParts)) { throw new \BadMethodCallException(); } } @@ -499,8 +473,8 @@ abstract class Container extends Element private function checkElementDocPart() { $isCellTextrun = in_array($this->container, array('cell', 'textrun')); - $docPart = $isCellTextrun ? $this->docPart : $this->container; - $docPartId = $isCellTextrun ? $this->docPartId : $this->containerId; + $docPart = $isCellTextrun ? $this->getDocPart() : $this->container; + $docPartId = $isCellTextrun ? $this->getDocPartId() : $this->containerId; $inHeaderFooter = ($docPart == 'header' || $docPart == 'footer'); return $inHeaderFooter ? $docPart . $docPartId : $docPart; diff --git a/src/PhpWord/Container/Footer.php b/src/PhpWord/Container/Footer.php index 56f06383..440b3b4e 100755 --- a/src/PhpWord/Container/Footer.php +++ b/src/PhpWord/Container/Footer.php @@ -23,5 +23,6 @@ class Footer extends Container { $this->container = 'footer'; $this->containerId = $sectionId; + $this->setDocPart($this->container, $this->containerId); } } diff --git a/src/PhpWord/Container/Header.php b/src/PhpWord/Container/Header.php index ba31994d..39c6ca98 100755 --- a/src/PhpWord/Container/Header.php +++ b/src/PhpWord/Container/Header.php @@ -42,6 +42,7 @@ class Header extends Container { $this->container = 'header'; $this->containerId = $sectionId; + $this->setDocPart($this->container, $this->containerId); } /** diff --git a/src/PhpWord/Container/Section.php b/src/PhpWord/Container/Section.php index 5e39ee06..69edec41 100644 --- a/src/PhpWord/Container/Section.php +++ b/src/PhpWord/Container/Section.php @@ -52,6 +52,7 @@ class Section extends Container { $this->container = 'section'; $this->containerId = $sectionCount; + $this->setDocPart($this->container, $this->containerId); $this->settings = new Settings(); $this->setSettings($settings); } @@ -168,6 +169,7 @@ class Section extends Container /** * Create header * + * @return Header * @deprecated 0.9.2 * @codeCoverageIgnore */ @@ -179,6 +181,7 @@ class Section extends Container /** * Create footer * + * @return Footer * @deprecated 0.9.2 * @codeCoverageIgnore */ diff --git a/src/PhpWord/Element/Cell.php b/src/PhpWord/Element/Cell.php index 61e2fa4c..5e035fb7 100755 --- a/src/PhpWord/Element/Cell.php +++ b/src/PhpWord/Element/Cell.php @@ -42,8 +42,7 @@ class Cell extends Container public function __construct($docPart, $docPartId, $width = null, $style = null) { $this->container = 'cell'; - $this->docPart = $docPart; - $this->docPartId = $docPartId; + $this->setDocPart($docPart, $docPartId); $this->width = $width; $this->cellStyle = $this->setStyle(new CellStyle(), $style, true); } diff --git a/src/PhpWord/Element/Element.php b/src/PhpWord/Element/Element.php index b94a62c3..b3565775 100644 --- a/src/PhpWord/Element/Element.php +++ b/src/PhpWord/Element/Element.php @@ -16,6 +16,24 @@ namespace PhpOffice\PhpWord\Element; */ abstract class Element { + /** + * Document part type: section|header|footer + * + * Used by textrun and cell container to determine where the element is + * located because it will affect the availability of other element, + * e.g. footnote will not be available when $docPart is header or footer. + * + * @var string + */ + private $docPart = 'section'; + + /** + * Document part Id + * + * @var integer + */ + private $docPartId = 1; + /** * Set style value * @@ -39,4 +57,46 @@ abstract class Element return $style; } + + /** + * Set doc part + * + * @param string $docPart + * @param integer $docPartId + */ + public function setDocPart($docPart, $docPartId = 1) + { + $this->docPart = $docPart; + $this->docPartId = $docPartId; + } + + /** + * Get doc part + * + * @return string + */ + public function getDocPart() + { + return $this->docPart; + } + + /** + * Get doc part Id + * + * @return integer + */ + public function getDocPartId() + { + return $this->docPartId; + } + + /** + * Check if element is located in section doc part (as opposed to header/footer) + * + * @return boolean + */ + public function isInSection() + { + return ($this->docPart == 'section'); + } } diff --git a/src/PhpWord/Element/Object.php b/src/PhpWord/Element/Object.php index d885bfed..5716b7e6 100644 --- a/src/PhpWord/Element/Object.php +++ b/src/PhpWord/Element/Object.php @@ -44,13 +44,6 @@ class Object extends Element */ private $imageRelationId; - /** - * Object ID - * - * @var int - */ - private $objectId; - /** * Create a new Ole-Object Element * @@ -135,19 +128,23 @@ class Object extends Element * Get Object ID * * @return int + * @deprecated 0.9.2 + * @codeCoverageIgnore */ public function getObjectId() { - return $this->objectId; + return $this->relationId + 1325353440; } /** * Set Object ID * * @param int $objId + * @deprecated 0.9.2 + * @codeCoverageIgnore */ public function setObjectId($objId) { - $this->objectId = $objId; + $this->relationId = $objId; } } diff --git a/src/PhpWord/Element/Row.php b/src/PhpWord/Element/Row.php index 25357062..53990850 100644 --- a/src/PhpWord/Element/Row.php +++ b/src/PhpWord/Element/Row.php @@ -37,21 +37,6 @@ class Row extends Element */ private $cells = array(); - /** - * Table holder - * - * @var string - */ - private $docPart; - - /** - * Section/Header/Footer count - * - * @var int - */ - private $docPartId; - - /** * Create a new table row * @@ -62,8 +47,7 @@ class Row extends Element */ public function __construct($docPart, $docPartId, $height = null, $style = null) { - $this->docPart = $docPart; - $this->docPartId = $docPartId; + $this->setDocPart($docPart, $docPartId); $this->height = $height; $this->style = $this->setStyle(new RowStyle(), $style, true); } @@ -76,7 +60,7 @@ class Row extends Element */ public function addCell($width = null, $style = null) { - $cell = new Cell($this->docPart, $this->docPartId, $width, $style); + $cell = new Cell($this->getDocPart(), $this->getDocPartId(), $width, $style); $this->cells[] = $cell; return $cell; } diff --git a/src/PhpWord/Element/Table.php b/src/PhpWord/Element/Table.php index fb50caf2..5808a065 100644 --- a/src/PhpWord/Element/Table.php +++ b/src/PhpWord/Element/Table.php @@ -31,20 +31,6 @@ class Table extends Element */ private $rows = array(); - /** - * Table holder - * - * @var string - */ - private $docPart = null; - - /** - * Table holder count - * - * @var int - */ - private $docPartId; - /** * Table width * @@ -62,8 +48,7 @@ class Table extends Element */ public function __construct($docPart, $docPartId, $style = null) { - $this->docPart = $docPart; - $this->docPartId = $docPartId; + $this->setDocPart($docPart, $docPartId); $this->style = $this->setStyle(new TableStyle(), $style); } @@ -75,7 +60,7 @@ class Table extends Element */ public function addRow($height = null, $style = null) { - $row = new Row($this->docPart, $this->docPartId, $height, $style); + $row = new Row($this->getDocPart(), $this->getDocPartId(), $height, $style); $this->rows[] = $row; return $row; } diff --git a/src/PhpWord/Element/TextRun.php b/src/PhpWord/Element/TextRun.php index 4a980424..794ad230 100755 --- a/src/PhpWord/Element/TextRun.php +++ b/src/PhpWord/Element/TextRun.php @@ -31,11 +31,9 @@ class TextRun extends Container * @param string $docPart section|header|footer * @param int $docPartId */ - public function __construct($paragraphStyle = null, $docPart = 'section', $docPartId = 1) + public function __construct($paragraphStyle = null) { $this->container = 'textrun'; - $this->docPart = $docPart; - $this->docPartId = $docPartId; $this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle); } diff --git a/src/PhpWord/Media.php b/src/PhpWord/Media.php index 0a24d12a..d354e399 100755 --- a/src/PhpWord/Media.php +++ b/src/PhpWord/Media.php @@ -23,13 +23,6 @@ class Media */ private static $media = array(); - /** - * ObjectID Counter - * - * @var integer - */ - private static $objectId = 1325353440; - /** * Add new media element * @@ -37,7 +30,7 @@ class Media * @param string $mediaType image|object|link * @param string $source * @param Image $image - * @return integer|array + * @return integer */ public static function addMediaElement($container, $mediaType, $source, Image $image = null) { @@ -84,18 +77,9 @@ class Media $mediaData['type'] = $mediaType; $mediaData['rID'] = $relId; self::$media[$container][$mediaId] = $mediaData; - if ($mediaType === 'object') { - return array($relId, ++self::$objectId); - } else { - return $relId; - } + return $relId; } else { - if ($mediaType === 'object') { - $relId = self::$media[$container][$mediaId]['rID']; - return array($relId, ++self::$objectId); - } else { - return self::$media[$container][$mediaId]['rID']; - } + return self::$media[$container][$mediaId]['rID']; } } @@ -246,7 +230,6 @@ class Media /** * Get Header Media Elements * - * @param string $prefix header|footer * @return array * @deprecated 0.9.2 * @codeCoverageIgnore diff --git a/src/PhpWord/Reader/Word2007.php b/src/PhpWord/Reader/Word2007.php index 175d7fd3..f8add95a 100644 --- a/src/PhpWord/Reader/Word2007.php +++ b/src/PhpWord/Reader/Word2007.php @@ -428,8 +428,8 @@ class Word2007 extends Reader implements IReader * Return item of array * * @param array $array - * @param mixed $key - * @return mixed|null + * @param integer $key + * @return string */ private static function arrayItem($array, $key = 0) { diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index 352bb7ca..084deb8d 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -250,7 +250,7 @@ class Word2007 extends Writer implements IWriter * Check content types * * @param mixed $objZip - * @param mixed $element + * @param mixed $elements */ private function addFilesToPackage($objZip, $elements) { @@ -277,6 +277,9 @@ class Word2007 extends Writer implements IWriter /** * Add header/footer media elements + * + * @param mixed $objZip + * @param string $docPart */ private function addHeaderFooterMedia($objZip, $docPart) { diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index 10cabe49..762bc7ea 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -93,7 +93,7 @@ class Base extends WriterPart */ protected function writeLink(XMLWriter $xmlWriter, Link $link, $withoutP = false) { - $rID = $link->getRelationId(); + $rID = $link->getRelationId() + ($link->isInSection() ? 6 : 0); $linkName = $link->getLinkName(); if (is_null($linkName)) { $linkName = $link->getLinkSrc(); @@ -417,7 +417,7 @@ class Base extends WriterPart */ protected function writeImage(XMLWriter $xmlWriter, Image $image, $withoutP = false) { - $rId = $image->getRelationId(); + $rId = $image->getRelationId() + ($image->isInSection() ? 6 : 0); $style = $image->getStyle(); $width = $style->getWidth(); @@ -550,10 +550,10 @@ class Base extends WriterPart */ protected function writeObject(XMLWriter $xmlWriter, Object $object, $withoutP = false) { - $rIdObject = $object->getRelationId(); - $rIdImage = $object->getImageRelationId(); + $rIdObject = $object->getRelationId() + ($object->isInSection() ? 6 : 0); + $rIdImage = $object->getImageRelationId() + ($object->isInSection() ? 6 : 0); $shapeId = md5($rIdObject . '_' . $rIdImage); - $objectId = $object->getObjectId(); + $objectId = $object->getRelationId() + 1325353440; $style = $object->getStyle(); $align = $style->getAlign(); diff --git a/src/PhpWord/Writer/Word2007/ContentTypes.php b/src/PhpWord/Writer/Word2007/ContentTypes.php index 98bf4e08..9e6853d0 100755 --- a/src/PhpWord/Writer/Word2007/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/ContentTypes.php @@ -26,173 +26,76 @@ class ContentTypes extends WriterPart */ public function writeContentTypes($imageTypes, $objectTypes, $cHdrs, $footers) { - // Create XML writer - $xmlWriter = $this->getXmlWriter(); - // XML header - $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); + $OpenXMLPrefix = 'application/vnd.openxmlformats-'; + $WordMLPrefix = $OpenXMLPrefix . 'officedocument.wordprocessingml.'; - // Types - $xmlWriter->startElement('Types'); - $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types'); + $defaults = array( + 'rels' => $OpenXMLPrefix . 'package.relationships+xml', + 'xml' => 'application/xml', - // Rels - $this->writeDefaultContentType( - $xmlWriter, - 'rels', - 'application/vnd.openxmlformats-package.relationships+xml' ); - - // XML - $this->writeDefaultContentType( - $xmlWriter, - 'xml', - 'application/xml' - ); - - // Add media content-types - foreach ($imageTypes as $key => $value) { - $this->writeDefaultContentType($xmlWriter, $key, $value); + if (is_array($imageTypes)) { + $defaults = array_merge($defaults, $imageTypes); } - - // Add embedding content-types if (count($objectTypes) > 0) { - $this->writeDefaultContentType( - $xmlWriter, - 'bin', - 'application/vnd.openxmlformats-officedocument.oleObject' - ); + $defaults['bin'] = $OpenXMLPrefix . 'officedocument.oleObject'; } - - // DocProps - $this->writeOverrideContentType( - $xmlWriter, - '/docProps/app.xml', - 'application/vnd.openxmlformats-officedocument.extended-properties+xml' + $overrides = array( + '/docProps/core.xml' => $OpenXMLPrefix . 'package.core-properties+xml', + '/docProps/app.xml' => $OpenXMLPrefix . 'officedocument.extended-properties+xml', + '/word/document.xml' => $WordMLPrefix . 'document.main+xml', + '/word/styles.xml' => $WordMLPrefix . 'styles+xml', + '/word/numbering.xml' => $WordMLPrefix . 'numbering+xml', + '/word/settings.xml' => $WordMLPrefix . 'settings+xml', + '/word/theme/theme1.xml' => $OpenXMLPrefix . 'officedocument.theme+xml', + '/word/webSettings.xml' => $WordMLPrefix . 'webSettings+xml', + '/word/fontTable.xml' => $WordMLPrefix . 'fontTable+xml', + '/word/footnotes.xml' => $WordMLPrefix . 'footnotes+xml', ); - - $this->writeOverrideContentType( - $xmlWriter, - '/docProps/core.xml', - 'application/vnd.openxmlformats-package.core-properties+xml' - ); - - // Document - $this->writeOverrideContentType( - $xmlWriter, - '/word/document.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml' - ); - - // Styles - $this->writeOverrideContentType( - $xmlWriter, - '/word/styles.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml' - ); - - // Numbering - $this->writeOverrideContentType( - $xmlWriter, - '/word/numbering.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml' - ); - - // Settings - $this->writeOverrideContentType( - $xmlWriter, - '/word/settings.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml' - ); - - // Theme1 - $this->writeOverrideContentType( - $xmlWriter, - '/word/theme/theme1.xml', - 'application/vnd.openxmlformats-officedocument.theme+xml' - ); - - // WebSettings - $this->writeOverrideContentType( - $xmlWriter, - '/word/webSettings.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml' - ); - - // Font Table - $this->writeOverrideContentType( - $xmlWriter, - '/word/fontTable.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml' - ); - - // Footnotes - $this->writeOverrideContentType( - $xmlWriter, - '/word/footnotes.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml' - ); - for ($i = 1; $i <= $cHdrs; $i++) { - $this->writeOverrideContentType( - $xmlWriter, - '/word/header' . $i . '.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml' - ); + $overrides["/word/header{$i}.xml"] = $WordMLPrefix . 'header+xml'; } - for ($i = 1; $i <= count($footers); $i++) { if (!is_null($footers[$i])) { - $this->writeOverrideContentType( - $xmlWriter, - '/word/footer' . $i . '.xml', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml' - ); + $overrides["/word/footer{$i}.xml"] = $WordMLPrefix . 'footer+xml'; } } + $xmlWriter = $this->getXmlWriter(); + $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); + $xmlWriter->startElement('Types'); + $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types'); + foreach ($defaults as $key => $value) + { + $this->writeContentType($xmlWriter, true, $key, $value); + } + foreach ($overrides as $key => $value) + { + $this->writeContentType($xmlWriter, false, $key, $value); + } $xmlWriter->endElement(); - // Return return $xmlWriter->getData(); } /** - * Write Default XML element + * Write content types element * - * @param XMLWriter $xmlWriter XML Writer - * @param string $pPartname Part name - * @param string $pContentType Content type + * @param XMLWriter $xmlWriter XML Writer + * @param boolean $isDefault + * @param string $partName Part name + * @param string $contentType Content type * @throws Exception */ - private function writeDefaultContentType(XMLWriter $xmlWriter, $pPartname = '', $pContentType = '') + private function writeContentType(XMLWriter $xmlWriter, $isDefault, $partName = '', $contentType = '') { - if ($pPartname != '' && $pContentType != '') { - // Write content type - $xmlWriter->startElement('Default'); - $xmlWriter->writeAttribute('Extension', $pPartname); - $xmlWriter->writeAttribute('ContentType', $pContentType); - $xmlWriter->endElement(); - } else { - throw new Exception("Invalid parameters passed."); - } - } - - /** - * Write Override XML element - * - * @param XMLWriter $xmlWriter - * @param string $pPartname Part name - * @param string $pContentType Content type - * @throws Exception - */ - private function writeOverrideContentType(XMLWriter $xmlWriter, $pPartname = '', $pContentType = '') - { - if ($pPartname != '' && $pContentType != '') { - // Write content type - $xmlWriter->startElement('Override'); - $xmlWriter->writeAttribute('PartName', $pPartname); - $xmlWriter->writeAttribute('ContentType', $pContentType); + if ($partName != '' && $contentType != '') { + $element = $isDefault ? 'Default' : 'Override'; + $partAttribute = $isDefault ? 'Extension' : 'PartName'; + $xmlWriter->startElement($element); + $xmlWriter->writeAttribute($partAttribute, $partName); + $xmlWriter->writeAttribute('ContentType', $contentType); $xmlWriter->endElement(); } else { throw new Exception("Invalid parameters passed."); @@ -215,4 +118,32 @@ class ContentTypes extends WriterPart throw new Exception("File $pFile does not exist"); } } + + /** + * Write Default XML element + * + * @param XMLWriter $xmlWriter + * @param string $partName Part name + * @param string $contentType Content type + * @deprecated 0.9.2 + * @codeCoverageIgnore + */ + private function writeDefaultContentType(XMLWriter $xmlWriter, $partName = '', $contentType = '') + { + $this->writeContentType($xmlWriter, true, $partName, $contentType); + } + + /** + * Write Override XML element + * + * @param XMLWriter $xmlWriter + * @param string $partName Part name + * @param string $contentType Content type + * @deprecated 0.9.2 + * @codeCoverageIgnore + */ + private function writeOverrideContentType(XMLWriter $xmlWriter, $partName = '', $contentType = '') + { + $this->writeContentType($xmlWriter, false, $partName, $contentType); + } } diff --git a/tests/PhpWord/Tests/Element/ObjectTest.php b/tests/PhpWord/Tests/Element/ObjectTest.php index 22b4787f..0f5f191a 100644 --- a/tests/PhpWord/Tests/Element/ObjectTest.php +++ b/tests/PhpWord/Tests/Element/ObjectTest.php @@ -83,17 +83,4 @@ class ObjectTest extends \PHPUnit_Framework_TestCase $oObject->setImageRelationId($iVal); $this->assertEquals($oObject->getImageRelationId(), $iVal); } - - /** - * Set/get object relation Id - */ - public function testObjectId() - { - $src = __DIR__ . "/../_files/documents/sheet.xls"; - $oObject = new Object($src); - - $iVal = rand(1, 1000); - $oObject->setObjectId($iVal); - $this->assertEquals($oObject->getObjectId(), $iVal); - } } From dd9faaee06f0500deb1b175984c81ba37691a6e3 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 5 Apr 2014 00:57:50 +0700 Subject: [PATCH 18/27] Add Media::reset(), Style::reset(), Footnote::reset(), and TOC::reset() --- CHANGELOG.md | 4 + src/PhpWord/Footnote.php | 8 ++ src/PhpWord/Media.php | 8 ++ src/PhpWord/Settings.php | 12 +-- src/PhpWord/Style.php | 86 +++++++++----------- src/PhpWord/TOC.php | 40 +++++---- tests/PhpWord/Tests/Container/HeaderTest.php | 11 +++ tests/PhpWord/Tests/FootnoteTest.php | 3 + tests/PhpWord/Tests/MediaTest.php | 5 +- tests/PhpWord/Tests/StyleTest.php | 4 + tests/PhpWord/Tests/TOCTest.php | 4 + 11 files changed, 114 insertions(+), 71 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80a6253a..fea9e22c 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,10 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - CheckBox: Ability to add checkbox in header/footer - @ivanlanin GH-187 - 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::reset()` to reset all media data - @juzi GH-19 +- Style: Add `Style::reset()` to reset all styles +- Footnote: Add `Footnote::reset()` to reset all footnotes +- TOC: Add `TOC::reset()` to reset all TOC ### Bugfixes diff --git a/src/PhpWord/Footnote.php b/src/PhpWord/Footnote.php index f656dc3a..1c11bbff 100644 --- a/src/PhpWord/Footnote.php +++ b/src/PhpWord/Footnote.php @@ -59,6 +59,14 @@ class Footnote return count(self::$elements); } + /** + * Reset footer elements + */ + public static function reset() + { + self::$elements = array(); + } + /** * Add new Footnote Link Element * diff --git a/src/PhpWord/Media.php b/src/PhpWord/Media.php index d354e399..63c88361 100755 --- a/src/PhpWord/Media.php +++ b/src/PhpWord/Media.php @@ -145,6 +145,14 @@ class Media return $mediaElements; } + /** + * Reset media elements + */ + public static function reset() + { + self::$media = array(); + } + /** * Add new Section Media Element * diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php index 0fb8bad0..0af6cf72 100644 --- a/src/PhpWord/Settings.php +++ b/src/PhpWord/Settings.php @@ -23,7 +23,7 @@ class Settings * * @var boolean */ - private static $_xmlWriterCompatibility = true; + private static $xmlWriterCompatibility = true; /** * Name of the class used for Zip file management @@ -32,7 +32,7 @@ class Settings * * @var string */ - private static $_zipClass = self::ZIPARCHIVE; + private static $zipClass = self::ZIPARCHIVE; /** * Set the compatibility option used by the XMLWriter @@ -43,7 +43,7 @@ class Settings public static function setCompatibility($compatibility) { if (is_bool($compatibility)) { - self::$_xmlWriterCompatibility = $compatibility; + self::$xmlWriterCompatibility = $compatibility; return true; } return false; @@ -56,7 +56,7 @@ class Settings */ public static function getCompatibility() { - return self::$_xmlWriterCompatibility; + return self::$xmlWriterCompatibility; } /** @@ -70,7 +70,7 @@ class Settings { if (($zipClass === self::PCLZIP) || ($zipClass === self::ZIPARCHIVE)) { - self::$_zipClass = $zipClass; + self::$zipClass = $zipClass; return true; } return false; @@ -86,6 +86,6 @@ class Settings */ public static function getZipClass() { - return self::$_zipClass; + return self::$zipClass; } // function getZipClass() } diff --git a/src/PhpWord/Style.php b/src/PhpWord/Style.php index f7b8e4c6..3db67cb5 100755 --- a/src/PhpWord/Style.php +++ b/src/PhpWord/Style.php @@ -23,7 +23,7 @@ class Style * * @var array */ - private static $_styleElements = array(); + private static $styles = array(); /** * Add paragraph style @@ -33,17 +33,7 @@ class Style */ public static function addParagraphStyle($styleName, $styles) { - if (!array_key_exists($styleName, self::$_styleElements)) { - $style = new Paragraph(); - foreach ($styles as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $style->setStyleValue($key, $value); - } - - self::$_styleElements[$styleName] = $style; - } + self::setStyleValues($styleName, $styles, new Paragraph()); } /** @@ -55,16 +45,7 @@ class Style */ public static function addFontStyle($styleName, $styleFont, $styleParagraph = null) { - if (!array_key_exists($styleName, self::$_styleElements)) { - $font = new Font('text', $styleParagraph); - foreach ($styleFont as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $font->setStyleValue($key, $value); - } - self::$_styleElements[$styleName] = $font; - } + self::setStyleValues($styleName, $styleFont, new Font('text', $styleParagraph)); } /** @@ -75,17 +56,7 @@ class Style */ public static function addLinkStyle($styleName, $styles) { - if (!array_key_exists($styleName, self::$_styleElements)) { - $style = new Font('link'); - foreach ($styles as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $style->setStyleValue($key, $value); - } - - self::$_styleElements[$styleName] = $style; - } + self::setStyleValues($styleName, $styles, new Font('link')); } /** @@ -97,10 +68,10 @@ class Style */ public static function addTableStyle($styleName, $styleTable, $styleFirstRow = null) { - if (!array_key_exists($styleName, self::$_styleElements)) { + if (!array_key_exists($styleName, self::$styles)) { $style = new Table($styleTable, $styleFirstRow); - self::$_styleElements[$styleName] = $style; + self::$styles[$styleName] = $style; } } @@ -114,17 +85,15 @@ class Style public static function addTitleStyle($titleCount, $styleFont, $styleParagraph = null) { $styleName = 'Heading_' . $titleCount; - if (!array_key_exists($styleName, self::$_styleElements)) { - $font = new Font('title', $styleParagraph); - foreach ($styleFont as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $font->setStyleValue($key, $value); - } + self::setStyleValues("Heading_{$titleCount}", $styleFont, new Font('title', $styleParagraph)); + } - self::$_styleElements[$styleName] = $font; - } + /** + * Reset styles + */ + public static function reset() + { + self::$styles = array(); } /** @@ -144,7 +113,7 @@ class Style */ public static function getStyles() { - return self::$_styleElements; + return self::$styles; } /** @@ -154,10 +123,31 @@ class Style */ public static function getStyle($styleName) { - if (array_key_exists($styleName, self::$_styleElements)) { - return self::$_styleElements[$styleName]; + if (array_key_exists($styleName, self::$styles)) { + return self::$styles[$styleName]; } else { return null; } } + + /** + * Set style values + * + * @param string $styleName + * @param array $styleValues + * @param mixed $styleObject + */ + private static function setStyleValues($styleName, $styleValues, $styleObject) + { + if (!array_key_exists($styleName, self::$styles)) { + foreach ($styleValues as $key => $value) { + if (substr($key, 0, 1) != '_') { + $key = '_' . $key; + } + $styleObject->setStyleValue($key, $value); + } + + self::$styles[$styleName] = $styleObject; + } + } } diff --git a/src/PhpWord/TOC.php b/src/PhpWord/TOC.php index bd1c3f38..0dc7c874 100644 --- a/src/PhpWord/TOC.php +++ b/src/PhpWord/TOC.php @@ -22,35 +22,35 @@ class TOC * * @var array */ - private static $_titles = array(); + private static $titles = array(); /** * TOC style * * @var TOCStyle */ - private static $_styleTOC; + private static $TOCStyle; /** * Font style * * @var Font|array|string */ - private static $_styleFont; + private static $fontStyle; /** * Title anchor * * @var int */ - private static $_anchor = 252634154; + private static $anchor = 252634154; /** * Title bookmark * * @var int */ - private static $_bookmarkId = 0; + private static $bookmarkId = 0; /** @@ -61,28 +61,28 @@ class TOC */ public function __construct($styleFont = null, $styleTOC = null) { - self::$_styleTOC = new TOCStyle(); + self::$TOCStyle = new TOCStyle(); if (!is_null($styleTOC) && is_array($styleTOC)) { foreach ($styleTOC as $key => $value) { if (substr($key, 0, 1) != '_') { $key = '_' . $key; } - self::$_styleTOC->setStyleValue($key, $value); + self::$TOCStyle->setStyleValue($key, $value); } } if (!is_null($styleFont)) { if (is_array($styleFont)) { - self::$_styleFont = new Font(); + self::$fontStyle = new Font(); foreach ($styleFont as $key => $value) { if (substr($key, 0, 1) != '_') { $key = '_' . $key; } - self::$_styleFont->setStyleValue($key, $value); + self::$fontStyle->setStyleValue($key, $value); } } else { - self::$_styleFont = $styleFont; + self::$fontStyle = $styleFont; } } } @@ -96,8 +96,8 @@ class TOC */ public static function addTitle($text, $depth = 0) { - $anchor = '_Toc' . ++self::$_anchor; - $bookmarkId = self::$_bookmarkId++; + $anchor = '_Toc' . ++self::$anchor; + $bookmarkId = self::$bookmarkId++; $title = array(); $title['text'] = $text; @@ -105,7 +105,7 @@ class TOC $title['anchor'] = $anchor; $title['bookmarkId'] = $bookmarkId; - self::$_titles[] = $title; + self::$titles[] = $title; return array($anchor, $bookmarkId); } @@ -117,7 +117,15 @@ class TOC */ public static function getTitles() { - return self::$_titles; + return self::$titles; + } + + /** + * Reset footnotes + */ + public static function reset() + { + self::$titles = array(); } /** @@ -127,7 +135,7 @@ class TOC */ public static function getStyleTOC() { - return self::$_styleTOC; + return self::$TOCStyle; } /** @@ -137,6 +145,6 @@ class TOC */ public static function getStyleFont() { - return self::$_styleFont; + return self::$fontStyle; } } diff --git a/tests/PhpWord/Tests/Container/HeaderTest.php b/tests/PhpWord/Tests/Container/HeaderTest.php index d7425bdc..9d13d57c 100644 --- a/tests/PhpWord/Tests/Container/HeaderTest.php +++ b/tests/PhpWord/Tests/Container/HeaderTest.php @@ -220,4 +220,15 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oHeader->getType(), Header::EVEN); } + + /** + * Add footnote exception + * + * @expectedException BadMethodCallException + */ + public function testAddFootnoteException() + { + $header = new Header(1); + $header->addFootnote(); + } } diff --git a/tests/PhpWord/Tests/FootnoteTest.php b/tests/PhpWord/Tests/FootnoteTest.php index 955d02e5..47aea9a3 100644 --- a/tests/PhpWord/Tests/FootnoteTest.php +++ b/tests/PhpWord/Tests/FootnoteTest.php @@ -31,5 +31,8 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase $this->assertEquals(1, $rIdLink); $this->assertEquals(1, count(Footnote::getFootnoteElements())); $this->assertEquals(1, count(Footnote::getFootnoteLinkElements())); + + Footnote::reset(); + $this->assertEquals(0, count(Footnote::getFootnoteElements())); } } diff --git a/tests/PhpWord/Tests/MediaTest.php b/tests/PhpWord/Tests/MediaTest.php index b1a49059..3df52e4a 100644 --- a/tests/PhpWord/Tests/MediaTest.php +++ b/tests/PhpWord/Tests/MediaTest.php @@ -83,7 +83,7 @@ class MediaTest extends \PHPUnit_Framework_TestCase } /** - * Add footer media element + * Add footer media element and reset media */ public function testAddFooterMediaElement() { @@ -94,5 +94,8 @@ class MediaTest extends \PHPUnit_Framework_TestCase Media::addMediaElement('footer1', 'image', $remote, new Image($remote)); $this->assertEquals(2, Media::countMediaElements('footer1')); + + Media::reset(); + $this->assertEquals(0, Media::countMediaElements('footer1')); } } diff --git a/tests/PhpWord/Tests/StyleTest.php b/tests/PhpWord/Tests/StyleTest.php index 6381cbc9..b323ece4 100644 --- a/tests/PhpWord/Tests/StyleTest.php +++ b/tests/PhpWord/Tests/StyleTest.php @@ -43,6 +43,10 @@ class StyleTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf("PhpOffice\\PhpWord\\Style\\{$style}", Style::getStyle($name)); } $this->assertNull(Style::getStyle('Unknown')); + + Style::reset(); + $this->assertEquals(0, count(Style::getStyles())); + } /** diff --git a/tests/PhpWord/Tests/TOCTest.php b/tests/PhpWord/Tests/TOCTest.php index 100206a4..0a060eb8 100644 --- a/tests/PhpWord/Tests/TOCTest.php +++ b/tests/PhpWord/Tests/TOCTest.php @@ -78,5 +78,9 @@ class TOCTest extends \PHPUnit_Framework_TestCase $this->assertEquals($depth, $savedTitles[$i]['depth']); $i++; } + + TOC::reset(); + $this->assertEquals(0, count(TOC::getTitles())); + } } From 6aa73544a618b7bfd17148be096e4b8b1eedd370 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 5 Apr 2014 01:41:48 +0700 Subject: [PATCH 19/27] Fix failed test --- src/PhpWord/Shared/ZipArchive.php | 2 ++ src/PhpWord/Writer/ODText/Content.php | 4 ++++ src/PhpWord/Writer/ODText/Meta.php | 4 ++++ src/PhpWord/Writer/ODText/Styles.php | 4 ++++ src/PhpWord/Writer/Word2007/ContentTypes.php | 6 ++---- src/PhpWord/Writer/Word2007/DocProps.php | 4 ++++ src/PhpWord/Writer/Word2007/Document.php | 4 ++++ 7 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/PhpWord/Shared/ZipArchive.php b/src/PhpWord/Shared/ZipArchive.php index 7ef0889e..46d5c036 100644 --- a/src/PhpWord/Shared/ZipArchive.php +++ b/src/PhpWord/Shared/ZipArchive.php @@ -170,6 +170,8 @@ class ZipArchive $list = $this->zip->listContent(); $listCount = count($list); $listIndex = -1; + $contents = null; + for ($i = 0; $i < $listCount; ++$i) { if (strtolower($list[$i]["filename"]) == strtolower($fileName) || strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) { diff --git a/src/PhpWord/Writer/ODText/Content.php b/src/PhpWord/Writer/ODText/Content.php index 0669bc41..7fc0d2df 100644 --- a/src/PhpWord/Writer/ODText/Content.php +++ b/src/PhpWord/Writer/ODText/Content.php @@ -39,6 +39,10 @@ class Content extends WriterPart */ public function writeContent(PhpWord $phpWord = null) { + if (is_null($phpWord)) { + throw new Exception("No PhpWord assigned."); + } + // Create XML writer $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/ODText/Meta.php b/src/PhpWord/Writer/ODText/Meta.php index d0d593e2..fc5314e9 100644 --- a/src/PhpWord/Writer/ODText/Meta.php +++ b/src/PhpWord/Writer/ODText/Meta.php @@ -24,6 +24,10 @@ class Meta extends WriterPart */ public function writeMeta(PhpWord $phpWord = null) { + if (is_null($phpWord)) { + throw new Exception("No PhpWord assigned."); + } + // Create XML writer $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/ODText/Styles.php b/src/PhpWord/Writer/ODText/Styles.php index fa81ea9e..8839f568 100644 --- a/src/PhpWord/Writer/ODText/Styles.php +++ b/src/PhpWord/Writer/ODText/Styles.php @@ -28,6 +28,10 @@ class Styles extends WriterPart */ public function writeStyles(PhpWord $phpWord = null) { + if (is_null($phpWord)) { + throw new Exception("No PhpWord assigned."); + } + // Create XML writer $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/Word2007/ContentTypes.php b/src/PhpWord/Writer/Word2007/ContentTypes.php index 9e6853d0..9a383e9b 100755 --- a/src/PhpWord/Writer/Word2007/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/ContentTypes.php @@ -66,12 +66,10 @@ class ContentTypes extends WriterPart $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); $xmlWriter->startElement('Types'); $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types'); - foreach ($defaults as $key => $value) - { + foreach ($defaults as $key => $value) { $this->writeContentType($xmlWriter, true, $key, $value); } - foreach ($overrides as $key => $value) - { + foreach ($overrides as $key => $value) { $this->writeContentType($xmlWriter, false, $key, $value); } $xmlWriter->endElement(); diff --git a/src/PhpWord/Writer/Word2007/DocProps.php b/src/PhpWord/Writer/Word2007/DocProps.php index ceb5db04..cfa238e5 100644 --- a/src/PhpWord/Writer/Word2007/DocProps.php +++ b/src/PhpWord/Writer/Word2007/DocProps.php @@ -21,6 +21,10 @@ class DocProps extends WriterPart */ public function writeDocPropsApp(PhpWord $phpWord = null) { + if (is_null($phpWord)) { + throw new Exception("No PhpWord assigned."); + } + // Create XML writer $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/Word2007/Document.php b/src/PhpWord/Writer/Word2007/Document.php index 8bf46a0b..499aad19 100644 --- a/src/PhpWord/Writer/Word2007/Document.php +++ b/src/PhpWord/Writer/Word2007/Document.php @@ -28,6 +28,10 @@ class Document extends Base */ public function writeDocument(PhpWord $phpWord = null) { + if (is_null($phpWord)) { + throw new Exception("No PhpWord assigned."); + } + // Create XML writer $xmlWriter = $this->getXmlWriter(); From 03934af33416fd940bac84f80757a8fe621a8143 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 5 Apr 2014 19:02:49 +0700 Subject: [PATCH 20/27] Refactor header/footer and media model --- CHANGELOG.md | 4 +- src/PhpWord/Container/Container.php | 28 ++- src/PhpWord/Container/Footer.php | 42 +++- src/PhpWord/Container/Header.php | 47 ++++- src/PhpWord/Container/Section.php | 92 ++++++--- src/PhpWord/Element/Element.php | 4 + src/PhpWord/Element/TextRun.php | 2 - src/PhpWord/Footnote.php | 4 +- src/PhpWord/Media.php | 75 +++---- src/PhpWord/Writer/ODText/Manifest.php | 18 -- src/PhpWord/Writer/Word2007.php | 188 +++++++----------- src/PhpWord/Writer/Word2007/ContentTypes.php | 110 +++------- src/PhpWord/Writer/Word2007/Document.php | 16 +- src/PhpWord/Writer/Word2007/Rels.php | 22 +- src/PhpWord/Writer/Word2007/Styles.php | 4 + tests/PhpWord/Tests/Container/SectionTest.php | 4 +- tests/PhpWord/Tests/MediaTest.php | 57 +++--- 17 files changed, 356 insertions(+), 361 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fea9e22c..548ef026 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,8 +39,8 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - `createSection` replaced by `addSection` - `Element\Footnote::getReferenceId` replaced by `Container\Container::getRelationId` - `Element\Footnote::setReferenceId` replaced by `Container\Container::setRelationId` -- `Footnote::addFootnoteLinkElement` replaced by `Media::addMediaElement` -- `Footnote::getFootnoteLinkElements` replaced by `Media::getMediaElements` +- `Footnote::addFootnoteLinkElement` replaced by `Media::addElement` +- `Footnote::getFootnoteLinkElements` replaced by `Media::getElements` - All current methods on `Media` ### Miscellaneous diff --git a/src/PhpWord/Container/Container.php b/src/PhpWord/Container/Container.php index ec66a5a0..2584a1f7 100644 --- a/src/PhpWord/Container/Container.php +++ b/src/PhpWord/Container/Container.php @@ -49,7 +49,7 @@ abstract class Container extends Element * * @var int */ - protected $containerId; + protected $sectionId; /** * Elements collection @@ -123,7 +123,7 @@ abstract class Container extends Element $link = new Link(String::toUTF8($linkSrc), String::toUTF8($linkName), $fontStyle, $paragraphStyle); $link->setDocPart($this->getDocPart(), $this->getDocPartId()); - $rID = Media::addMediaElement($elementDocPart, 'link', $linkSrc); + $rID = Media::addElement($elementDocPart, 'link', $linkSrc); $link->setRelationId($rID); $this->elements[] = $link; @@ -250,14 +250,10 @@ abstract class Container extends Element $image = new Image($src, $style, $isWatermark); $image->setDocPart($this->getDocPart(), $this->getDocPartId()); - if (!is_null($image->getSource())) { - $rID = Media::addMediaElement($elementDocPart, 'image', $src, $image); - $image->setRelationId($rID); - $this->elements[] = $image; - return $image; - } else { - throw new InvalidImageException; - } + $rID = Media::addElement($elementDocPart, 'image', $src, $image); + $image->setRelationId($rID); + $this->elements[] = $image; + return $image; } /** @@ -284,9 +280,9 @@ abstract class Container extends Element $ext = substr($ext, 0, -1); } $icon = realpath(__DIR__ . "/../_staticDocParts/_{$ext}.png"); - $rID = Media::addMediaElement($elementDocPart, 'object', $src); + $rID = Media::addElement($elementDocPart, 'object', $src); $object->setRelationId($rID); - $rIDimg = Media::addMediaElement($elementDocPart, 'image', $icon, new Image($icon)); + $rIDimg = Media::addElement($elementDocPart, 'image', $icon, new Image($icon)); $object->setImageRelationId($rIDimg); $this->elements[] = $object; return $object; @@ -307,7 +303,7 @@ abstract class Container extends Element $footnote = new FootnoteElement($paragraphStyle); $refID = FootnoteCollection::addFootnoteElement($footnote); - $footnote->setDocPart($this->getDocPart(), $this->getDocPartId()); + $footnote->setDocPart('footnote', $this->getDocPartId()); $footnote->setRelationId($refID); $this->elements[] = $footnote; @@ -337,11 +333,11 @@ abstract class Container extends Element /** * Get section number * - * @return array + * @return integer */ public function getSectionId() { - return $this->containerId; + return $this->sectionId; } /** @@ -474,7 +470,7 @@ abstract class Container extends Element { $isCellTextrun = in_array($this->container, array('cell', 'textrun')); $docPart = $isCellTextrun ? $this->getDocPart() : $this->container; - $docPartId = $isCellTextrun ? $this->getDocPartId() : $this->containerId; + $docPartId = $isCellTextrun ? $this->getDocPartId() : $this->sectionId; $inHeaderFooter = ($docPart == 'header' || $docPart == 'footer'); return $inHeaderFooter ? $docPart . $docPartId : $docPart; diff --git a/src/PhpWord/Container/Footer.php b/src/PhpWord/Container/Footer.php index 440b3b4e..47287c8b 100755 --- a/src/PhpWord/Container/Footer.php +++ b/src/PhpWord/Container/Footer.php @@ -14,15 +14,51 @@ namespace PhpOffice\PhpWord\Container; */ class Footer extends Container { + const AUTO = 'default'; // default and odd pages + const FIRST = 'first'; + const EVEN = 'even'; + + /** + * Header type + * + * @var string + */ + private $type = self::AUTO; + /** * Create new instance * * @param int $sectionId + * @param int $footerId + * @param string $type */ - public function __construct($sectionId) + public function __construct($sectionId, $footerId = 1, $type = self::AUTO) { $this->container = 'footer'; - $this->containerId = $sectionId; - $this->setDocPart($this->container, $this->containerId); + $this->sectionId = $sectionId; + $this->setType($type); + $this->setDocPart($this->container, ($sectionId - 1) * 3 + $footerId); + } + + /** + * Set type + * + * @param string $value + * @since 0.9.2 + */ + public function setType($value = self::AUTO) + { + $this->type = $value; + } + + /** + * Get type + * + * @return string + * @since 0.9.2 + */ + public function getType() + { + return $this->type; } } diff --git a/src/PhpWord/Container/Header.php b/src/PhpWord/Container/Header.php index 39c6ca98..2920e3cf 100755 --- a/src/PhpWord/Container/Header.php +++ b/src/PhpWord/Container/Header.php @@ -20,29 +20,32 @@ class Header extends Container * Header types constants * * @var string - * @link http://www.schemacentral.com/sc/ooxml/a-wheaderType-4.html Header or Footer Type + * @link http://www.schemacentral.com/sc/ooxml/a-wtype-4.html Header or Footer Type */ - const AUTO = 'default'; // Did not use DEFAULT because it is a PHP keyword - const EVEN = 'even'; + const AUTO = 'default'; // default and odd pages const FIRST = 'first'; + const EVEN = 'even'; /** * Header type * * @var string */ - private $headerType = self::AUTO; + private $type = self::AUTO; /** * Create new instance * * @param int $sectionId + * @param int $headerId + * @param string $type */ - public function __construct($sectionId) + public function __construct($sectionId, $headerId = 1, $type = self::AUTO) { $this->container = 'header'; - $this->containerId = $sectionId; - $this->setDocPart($this->container, $this->containerId); + $this->sectionId = $sectionId; + $this->setType($type); + $this->setDocPart($this->container, ($sectionId - 1) * 3 + $headerId); } /** @@ -57,35 +60,57 @@ class Header extends Container return $this->addImage($src, $style, true); } + /** + * Set header type + * + * @param string $value + * @since 0.9.2 + */ + 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->headerType; + return $this->type; } /** * Reset type to default + * + * @return string */ public function resetType() { - return $this->headerType = self::AUTO; + return $this->type = self::AUTO; } /** * First page only header + * + * @return string */ public function firstPage() { - return $this->headerType = self::FIRST; + return $this->type = self::FIRST; } /** * Even numbered pages only + * + * @return string */ public function evenPage() { - return $this->headerType = self::EVEN; + return $this->type = self::EVEN; } } diff --git a/src/PhpWord/Container/Section.php b/src/PhpWord/Container/Section.php index 69edec41..46f2d44e 100644 --- a/src/PhpWord/Container/Section.php +++ b/src/PhpWord/Container/Section.php @@ -9,6 +9,7 @@ namespace PhpOffice\PhpWord\Container; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\TOC; use PhpOffice\PhpWord\Container\Footer; use PhpOffice\PhpWord\Container\Header; @@ -28,19 +29,18 @@ class Section extends Container private $settings; /** - * Section headers + * Section headers, indexed from 1, not zero * * @var Header[] */ private $headers = array(); /** - * Section footer + * Section footers, indexed from 1, not zero * - * @var Footer + * @var Footer[] */ - private $footer = null; - + private $footers = array(); /** * Create new instance @@ -51,8 +51,8 @@ class Section extends Container public function __construct($sectionCount, $settings = null) { $this->container = 'section'; - $this->containerId = $sectionCount; - $this->setDocPart($this->container, $this->containerId); + $this->sectionId = $sectionCount; + $this->setDocPart($this->container, $this->sectionId); $this->settings = new Settings(); $this->setSettings($settings); } @@ -109,29 +109,29 @@ class Section extends Container /** * Add header * + * @param string $type * @return Header + * @since 0.9.2 */ - public function addHeader() + public function addHeader($type = Header::AUTO) { - $header = new Header($this->containerId); - $this->headers[] = $header; - return $header; + return $this->addHeaderFooter($type, true); } /** * Add footer * + * @param string $type * @return Footer + * @since 0.9.2 */ - public function addFooter() + public function addFooter($type = Header::AUTO) { - $footer = new Footer($this->containerId); - $this->footer = $footer; - return $footer; + return $this->addHeaderFooter($type, false); } /** - * Get Headers + * Get header elements * * @return Header[] */ @@ -141,13 +141,13 @@ class Section extends Container } /** - * Get footer element + * Get footer elements * - * @return Footer + * @return Footer[] */ - public function getFooter() + public function getFooters() { - return $this->footer; + return $this->footers; } /** @@ -160,10 +160,38 @@ class Section extends Container */ public function hasDifferentFirstPage() { - $value = array_filter($this->headers, function (Header &$header) { - return $header->getType() == Header::FIRST; - }); - return count($value) > 0; + foreach ($this->headers as $header) { + if ($header->getType() == Header::FIRST) { + return true; + } + } + return false; + } + + /** + * Add header/footer + * + * @param string $type + * @param string $header + * @return Header|Footer + * @since 0.9.2 + */ + private function addHeaderFooter($type = Header::AUTO, $header = true) + { + $collectionArray = $header ? 'headers' : 'footers'; + $containerClass = 'PhpOffice\\PhpWord\\Container\\'; + $containerClass .= ($header ? 'Header' : 'Footer'); + $collection = &$this->$collectionArray; + + if (in_array($type, array(Header::AUTO, Header::FIRST, Header::EVEN))) { + $index = count($collection); + $container = new $containerClass($this->sectionId, ++$index, $type); + $collection[$index] = $container; + return $container; + } else { + throw new Exception('Invalid header/footer type.'); + } + } /** @@ -189,4 +217,20 @@ class Section extends Container { return $this->addFooter(); } + + /** + * Get footer + * + * @return Footer + * @deprecated 0.9.2 + * @codeCoverageIgnore + */ + public function getFooter() + { + if (empty($this->footers)) { + return null; + } else { + return $this->footers[1]; + } + } } diff --git a/src/PhpWord/Element/Element.php b/src/PhpWord/Element/Element.php index b3565775..ec0dbc5c 100644 --- a/src/PhpWord/Element/Element.php +++ b/src/PhpWord/Element/Element.php @@ -30,6 +30,10 @@ abstract class Element /** * Document part Id * + * For header and footer, this will be = ($sectionId - 1) * 3 + $index + * because the max number of header/footer in every page is 3, i.e. + * AUTO, FIRST, and EVEN (AUTO = ODD) + * * @var integer */ private $docPartId = 1; diff --git a/src/PhpWord/Element/TextRun.php b/src/PhpWord/Element/TextRun.php index 794ad230..6be08fc4 100755 --- a/src/PhpWord/Element/TextRun.php +++ b/src/PhpWord/Element/TextRun.php @@ -28,8 +28,6 @@ class TextRun extends Container * Create new instance * * @param string|array|Paragraph $paragraphStyle - * @param string $docPart section|header|footer - * @param int $docPartId */ public function __construct($paragraphStyle = null) { diff --git a/src/PhpWord/Footnote.php b/src/PhpWord/Footnote.php index 1c11bbff..9b11518b 100644 --- a/src/PhpWord/Footnote.php +++ b/src/PhpWord/Footnote.php @@ -77,7 +77,7 @@ class Footnote */ public static function addFootnoteLinkElement($linkSrc) { - return Media::addMediaElement('footnotes', 'hyperlink', $linkSrc); + return Media::addElement('footnotes', 'link', $linkSrc); } /** @@ -89,6 +89,6 @@ class Footnote */ public static function getFootnoteLinkElements() { - return Media::getMediaElements('footnotes', 'hyperlink'); + return Media::getElements('footnotes', 'link'); } } diff --git a/src/PhpWord/Media.php b/src/PhpWord/Media.php index 63c88361..921fc154 100755 --- a/src/PhpWord/Media.php +++ b/src/PhpWord/Media.php @@ -9,6 +9,7 @@ namespace PhpOffice\PhpWord; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Element\Image; /** @@ -21,7 +22,7 @@ class Media * * @var array */ - private static $media = array(); + private static $elements = array(); /** * Add new media element @@ -31,19 +32,20 @@ class Media * @param string $source * @param Image $image * @return integer + * @since 0.9.2 */ - public static function addMediaElement($container, $mediaType, $source, Image $image = null) + public static function addElement($container, $mediaType, $source, Image $image = null) { // Assign unique media Id and initiate media container if none exists $mediaId = md5($container . $source); - if (!array_key_exists($container, self::$media)) { - self::$media[$container]= array(); + if (!array_key_exists($container, self::$elements)) { + self::$elements[$container]= array(); } // Add media if not exists or point to existing media - if (!array_key_exists($mediaId, self::$media[$container])) { - $mediaCount = self::countMediaElements($container); - $mediaTypeCount = self::countMediaElements($container, $mediaType); + if (!array_key_exists($mediaId, self::$elements[$container])) { + $mediaCount = self::countElements($container); + $mediaTypeCount = self::countElements($container, $mediaType); $mediaData = array(); $relId = ++$mediaCount; $target = null; @@ -51,16 +53,17 @@ class Media // Images if ($mediaType == 'image') { - $isMemImage = false; - if (!is_null($image)) { - $isMemImage = $image->getIsMemImage(); - $ext = $image->getImageExtension(); - $ext = strtolower($ext); + if (is_null($image)) { + throw new Exception('Image object not assigned.'); } + $isMemImage = $image->getIsMemImage(); + $ext = $image->getImageExtension(); + $mediaData['imageExtension'] = $ext; + $mediaData['imageType'] = $image->getImageType(); if ($isMemImage) { $mediaData['isMemImage'] = true; - $mediaData['createfunction'] = $image->getImageCreateFunction(); - $mediaData['imagefunction'] = $image->getImageFunction(); + $mediaData['createFunction'] = $image->getImageCreateFunction(); + $mediaData['imageFunction'] = $image->getImageFunction(); } $target = "media/{$container}_image{$mediaTypeCount}.{$ext}"; // Objects @@ -76,10 +79,10 @@ class Media $mediaData['target'] = $target; $mediaData['type'] = $mediaType; $mediaData['rID'] = $relId; - self::$media[$container][$mediaId] = $mediaData; + self::$elements[$container][$mediaId] = $mediaData; return $relId; } else { - return self::$media[$container][$mediaId]['rID']; + return self::$elements[$container][$mediaId]['rID']; } } @@ -89,13 +92,14 @@ class Media * @param string $container section|headerx|footerx|footnote * @param string $mediaType image|object|link * @return integer + * @since 0.9.2 */ - public static function countMediaElements($container, $mediaType = null) + public static function countElements($container, $mediaType = null) { $mediaCount = 0; - if (array_key_exists($container, self::$media)) { - foreach (self::$media[$container] as $mediaKey => $mediaData) { + if (array_key_exists($container, self::$elements)) { + foreach (self::$elements[$container] as $mediaKey => $mediaData) { if (!is_null($mediaType)) { if ($mediaType == $mediaData['type']) { $mediaCount++; @@ -115,23 +119,24 @@ class Media * @param string $container section|headerx|footerx|footnote * @param string $mediaType image|object|link * @return array + * @since 0.9.2 */ - public static function getMediaElements($container, $mediaType = null) + public static function getElements($container, $mediaType = null) { $mediaElements = array(); // If header/footer, search for headerx and footerx where x is number if ($container == 'header' || $container == 'footer') { - foreach (self::$media as $key => $val) { + foreach (self::$elements as $key => $val) { if (substr($key, 0, 6) == $container) { $mediaElements[$key] = $val; } } } else { - if (!array_key_exists($container, self::$media)) { + if (!array_key_exists($container, self::$elements)) { return $mediaElements; } - foreach (self::$media[$container] as $mediaKey => $mediaData) { + foreach (self::$elements[$container] as $mediaKey => $mediaData) { if (!is_null($mediaType)) { if ($mediaType == $mediaData['type']) { $mediaElements[$mediaKey] = $mediaData; @@ -150,7 +155,7 @@ class Media */ public static function reset() { - self::$media = array(); + self::$elements = array(); } /** @@ -159,13 +164,13 @@ class Media * @param string $src * @param string $type * @param Image $image - * @return integer|array + * @return integer * @deprecated 0.9.2 * @codeCoverageIgnore */ public static function addSectionMediaElement($src, $type, Image $image = null) { - return self::addMediaElement("section", $type, $src, $image); + return self::addElement('section', $type, $src, $image); } /** @@ -178,7 +183,7 @@ class Media */ public static function addSectionLinkElement($linkSrc) { - return self::addMediaElement('section', 'link', $linkSrc); + return self::addElement('section', 'link', $linkSrc); } /** @@ -191,7 +196,7 @@ class Media */ public static function getSectionMediaElements($key = null) { - return self::getMediaElements('section', $key); + return self::getElements('section', $key); } /** @@ -204,7 +209,7 @@ class Media */ public static function countSectionMediaElements($key = null) { - return self::countMediaElements('section', $key); + return self::countElements('section', $key); } /** @@ -219,7 +224,7 @@ class Media */ public static function addHeaderMediaElement($headerCount, $src, Image $image = null) { - return self::addMediaElement("header{$headerCount}", 'image', $src, $image); + return self::addElement("header{$headerCount}", 'image', $src, $image); } /** @@ -232,7 +237,7 @@ class Media */ public static function countHeaderMediaElements($key) { - return self::countMediaElements($key); + return self::countElements($key); } /** @@ -244,7 +249,7 @@ class Media */ public static function getHeaderMediaElements() { - return self::getMediaElements('header'); + return self::getElements('header'); } /** @@ -259,7 +264,7 @@ class Media */ public static function addFooterMediaElement($footerCount, $src, Image $image = null) { - return self::addMediaElement("footer{$footerCount}", 'image', $src, $image); + return self::addElement("footer{$footerCount}", 'image', $src, $image); } /** @@ -272,7 +277,7 @@ class Media */ public static function countFooterMediaElements($key) { - return self::countMediaElements($key); + return self::countElements($key); } /** @@ -284,6 +289,6 @@ class Media */ public static function getFooterMediaElements() { - return self::getMediaElements('footer'); + return self::getElements('footer'); } } diff --git a/src/PhpWord/Writer/ODText/Manifest.php b/src/PhpWord/Writer/ODText/Manifest.php index 3712cf66..ecda1918 100755 --- a/src/PhpWord/Writer/ODText/Manifest.php +++ b/src/PhpWord/Writer/ODText/Manifest.php @@ -63,22 +63,4 @@ class Manifest extends WriterPart // Return return $xmlWriter->getData(); } - - - /** - * Get image mime type - * - * @param string $pFile Filename - * @return string Mime Type - * @throws Exception - */ - private function getImageMimeType($pFile = '') - { - if (file_exists($pFile)) { - $image = getimagesize($pFile); - return image_type_to_mime_type($image[2]); - } else { - throw new Exception("File $pFile does not exist"); - } - } } diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index 084deb8d..ba887f01 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -29,18 +29,18 @@ use PhpOffice\PhpWord\Writer\Word2007\Styles; class Word2007 extends Writer implements IWriter { /** - * Types of images + * Content types values * * @var array */ - private $imageTypes = array(); + private $cTypes = array('default' => array(), 'override' => array()); /** - * Types of objects + * Document relationship * * @var array */ - private $objectTypes = array(); + private $docRels = array(); /** * Create new Word2007 writer @@ -98,14 +98,18 @@ class Word2007 extends Writer implements IWriter } } - // Add section media files - $sectionElements = array(); + // Content types + $this->cTypes['default'] = array( + 'rels' => 'application/vnd.openxmlformats-package.relationships+xml', + 'xml' => 'application/xml', + ); - $secElements = Media::getMediaElements('section'); - if (!empty($secElements)) { - $this->addFilesToPackage($objZip, $secElements); - foreach ($secElements as $element) { - $sectionElements[] = $element; + // Add section media files + $sectionMedia = Media::getElements('section'); + if (!empty($sectionMedia)) { + $this->addFilesToPackage($objZip, $sectionMedia); + foreach ($sectionMedia as $element) { + $this->docRels[] = $element; } } @@ -114,70 +118,32 @@ class Word2007 extends Writer implements IWriter $this->addHeaderFooterMedia($objZip, 'footer'); // Add header/footer contents - $cHdrs = 0; - $cFtrs = 0; - $rID = Media::countMediaElements('section') + 6; // @see Rels::writeDocRels for 6 first elements + $overrides = array(); + $rID = Media::countElements('section') + 6; // @see Rels::writeDocRels for 6 first elements $sections = $this->phpWord->getSections(); - $footers = array(); foreach ($sections as $section) { - $headers = $section->getHeaders(); - if (!empty($headers)) { - foreach ($headers as $index => &$header) { - $cHdrs++; - $header->setRelationId(++$rID); - $hdrFile = "header{$cHdrs}.xml"; - $sectionElements[] = array('target' => $hdrFile, 'type' => 'header', 'rID' => $rID); - $objZip->addFromString( - "word/{$hdrFile}", - $this->getWriterPart('header')->writeHeader($header) - ); - } - } - $footer = $section->getFooter(); - $footers[++$cFtrs] = $footer; - if (!is_null($footer)) { - $footer->setRelationId(++$rID); - $footerCount = $footer->getSectionId(); - $ftrFile = "footer{$footerCount}.xml"; - $sectionElements[] = array('target' => $ftrFile, 'type' => 'footer', 'rID' => $rID); - $objZip->addFromString( - "word/{$ftrFile}", - $this->getWriterPart('footer')->writeFooter($footer) - ); - } + $this->addHeaderFooterContent($section, $objZip, 'header', $rID); + $this->addHeaderFooterContent($section, $objZip, 'footer', $rID); } // Add footnotes media files, relations, and contents if (Footnote::countFootnoteElements() > 0) { - $sectionElements[] = array('target' => 'footnotes.xml', 'type' => 'footnotes', 'rID' => ++$rID); - $footnoteMedia = Media::getMediaElements('footnote'); + $footnoteMedia = Media::getElements('footnote'); $this->addFilesToPackage($objZip, $footnoteMedia); if (!empty($footnoteMedia)) { - $objZip->addFromString( - 'word/_rels/footnotes.xml.rels', - $this->getWriterPart('rels')->writeMediaRels($footnoteMedia) - ); + $objZip->addFromString('word/_rels/footnotes.xml.rels', $this->getWriterPart('rels')->writeMediaRels($footnoteMedia)); } - $objZip->addFromString( - 'word/footnotes.xml', - $this->getWriterPart('footnotes')->writeFootnotes(Footnote::getFootnoteElements()) - ); + $objZip->addFromString('word/footnotes.xml', $this->getWriterPart('footnotes')->writeFootnotes(Footnote::getFootnoteElements())); + $this->cTypes['override']["/word/footnotes.xml"] = 'footnotes'; + $this->docRels[] = array('target' => 'footnotes.xml', 'type' => 'footnotes', 'rID' => ++$rID); } // Write dynamic files - $objZip->addFromString( - '[Content_Types].xml', - $this->getWriterPart('contenttypes')->writeContentTypes( - $this->imageTypes, - $this->objectTypes, - $cHdrs, - $footers - ) - ); + $objZip->addFromString('[Content_Types].xml', $this->getWriterPart('contenttypes')->writeContentTypes($this->cTypes)); $objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeMainRels()); $objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->phpWord)); $objZip->addFromString('docProps/core.xml', $this->getWriterPart('docprops')->writeDocPropsCore($this->phpWord)); - $objZip->addFromString('word/_rels/document.xml.rels', $this->getWriterPart('rels')->writeDocRels($sectionElements)); + $objZip->addFromString('word/_rels/document.xml.rels', $this->getWriterPart('rels')->writeDocRels($this->docRels)); $objZip->addFromString('word/document.xml', $this->getWriterPart('document')->writeDocument($this->phpWord)); $objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->phpWord)); @@ -200,54 +166,7 @@ class Word2007 extends Writer implements IWriter } /** - * Check content types - * - * @param string $src - */ - private function checkContentTypes($src) - { - $extension = null; - if (stripos(strrev($src), strrev('.php')) === 0) { - $extension = 'php'; - } else { - if (function_exists('exif_imagetype')) { - $imageType = exif_imagetype($src); - } else { - $tmp = getimagesize($src); - $imageType = $tmp[2]; - } - if ($imageType === \IMAGETYPE_JPEG) { - $extension = 'jpg'; - } elseif ($imageType === \IMAGETYPE_GIF) { - $extension = 'gif'; - } elseif ($imageType === \IMAGETYPE_PNG) { - $extension = 'png'; - } elseif ($imageType === \IMAGETYPE_BMP) { - $extension = 'bmp'; - } elseif ($imageType === \IMAGETYPE_TIFF_II || $imageType === \IMAGETYPE_TIFF_MM) { - $extension = 'tif'; - } - } - - if (isset($extension)) { - $imageData = getimagesize($src); - $imageType = image_type_to_mime_type($imageData[2]); - $imageExtension = str_replace('.', '', image_type_to_extension($imageData[2])); - if ($imageExtension === 'jpeg') { - $imageExtension = 'jpg'; - } - if (!in_array($imageType, $this->imageTypes)) { - $this->imageTypes[$imageExtension] = $imageType; - } - } else { - if (!in_array($extension, $this->objectTypes)) { - $this->objectTypes[] = $extension; - } - } - } - - /** - * Check content types + * Add section files to package * * @param mixed $objZip * @param mixed $elements @@ -255,47 +174,78 @@ class Word2007 extends Writer implements IWriter private function addFilesToPackage($objZip, $elements) { foreach ($elements as $element) { + // Do not add link if ($element['type'] == 'link') { continue; } + // Retrieve remote image if (isset($element['isMemImage']) && $element['isMemImage']) { - $image = call_user_func($element['createfunction'], $element['source']); + $image = call_user_func($element['createFunction'], $element['source']); ob_start(); - call_user_func($element['imagefunction'], $image); + call_user_func($element['imageFunction'], $image); $imageContents = ob_get_contents(); ob_end_clean(); $objZip->addFromString('word/' . $element['target'], $imageContents); imagedestroy($image); - - $this->checkContentTypes($element['source']); } else { $objZip->addFile($element['source'], 'word/' . $element['target']); - $this->checkContentTypes($element['source']); + } + // Register content types + if ($element['type'] == 'image') { + $imageExtension = $element['imageExtension']; + $imageType = $element['imageType']; + if (!array_key_exists($imageExtension, $this->cTypes['default'])) { + $this->cTypes['default'][$imageExtension] = $imageType; + } + } else { + if (!array_key_exists('bin', $this->cTypes['default'])) { + $this->cTypes['default']['bin'] = 'application/vnd.openxmlformats-officedocument.oleObject'; + } } } } /** - * Add header/footer media elements + * Add header/footer media files * * @param mixed $objZip * @param string $docPart */ private function addHeaderFooterMedia($objZip, $docPart) { - $elements = Media::getMediaElements($docPart); + $elements = Media::getElements($docPart); if (!empty($elements)) { foreach ($elements as $file => $media) { if (count($media) > 0) { - $objZip->addFromString( - 'word/_rels/' . $file . '.xml.rels', - $this->getWriterPart('rels')->writeMediaRels($media) - ); if (!empty($media)) { $this->addFilesToPackage($objZip, $media); } + $objZip->addFromString("word/_rels/{$file}.xml.rels", $this->getWriterPart('rels')->writeMediaRels($media)); } } } } + + /** + * Add header/footer content + * + * @param Section $section + * @param string $elmType + * @param integer $rID + */ + private function addHeaderFooterContent(&$section, $objZip, $elmType, &$rID) + { + $getFunction = $elmType == 'header' ? 'getHeaders' : 'getFooters'; + $writeFunction = $elmType == 'header' ? 'writeHeader' : 'writeFooter'; + $elmCount = ($section->getSectionId() - 1) * 3; + $elmObjects = $section->$getFunction(); + foreach ($elmObjects as $index => &$elmObject) { + $elmCount++; + $elmObject->setRelationId(++$rID); + $elmFile = "{$elmType}{$elmCount}.xml"; + $objZip->addFromString("word/$elmFile", $this->getWriterPart($elmType)->$writeFunction($elmObject)); + $this->cTypes['override']["/word/$elmFile"] = $elmType; + $this->docRels[] = array('target' => $elmFile, 'type' => $elmType, 'rID' => $rID); + } + } } diff --git a/src/PhpWord/Writer/Word2007/ContentTypes.php b/src/PhpWord/Writer/Word2007/ContentTypes.php index 9a383e9b..7b371b07 100755 --- a/src/PhpWord/Writer/Word2007/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/ContentTypes.php @@ -19,28 +19,13 @@ class ContentTypes extends WriterPart { /** * Write [Content_Types].xml - * @param array $imageTypes - * @param array $objectTypes - * @param int $cHdrs - * @param array $footers + * + * @param array $contentTypes */ - public function writeContentTypes($imageTypes, $objectTypes, $cHdrs, $footers) + public function writeContentTypes($contentTypes) { - $OpenXMLPrefix = 'application/vnd.openxmlformats-'; - $WordMLPrefix = $OpenXMLPrefix . 'officedocument.wordprocessingml.'; - - $defaults = array( - 'rels' => $OpenXMLPrefix . 'package.relationships+xml', - 'xml' => 'application/xml', - - ); - if (is_array($imageTypes)) { - $defaults = array_merge($defaults, $imageTypes); - } - if (count($objectTypes) > 0) { - $defaults['bin'] = $OpenXMLPrefix . 'officedocument.oleObject'; - } + $WordMLPrefix = $OpenXMLPrefix . 'officedocument.wordprocessingml.'; $overrides = array( '/docProps/core.xml' => $OpenXMLPrefix . 'package.core-properties+xml', '/docProps/app.xml' => $OpenXMLPrefix . 'officedocument.extended-properties+xml', @@ -51,14 +36,12 @@ class ContentTypes extends WriterPart '/word/theme/theme1.xml' => $OpenXMLPrefix . 'officedocument.theme+xml', '/word/webSettings.xml' => $WordMLPrefix . 'webSettings+xml', '/word/fontTable.xml' => $WordMLPrefix . 'fontTable+xml', - '/word/footnotes.xml' => $WordMLPrefix . 'footnotes+xml', ); - for ($i = 1; $i <= $cHdrs; $i++) { - $overrides["/word/header{$i}.xml"] = $WordMLPrefix . 'header+xml'; - } - for ($i = 1; $i <= count($footers); $i++) { - if (!is_null($footers[$i])) { - $overrides["/word/footer{$i}.xml"] = $WordMLPrefix . 'footer+xml'; + + $defaults = $contentTypes['default']; + if (!empty($contentTypes['override'])) { + foreach ($contentTypes['override'] as $key => $val) { + $overrides[$key] = $WordMLPrefix . $val . '+xml'; } } @@ -66,12 +49,8 @@ class ContentTypes extends WriterPart $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); $xmlWriter->startElement('Types'); $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types'); - foreach ($defaults as $key => $value) { - $this->writeContentType($xmlWriter, true, $key, $value); - } - foreach ($overrides as $key => $value) { - $this->writeContentType($xmlWriter, false, $key, $value); - } + $this->writeContentType($xmlWriter, $defaults, true); + $this->writeContentType($xmlWriter, $overrides, false); $xmlWriter->endElement(); return $xmlWriter->getData(); @@ -86,62 +65,19 @@ class ContentTypes extends WriterPart * @param string $contentType Content type * @throws Exception */ - private function writeContentType(XMLWriter $xmlWriter, $isDefault, $partName = '', $contentType = '') + private function writeContentType(XMLWriter $xmlWriter, $parts, $isDefault) { - if ($partName != '' && $contentType != '') { - $element = $isDefault ? 'Default' : 'Override'; - $partAttribute = $isDefault ? 'Extension' : 'PartName'; - $xmlWriter->startElement($element); - $xmlWriter->writeAttribute($partAttribute, $partName); - $xmlWriter->writeAttribute('ContentType', $contentType); - $xmlWriter->endElement(); - } else { - throw new Exception("Invalid parameters passed."); + foreach ($parts as $partName => $contentType) { + if ($partName != '' && $contentType != '') { + $partType = $isDefault ? 'Default' : 'Override'; + $partAttribute = $isDefault ? 'Extension' : 'PartName'; + $xmlWriter->startElement($partType); + $xmlWriter->writeAttribute($partAttribute, $partName); + $xmlWriter->writeAttribute('ContentType', $contentType); + $xmlWriter->endElement(); + } else { + throw new Exception("Invalid parameters passed."); + } } } - - /** - * Get image mime type - * - * @param string $pFile Filename - * @return string Mime Type - * @throws Exception - */ - private function getImageMimeType($pFile = '') - { - if (file_exists($pFile)) { - $image = getimagesize($pFile); - return image_type_to_mime_type($image[2]); - } else { - throw new Exception("File $pFile does not exist"); - } - } - - /** - * Write Default XML element - * - * @param XMLWriter $xmlWriter - * @param string $partName Part name - * @param string $contentType Content type - * @deprecated 0.9.2 - * @codeCoverageIgnore - */ - private function writeDefaultContentType(XMLWriter $xmlWriter, $partName = '', $contentType = '') - { - $this->writeContentType($xmlWriter, true, $partName, $contentType); - } - - /** - * Write Override XML element - * - * @param XMLWriter $xmlWriter - * @param string $partName Part name - * @param string $contentType Content type - * @deprecated 0.9.2 - * @codeCoverageIgnore - */ - private function writeOverrideContentType(XMLWriter $xmlWriter, $partName = '', $contentType = '') - { - $this->writeContentType($xmlWriter, false, $partName, $contentType); - } } diff --git a/src/PhpWord/Writer/Word2007/Document.php b/src/PhpWord/Writer/Word2007/Document.php index 499aad19..98f158fc 100644 --- a/src/PhpWord/Writer/Word2007/Document.php +++ b/src/PhpWord/Writer/Word2007/Document.php @@ -103,7 +103,7 @@ class Document extends Base { $settings = $section->getSettings(); $headers = $section->getHeaders(); - $footer = $section->getFooter(); + $footers = $section->getFooters(); $pgSzW = $settings->getPageSizeW(); $pgSzH = $settings->getPageSizeH(); $orientation = $settings->getOrientation(); @@ -139,19 +139,19 @@ class Document extends Base $xmlWriter->writeAttribute('r:id', 'rId' . $rId); $xmlWriter->endElement(); } - if ($section->hasDifferentFirstPage()) { - $xmlWriter->startElement('w:titlePg'); - $xmlWriter->endElement(); - } - // Footer reference - if (!is_null($footer)) { + foreach ($footers as &$footer) { $rId = $footer->getRelationId(); $xmlWriter->startElement('w:footerReference'); - $xmlWriter->writeAttribute('w:type', 'default'); + $xmlWriter->writeAttribute('w:type', $footer->getType()); $xmlWriter->writeAttribute('r:id', 'rId' . $rId); $xmlWriter->endElement(); } + // Different first page + if ($section->hasDifferentFirstPage()) { + $xmlWriter->startElement('w:titlePg'); + $xmlWriter->endElement(); + } // Page size & orientation $xmlWriter->startElement('w:pgSz'); diff --git a/src/PhpWord/Writer/Word2007/Rels.php b/src/PhpWord/Writer/Word2007/Rels.php index bbf0634c..d7552410 100755 --- a/src/PhpWord/Writer/Word2007/Rels.php +++ b/src/PhpWord/Writer/Word2007/Rels.php @@ -29,13 +29,13 @@ class Rels extends WriterPart */ public function writeMainRels() { - $rels = array( - 'word/document.xml' => 'officeDocument/2006/relationships/officeDocument', + $xmlRels = array( 'docProps/core.xml' => 'package/2006/relationships/metadata/core-properties', 'docProps/app.xml' => 'officeDocument/2006/relationships/extended-properties', + 'word/document.xml' => 'officeDocument/2006/relationships/officeDocument', ); $xmlWriter = $this->getXmlWriter(); - $this->writeRels($xmlWriter, $rels); + $this->writeRels($xmlWriter, $xmlRels); return $xmlWriter->getData(); } @@ -47,7 +47,7 @@ class Rels extends WriterPart */ public function writeDocRels($mediaRels) { - $rels = array( + $xmlRels = array( 'styles.xml' => 'officeDocument/2006/relationships/styles', 'numbering.xml' => 'officeDocument/2006/relationships/numbering', 'settings.xml' => 'officeDocument/2006/relationships/settings', @@ -56,7 +56,7 @@ class Rels extends WriterPart 'fontTable.xml' => 'officeDocument/2006/relationships/fontTable', ); $xmlWriter = $this->getXmlWriter(); - $this->writeRels($xmlWriter, $rels, $mediaRels); + $this->writeRels($xmlWriter, $xmlRels, $mediaRels); return $xmlWriter->getData(); } @@ -79,20 +79,24 @@ class Rels extends WriterPart * Write relationships * * @param XMLWriter $xmlWriter - * @param null|array $rels + * @param null|array $xmlRels * @param null|array $mediaRels * @param integer $id */ - private function writeRels(XMLWriter $xmlWriter, $rels = null, $mediaRels = null, $id = 1) + private function writeRels(XMLWriter $xmlWriter, $xmlRels = null, $mediaRels = null, $id = 1) { $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); $xmlWriter->startElement('Relationships'); $xmlWriter->writeAttribute('xmlns', self::RELS_BASE . 'package/2006/relationships'); - if (is_array($rels)) { - foreach ($rels as $target => $type) { + + // XML files relationships + if (is_array($xmlRels)) { + foreach ($xmlRels as $target => $type) { $this->writeRel($xmlWriter, $id++, $type, $target); } } + + // Media relationships if (!is_null($mediaRels) && is_array($mediaRels)) { $mapping = array('image' => 'image', 'object' => 'oleObject', 'link' => 'hyperlink'); foreach ($mediaRels as $mediaRel) { diff --git a/src/PhpWord/Writer/Word2007/Styles.php b/src/PhpWord/Writer/Word2007/Styles.php index c7b35faf..f0305f7f 100644 --- a/src/PhpWord/Writer/Word2007/Styles.php +++ b/src/PhpWord/Writer/Word2007/Styles.php @@ -28,6 +28,10 @@ class Styles extends Base */ public function writeStyles(PhpWord $phpWord = null) { + if (is_null($phpWord)) { + throw new Exception("No PhpWord assigned."); + } + // Create XML writer $xmlWriter = $this->getXmlWriter(); diff --git a/tests/PhpWord/Tests/Container/SectionTest.php b/tests/PhpWord/Tests/Container/SectionTest.php index 268f66e8..49b59b20 100644 --- a/tests/PhpWord/Tests/Container/SectionTest.php +++ b/tests/PhpWord/Tests/Container/SectionTest.php @@ -40,10 +40,10 @@ class SectionTest extends \PHPUnit_Framework_TestCase /** * Get footer */ - public function testGetFooter() + public function testGetFooters() { $oSection = new Section(0); - $this->assertAttributeEquals($oSection->getFooter(), 'footer', new Section(0)); + $this->assertAttributeEquals($oSection->getFooters(), 'footers', new Section(0)); } /** diff --git a/tests/PhpWord/Tests/MediaTest.php b/tests/PhpWord/Tests/MediaTest.php index 3df52e4a..14a38c2d 100644 --- a/tests/PhpWord/Tests/MediaTest.php +++ b/tests/PhpWord/Tests/MediaTest.php @@ -25,7 +25,7 @@ class MediaTest extends \PHPUnit_Framework_TestCase */ public function testGetSectionMediaElementsWithNull() { - $this->assertEquals(Media::getMediaElements('section'), array()); + $this->assertEquals(Media::getElements('section'), array()); } /** @@ -33,7 +33,7 @@ class MediaTest extends \PHPUnit_Framework_TestCase */ public function testCountSectionMediaElementsWithNull() { - $this->assertEquals(Media::countMediaElements('section'), 0); + $this->assertEquals(Media::countElements('section'), 0); } /** @@ -44,13 +44,13 @@ class MediaTest extends \PHPUnit_Framework_TestCase $local = __DIR__ . "/_files/images/mars.jpg"; $object = __DIR__ . "/_files/documents/sheet.xls"; $remote = 'http://php.net/images/logos/php-med-trans-light.gif'; - Media::addMediaElement('section', 'image', $local, new Image($local)); - Media::addMediaElement('section', 'image', $local, new Image($local)); - Media::addMediaElement('section', 'image', $remote, new Image($local)); - Media::addMediaElement('section', 'object', $object); - Media::addMediaElement('section', 'object', $object); + Media::addElement('section', 'image', $local, new Image($local)); + Media::addElement('section', 'image', $local, new Image($local)); + Media::addElement('section', 'image', $remote, new Image($local)); + Media::addElement('section', 'object', $object); + Media::addElement('section', 'object', $object); - $this->assertEquals(3, Media::countMediaElements('section')); + $this->assertEquals(3, Media::countElements('section')); } /** @@ -58,12 +58,12 @@ class MediaTest extends \PHPUnit_Framework_TestCase */ public function testAddSectionLinkElement() { - $expected = Media::countMediaElements('section') + 1; - $actual = Media::addMediaElement('section', 'link', 'http://test.com'); + $expected = Media::countElements('section') + 1; + $actual = Media::addElement('section', 'link', 'http://test.com'); $this->assertEquals($expected, $actual); - $this->assertEquals(1, Media::countMediaElements('section', 'link')); - $this->assertEquals(1, count(Media::getMediaElements('section', 'link'))); + $this->assertEquals(1, Media::countElements('section', 'link')); + $this->assertEquals(1, count(Media::getElements('section', 'link'))); } /** @@ -73,13 +73,13 @@ class MediaTest extends \PHPUnit_Framework_TestCase { $local = __DIR__ . "/_files/images/mars.jpg"; $remote = 'http://php.net/images/logos/php-med-trans-light.gif'; - Media::addMediaElement('header1', 'image', $local, new Image($local)); - Media::addMediaElement('header1', 'image', $local, new Image($local)); - Media::addMediaElement('header1', 'image', $remote, new Image($remote)); + Media::addElement('header1', 'image', $local, new Image($local)); + Media::addElement('header1', 'image', $local, new Image($local)); + Media::addElement('header1', 'image', $remote, new Image($remote)); - $this->assertEquals(2, Media::countMediaElements('header1')); - $this->assertEquals(2, count(Media::getMediaElements('header1'))); - $this->assertEmpty(Media::getMediaElements('header2')); + $this->assertEquals(2, Media::countElements('header1')); + $this->assertEquals(2, count(Media::getElements('header1'))); + $this->assertEmpty(Media::getElements('header2')); } /** @@ -89,13 +89,24 @@ class MediaTest extends \PHPUnit_Framework_TestCase { $local = __DIR__ . "/_files/images/mars.jpg"; $remote = 'http://php.net/images/logos/php-med-trans-light.gif'; - Media::addMediaElement('footer1', 'image', $local, new Image($local)); - Media::addMediaElement('footer1', 'image', $local, new Image($local)); - Media::addMediaElement('footer1', 'image', $remote, new Image($remote)); + Media::addElement('footer1', 'image', $local, new Image($local)); + Media::addElement('footer1', 'image', $local, new Image($local)); + Media::addElement('footer1', 'image', $remote, new Image($remote)); - $this->assertEquals(2, Media::countMediaElements('footer1')); + $this->assertEquals(2, Media::countElements('footer1')); Media::reset(); - $this->assertEquals(0, Media::countMediaElements('footer1')); + $this->assertEquals(0, Media::countElements('footer1')); + } + + /** + * Add image element exception + * + * @expectedException Exception + * @expectedExceptionMessage Image object not assigned. + */ + public function testAddElementImageException() + { + Media::addElement('section', 'image', __DIR__ . "/_files/images/mars.jpg"); } } From 8d2c6ebd165b08086b0ae4e84d26517c3c6c9623 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 6 Apr 2014 11:52:16 +0700 Subject: [PATCH 21/27] Fix TOCTest error --- tests/PhpWord/Tests/TOCTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PhpWord/Tests/TOCTest.php b/tests/PhpWord/Tests/TOCTest.php index a5f509c9..2b9d50e7 100644 --- a/tests/PhpWord/Tests/TOCTest.php +++ b/tests/PhpWord/Tests/TOCTest.php @@ -81,7 +81,7 @@ class TOCTest extends \PHPUnit_Framework_TestCase } TOC::reset(); - $this->assertEquals(0, count(TOC::getTitles())); + $this->assertEquals(0, count($toc->getTitles())); } From a218202dbdbe1d9f5d3da2d43598d4efff71194a Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 6 Apr 2014 15:19:09 +0700 Subject: [PATCH 22/27] Enhance unit tests --- src/PhpWord/Container/Section.php | 2 ++ src/PhpWord/Element/Image.php | 13 +++++++--- src/PhpWord/TOC.php | 2 ++ src/PhpWord/Writer/Word2007.php | 3 ++- src/PhpWord/Writer/Word2007/ContentTypes.php | 3 +-- tests/PhpWord/Tests/Container/FooterTest.php | 2 ++ tests/PhpWord/Tests/Container/HeaderTest.php | 12 +++++++++ tests/PhpWord/Tests/Container/SectionTest.php | 25 +++++++++++++++++++ tests/PhpWord/Tests/Element/ImageTest.php | 10 ++++++++ 9 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/PhpWord/Container/Section.php b/src/PhpWord/Container/Section.php index 01e7fafe..26823df4 100644 --- a/src/PhpWord/Container/Section.php +++ b/src/PhpWord/Container/Section.php @@ -97,6 +97,8 @@ class Section extends Container * * @param mixed $styleFont * @param mixed $styleTOC + * @param integer $minDepth + * @param integer $maxDepth * @return TOC */ public function addTOC($styleFont = null, $styleTOC = null, $minDepth = 1, $maxDepth = 9) diff --git a/src/PhpWord/Element/Image.php b/src/PhpWord/Element/Image.php index ec82665e..fec8791c 100755 --- a/src/PhpWord/Element/Image.php +++ b/src/PhpWord/Element/Image.php @@ -102,10 +102,13 @@ class Image extends Element // Check supported types if ($this->isMemImage) { $supportedTypes = array('image/jpeg', 'image/gif', 'image/png'); - $imgData = getimagesize($source); + $imgData = @getimagesize($source); + if (!is_array($imgData)) { + throw new InvalidImageException(); + } $this->imageType = $imgData['mime']; // string if (!in_array($this->imageType, $supportedTypes)) { - throw new UnsupportedImageTypeException; + throw new UnsupportedImageTypeException(); } } else { $supportedTypes = array( @@ -114,17 +117,19 @@ class Image extends Element \IMAGETYPE_TIFF_II, \IMAGETYPE_TIFF_MM ); if (!file_exists($source)) { - throw new InvalidImageException; + throw new InvalidImageException(); } $imgData = getimagesize($source); if (function_exists('exif_imagetype')) { $this->imageType = exif_imagetype($source); } else { + // @codeCoverageIgnoreStart $tmp = getimagesize($source); $this->imageType = $tmp[2]; + // @codeCoverageIgnoreEnd } if (!in_array($this->imageType, $supportedTypes)) { - throw new UnsupportedImageTypeException; + throw new UnsupportedImageTypeException(); } $this->imageType = \image_type_to_mime_type($this->imageType); } diff --git a/src/PhpWord/TOC.php b/src/PhpWord/TOC.php index a992cd55..2793d2f0 100644 --- a/src/PhpWord/TOC.php +++ b/src/PhpWord/TOC.php @@ -73,6 +73,8 @@ class TOC * * @param mixed $styleFont * @param array $styleTOC + * @param integer $minDepth + * @param integer $maxDepth */ public function __construct($styleFont = null, $styleTOC = null, $minDepth = 1, $maxDepth = 9) { diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index f7b49cfe..8fe90339 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -229,7 +229,8 @@ class Word2007 extends Writer implements IWriter /** * Add header/footer content * - * @param PhpOffice\PhpWord\Container\Section $section + * @param \PhpOffice\PhpWord\Container\Section $section + * @param mixed $objZip * @param string $elmType * @param integer $rID */ diff --git a/src/PhpWord/Writer/Word2007/ContentTypes.php b/src/PhpWord/Writer/Word2007/ContentTypes.php index 7b371b07..8eb10bc1 100755 --- a/src/PhpWord/Writer/Word2007/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/ContentTypes.php @@ -60,9 +60,8 @@ class ContentTypes extends WriterPart * Write content types element * * @param XMLWriter $xmlWriter XML Writer + * @param array $parts * @param boolean $isDefault - * @param string $partName Part name - * @param string $contentType Content type * @throws Exception */ private function writeContentType(XMLWriter $xmlWriter, $parts, $isDefault) diff --git a/tests/PhpWord/Tests/Container/FooterTest.php b/tests/PhpWord/Tests/Container/FooterTest.php index bcbf45a9..db982dc5 100644 --- a/tests/PhpWord/Tests/Container/FooterTest.php +++ b/tests/PhpWord/Tests/Container/FooterTest.php @@ -162,6 +162,8 @@ class FooterTest extends \PHPUnit_Framework_TestCase $iVal = rand(1, 1000); $oFooter->setRelationId($iVal); + $this->assertEquals($oFooter->getRelationId(), $iVal); + $this->assertEquals(Footer::AUTO, $oFooter->getType()); } } diff --git a/tests/PhpWord/Tests/Container/HeaderTest.php b/tests/PhpWord/Tests/Container/HeaderTest.php index 9d13d57c..ef1703f3 100644 --- a/tests/PhpWord/Tests/Container/HeaderTest.php +++ b/tests/PhpWord/Tests/Container/HeaderTest.php @@ -231,4 +231,16 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $header = new Header(1); $header->addFootnote(); } + + /** + * Set/get type + */ + public function testSetGetType() + { + $object = new Header(1); + $this->assertEquals(Header::AUTO, $object->getType()); + + $object->setType('ODD'); + $this->assertEquals(Header::AUTO, $object->getType()); + } } diff --git a/tests/PhpWord/Tests/Container/SectionTest.php b/tests/PhpWord/Tests/Container/SectionTest.php index 49b59b20..e077ff82 100644 --- a/tests/PhpWord/Tests/Container/SectionTest.php +++ b/tests/PhpWord/Tests/Container/SectionTest.php @@ -9,7 +9,9 @@ namespace PhpOffice\PhpWord\Tests\Container; +use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Container\Section; +use PhpOffice\PhpWord\Container\Header; use PhpOffice\PhpWord\Style; /** @@ -141,4 +143,27 @@ class SectionTest extends \PHPUnit_Framework_TestCase } $this->assertFalse($object->hasDifferentFirstPage()); } + + /** + * Add header has different first page + */ + public function testHasDifferentFirstPage() + { + $object = new Section(1); + $header = $object->addHeader(); + $header->setType(Header::FIRST); + $this->assertTrue($object->hasDifferentFirstPage()); + } + + /** + * Add header exception + * + * @expectedException Exception + * @expectedExceptionMesssage Invalid header/footer type. + */ + public function testAddHeaderException() + { + $object = new Section(1); + $header = $object->addHeader('ODD'); + } } diff --git a/tests/PhpWord/Tests/Element/ImageTest.php b/tests/PhpWord/Tests/Element/ImageTest.php index b0c3d88c..5b9aaa45 100644 --- a/tests/PhpWord/Tests/Element/ImageTest.php +++ b/tests/PhpWord/Tests/Element/ImageTest.php @@ -192,4 +192,14 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oImage->getImageFunction(), null); $this->assertEquals($oImage->getImageType(), 'image/tiff'); } + + /** + * Test PHP Image + * + * @expectedException \PhpOffice\PhpWord\Exception\InvalidImageException + */ + public function testPhpImage() + { + $object = new Image('test.php'); + } } From 177c523799664251de93289170e26f488b19823b Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 6 Apr 2014 18:03:03 +0700 Subject: [PATCH 23/27] Remove underscore prefix from all private properties name --- CHANGELOG.md | 1 + src/PhpWord/Container/Section.php | 4 +- src/PhpWord/Container/Settings.php | 181 ++++++++++---------- src/PhpWord/Element/Element.php | 4 +- src/PhpWord/Style.php | 4 +- src/PhpWord/Style/Cell.php | 129 +++++++------- src/PhpWord/Style/Font.php | 103 +++++------ src/PhpWord/Style/Image.php | 43 ++--- src/PhpWord/Style/ListItem.php | 11 +- src/PhpWord/Style/Paragraph.php | 92 +++++----- src/PhpWord/Style/Row.php | 21 ++- src/PhpWord/Style/TOC.php | 24 +-- src/PhpWord/Style/Tab.php | 28 +-- src/PhpWord/Style/Table.php | 180 +++++++++---------- src/PhpWord/Style/Tabs.php | 6 +- src/PhpWord/TOC.php | 8 +- tests/PhpWord/Tests/Style/CellTest.php | 6 +- tests/PhpWord/Tests/Style/FontTest.php | 4 +- tests/PhpWord/Tests/Style/ImageTest.php | 2 +- tests/PhpWord/Tests/Style/ListItemTest.php | 2 +- tests/PhpWord/Tests/Style/ParagraphTest.php | 6 +- tests/PhpWord/Tests/Style/RowTest.php | 2 +- tests/PhpWord/Tests/Style/TOCTest.php | 2 +- tests/PhpWord/Tests/Style/TableTest.php | 6 +- 24 files changed, 446 insertions(+), 423 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b1ea897..5c3ed441 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - Container: Create new Container abstract class - @ivanlanin GH-187 - Element: Create new Element abstract class - @ivanlanin GH-187 - Media: Refactor media class to use one method for all docPart (section, header, footer, footnote) - @ivanlanin GH-187 +- General: Remove underscore prefix from all private properties name ## 0.9.1 - 27 Mar 2014 diff --git a/src/PhpWord/Container/Section.php b/src/PhpWord/Container/Section.php index 26823df4..dae4f26c 100644 --- a/src/PhpWord/Container/Section.php +++ b/src/PhpWord/Container/Section.php @@ -66,8 +66,8 @@ class Section extends Container { if (!is_null($settings) && is_array($settings)) { foreach ($settings as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; + if (substr($key, 0, 1) == '_') { + $key = substr($key, 1); } $this->settings->setSettingValue($key, $value); } diff --git a/src/PhpWord/Container/Settings.php b/src/PhpWord/Container/Settings.php index daf5d13b..762d45c3 100644 --- a/src/PhpWord/Container/Settings.php +++ b/src/PhpWord/Container/Settings.php @@ -19,119 +19,119 @@ class Settings * * @var int */ - private $_defaultPageSizeW = 11906; + private $defaultPageSizeW = 11906; /** * Default Page Size Height * * @var int */ - private $_defaultPageSizeH = 16838; + private $defaultPageSizeH = 16838; /** * Page Orientation * * @var string */ - private $_orientation; + private $orientation; /** * Page Margin Top * * @var int */ - private $_marginTop; + private $marginTop; /** * Page Margin Left * * @var int */ - private $_marginLeft; + private $marginLeft; /** * Page Margin Right * * @var int */ - private $_marginRight; + private $marginRight; /** * Page Margin Bottom * * @var int */ - private $_marginBottom; + private $marginBottom; /** * Page Size Width * * @var int */ - private $_pageSizeW; + private $pageSizeW; /** * Page Size Height * * @var int */ - private $_pageSizeH; + private $pageSizeH; /** * Page Border Top Size * * @var int */ - private $_borderTopSize; + private $borderTopSize; /** * Page Border Top Color * * @var int */ - private $_borderTopColor; + private $borderTopColor; /** * Page Border Left Size * * @var int */ - private $_borderLeftSize; + private $borderLeftSize; /** * Page Border Left Color * * @var int */ - private $_borderLeftColor; + private $borderLeftColor; /** * Page Border Right Size * * @var int */ - private $_borderRightSize; + private $borderRightSize; /** * Page Border Right Color * * @var int */ - private $_borderRightColor; + private $borderRightColor; /** * Page Border Bottom Size * * @var int */ - private $_borderBottomSize; + private $borderBottomSize; /** * Page Border Bottom Color * * @var int */ - private $_borderBottomColor; + private $borderBottomColor; /** * Page Numbering Start @@ -159,14 +159,14 @@ class Settings * * @var int */ - private $_colsNum; + private $colsNum; /** * Section spacing between columns * * @var int */ - private $_colsSpace; + private $colsSpace; /** * Section break type @@ -180,33 +180,33 @@ class Settings * * @var string */ - private $_breakType; + private $breakType; /** * Create new Section Settings */ public function __construct() { - $this->_orientation = null; - $this->_marginTop = 1418; - $this->_marginLeft = 1418; - $this->_marginRight = 1418; - $this->_marginBottom = 1134; - $this->_pageSizeW = $this->_defaultPageSizeW; - $this->_pageSizeH = $this->_defaultPageSizeH; - $this->_borderTopSize = null; - $this->_borderTopColor = null; - $this->_borderLeftSize = null; - $this->_borderLeftColor = null; - $this->_borderRightSize = null; - $this->_borderRightColor = null; - $this->_borderBottomSize = null; - $this->_borderBottomColor = null; + $this->orientation = null; + $this->marginTop = 1418; + $this->marginLeft = 1418; + $this->marginRight = 1418; + $this->marginBottom = 1134; + $this->pageSizeW = $this->defaultPageSizeW; + $this->pageSizeH = $this->defaultPageSizeH; + $this->borderTopSize = null; + $this->borderTopColor = null; + $this->borderLeftSize = null; + $this->borderLeftColor = null; + $this->borderRightSize = null; + $this->borderRightColor = null; + $this->borderBottomSize = null; + $this->borderBottomColor = null; $this->headerHeight = 720; // set default header and footer to 720 twips (.5 inches) $this->footerHeight = 720; - $this->_colsNum = 1; - $this->_colsSpace = 720; - $this->_breakType = null; + $this->colsNum = 1; + $this->colsSpace = 720; + $this->breakType = null; } /** @@ -217,13 +217,16 @@ class Settings */ public function setSettingValue($key, $value) { - if ($key == '_orientation' && $value == 'landscape') { + if (substr($key, 0, 1) == '_') { + $key = substr($key, 1); + } + if ($key == 'orientation' && $value == 'landscape') { $this->setLandscape(); - } elseif ($key == '_orientation' && is_null($value)) { + } elseif ($key == 'orientation' && is_null($value)) { $this->setPortrait(); - } elseif ($key == '_borderSize') { + } elseif ($key == 'borderSize') { $this->setBorderSize($value); - } elseif ($key == '_borderColor') { + } elseif ($key == 'borderColor') { $this->setBorderColor($value); } else { $this->$key = $value; @@ -237,7 +240,7 @@ class Settings */ public function getMarginTop() { - return $this->_marginTop; + return $this->marginTop; } /** @@ -247,7 +250,7 @@ class Settings */ public function setMarginTop($pValue = '') { - $this->_marginTop = $pValue; + $this->marginTop = $pValue; return $this; } @@ -258,7 +261,7 @@ class Settings */ public function getMarginLeft() { - return $this->_marginLeft; + return $this->marginLeft; } /** @@ -268,7 +271,7 @@ class Settings */ public function setMarginLeft($pValue = '') { - $this->_marginLeft = $pValue; + $this->marginLeft = $pValue; return $this; } @@ -279,7 +282,7 @@ class Settings */ public function getMarginRight() { - return $this->_marginRight; + return $this->marginRight; } /** @@ -289,7 +292,7 @@ class Settings */ public function setMarginRight($pValue = '') { - $this->_marginRight = $pValue; + $this->marginRight = $pValue; return $this; } @@ -300,7 +303,7 @@ class Settings */ public function getMarginBottom() { - return $this->_marginBottom; + return $this->marginBottom; } /** @@ -310,7 +313,7 @@ class Settings */ public function setMarginBottom($pValue = '') { - $this->_marginBottom = $pValue; + $this->marginBottom = $pValue; return $this; } @@ -319,9 +322,9 @@ class Settings */ public function setLandscape() { - $this->_orientation = 'landscape'; - $this->_pageSizeW = $this->_defaultPageSizeH; - $this->_pageSizeH = $this->_defaultPageSizeW; + $this->orientation = 'landscape'; + $this->pageSizeW = $this->defaultPageSizeH; + $this->pageSizeH = $this->defaultPageSizeW; } /** @@ -329,9 +332,9 @@ class Settings */ public function setPortrait() { - $this->_orientation = null; - $this->_pageSizeW = $this->_defaultPageSizeW; - $this->_pageSizeH = $this->_defaultPageSizeH; + $this->orientation = null; + $this->pageSizeW = $this->defaultPageSizeW; + $this->pageSizeH = $this->defaultPageSizeH; } /** @@ -341,7 +344,7 @@ class Settings */ public function getPageSizeW() { - return $this->_pageSizeW; + return $this->pageSizeW; } /** @@ -351,7 +354,7 @@ class Settings */ public function getPageSizeH() { - return $this->_pageSizeH; + return $this->pageSizeH; } /** @@ -361,7 +364,7 @@ class Settings */ public function getOrientation() { - return $this->_orientation; + return $this->orientation; } /** @@ -371,10 +374,10 @@ class Settings */ public function setBorderSize($pValue = null) { - $this->_borderTopSize = $pValue; - $this->_borderLeftSize = $pValue; - $this->_borderRightSize = $pValue; - $this->_borderBottomSize = $pValue; + $this->borderTopSize = $pValue; + $this->borderLeftSize = $pValue; + $this->borderRightSize = $pValue; + $this->borderBottomSize = $pValue; } /** @@ -399,10 +402,10 @@ class Settings */ public function setBorderColor($pValue = null) { - $this->_borderTopColor = $pValue; - $this->_borderLeftColor = $pValue; - $this->_borderRightColor = $pValue; - $this->_borderBottomColor = $pValue; + $this->borderTopColor = $pValue; + $this->borderLeftColor = $pValue; + $this->borderRightColor = $pValue; + $this->borderBottomColor = $pValue; } /** @@ -427,7 +430,7 @@ class Settings */ public function setBorderTopSize($pValue = null) { - $this->_borderTopSize = $pValue; + $this->borderTopSize = $pValue; } /** @@ -437,7 +440,7 @@ class Settings */ public function getBorderTopSize() { - return $this->_borderTopSize; + return $this->borderTopSize; } /** @@ -447,7 +450,7 @@ class Settings */ public function setBorderTopColor($pValue = null) { - $this->_borderTopColor = $pValue; + $this->borderTopColor = $pValue; } /** @@ -457,7 +460,7 @@ class Settings */ public function getBorderTopColor() { - return $this->_borderTopColor; + return $this->borderTopColor; } /** @@ -467,7 +470,7 @@ class Settings */ public function setBorderLeftSize($pValue = null) { - $this->_borderLeftSize = $pValue; + $this->borderLeftSize = $pValue; } /** @@ -477,7 +480,7 @@ class Settings */ public function getBorderLeftSize() { - return $this->_borderLeftSize; + return $this->borderLeftSize; } /** @@ -487,7 +490,7 @@ class Settings */ public function setBorderLeftColor($pValue = null) { - $this->_borderLeftColor = $pValue; + $this->borderLeftColor = $pValue; } /** @@ -497,7 +500,7 @@ class Settings */ public function getBorderLeftColor() { - return $this->_borderLeftColor; + return $this->borderLeftColor; } /** @@ -507,7 +510,7 @@ class Settings */ public function setBorderRightSize($pValue = null) { - $this->_borderRightSize = $pValue; + $this->borderRightSize = $pValue; } /** @@ -517,7 +520,7 @@ class Settings */ public function getBorderRightSize() { - return $this->_borderRightSize; + return $this->borderRightSize; } /** @@ -527,7 +530,7 @@ class Settings */ public function setBorderRightColor($pValue = null) { - $this->_borderRightColor = $pValue; + $this->borderRightColor = $pValue; } /** @@ -537,7 +540,7 @@ class Settings */ public function getBorderRightColor() { - return $this->_borderRightColor; + return $this->borderRightColor; } /** @@ -547,7 +550,7 @@ class Settings */ public function setBorderBottomSize($pValue = null) { - $this->_borderBottomSize = $pValue; + $this->borderBottomSize = $pValue; } /** @@ -557,7 +560,7 @@ class Settings */ public function getBorderBottomSize() { - return $this->_borderBottomSize; + return $this->borderBottomSize; } /** @@ -567,7 +570,7 @@ class Settings */ public function setBorderBottomColor($pValue = null) { - $this->_borderBottomColor = $pValue; + $this->borderBottomColor = $pValue; } /** @@ -577,7 +580,7 @@ class Settings */ public function getBorderBottomColor() { - return $this->_borderBottomColor; + return $this->borderBottomColor; } /** @@ -660,7 +663,7 @@ class Settings if (!is_numeric($pValue)) { $pValue = 1; } - $this->_colsNum = $pValue; + $this->colsNum = $pValue; return $this; } @@ -671,7 +674,7 @@ class Settings */ public function getColsNum() { - return $this->_colsNum; + return $this->colsNum; } /** @@ -684,7 +687,7 @@ class Settings if (!is_numeric($pValue)) { $pValue = 720; } - $this->_colsSpace = $pValue; + $this->colsSpace = $pValue; return $this; } @@ -695,7 +698,7 @@ class Settings */ public function getColsSpace() { - return $this->_colsSpace; + return $this->colsSpace; } /** @@ -705,7 +708,7 @@ class Settings */ public function setBreakType($pValue = null) { - $this->_breakType = $pValue; + $this->breakType = $pValue; return $this; } @@ -716,6 +719,6 @@ class Settings */ public function getBreakType() { - return $this->_breakType; + return $this->breakType; } } diff --git a/src/PhpWord/Element/Element.php b/src/PhpWord/Element/Element.php index ec0dbc5c..6cebd0d5 100644 --- a/src/PhpWord/Element/Element.php +++ b/src/PhpWord/Element/Element.php @@ -49,8 +49,8 @@ abstract class Element { if (!is_null($styleValue) && is_array($styleValue)) { foreach ($styleValue as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; + if (substr($key, 0, 1) == '_') { + $key = substr($key, 1); } $styleObject->setStyleValue($key, $value); } diff --git a/src/PhpWord/Style.php b/src/PhpWord/Style.php index 3db67cb5..e883b9ed 100755 --- a/src/PhpWord/Style.php +++ b/src/PhpWord/Style.php @@ -141,8 +141,8 @@ class Style { if (!array_key_exists($styleName, self::$styles)) { foreach ($styleValues as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; + if (substr($key, 0, 1) == '_') { + $key = substr($key, 1); } $styleObject->setStyleValue($key, $value); } diff --git a/src/PhpWord/Style/Cell.php b/src/PhpWord/Style/Cell.php index 3949cddd..1fbed6e7 100644 --- a/src/PhpWord/Style/Cell.php +++ b/src/PhpWord/Style/Cell.php @@ -22,91 +22,91 @@ class Cell * * @var string */ - private $_valign; + private $valign; /** * Text Direction * * @var string */ - private $_textDirection; + private $textDirection; /** * Background-Color * * @var string */ - private $_bgColor; + private $bgColor; /** * Border Top Size * * @var int */ - private $_borderTopSize; + private $borderTopSize; /** * Border Top Color * * @var string */ - private $_borderTopColor; + private $borderTopColor; /** * Border Left Size * * @var int */ - private $_borderLeftSize; + private $borderLeftSize; /** * Border Left Color * * @var string */ - private $_borderLeftColor; + private $borderLeftColor; /** * Border Right Size * * @var int */ - private $_borderRightSize; + private $borderRightSize; /** * Border Right Color * * @var string */ - private $_borderRightColor; + private $borderRightColor; /** * Border Bottom Size * * @var int */ - private $_borderBottomSize; + private $borderBottomSize; /** * Border Bottom Color * * @var string */ - private $_borderBottomColor; + private $borderBottomColor; /** * Border Default Color * * @var string */ - private $_defaultBorderColor; + private $defaultBorderColor; /** * colspan * * @var integer */ - private $_gridSpan = null; + private $gridSpan = null; /** * rowspan (restart, continue) @@ -116,25 +116,25 @@ class Cell * * @var string */ - private $_vMerge = null; + private $vMerge = null; /** * Create a new Cell Style */ public function __construct() { - $this->_valign = null; - $this->_textDirection = null; - $this->_bgColor = null; - $this->_borderTopSize = null; - $this->_borderTopColor = null; - $this->_borderLeftSize = null; - $this->_borderLeftColor = null; - $this->_borderRightSize = null; - $this->_borderRightColor = null; - $this->_borderBottomSize = null; - $this->_borderBottomColor = null; - $this->_defaultBorderColor = '000000'; + $this->valign = null; + $this->textDirection = null; + $this->bgColor = null; + $this->borderTopSize = null; + $this->borderTopColor = null; + $this->borderLeftSize = null; + $this->borderLeftColor = null; + $this->borderRightSize = null; + $this->borderRightColor = null; + $this->borderBottomSize = null; + $this->borderBottomColor = null; + $this->defaultBorderColor = '000000'; } /** @@ -145,9 +145,12 @@ class Cell */ public function setStyleValue($key, $value) { - if ($key == '_borderSize') { + if (substr($key, 0, 1) == '_') { + $key = substr($key, 1); + } + if ($key == 'borderSize') { $this->setBorderSize($value); - } elseif ($key == '_borderColor') { + } elseif ($key == 'borderColor') { $this->setBorderColor($value); } else { $this->$key = $value; @@ -159,7 +162,7 @@ class Cell */ public function getVAlign() { - return $this->_valign; + return $this->valign; } /** @@ -169,7 +172,7 @@ class Cell */ public function setVAlign($pValue = null) { - $this->_valign = $pValue; + $this->valign = $pValue; } /** @@ -177,7 +180,7 @@ class Cell */ public function getTextDirection() { - return $this->_textDirection; + return $this->textDirection; } /** @@ -187,7 +190,7 @@ class Cell */ public function setTextDirection($pValue = null) { - $this->_textDirection = $pValue; + $this->textDirection = $pValue; } /** @@ -195,7 +198,7 @@ class Cell */ public function getBgColor() { - return $this->_bgColor; + return $this->bgColor; } /** @@ -205,7 +208,7 @@ class Cell */ public function setBgColor($pValue = null) { - $this->_bgColor = $pValue; + $this->bgColor = $pValue; } /** @@ -215,10 +218,10 @@ class Cell */ public function setBorderSize($pValue = null) { - $this->_borderTopSize = $pValue; - $this->_borderLeftSize = $pValue; - $this->_borderRightSize = $pValue; - $this->_borderBottomSize = $pValue; + $this->borderTopSize = $pValue; + $this->borderLeftSize = $pValue; + $this->borderRightSize = $pValue; + $this->borderBottomSize = $pValue; } /** @@ -241,10 +244,10 @@ class Cell */ public function setBorderColor($pValue = null) { - $this->_borderTopColor = $pValue; - $this->_borderLeftColor = $pValue; - $this->_borderRightColor = $pValue; - $this->_borderBottomColor = $pValue; + $this->borderTopColor = $pValue; + $this->borderLeftColor = $pValue; + $this->borderRightColor = $pValue; + $this->borderBottomColor = $pValue; } /** @@ -267,7 +270,7 @@ class Cell */ public function setBorderTopSize($pValue = null) { - $this->_borderTopSize = $pValue; + $this->borderTopSize = $pValue; } /** @@ -275,7 +278,7 @@ class Cell */ public function getBorderTopSize() { - return $this->_borderTopSize; + return $this->borderTopSize; } /** @@ -285,7 +288,7 @@ class Cell */ public function setBorderTopColor($pValue = null) { - $this->_borderTopColor = $pValue; + $this->borderTopColor = $pValue; } /** @@ -293,7 +296,7 @@ class Cell */ public function getBorderTopColor() { - return $this->_borderTopColor; + return $this->borderTopColor; } /** @@ -303,7 +306,7 @@ class Cell */ public function setBorderLeftSize($pValue = null) { - $this->_borderLeftSize = $pValue; + $this->borderLeftSize = $pValue; } /** @@ -311,7 +314,7 @@ class Cell */ public function getBorderLeftSize() { - return $this->_borderLeftSize; + return $this->borderLeftSize; } /** @@ -321,7 +324,7 @@ class Cell */ public function setBorderLeftColor($pValue = null) { - $this->_borderLeftColor = $pValue; + $this->borderLeftColor = $pValue; } /** @@ -329,7 +332,7 @@ class Cell */ public function getBorderLeftColor() { - return $this->_borderLeftColor; + return $this->borderLeftColor; } /** @@ -339,7 +342,7 @@ class Cell */ public function setBorderRightSize($pValue = null) { - $this->_borderRightSize = $pValue; + $this->borderRightSize = $pValue; } /** @@ -347,7 +350,7 @@ class Cell */ public function getBorderRightSize() { - return $this->_borderRightSize; + return $this->borderRightSize; } /** @@ -357,7 +360,7 @@ class Cell */ public function setBorderRightColor($pValue = null) { - $this->_borderRightColor = $pValue; + $this->borderRightColor = $pValue; } /** @@ -365,7 +368,7 @@ class Cell */ public function getBorderRightColor() { - return $this->_borderRightColor; + return $this->borderRightColor; } /** @@ -375,7 +378,7 @@ class Cell */ public function setBorderBottomSize($pValue = null) { - $this->_borderBottomSize = $pValue; + $this->borderBottomSize = $pValue; } /** @@ -383,7 +386,7 @@ class Cell */ public function getBorderBottomSize() { - return $this->_borderBottomSize; + return $this->borderBottomSize; } /** @@ -393,7 +396,7 @@ class Cell */ public function setBorderBottomColor($pValue = null) { - $this->_borderBottomColor = $pValue; + $this->borderBottomColor = $pValue; } /** @@ -401,7 +404,7 @@ class Cell */ public function getBorderBottomColor() { - return $this->_borderBottomColor; + return $this->borderBottomColor; } /** @@ -409,7 +412,7 @@ class Cell */ public function getDefaultBorderColor() { - return $this->_defaultBorderColor; + return $this->defaultBorderColor; } /** @@ -419,7 +422,7 @@ class Cell */ public function setGridSpan($pValue = null) { - $this->_gridSpan = $pValue; + $this->gridSpan = $pValue; } /** @@ -427,7 +430,7 @@ class Cell */ public function getGridSpan() { - return $this->_gridSpan; + return $this->gridSpan; } /** @@ -437,7 +440,7 @@ class Cell */ public function setVMerge($pValue = null) { - $this->_vMerge = $pValue; + $this->vMerge = $pValue; } /** @@ -445,6 +448,6 @@ class Cell */ public function getVMerge() { - return $this->_vMerge; + return $this->vMerge; } } diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index 69520804..999fc3fa 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -56,91 +56,91 @@ class Font * * @var string */ - private $_type; + private $type; /** * Paragraph style * * @var \PhpOffice\PhpWord\Style\Paragraph */ - private $_paragraphStyle; + private $paragraphStyle; /** * Font name * * @var int|float */ - private $_name = PhpWord::DEFAULT_FONT_NAME; + private $name = PhpWord::DEFAULT_FONT_NAME; /** * Font size * * @var int|float */ - private $_size = PhpWord::DEFAULT_FONT_SIZE; + private $size = PhpWord::DEFAULT_FONT_SIZE; /** * Bold * * @var bool */ - private $_bold = false; + private $bold = false; /** * Italic * * @var bool */ - private $_italic = false; + private $italic = false; /** * Superscript * * @var bool */ - private $_superScript = false; + private $superScript = false; /** * Subscript * * @var bool */ - private $_subScript = false; + private $subScript = false; /** * Undeline * * @var string */ - private $_underline = self::UNDERLINE_NONE; + private $underline = self::UNDERLINE_NONE; /** * Strikethrough * * @var bool */ - private $_strikethrough = false; + private $strikethrough = false; /** * Font color * * @var string */ - private $_color = PhpWord::DEFAULT_FONT_COLOR; + private $color = PhpWord::DEFAULT_FONT_COLOR; /** * Foreground/highlight * * @var string */ - private $_fgColor = null; + private $fgColor = null; /** * Background color * * @var string */ - private $_bgColor = null; + private $bgColor = null; /** * Text line height * @@ -159,7 +159,7 @@ class Font * * @var string */ - private $_hint = PhpWord::DEFAULT_FONT_CONTENT_TYPE; + private $hint = PhpWord::DEFAULT_FONT_CONTENT_TYPE; /** * Create new font style @@ -169,15 +169,15 @@ class Font */ public function __construct($type = 'text', $paragraphStyle = null) { - $this->_type = $type; + $this->type = $type; if ($paragraphStyle instanceof Paragraph) { - $this->_paragraphStyle = $paragraphStyle; + $this->paragraphStyle = $paragraphStyle; } elseif (is_array($paragraphStyle)) { - $this->_paragraphStyle = new Paragraph; - $this->_paragraphStyle->setArrayStyle($paragraphStyle); + $this->paragraphStyle = new Paragraph; + $this->paragraphStyle->setArrayStyle($paragraphStyle); } else { - $this->_paragraphStyle = $paragraphStyle; + $this->paragraphStyle = $paragraphStyle; } } @@ -193,8 +193,8 @@ class Font if ($key === 'line-height') { $this->setLineHeight($value); null; - } elseif (substr($key, 0, 1) !== '_') { - $key = '_' . $key; + } elseif (substr($key, 0, 1) == '_') { + $key = substr($key, 1); } $this->setStyleValue($key, $value); } @@ -210,7 +210,10 @@ class Font */ public function setStyleValue($key, $value) { - $method = 'set' . substr($key, 1); + if (substr($key, 0, 1) == '_') { + $key = substr($key, 1); + } + $method = 'set' . $key; if (method_exists($this, $method)) { $this->$method($value); } @@ -223,7 +226,7 @@ class Font */ public function getName() { - return $this->_name; + return $this->name; } /** @@ -237,7 +240,7 @@ class Font if (is_null($pValue) || $pValue == '') { $pValue = PhpWord::DEFAULT_FONT_NAME; } - $this->_name = $pValue; + $this->name = $pValue; return $this; } @@ -249,7 +252,7 @@ class Font */ public function getSize() { - return $this->_size; + return $this->size; } /** @@ -263,7 +266,7 @@ class Font if (!is_numeric($pValue)) { $pValue = PhpWord::DEFAULT_FONT_SIZE; } - $this->_size = $pValue; + $this->size = $pValue; return $this; } @@ -274,7 +277,7 @@ class Font */ public function getBold() { - return $this->_bold; + return $this->bold; } /** @@ -288,7 +291,7 @@ class Font if (!is_bool($pValue)) { $pValue = false; } - $this->_bold = $pValue; + $this->bold = $pValue; return $this; } @@ -299,7 +302,7 @@ class Font */ public function getItalic() { - return $this->_italic; + return $this->italic; } /** @@ -313,7 +316,7 @@ class Font if (!is_bool($pValue)) { $pValue = false; } - $this->_italic = $pValue; + $this->italic = $pValue; return $this; } @@ -324,7 +327,7 @@ class Font */ public function getSuperScript() { - return $this->_superScript; + return $this->superScript; } /** @@ -338,8 +341,8 @@ class Font if (!is_bool($pValue)) { $pValue = false; } - $this->_superScript = $pValue; - $this->_subScript = !$pValue; + $this->superScript = $pValue; + $this->subScript = !$pValue; return $this; } @@ -350,7 +353,7 @@ class Font */ public function getSubScript() { - return $this->_subScript; + return $this->subScript; } /** @@ -364,8 +367,8 @@ class Font if (!is_bool($pValue)) { $pValue = false; } - $this->_subScript = $pValue; - $this->_superScript = !$pValue; + $this->subScript = $pValue; + $this->superScript = !$pValue; return $this; } @@ -376,7 +379,7 @@ class Font */ public function getUnderline() { - return $this->_underline; + return $this->underline; } /** @@ -390,7 +393,7 @@ class Font if ($pValue == '') { $pValue = self::UNDERLINE_NONE; } - $this->_underline = $pValue; + $this->underline = $pValue; return $this; } @@ -401,7 +404,7 @@ class Font */ public function getStrikethrough() { - return $this->_strikethrough; + return $this->strikethrough; } /** @@ -415,7 +418,7 @@ class Font if (!is_bool($pValue)) { $pValue = false; } - $this->_strikethrough = $pValue; + $this->strikethrough = $pValue; return $this; } @@ -426,7 +429,7 @@ class Font */ public function getColor() { - return $this->_color; + return $this->color; } /** @@ -440,7 +443,7 @@ class Font if (is_null($pValue) || $pValue == '') { $pValue = PhpWord::DEFAULT_FONT_COLOR; } - $this->_color = $pValue; + $this->color = $pValue; return $this; } @@ -451,7 +454,7 @@ class Font */ public function getFgColor() { - return $this->_fgColor; + return $this->fgColor; } /** @@ -462,7 +465,7 @@ class Font */ public function setFgColor($pValue = null) { - $this->_fgColor = $pValue; + $this->fgColor = $pValue; return $this; } @@ -473,7 +476,7 @@ class Font */ public function getBgColor() { - return $this->_bgColor; + return $this->bgColor; } /** @@ -484,7 +487,7 @@ class Font */ public function setBgColor($pValue = null) { - $this->_bgColor = $pValue; + $this->bgColor = $pValue; return $this; } @@ -495,7 +498,7 @@ class Font */ public function getStyleType() { - return $this->_type; + return $this->type; } /** @@ -505,7 +508,7 @@ class Font */ public function getParagraphStyle() { - return $this->_paragraphStyle; + return $this->paragraphStyle; } /** @@ -547,7 +550,7 @@ class Font */ public function getHint() { - return $this->_hint; + return $this->hint; } /** @@ -561,7 +564,7 @@ class Font if (is_null($pValue) || $pValue == '') { $pValue = PhpWord::DEFAULT_FONT_CONTENT_TYPE; } - $this->_hint = $pValue; + $this->hint = $pValue; return $this; } } diff --git a/src/PhpWord/Style/Image.php b/src/PhpWord/Style/Image.php index f2065a7e..e973f1ac 100644 --- a/src/PhpWord/Style/Image.php +++ b/src/PhpWord/Style/Image.php @@ -25,21 +25,21 @@ class Image * * @var int */ - private $_width; + private $width; /** * Image width * * @var int */ - private $_height; + private $height; /** * Alignment * * @var string */ - private $_align; + private $align; /** * Wrapping style @@ -53,25 +53,25 @@ class Image * * @var int */ - private $_marginTop; + private $marginTop; /** * Margin Left * * @var int */ - private $_marginLeft; + private $marginLeft; /** * Create new image style */ public function __construct() { - $this->_width = null; - $this->_height = null; - $this->_align = null; - $this->_marginTop = null; - $this->_marginLeft = null; + $this->width = null; + $this->height = null; + $this->align = null; + $this->marginTop = null; + $this->marginLeft = null; $this->setWrappingStyle(self::WRAPPING_STYLE_INLINE); } @@ -83,6 +83,9 @@ class Image */ public function setStyleValue($key, $value) { + if (substr($key, 0, 1) == '_') { + $key = substr($key, 1); + } $this->$key = $value; } @@ -91,7 +94,7 @@ class Image */ public function getWidth() { - return $this->_width; + return $this->width; } /** @@ -101,7 +104,7 @@ class Image */ public function setWidth($pValue = null) { - $this->_width = $pValue; + $this->width = $pValue; } /** @@ -109,7 +112,7 @@ class Image */ public function getHeight() { - return $this->_height; + return $this->height; } /** @@ -119,7 +122,7 @@ class Image */ public function setHeight($pValue = null) { - $this->_height = $pValue; + $this->height = $pValue; } /** @@ -127,7 +130,7 @@ class Image */ public function getAlign() { - return $this->_align; + return $this->align; } /** @@ -137,7 +140,7 @@ class Image */ public function setAlign($pValue = null) { - $this->_align = $pValue; + $this->align = $pValue; } /** @@ -147,7 +150,7 @@ class Image */ public function getMarginTop() { - return $this->_marginTop; + return $this->marginTop; } /** @@ -158,7 +161,7 @@ class Image */ public function setMarginTop($pValue = null) { - $this->_marginTop = $pValue; + $this->marginTop = $pValue; return $this; } @@ -169,7 +172,7 @@ class Image */ public function getMarginLeft() { - return $this->_marginLeft; + return $this->marginLeft; } /** @@ -180,7 +183,7 @@ class Image */ public function setMarginLeft($pValue = null) { - $this->_marginLeft = $pValue; + $this->marginLeft = $pValue; return $this; } diff --git a/src/PhpWord/Style/ListItem.php b/src/PhpWord/Style/ListItem.php index 9c515e07..6681be16 100644 --- a/src/PhpWord/Style/ListItem.php +++ b/src/PhpWord/Style/ListItem.php @@ -24,14 +24,14 @@ class ListItem /** * List Type */ - private $_listType; + private $listType; /** * Create a new ListItem Style */ public function __construct() { - $this->_listType = self::TYPE_BULLET_FILLED; + $this->listType = self::TYPE_BULLET_FILLED; } /** @@ -42,6 +42,9 @@ class ListItem */ public function setStyleValue($key, $value) { + if (substr($key, 0, 1) == '_') { + $key = substr($key, 1); + } $this->$key = $value; } @@ -52,7 +55,7 @@ class ListItem */ public function setListType($pValue = self::TYPE_BULLET_FILLED) { - $this->_listType = $pValue; + $this->listType = $pValue; } /** @@ -60,6 +63,6 @@ class ListItem */ public function getListType() { - return $this->_listType; + return $this->listType; } } diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index da1c8c05..c55d9db1 100755 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -30,91 +30,91 @@ class Paragraph * * @var string */ - private $_align; + private $align; /** * Space before Paragraph * * @var int */ - private $_spaceBefore; + private $spaceBefore; /** * Space after Paragraph * * @var int */ - private $_spaceAfter; + private $spaceAfter; /** * Spacing between breaks * * @var int */ - private $_spacing; + private $spacing; /** * Set of Custom Tab Stops * * @var array */ - private $_tabs; + private $tabs; /** * Indent by how much * * @var int */ - private $_indent; + private $indent; /** * Hanging by how much * * @var int */ - private $_hanging; + private $hanging; /** * Parent style * * @var string */ - private $_basedOn = 'Normal'; + private $basedOn = 'Normal'; /** * Style for next paragraph * * @var string */ - private $_next; + private $next; /** * Allow first/last line to display on a separate page * * @var bool */ - private $_widowControl = true; + private $widowControl = true; /** * Keep paragraph with next paragraph * * @var bool */ - private $_keepNext = false; + private $keepNext = false; /** * Keep all lines on one page * * @var bool */ - private $_keepLines = false; + private $keepLines = false; /** * Start paragraph on next page * * @var bool */ - private $_pageBreakBefore = false; + private $pageBreakBefore = false; /** * Set style by array @@ -127,8 +127,8 @@ class Paragraph foreach ($style as $key => $value) { if ($key === 'line-height') { null; - } elseif (substr($key, 0, 1) !== '_') { - $key = '_' . $key; + } elseif (substr($key, 0, 1) == '_') { + $key = substr($key, 1); } $this->setStyleValue($key, $value); } @@ -144,16 +144,18 @@ class Paragraph */ public function setStyleValue($key, $value) { - if ($key == '_indent' || $key == '_hanging') { + if (substr($key, 0, 1) == '_') { + $key = substr($key, 1); + } + if ($key == 'indent' || $key == 'hanging') { $value = $value * 720; - } elseif ($key == '_spacing') { + } elseif ($key == 'spacing') { $value += 240; // because line height of 1 matches 240 twips } elseif ($key === 'line-height') { $this->setLineHeight($value); return; } - $this->$key = $value; - $method = 'set' . substr($key, 1); + $method = 'set' . $key; if (method_exists($this, $method)) { $this->$method($value); } @@ -166,7 +168,7 @@ class Paragraph */ public function getAlign() { - return $this->_align; + return $this->align; } /** @@ -181,7 +183,7 @@ class Paragraph // justify becames both $pValue = 'both'; } - $this->_align = $pValue; + $this->align = $pValue; return $this; } @@ -192,7 +194,7 @@ class Paragraph */ public function getSpaceBefore() { - return $this->_spaceBefore; + return $this->spaceBefore; } /** @@ -203,7 +205,7 @@ class Paragraph */ public function setSpaceBefore($pValue = null) { - $this->_spaceBefore = $pValue; + $this->spaceBefore = $pValue; return $this; } @@ -214,7 +216,7 @@ class Paragraph */ public function getSpaceAfter() { - return $this->_spaceAfter; + return $this->spaceAfter; } /** @@ -225,7 +227,7 @@ class Paragraph */ public function setSpaceAfter($pValue = null) { - $this->_spaceAfter = $pValue; + $this->spaceAfter = $pValue; return $this; } @@ -236,7 +238,7 @@ class Paragraph */ public function getSpacing() { - return $this->_spacing; + return $this->spacing; } /** @@ -247,7 +249,7 @@ class Paragraph */ public function setSpacing($pValue = null) { - $this->_spacing = $pValue; + $this->spacing = $pValue; return $this; } @@ -258,7 +260,7 @@ class Paragraph */ public function getIndent() { - return $this->_indent; + return $this->indent; } /** @@ -269,7 +271,7 @@ class Paragraph */ public function setIndent($pValue = null) { - $this->_indent = $pValue; + $this->indent = $pValue; return $this; } @@ -280,7 +282,7 @@ class Paragraph */ public function getHanging() { - return $this->_hanging; + return $this->hanging; } /** @@ -291,7 +293,7 @@ class Paragraph */ public function setHanging($pValue = null) { - $this->_hanging = $pValue; + $this->hanging = $pValue; return $this; } @@ -302,7 +304,7 @@ class Paragraph */ public function getTabs() { - return $this->_tabs; + return $this->tabs; } /** @@ -314,7 +316,7 @@ class Paragraph public function setTabs($pValue = null) { if (is_array($pValue)) { - $this->_tabs = new Tabs($pValue); + $this->tabs = new Tabs($pValue); } return $this; } @@ -326,7 +328,7 @@ class Paragraph */ public function getBasedOn() { - return $this->_basedOn; + return $this->basedOn; } /** @@ -337,7 +339,7 @@ class Paragraph */ public function setBasedOn($pValue = 'Normal') { - $this->_basedOn = $pValue; + $this->basedOn = $pValue; return $this; } @@ -348,7 +350,7 @@ class Paragraph */ public function getNext() { - return $this->_next; + return $this->next; } /** @@ -359,7 +361,7 @@ class Paragraph */ public function setNext($pValue = null) { - $this->_next = $pValue; + $this->next = $pValue; return $this; } @@ -370,7 +372,7 @@ class Paragraph */ public function getWidowControl() { - return $this->_widowControl; + return $this->widowControl; } /** @@ -384,7 +386,7 @@ class Paragraph if (!is_bool($pValue)) { $pValue = true; } - $this->_widowControl = $pValue; + $this->widowControl = $pValue; return $this; } @@ -395,7 +397,7 @@ class Paragraph */ public function getKeepNext() { - return $this->_keepNext; + return $this->keepNext; } /** @@ -409,7 +411,7 @@ class Paragraph if (!is_bool($pValue)) { $pValue = false; } - $this->_keepNext = $pValue; + $this->keepNext = $pValue; return $this; } @@ -420,7 +422,7 @@ class Paragraph */ public function getKeepLines() { - return $this->_keepLines; + return $this->keepLines; } /** @@ -434,7 +436,7 @@ class Paragraph if (!is_bool($pValue)) { $pValue = false; } - $this->_keepLines = $pValue; + $this->keepLines = $pValue; return $this; } @@ -445,7 +447,7 @@ class Paragraph */ public function getPageBreakBefore() { - return $this->_pageBreakBefore; + return $this->pageBreakBefore; } /** @@ -459,7 +461,7 @@ class Paragraph if (!is_bool($pValue)) { $pValue = false; } - $this->_pageBreakBefore = $pValue; + $this->pageBreakBefore = $pValue; return $this; } diff --git a/src/PhpWord/Style/Row.php b/src/PhpWord/Style/Row.php index 2b714024..113baaf2 100644 --- a/src/PhpWord/Style/Row.php +++ b/src/PhpWord/Style/Row.php @@ -19,21 +19,21 @@ class Row * * @var bool */ - private $_tblHeader = false; + private $tblHeader = false; /** * Table row cannot break across pages * * @var bool */ - private $_cantSplit = false; + private $cantSplit = false; /** * Table row exact height * * @var bool */ - private $_exactHeight = false; + private $exactHeight = false; /** * Create a new row style @@ -50,6 +50,9 @@ class Row */ public function setStyleValue($key, $value) { + if (substr($key, 0, 1) == '_') { + $key = substr($key, 1); + } $this->$key = $value; } @@ -64,7 +67,7 @@ class Row if (!is_bool($pValue)) { $pValue = false; } - $this->_tblHeader = $pValue; + $this->tblHeader = $pValue; return $this; } @@ -75,7 +78,7 @@ class Row */ public function getTblHeader() { - return $this->_tblHeader; + return $this->tblHeader; } /** @@ -89,7 +92,7 @@ class Row if (!is_bool($pValue)) { $pValue = false; } - $this->_cantSplit = $pValue; + $this->cantSplit = $pValue; return $this; } @@ -100,7 +103,7 @@ class Row */ public function getCantSplit() { - return $this->_cantSplit; + return $this->cantSplit; } /** @@ -114,7 +117,7 @@ class Row if (!is_bool($pValue)) { $pValue = false; } - $this->_exactHeight = $pValue; + $this->exactHeight = $pValue; return $this; } @@ -125,6 +128,6 @@ class Row */ public function getExactHeight() { - return $this->_exactHeight; + return $this->exactHeight; } } diff --git a/src/PhpWord/Style/TOC.php b/src/PhpWord/Style/TOC.php index f1aa10bc..f49d9847 100644 --- a/src/PhpWord/Style/TOC.php +++ b/src/PhpWord/Style/TOC.php @@ -24,21 +24,21 @@ class TOC * * @var string */ - private $_tabLeader; + private $tabLeader; /** * Tab Position * * @var int */ - private $_tabPos; + private $tabPos; /** * Indent * * @var int */ - private $_indent; + private $indent; /** @@ -46,9 +46,9 @@ class TOC */ public function __construct() { - $this->_tabPos = 9062; - $this->_tabLeader = self::TABLEADER_DOT; - $this->_indent = 200; + $this->tabPos = 9062; + $this->tabLeader = self::TABLEADER_DOT; + $this->indent = 200; } /** @@ -58,7 +58,7 @@ class TOC */ public function getTabPos() { - return $this->_tabPos; + return $this->tabPos; } /** @@ -68,7 +68,7 @@ class TOC */ public function setTabPos($pValue) { - $this->_tabPos = $pValue; + $this->tabPos = $pValue; } /** @@ -78,7 +78,7 @@ class TOC */ public function getTabLeader() { - return $this->_tabLeader; + return $this->tabLeader; } /** @@ -88,7 +88,7 @@ class TOC */ public function setTabLeader($pValue = self::TABLEADER_DOT) { - $this->_tabLeader = $pValue; + $this->tabLeader = $pValue; } /** @@ -98,7 +98,7 @@ class TOC */ public function getIndent() { - return $this->_indent; + return $this->indent; } /** @@ -108,7 +108,7 @@ class TOC */ public function setIndent($pValue) { - $this->_indent = $pValue; + $this->indent = $pValue; } /** diff --git a/src/PhpWord/Style/Tab.php b/src/PhpWord/Style/Tab.php index 8524143b..59757ea8 100644 --- a/src/PhpWord/Style/Tab.php +++ b/src/PhpWord/Style/Tab.php @@ -21,21 +21,21 @@ class Tab * * @var string */ - private $_val; + private $val; /** * Tab Leader Character * * @var string */ - private $_leader; + private $leader; /** * Tab Stop Position * * @var int */ - private $_position; + private $position; /** * Tab Stop Type @@ -43,7 +43,7 @@ class Tab * @var array * @link http://www.schemacentral.com/sc/ooxml/a-w_val-26.html Tab Stop Type */ - private static $_possibleStopTypes = array( + private static $possibleStopTypes = array( 'clear', // No Tab Stop 'left', // Left Tab Stop 'center', // Center Tab Stop @@ -59,7 +59,7 @@ class Tab * @var array * @link http://www.schemacentral.com/sc/ooxml/a-w_leader-1.html Tab Leader Character */ - private static $_possibleLeaders = array( + private static $possibleLeaders = array( 'none', // No tab stop leader 'dot', // Dotted leader line 'hyphen', // Dashed tab stop leader line @@ -80,13 +80,13 @@ class Tab public function __construct($val = null, $position = 0, $leader = null) { // Default to clear if the stop type is not matched - $this->_val = (self::isStopType($val)) ? $val : 'clear'; + $this->val = (self::isStopType($val)) ? $val : 'clear'; // Default to 0 if the position is non-numeric - $this->_position = (is_numeric($position)) ? intval($position) : 0; + $this->position = (is_numeric($position)) ? intval($position) : 0; // Default to NULL if no tab leader - $this->_leader = (self::isLeaderType($leader)) ? $leader : null; + $this->leader = (self::isLeaderType($leader)) ? $leader : null; } /** @@ -98,11 +98,11 @@ class Tab { if (isset($xmlWriter)) { $xmlWriter->startElement("w:tab"); - $xmlWriter->writeAttribute("w:val", $this->_val); - if (!is_null($this->_leader)) { - $xmlWriter->writeAttribute("w:leader", $this->_leader); + $xmlWriter->writeAttribute("w:val", $this->val); + if (!is_null($this->leader)) { + $xmlWriter->writeAttribute("w:leader", $this->leader); } - $xmlWriter->writeAttribute("w:pos", $this->_position); + $xmlWriter->writeAttribute("w:pos", $this->position); $xmlWriter->endElement(); } } @@ -115,7 +115,7 @@ class Tab */ private static function isStopType($attribute) { - return in_array($attribute, self::$_possibleStopTypes); + return in_array($attribute, self::$possibleStopTypes); } /** @@ -126,6 +126,6 @@ class Tab */ private static function isLeaderType($attribute) { - return in_array($attribute, self::$_possibleLeaders); + return in_array($attribute, self::$possibleLeaders); } } diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index bf744a3e..178c2854 100755 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -19,126 +19,126 @@ class Table * * @var \PhpOffice\PhpWord\Style\Table */ - private $_firstRow = null; + private $firstRow = null; /** * Cell margin top * * @var int */ - private $_cellMarginTop = null; + private $cellMarginTop = null; /** * Cell margin left * * @var int */ - private $_cellMarginLeft = null; + private $cellMarginLeft = null; /** * Cell margin right * * @var int */ - private $_cellMarginRight = null; + private $cellMarginRight = null; /** * Cell margin bottom * * @var int */ - private $_cellMarginBottom = null; + private $cellMarginBottom = null; /** * Background color * * @var string */ - private $_bgColor; + private $bgColor; /** * Border size top * * @var int */ - private $_borderTopSize; + private $borderTopSize; /** * Border color * * @var string top */ - private $_borderTopColor; + private $borderTopColor; /** * Border size left * * @var int */ - private $_borderLeftSize; + private $borderLeftSize; /** * Border color left * * @var string */ - private $_borderLeftColor; + private $borderLeftColor; /** * Border size right * * @var int */ - private $_borderRightSize; + private $borderRightSize; /** * Border color right * * @var string */ - private $_borderRightColor; + private $borderRightColor; /** * Border size bottom * * @var int */ - private $_borderBottomSize; + private $borderBottomSize; /** * Border color bottom * * @var string */ - private $_borderBottomColor; + private $borderBottomColor; /** * Border size inside horizontal * * @var int */ - private $_borderInsideHSize; + private $borderInsideHSize; /** * Border color inside horizontal * * @var string */ - private $_borderInsideHColor; + private $borderInsideHColor; /** * Border size inside vertical * * @var int */ - private $_borderInsideVSize; + private $borderInsideVSize; /** * Border color inside vertical * * @var string */ - private $_borderInsideVColor; + private $borderInsideVColor; /** * Create new table style @@ -149,30 +149,29 @@ class Table public function __construct($styleTable = null, $styleFirstRow = null) { if (!is_null($styleFirstRow) && is_array($styleFirstRow)) { - $this->_firstRow = clone $this; + $this->firstRow = clone $this; - unset($this->_firstRow->_firstRow); - unset($this->_firstRow->_cellMarginBottom); - unset($this->_firstRow->_cellMarginTop); - unset($this->_firstRow->_cellMarginLeft); - unset($this->_firstRow->_cellMarginRight); - unset($this->_firstRow->_borderInsideVColor); - unset($this->_firstRow->_borderInsideVSize); - unset($this->_firstRow->_borderInsideHColor); - unset($this->_firstRow->_borderInsideHSize); + unset($this->firstRow->firstRow); + unset($this->firstRow->cellMarginBottom); + unset($this->firstRow->cellMarginTop); + unset($this->firstRow->cellMarginLeft); + unset($this->firstRow->cellMarginRight); + unset($this->firstRow->borderInsideVColor); + unset($this->firstRow->borderInsideVSize); + unset($this->firstRow->borderInsideHColor); + unset($this->firstRow->borderInsideHSize); foreach ($styleFirstRow as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; + if (substr($key, 0, 1) == '_') { + $key = substr($key, 1); } - - $this->_firstRow->setStyleValue($key, $value); + $this->firstRow->setStyleValue($key, $value); } } if (!is_null($styleTable) && is_array($styleTable)) { foreach ($styleTable as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; + if (substr($key, 0, 1) == '_') { + $key = substr($key, 1); } $this->setStyleValue($key, $value); } @@ -187,11 +186,14 @@ class Table */ public function setStyleValue($key, $value) { - if ($key == '_borderSize') { + if (substr($key, 0, 1) == '_') { + $key = substr($key, 1); + } + if ($key == 'borderSize') { $this->setBorderSize($value); - } elseif ($key == '_borderColor') { + } elseif ($key == 'borderColor') { $this->setBorderColor($value); - } elseif ($key == '_cellMargin') { + } elseif ($key == 'cellMargin') { $this->setCellMargin($value); } else { $this->$key = $value; @@ -205,7 +207,7 @@ class Table */ public function getFirstRow() { - return $this->_firstRow; + return $this->firstRow; } /** @@ -215,7 +217,7 @@ class Table */ public function getBgColor() { - return $this->_bgColor; + return $this->bgColor; } /** @@ -226,7 +228,7 @@ class Table */ public function setBgColor($pValue = null) { - $this->_bgColor = $pValue; + $this->bgColor = $pValue; } /** @@ -236,12 +238,12 @@ class Table */ public function setBorderSize($pValue = null) { - $this->_borderTopSize = $pValue; - $this->_borderLeftSize = $pValue; - $this->_borderRightSize = $pValue; - $this->_borderBottomSize = $pValue; - $this->_borderInsideHSize = $pValue; - $this->_borderInsideVSize = $pValue; + $this->borderTopSize = $pValue; + $this->borderLeftSize = $pValue; + $this->borderRightSize = $pValue; + $this->borderBottomSize = $pValue; + $this->borderInsideHSize = $pValue; + $this->borderInsideVSize = $pValue; } /** @@ -267,12 +269,12 @@ class Table */ public function setBorderColor($pValue = null) { - $this->_borderTopColor = $pValue; - $this->_borderLeftColor = $pValue; - $this->_borderRightColor = $pValue; - $this->_borderBottomColor = $pValue; - $this->_borderInsideHColor = $pValue; - $this->_borderInsideVColor = $pValue; + $this->borderTopColor = $pValue; + $this->borderLeftColor = $pValue; + $this->borderRightColor = $pValue; + $this->borderBottomColor = $pValue; + $this->borderInsideHColor = $pValue; + $this->borderInsideVColor = $pValue; } /** @@ -299,7 +301,7 @@ class Table */ public function setBorderTopSize($pValue = null) { - $this->_borderTopSize = $pValue; + $this->borderTopSize = $pValue; } /** @@ -309,7 +311,7 @@ class Table */ public function getBorderTopSize() { - return $this->_borderTopSize; + return $this->borderTopSize; } /** @@ -319,7 +321,7 @@ class Table */ public function setBorderTopColor($pValue = null) { - $this->_borderTopColor = $pValue; + $this->borderTopColor = $pValue; } /** @@ -329,7 +331,7 @@ class Table */ public function getBorderTopColor() { - return $this->_borderTopColor; + return $this->borderTopColor; } /** @@ -339,7 +341,7 @@ class Table */ public function setBorderLeftSize($pValue = null) { - $this->_borderLeftSize = $pValue; + $this->borderLeftSize = $pValue; } /** @@ -349,7 +351,7 @@ class Table */ public function getBorderLeftSize() { - return $this->_borderLeftSize; + return $this->borderLeftSize; } /** @@ -359,7 +361,7 @@ class Table */ public function setBorderLeftColor($pValue = null) { - $this->_borderLeftColor = $pValue; + $this->borderLeftColor = $pValue; } /** @@ -369,7 +371,7 @@ class Table */ public function getBorderLeftColor() { - return $this->_borderLeftColor; + return $this->borderLeftColor; } /** @@ -379,7 +381,7 @@ class Table */ public function setBorderRightSize($pValue = null) { - $this->_borderRightSize = $pValue; + $this->borderRightSize = $pValue; } /** @@ -389,7 +391,7 @@ class Table */ public function getBorderRightSize() { - return $this->_borderRightSize; + return $this->borderRightSize; } /** @@ -399,7 +401,7 @@ class Table */ public function setBorderRightColor($pValue = null) { - $this->_borderRightColor = $pValue; + $this->borderRightColor = $pValue; } /** @@ -409,7 +411,7 @@ class Table */ public function getBorderRightColor() { - return $this->_borderRightColor; + return $this->borderRightColor; } /** @@ -419,7 +421,7 @@ class Table */ public function setBorderBottomSize($pValue = null) { - $this->_borderBottomSize = $pValue; + $this->borderBottomSize = $pValue; } /** @@ -429,7 +431,7 @@ class Table */ public function getBorderBottomSize() { - return $this->_borderBottomSize; + return $this->borderBottomSize; } /** @@ -439,7 +441,7 @@ class Table */ public function setBorderBottomColor($pValue = null) { - $this->_borderBottomColor = $pValue; + $this->borderBottomColor = $pValue; } /** @@ -449,7 +451,7 @@ class Table */ public function getBorderBottomColor() { - return $this->_borderBottomColor; + return $this->borderBottomColor; } /** @@ -459,7 +461,7 @@ class Table */ public function setBorderInsideHColor($pValue = null) { - $this->_borderInsideHColor = $pValue; + $this->borderInsideHColor = $pValue; } /** @@ -469,7 +471,7 @@ class Table */ public function getBorderInsideHColor() { - return (isset($this->_borderInsideHColor)) ? $this->_borderInsideHColor : null; + return (isset($this->borderInsideHColor)) ? $this->borderInsideHColor : null; } /** @@ -479,7 +481,7 @@ class Table */ public function setBorderInsideVColor($pValue = null) { - $this->_borderInsideVColor = $pValue; + $this->borderInsideVColor = $pValue; } /** @@ -489,7 +491,7 @@ class Table */ public function getBorderInsideVColor() { - return (isset($this->_borderInsideVColor)) ? $this->_borderInsideVColor : null; + return (isset($this->borderInsideVColor)) ? $this->borderInsideVColor : null; } /** @@ -499,7 +501,7 @@ class Table */ public function setBorderInsideHSize($pValue = null) { - $this->_borderInsideHSize = $pValue; + $this->borderInsideHSize = $pValue; } /** @@ -509,7 +511,7 @@ class Table */ public function getBorderInsideHSize() { - return (isset($this->_borderInsideHSize)) ? $this->_borderInsideHSize : null; + return (isset($this->borderInsideHSize)) ? $this->borderInsideHSize : null; } /** @@ -519,7 +521,7 @@ class Table */ public function setBorderInsideVSize($pValue = null) { - $this->_borderInsideVSize = $pValue; + $this->borderInsideVSize = $pValue; } /** @@ -529,7 +531,7 @@ class Table */ public function getBorderInsideVSize() { - return (isset($this->_borderInsideVSize)) ? $this->_borderInsideVSize : null; + return (isset($this->borderInsideVSize)) ? $this->borderInsideVSize : null; } /** @@ -539,7 +541,7 @@ class Table */ public function setCellMarginTop($pValue = null) { - $this->_cellMarginTop = $pValue; + $this->cellMarginTop = $pValue; } /** @@ -549,7 +551,7 @@ class Table */ public function getCellMarginTop() { - return $this->_cellMarginTop; + return $this->cellMarginTop; } /** @@ -559,7 +561,7 @@ class Table */ public function setCellMarginLeft($pValue = null) { - $this->_cellMarginLeft = $pValue; + $this->cellMarginLeft = $pValue; } /** @@ -569,7 +571,7 @@ class Table */ public function getCellMarginLeft() { - return $this->_cellMarginLeft; + return $this->cellMarginLeft; } /** @@ -579,7 +581,7 @@ class Table */ public function setCellMarginRight($pValue = null) { - $this->_cellMarginRight = $pValue; + $this->cellMarginRight = $pValue; } /** @@ -589,7 +591,7 @@ class Table */ public function getCellMarginRight() { - return $this->_cellMarginRight; + return $this->cellMarginRight; } /** @@ -599,7 +601,7 @@ class Table */ public function setCellMarginBottom($pValue = null) { - $this->_cellMarginBottom = $pValue; + $this->cellMarginBottom = $pValue; } /** @@ -609,7 +611,7 @@ class Table */ public function getCellMarginBottom() { - return $this->_cellMarginBottom; + return $this->cellMarginBottom; } /** @@ -619,10 +621,10 @@ class Table */ public function setCellMargin($pValue = null) { - $this->_cellMarginTop = $pValue; - $this->_cellMarginLeft = $pValue; - $this->_cellMarginRight = $pValue; - $this->_cellMarginBottom = $pValue; + $this->cellMarginTop = $pValue; + $this->cellMarginLeft = $pValue; + $this->cellMarginRight = $pValue; + $this->cellMarginBottom = $pValue; } /** @@ -632,6 +634,6 @@ class Table */ public function getCellMargin() { - return array($this->_cellMarginTop, $this->_cellMarginLeft, $this->_cellMarginRight, $this->_cellMarginBottom); + return array($this->cellMarginTop, $this->cellMarginLeft, $this->cellMarginRight, $this->cellMarginBottom); } } diff --git a/src/PhpWord/Style/Tabs.php b/src/PhpWord/Style/Tabs.php index 343a2b35..497e09f1 100755 --- a/src/PhpWord/Style/Tabs.php +++ b/src/PhpWord/Style/Tabs.php @@ -21,7 +21,7 @@ class Tabs * * @var array */ - private $_tabs; + private $tabs; /** * Create new tab collection style @@ -30,7 +30,7 @@ class Tabs */ public function __construct(array $tabs) { - $this->_tabs = $tabs; + $this->tabs = $tabs; } /** @@ -42,7 +42,7 @@ class Tabs { if (isset($xmlWriter)) { $xmlWriter->startElement("w:tabs"); - foreach ($this->_tabs as &$tab) { + foreach ($this->tabs as &$tab) { $tab->toXml($xmlWriter); } $xmlWriter->endElement(); diff --git a/src/PhpWord/TOC.php b/src/PhpWord/TOC.php index 2793d2f0..cb3ad02f 100644 --- a/src/PhpWord/TOC.php +++ b/src/PhpWord/TOC.php @@ -82,8 +82,8 @@ class TOC if (!is_null($styleTOC) && is_array($styleTOC)) { foreach ($styleTOC as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; + if (substr($key, 0, 1) == '_') { + $key = substr($key, 1); } self::$TOCStyle->setStyleValue($key, $value); } @@ -93,8 +93,8 @@ class TOC if (is_array($styleFont)) { self::$fontStyle = new Font(); foreach ($styleFont as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; + if (substr($key, 0, 1) == '_') { + $key = substr($key, 1); } self::$fontStyle->setStyleValue($key, $value); } diff --git a/tests/PhpWord/Tests/Style/CellTest.php b/tests/PhpWord/Tests/Style/CellTest.php index bfe97282..9bf5a917 100644 --- a/tests/PhpWord/Tests/Style/CellTest.php +++ b/tests/PhpWord/Tests/Style/CellTest.php @@ -61,10 +61,10 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertEquals($default, $object->getDefaultBorderColor()); - $object->setStyleValue('_defaultBorderColor', $value); + $object->setStyleValue('defaultBorderColor', $value); $this->assertEquals($value, $object->getDefaultBorderColor()); - $object->setStyleValue('_borderColor', $value); + $object->setStyleValue('borderColor', $value); $expected = array($value, $value, $value, $value); $this->assertEquals($expected, $object->getBorderColor()); } @@ -78,7 +78,7 @@ class CellTest extends \PHPUnit_Framework_TestCase $value = 120; $expected = array($value, $value, $value, $value); - $object->setStyleValue('_borderSize', $value); + $object->setStyleValue('borderSize', $value); $this->assertEquals($expected, $object->getBorderSize()); } } diff --git a/tests/PhpWord/Tests/Style/FontTest.php b/tests/PhpWord/Tests/Style/FontTest.php index d26b97fe..9185d646 100644 --- a/tests/PhpWord/Tests/Style/FontTest.php +++ b/tests/PhpWord/Tests/Style/FontTest.php @@ -62,9 +62,9 @@ class FontTest extends \PHPUnit_Framework_TestCase ); foreach ($attributes as $key => $default) { $get = "get{$key}"; - $object->setStyleValue("_$key", null); + $object->setStyleValue("$key", null); $this->assertEquals($default, $object->$get()); - $object->setStyleValue("_$key", ''); + $object->setStyleValue("$key", ''); $this->assertEquals($default, $object->$get()); } } diff --git a/tests/PhpWord/Tests/Style/ImageTest.php b/tests/PhpWord/Tests/Style/ImageTest.php index 2d96201d..b35c8cb2 100644 --- a/tests/PhpWord/Tests/Style/ImageTest.php +++ b/tests/PhpWord/Tests/Style/ImageTest.php @@ -58,7 +58,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase ); foreach ($properties as $key => $value) { $get = "get{$key}"; - $object->setStyleValue("_{$key}", $value); + $object->setStyleValue("{$key}", $value); $this->assertEquals($value, $object->$get()); } } diff --git a/tests/PhpWord/Tests/Style/ListItemTest.php b/tests/PhpWord/Tests/Style/ListItemTest.php index ed9c9048..0fb67da3 100644 --- a/tests/PhpWord/Tests/Style/ListItemTest.php +++ b/tests/PhpWord/Tests/Style/ListItemTest.php @@ -38,7 +38,7 @@ class ListItemTest extends \PHPUnit_Framework_TestCase $object = new ListItem(); $value = ListItem::TYPE_ALPHANUM; - $object->setStyleValue('_listType', $value); + $object->setStyleValue('listType', $value); $this->assertEquals($value, $object->getListType()); } diff --git a/tests/PhpWord/Tests/Style/ParagraphTest.php b/tests/PhpWord/Tests/Style/ParagraphTest.php index 10a74dae..413eb149 100644 --- a/tests/PhpWord/Tests/Style/ParagraphTest.php +++ b/tests/PhpWord/Tests/Style/ParagraphTest.php @@ -45,9 +45,9 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase ); foreach ($attributes as $key => $default) { $get = "get{$key}"; - $object->setStyleValue("_$key", null); + $object->setStyleValue("$key", null); $this->assertEquals($default, $object->$get()); - $object->setStyleValue("_$key", ''); + $object->setStyleValue("$key", ''); $this->assertEquals($default, $object->$get()); } } @@ -75,7 +75,7 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase ); foreach ($attributes as $key => $value) { $get = "get{$key}"; - $object->setStyleValue("_$key", $value); + $object->setStyleValue("$key", $value); if ($key == 'align') { if ($value == 'justify') { $value = 'both'; diff --git a/tests/PhpWord/Tests/Style/RowTest.php b/tests/PhpWord/Tests/Style/RowTest.php index fefb7b0a..ad193f80 100644 --- a/tests/PhpWord/Tests/Style/RowTest.php +++ b/tests/PhpWord/Tests/Style/RowTest.php @@ -42,7 +42,7 @@ class RowTest extends \PHPUnit_Framework_TestCase // setStyleValue $value = !$value; $expected = $value ? 1 : 0; - $object->setStyleValue("_{$key}", $value); + $object->setStyleValue("{$key}", $value); $this->assertEquals($expected, $object->$get()); } } diff --git a/tests/PhpWord/Tests/Style/TOCTest.php b/tests/PhpWord/Tests/Style/TOCTest.php index 34c86186..2c10de8d 100644 --- a/tests/PhpWord/Tests/Style/TOCTest.php +++ b/tests/PhpWord/Tests/Style/TOCTest.php @@ -39,7 +39,7 @@ class TOCTest extends \PHPUnit_Framework_TestCase $this->assertEquals($value, $object->$get()); // setStyleValue - $object->setStyleValue("_{$key}", null); + $object->setStyleValue("{$key}", null); $this->assertEquals(null, $object->$get()); } } diff --git a/tests/PhpWord/Tests/Style/TableTest.php b/tests/PhpWord/Tests/Style/TableTest.php index a193f43d..e5c3ab66 100644 --- a/tests/PhpWord/Tests/Style/TableTest.php +++ b/tests/PhpWord/Tests/Style/TableTest.php @@ -143,9 +143,9 @@ class TableTest extends \PHPUnit_Framework_TestCase public function testSetStyleValue() { $object = new Table(); - $object->setStyleValue('_borderSize', 120); - $object->setStyleValue('_cellMargin', 240); - $object->setStyleValue('_borderColor', '999999'); + $object->setStyleValue('borderSize', 120); + $object->setStyleValue('cellMargin', 240); + $object->setStyleValue('borderColor', '999999'); $this->assertEquals( array(120, 120, 120, 120, 120, 120), From b594e32f04b6fbacdb506a43509650baa0632e59 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Tue, 8 Apr 2014 00:03:53 +0700 Subject: [PATCH 24/27] Move container files --- .../{Container/Container.php => Element/AbstractElement.php} | 0 src/PhpWord/{Container => Element}/Footer.php | 0 src/PhpWord/{Container => Element}/Header.php | 0 src/PhpWord/{Container => Element}/Section.php | 0 src/PhpWord/{Container/Settings.php => Style/Section.php} | 0 tests/PhpWord/Tests/{Container => Element}/FooterTest.php | 0 tests/PhpWord/Tests/{Container => Element}/HeaderTest.php | 0 tests/PhpWord/Tests/{Container => Element}/SectionTest.php | 0 .../Tests/{Container/SettingsTest.php => Style/SectionTest.php} | 0 9 files changed, 0 insertions(+), 0 deletions(-) rename src/PhpWord/{Container/Container.php => Element/AbstractElement.php} (100%) rename src/PhpWord/{Container => Element}/Footer.php (100%) rename src/PhpWord/{Container => Element}/Header.php (100%) rename src/PhpWord/{Container => Element}/Section.php (100%) rename src/PhpWord/{Container/Settings.php => Style/Section.php} (100%) rename tests/PhpWord/Tests/{Container => Element}/FooterTest.php (100%) rename tests/PhpWord/Tests/{Container => Element}/HeaderTest.php (100%) rename tests/PhpWord/Tests/{Container => Element}/SectionTest.php (100%) rename tests/PhpWord/Tests/{Container/SettingsTest.php => Style/SectionTest.php} (100%) diff --git a/src/PhpWord/Container/Container.php b/src/PhpWord/Element/AbstractElement.php similarity index 100% rename from src/PhpWord/Container/Container.php rename to src/PhpWord/Element/AbstractElement.php diff --git a/src/PhpWord/Container/Footer.php b/src/PhpWord/Element/Footer.php similarity index 100% rename from src/PhpWord/Container/Footer.php rename to src/PhpWord/Element/Footer.php diff --git a/src/PhpWord/Container/Header.php b/src/PhpWord/Element/Header.php similarity index 100% rename from src/PhpWord/Container/Header.php rename to src/PhpWord/Element/Header.php diff --git a/src/PhpWord/Container/Section.php b/src/PhpWord/Element/Section.php similarity index 100% rename from src/PhpWord/Container/Section.php rename to src/PhpWord/Element/Section.php diff --git a/src/PhpWord/Container/Settings.php b/src/PhpWord/Style/Section.php similarity index 100% rename from src/PhpWord/Container/Settings.php rename to src/PhpWord/Style/Section.php diff --git a/tests/PhpWord/Tests/Container/FooterTest.php b/tests/PhpWord/Tests/Element/FooterTest.php similarity index 100% rename from tests/PhpWord/Tests/Container/FooterTest.php rename to tests/PhpWord/Tests/Element/FooterTest.php diff --git a/tests/PhpWord/Tests/Container/HeaderTest.php b/tests/PhpWord/Tests/Element/HeaderTest.php similarity index 100% rename from tests/PhpWord/Tests/Container/HeaderTest.php rename to tests/PhpWord/Tests/Element/HeaderTest.php diff --git a/tests/PhpWord/Tests/Container/SectionTest.php b/tests/PhpWord/Tests/Element/SectionTest.php similarity index 100% rename from tests/PhpWord/Tests/Container/SectionTest.php rename to tests/PhpWord/Tests/Element/SectionTest.php diff --git a/tests/PhpWord/Tests/Container/SettingsTest.php b/tests/PhpWord/Tests/Style/SectionTest.php similarity index 100% rename from tests/PhpWord/Tests/Container/SettingsTest.php rename to tests/PhpWord/Tests/Style/SectionTest.php From f1108c48e60bdd3f509683a618481e03c9866258 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Tue, 8 Apr 2014 00:23:49 +0700 Subject: [PATCH 25/27] Add Abstract- prefix and -Interface suffix for corresponding classes --- CHANGELOG.md | 16 +- src/PhpWord/Element/AbstractElement.php | 225 ++++++++++++------ src/PhpWord/Element/Cell.php | 3 +- src/PhpWord/Element/CheckBox.php | 2 +- src/PhpWord/Element/Element.php | 106 --------- src/PhpWord/Element/Footer.php | 4 +- src/PhpWord/Element/Footnote.php | 3 +- src/PhpWord/Element/Header.php | 4 +- src/PhpWord/Element/Image.php | 5 +- src/PhpWord/Element/Link.php | 2 +- src/PhpWord/Element/ListItem.php | 2 +- src/PhpWord/Element/Object.php | 2 +- src/PhpWord/Element/PageBreak.php | 2 +- src/PhpWord/Element/PreserveText.php | 2 +- src/PhpWord/Element/Row.php | 2 +- src/PhpWord/Element/Section.php | 18 +- src/PhpWord/Element/Table.php | 2 +- src/PhpWord/Element/Text.php | 2 +- src/PhpWord/Element/TextBreak.php | 2 +- src/PhpWord/Element/TextRun.php | 3 +- src/PhpWord/Element/Title.php | 2 +- src/PhpWord/IOFactory.php | 8 +- src/PhpWord/PhpWord.php | 4 +- .../Reader/{Reader.php => AbstractReader.php} | 6 +- .../{IReader.php => ReaderInterface.php} | 4 +- src/PhpWord/Reader/Word2007.php | 4 +- src/PhpWord/Style/Section.php | 4 +- .../Writer/{Writer.php => AbstractWriter.php} | 2 +- src/PhpWord/Writer/ODText.php | 2 +- ...{WriterPart.php => AbstractWriterPart.php} | 2 +- src/PhpWord/Writer/ODText/Content.php | 2 +- src/PhpWord/Writer/ODText/Manifest.php | 2 +- src/PhpWord/Writer/ODText/Meta.php | 2 +- src/PhpWord/Writer/ODText/Mimetype.php | 2 +- src/PhpWord/Writer/ODText/Styles.php | 2 +- src/PhpWord/Writer/RTF.php | 2 +- src/PhpWord/Writer/Word2007.php | 4 +- ...{WriterPart.php => AbstractWriterPart.php} | 14 +- src/PhpWord/Writer/Word2007/Base.php | 8 +- src/PhpWord/Writer/Word2007/ContentTypes.php | 2 +- src/PhpWord/Writer/Word2007/DocProps.php | 2 +- src/PhpWord/Writer/Word2007/Document.php | 2 +- src/PhpWord/Writer/Word2007/Footer.php | 2 +- src/PhpWord/Writer/Word2007/Header.php | 2 +- src/PhpWord/Writer/Word2007/Rels.php | 2 +- .../{IWriter.php => WriterInterface.php} | 2 +- tests/PhpWord/Tests/Element/FooterTest.php | 8 +- tests/PhpWord/Tests/Element/HeaderTest.php | 8 +- tests/PhpWord/Tests/Element/PageBreakTest.php | 1 - tests/PhpWord/Tests/Element/SectionTest.php | 10 +- tests/PhpWord/Tests/MediaTest.php | 2 +- tests/PhpWord/Tests/PhpWordTest.php | 2 +- tests/PhpWord/Tests/Style/SectionTest.php | 36 +-- ...artTest.php => AbstractWriterPartTest.php} | 12 +- ...artTest.php => AbstractWriterPartTest.php} | 14 +- .../Tests/Writer/Word2007/FooterTest.php | 2 +- .../Tests/Writer/Word2007/HeaderTest.php | 2 +- 57 files changed, 284 insertions(+), 310 deletions(-) delete mode 100644 src/PhpWord/Element/Element.php rename src/PhpWord/Reader/{Reader.php => AbstractReader.php} (91%) rename src/PhpWord/Reader/{IReader.php => ReaderInterface.php} (86%) rename src/PhpWord/Writer/{Writer.php => AbstractWriter.php} (98%) rename src/PhpWord/Writer/ODText/{WriterPart.php => AbstractWriterPart.php} (73%) rename src/PhpWord/Writer/Word2007/{WriterPart.php => AbstractWriterPart.php} (80%) rename src/PhpWord/Writer/{IWriter.php => WriterInterface.php} (93%) rename tests/PhpWord/Tests/Writer/ODText/{WriterPartTest.php => AbstractWriterPartTest.php} (69%) rename tests/PhpWord/Tests/Writer/Word2007/{WriterPartTest.php => AbstractWriterPartTest.php} (66%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c3ed441..1e159009 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,8 +39,8 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - `createHeader` replaced by `addHeader` - `createFooter` replaced by `addFooter` - `createSection` replaced by `addSection` -- `Element\Footnote::getReferenceId` replaced by `Container\Container::getRelationId` -- `Element\Footnote::setReferenceId` replaced by `Container\Container::setRelationId` +- `Element\Footnote::getReferenceId` replaced by `Element\AbstractElement::getRelationId` +- `Element\Footnote::setReferenceId` replaced by `Element\AbstractElement::setRelationId` - `Footnote::addFootnoteLinkElement` replaced by `Media::addElement` - `Footnote::getFootnoteLinkElements` replaced by `Media::getElements` - All current methods on `Media` @@ -48,14 +48,14 @@ This is the changelog between releases of PHPWord. Releases are listed in revers ### Miscellaneous - Documentation: Simplify page level docblock - @ivanlanin GH-179 -- Writer: Refactor writer classes and make a new Writer abstract class - @ivanlanin GH-160 -- Reader: Rename AbstractReader > Reader - @ivanlanin -- General: Refactor folders: Element, Container, and Exception - @ivanlanin GH-187 +- Writer: Refactor writer classes and make a new AbstractWriter abstract class - @ivanlanin GH-160 +- General: Refactor folders: Element and Exception - @ivanlanin GH-187 - General: Remove legacy HashTable and ZipStreamWrapper and all related properties/methods - @ivanlanin GH-187 -- Container: Create new Container abstract class - @ivanlanin GH-187 -- Element: Create new Element abstract class - @ivanlanin GH-187 +- Element: Create new AbstractElement abstract class - @ivanlanin GH-187 - Media: Refactor media class to use one method for all docPart (section, header, footer, footnote) - @ivanlanin GH-187 -- General: Remove underscore prefix from all private properties name +- General: Remove underscore prefix from all private properties name - @ivanlanin GH-187 +- General: Move Section Settings to Style\Section - @ivanlanin GH-187 +- General: Give `Abstract` prefix and `Interface` suffix for all abstract classes and interfaces as per [PHP-FIG recommendation](https://github.com/php-fig/fig-standards/blob/master/bylaws/002-psr-naming-conventions.md) - @ivanlanin GH-187 ## 0.9.1 - 27 Mar 2014 diff --git a/src/PhpWord/Element/AbstractElement.php b/src/PhpWord/Element/AbstractElement.php index 2584a1f7..eabb1963 100644 --- a/src/PhpWord/Element/AbstractElement.php +++ b/src/PhpWord/Element/AbstractElement.php @@ -7,7 +7,7 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Container; +namespace PhpOffice\PhpWord\Element; use PhpOffice\PhpWord\Exception\InvalidImageException; use PhpOffice\PhpWord\Exception\InvalidObjectException; @@ -35,7 +35,7 @@ use PhpOffice\PhpWord\Element\CheckBox; * * @since 0.9.2 */ -abstract class Container extends Element +abstract class AbstractElement { /** * Container type section|header|footer|cell|textrun|footnote @@ -51,6 +51,28 @@ abstract class Container extends Element */ protected $sectionId; + /** + * Document part type: section|header|footer + * + * Used by textrun and cell container to determine where the element is + * located because it will affect the availability of other element, + * e.g. footnote will not be available when $docPart is header or footer. + * + * @var string + */ + private $docPart = 'section'; + + /** + * Document part Id + * + * For header and footer, this will be = ($sectionId - 1) * 3 + $index + * because the max number of header/footer in every page is 3, i.e. + * AUTO, FIRST, and EVEN (AUTO = ODD) + * + * @var integer + */ + private $docPartId = 1; + /** * Elements collection * @@ -340,6 +362,38 @@ abstract class Container extends Element return $this->sectionId; } + /** + * Set doc part + * + * @param string $docPart + * @param integer $docPartId + */ + public function setDocPart($docPart, $docPartId = 1) + { + $this->docPart = $docPart; + $this->docPartId = $docPartId; + } + + /** + * Get doc part + * + * @return string + */ + public function getDocPart() + { + return $this->docPart; + } + + /** + * Get doc part Id + * + * @return integer + */ + public function getDocPartId() + { + return $this->docPartId; + } + /** * Get all elements * @@ -372,6 +426,106 @@ abstract class Container extends Element $this->relationId = $rId; } + /** + * Check if element is located in section doc part (as opposed to header/footer) + * + * @return boolean + */ + public function isInSection() + { + return ($this->docPart == 'section'); + } + + /** + * Set style value + * + * @param mixed $styleObject Style object + * @param mixed $styleValue Style value + * @param boolean $returnObject Always return object + */ + protected function setStyle($styleObject, $styleValue = null, $returnObject = false) + { + if (!is_null($styleValue) && is_array($styleValue)) { + foreach ($styleValue as $key => $value) { + if (substr($key, 0, 1) == '_') { + $key = substr($key, 1); + } + $styleObject->setStyleValue($key, $value); + } + $style = $styleObject; + } else { + $style = $returnObject ? $styleObject : $styleValue; + } + + return $style; + } + + /** + * Check if a method is allowed for the current container + * + * @param string $method + * @return boolean + */ + private function checkValidity($method) + { + // Valid containers for each element + $allContainers = array('section', 'header', 'footer', 'cell', 'textrun', 'footnote'); + $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'), + 'preservetext' => array('header', 'footer', 'cell'), + 'relationid' => array('header', 'footer', 'footnote'), + 'title' => array('section'), + ); + // Special condition, e.g. preservetext can only exists in cell when + // the cell is located in header or footer + $validContainerInContainers = array( + 'preservetext' => array(array('cell'), array('header', 'footer')), + 'footnote' => array(array('cell', 'textrun'), array('section')), + ); + + // Check if a method is valid for current container + if (array_key_exists($method, $validContainers)) { + if (!in_array($this->container, $validContainers[$method])) { + throw new \BadMethodCallException(); + } + } + // Check if a method is valid for current container, located in other container + if (array_key_exists($method, $validContainerInContainers)) { + $rules = $validContainerInContainers[$method]; + $containers = $rules[0]; + $allowedDocParts = $rules[1]; + foreach ($containers as $container) { + if ($this->container == $container && !in_array($this->getDocPart(), $allowedDocParts)) { + throw new \BadMethodCallException(); + } + } + } + + return true; + } + + /** + * Return element location in document: section, headerx, or footerx + */ + private function checkElementDocPart() + { + $isCellTextrun = in_array($this->container, array('cell', 'textrun')); + $docPart = $isCellTextrun ? $this->getDocPart() : $this->container; + $docPartId = $isCellTextrun ? $this->getDocPartId() : $this->sectionId; + $inHeaderFooter = ($docPart == 'header' || $docPart == 'footer'); + + return $inHeaderFooter ? $docPart . $docPartId : $docPart; + } + /** * Add memory image element * @@ -408,71 +562,4 @@ abstract class Container extends Element { return $this->addFootnote($paragraphStyle); } - - /** - * Check if a method is allowed for the current container - * - * @param string $method - * @return boolean - */ - private function checkValidity($method) - { - // Empty array means the element can be accepted by all containers - $validContainers = array( - 'text' => array(), - 'link' => array(), - 'textbreak' => array(), - 'image' => array(), - 'object' => array(), - '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'), - 'preservetext' => array('header', 'footer', 'cell'), - 'relationid' => array('header', 'footer', 'footnote'), - 'title' => array('section'), - ); - // Special condition, e.g. preservetext can only exists in cell when - // the cell is located in header or footer - $validContainerInContainers = array( - 'preservetext' => array(array('cell'), array('header', 'footer')), - 'footnote' => array(array('cell', 'textrun'), array('section')), - ); - - // Check if a method is valid for current container - if (array_key_exists($method, $validContainers)) { - if (!empty($validContainers[$method])) { - if (!in_array($this->container, $validContainers[$method])) { - throw new \BadMethodCallException(); - } - } - } - // Check if a method is valid for current container, located in other container - if (array_key_exists($method, $validContainerInContainers)) { - $rules = $validContainerInContainers[$method]; - $containers = $rules[0]; - $allowedDocParts = $rules[1]; - foreach ($containers as $container) { - if ($this->container == $container && !in_array($this->getDocPart(), $allowedDocParts)) { - throw new \BadMethodCallException(); - } - } - } - - return true; - } - - /** - * Return element location in document: section, headerx, or footerx - */ - private function checkElementDocPart() - { - $isCellTextrun = in_array($this->container, array('cell', 'textrun')); - $docPart = $isCellTextrun ? $this->getDocPart() : $this->container; - $docPartId = $isCellTextrun ? $this->getDocPartId() : $this->sectionId; - $inHeaderFooter = ($docPart == 'header' || $docPart == 'footer'); - - return $inHeaderFooter ? $docPart . $docPartId : $docPart; - } } diff --git a/src/PhpWord/Element/Cell.php b/src/PhpWord/Element/Cell.php index 5e035fb7..2f92bce1 100755 --- a/src/PhpWord/Element/Cell.php +++ b/src/PhpWord/Element/Cell.php @@ -9,13 +9,12 @@ namespace PhpOffice\PhpWord\Element; -use PhpOffice\PhpWord\Container\Container; use PhpOffice\PhpWord\Style\Cell as CellStyle; /** * Table cell element */ -class Cell extends Container +class Cell extends AbstractElement { /** * Cell width diff --git a/src/PhpWord/Element/CheckBox.php b/src/PhpWord/Element/CheckBox.php index 187bd132..31f067d7 100644 --- a/src/PhpWord/Element/CheckBox.php +++ b/src/PhpWord/Element/CheckBox.php @@ -15,7 +15,7 @@ use PhpOffice\PhpWord\Style\Paragraph; /** * Check box element */ -class CheckBox extends Element +class CheckBox extends AbstractElement { /** * Name content diff --git a/src/PhpWord/Element/Element.php b/src/PhpWord/Element/Element.php deleted file mode 100644 index 6cebd0d5..00000000 --- a/src/PhpWord/Element/Element.php +++ /dev/null @@ -1,106 +0,0 @@ - $value) { - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } - $styleObject->setStyleValue($key, $value); - } - $style = $styleObject; - } else { - $style = $returnObject ? $styleObject : $styleValue; - } - - return $style; - } - - /** - * Set doc part - * - * @param string $docPart - * @param integer $docPartId - */ - public function setDocPart($docPart, $docPartId = 1) - { - $this->docPart = $docPart; - $this->docPartId = $docPartId; - } - - /** - * Get doc part - * - * @return string - */ - public function getDocPart() - { - return $this->docPart; - } - - /** - * Get doc part Id - * - * @return integer - */ - public function getDocPartId() - { - return $this->docPartId; - } - - /** - * Check if element is located in section doc part (as opposed to header/footer) - * - * @return boolean - */ - public function isInSection() - { - return ($this->docPart == 'section'); - } -} diff --git a/src/PhpWord/Element/Footer.php b/src/PhpWord/Element/Footer.php index 47287c8b..6d441126 100755 --- a/src/PhpWord/Element/Footer.php +++ b/src/PhpWord/Element/Footer.php @@ -7,12 +7,12 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Container; +namespace PhpOffice\PhpWord\Element; /** * Footer element */ -class Footer extends Container +class Footer extends AbstractElement { const AUTO = 'default'; // default and odd pages const FIRST = 'first'; diff --git a/src/PhpWord/Element/Footnote.php b/src/PhpWord/Element/Footnote.php index ff0c9960..e79e9c79 100644 --- a/src/PhpWord/Element/Footnote.php +++ b/src/PhpWord/Element/Footnote.php @@ -9,13 +9,12 @@ namespace PhpOffice\PhpWord\Element; -use PhpOffice\PhpWord\Container\Container; use PhpOffice\PhpWord\Style\Paragraph; /** * Footnote element */ -class Footnote extends Container +class Footnote extends AbstractElement { /** * Paragraph style diff --git a/src/PhpWord/Element/Header.php b/src/PhpWord/Element/Header.php index 2920e3cf..c14ed0e7 100755 --- a/src/PhpWord/Element/Header.php +++ b/src/PhpWord/Element/Header.php @@ -7,14 +7,14 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Container; +namespace PhpOffice\PhpWord\Element; use PhpOffice\PhpWord\Element\Image; /** * Header element */ -class Header extends Container +class Header extends AbstractElement { /** * Header types constants diff --git a/src/PhpWord/Element/Image.php b/src/PhpWord/Element/Image.php index fec8791c..bf29220b 100755 --- a/src/PhpWord/Element/Image.php +++ b/src/PhpWord/Element/Image.php @@ -16,7 +16,7 @@ use PhpOffice\PhpWord\Style\Image as ImageStyle; /** * Image element */ -class Image extends Element +class Image extends AbstractElement { /** * Image source @@ -138,9 +138,6 @@ class Image extends Element $this->source = $source; $this->isWatermark = $isWatermark; $this->style = $this->setStyle(new ImageStyle(), $style, true); - if (isset($style['wrappingStyle'])) { - $this->style->setWrappingStyle($style['wrappingStyle']); - } if ($this->style->getWidth() == null && $this->style->getHeight() == null) { $this->style->setWidth($imgData[0]); $this->style->setHeight($imgData[1]); diff --git a/src/PhpWord/Element/Link.php b/src/PhpWord/Element/Link.php index ee1af203..96c66339 100644 --- a/src/PhpWord/Element/Link.php +++ b/src/PhpWord/Element/Link.php @@ -15,7 +15,7 @@ use PhpOffice\PhpWord\Style\Paragraph; /** * Link element */ -class Link extends Element +class Link extends AbstractElement { /** * Link source diff --git a/src/PhpWord/Element/ListItem.php b/src/PhpWord/Element/ListItem.php index 6cf5d1d6..64ac149d 100644 --- a/src/PhpWord/Element/ListItem.php +++ b/src/PhpWord/Element/ListItem.php @@ -14,7 +14,7 @@ use PhpOffice\PhpWord\Style\ListItem as ListItemStyle; /** * List item element */ -class ListItem extends Element +class ListItem extends AbstractElement { /** * ListItem Style diff --git a/src/PhpWord/Element/Object.php b/src/PhpWord/Element/Object.php index 5716b7e6..f6b4161b 100644 --- a/src/PhpWord/Element/Object.php +++ b/src/PhpWord/Element/Object.php @@ -14,7 +14,7 @@ use PhpOffice\PhpWord\Style\Image as ImageStyle; /** * Object element */ -class Object extends Element +class Object extends AbstractElement { /** * Ole-Object Src diff --git a/src/PhpWord/Element/PageBreak.php b/src/PhpWord/Element/PageBreak.php index c297681d..d2f85f20 100644 --- a/src/PhpWord/Element/PageBreak.php +++ b/src/PhpWord/Element/PageBreak.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Element; /** * Page break element */ -class PageBreak extends Element +class PageBreak extends AbstractElement { /** * Create new page break diff --git a/src/PhpWord/Element/PreserveText.php b/src/PhpWord/Element/PreserveText.php index 0fe77d9a..76e860fe 100644 --- a/src/PhpWord/Element/PreserveText.php +++ b/src/PhpWord/Element/PreserveText.php @@ -15,7 +15,7 @@ use PhpOffice\PhpWord\Style\Paragraph; /** * Preserve text/field element */ -class PreserveText extends Element +class PreserveText extends AbstractElement { /** * Text content diff --git a/src/PhpWord/Element/Row.php b/src/PhpWord/Element/Row.php index 53990850..c20b018b 100644 --- a/src/PhpWord/Element/Row.php +++ b/src/PhpWord/Element/Row.php @@ -14,7 +14,7 @@ use PhpOffice\PhpWord\Style\Row as RowStyle; /** * Table row element */ -class Row extends Element +class Row extends AbstractElement { /** * Row height diff --git a/src/PhpWord/Element/Section.php b/src/PhpWord/Element/Section.php index dae4f26c..2be0794a 100644 --- a/src/PhpWord/Element/Section.php +++ b/src/PhpWord/Element/Section.php @@ -7,24 +7,24 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Container; +namespace PhpOffice\PhpWord\Element; use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\TOC; -use PhpOffice\PhpWord\Container\Footer; -use PhpOffice\PhpWord\Container\Header; -use PhpOffice\PhpWord\Container\Settings; +use PhpOffice\PhpWord\Element\Footer; +use PhpOffice\PhpWord\Element\Header; use PhpOffice\PhpWord\Element\PageBreak; +use PhpOffice\PhpWord\Style\Section as SectionSettings; /** * Section */ -class Section extends Container +class Section extends AbstractElement { /** * Section settings * - * @var Settings + * @var SectionSettings */ private $settings; @@ -53,7 +53,7 @@ class Section extends Container $this->container = 'section'; $this->sectionId = $sectionCount; $this->setDocPart($this->container, $this->sectionId); - $this->settings = new Settings(); + $this->settings = new SectionSettings(); $this->setSettings($settings); } @@ -77,7 +77,7 @@ class Section extends Container /** * Get Section Settings * - * @return Settings + * @return SectionSettings */ public function getSettings() { @@ -181,7 +181,7 @@ class Section extends Container private function addHeaderFooter($type = Header::AUTO, $header = true) { $collectionArray = $header ? 'headers' : 'footers'; - $containerClass = 'PhpOffice\\PhpWord\\Container\\'; + $containerClass = 'PhpOffice\\PhpWord\\Element\\'; $containerClass .= ($header ? 'Header' : 'Footer'); $collection = &$this->$collectionArray; diff --git a/src/PhpWord/Element/Table.php b/src/PhpWord/Element/Table.php index 5808a065..0c8d494d 100644 --- a/src/PhpWord/Element/Table.php +++ b/src/PhpWord/Element/Table.php @@ -15,7 +15,7 @@ use PhpOffice\PhpWord\Style\Table as TableStyle; /** * Table element */ -class Table extends Element +class Table extends AbstractElement { /** * Table style diff --git a/src/PhpWord/Element/Text.php b/src/PhpWord/Element/Text.php index 620095b8..bb1d5aac 100644 --- a/src/PhpWord/Element/Text.php +++ b/src/PhpWord/Element/Text.php @@ -15,7 +15,7 @@ use PhpOffice\PhpWord\Style\Paragraph; /** * Text element */ -class Text extends Element +class Text extends AbstractElement { /** * Text content diff --git a/src/PhpWord/Element/TextBreak.php b/src/PhpWord/Element/TextBreak.php index adf144ba..aa3d5cca 100755 --- a/src/PhpWord/Element/TextBreak.php +++ b/src/PhpWord/Element/TextBreak.php @@ -15,7 +15,7 @@ use PhpOffice\PhpWord\Style\Paragraph; /** * Text break element */ -class TextBreak extends Element +class TextBreak extends AbstractElement { /** * Paragraph style diff --git a/src/PhpWord/Element/TextRun.php b/src/PhpWord/Element/TextRun.php index 6be08fc4..f7286d8f 100755 --- a/src/PhpWord/Element/TextRun.php +++ b/src/PhpWord/Element/TextRun.php @@ -9,13 +9,12 @@ namespace PhpOffice\PhpWord\Element; -use PhpOffice\PhpWord\Container\Container; use PhpOffice\PhpWord\Style\Paragraph; /** * Textrun/paragraph element */ -class TextRun extends Container +class TextRun extends AbstractElement { /** * Paragraph style diff --git a/src/PhpWord/Element/Title.php b/src/PhpWord/Element/Title.php index c8c29a35..5ed1bfbc 100644 --- a/src/PhpWord/Element/Title.php +++ b/src/PhpWord/Element/Title.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Element; /** * Title element */ -class Title extends Element +class Title extends AbstractElement { /** * Title Text content diff --git a/src/PhpWord/IOFactory.php b/src/PhpWord/IOFactory.php index eeafb057..7b32ef44 100644 --- a/src/PhpWord/IOFactory.php +++ b/src/PhpWord/IOFactory.php @@ -21,12 +21,12 @@ abstract class IOFactory * * @param \PhpOffice\PhpWord\PhpWord $phpWord * @param string $name - * @return \PhpOffice\PhpWord\Writer\IWriter + * @return \PhpOffice\PhpWord\Writer\WriterInterface * @throws Exception */ public static function createWriter(PhpWord $phpWord, $name = 'Word2007') { - if ($name !== 'IWriter' && $name !== 'ODText' && $name !== 'RTF' && $name !== 'Word2007') { + if ($name !== 'WriterInterface' && $name !== 'ODText' && $name !== 'RTF' && $name !== 'Word2007') { throw new Exception("\"{$name}\" is not a valid writer."); } @@ -38,12 +38,12 @@ abstract class IOFactory * Create new reader * * @param string $name - * @return \PhpOffice\PhpWord\Reader\IReader + * @return \PhpOffice\PhpWord\Reader\ReaderInterface * @throws Exception */ public static function createReader($name = 'Word2007') { - if ($name !== 'IReader' && $name !== 'Word2007') { + if ($name !== 'ReaderInterface' && $name !== 'Word2007') { throw new Exception("\"{$name}\" is not a valid reader."); } diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index 447706af..63d26a09 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -11,7 +11,7 @@ namespace PhpOffice\PhpWord; use PhpOffice\PhpWord\DocumentProperties; use PhpOffice\PhpWord\Exception\Exception; -use PhpOffice\PhpWord\Container\Section; +use PhpOffice\PhpWord\Element\Section; use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Template; @@ -217,7 +217,7 @@ class PhpWord /** * Get all sections * - * @return \PhpOffice\PhpWord\Container\Section[] + * @return \PhpOffice\PhpWord\Element\Section[] */ public function getSections() { diff --git a/src/PhpWord/Reader/Reader.php b/src/PhpWord/Reader/AbstractReader.php similarity index 91% rename from src/PhpWord/Reader/Reader.php rename to src/PhpWord/Reader/AbstractReader.php index e93b4636..10c864f3 100644 --- a/src/PhpWord/Reader/Reader.php +++ b/src/PhpWord/Reader/AbstractReader.php @@ -16,7 +16,7 @@ use PhpOffice\PhpWord\Exception\Exception; * * @codeCoverageIgnore Abstract class */ -abstract class Reader implements IReader +abstract class AbstractReader implements ReaderInterface { /** * Read data only? @@ -47,7 +47,7 @@ abstract class Reader implements IReader * Set read data only * * @param bool $pValue - * @return \PhpOffice\PhpWord\Reader\IReader + * @return \PhpOffice\PhpWord\Reader\ReaderInterface */ public function setReadDataOnly($pValue = true) { @@ -77,7 +77,7 @@ abstract class Reader implements IReader } /** - * Can the current IReader read the file? + * Can the current ReaderInterface read the file? * * @param string $pFilename * @return bool diff --git a/src/PhpWord/Reader/IReader.php b/src/PhpWord/Reader/ReaderInterface.php similarity index 86% rename from src/PhpWord/Reader/IReader.php rename to src/PhpWord/Reader/ReaderInterface.php index a4bea94e..2829d4ab 100644 --- a/src/PhpWord/Reader/IReader.php +++ b/src/PhpWord/Reader/ReaderInterface.php @@ -12,10 +12,10 @@ namespace PhpOffice\PhpWord\Reader; /** * Reader interface */ -interface IReader +interface ReaderInterface { /** - * Can the current IReader read the file? + * Can the current ReaderInterface read the file? * * @param string $pFilename * @return boolean diff --git a/src/PhpWord/Reader/Word2007.php b/src/PhpWord/Reader/Word2007.php index 2cdb7f9a..3d26bb63 100644 --- a/src/PhpWord/Reader/Word2007.php +++ b/src/PhpWord/Reader/Word2007.php @@ -17,10 +17,10 @@ use PhpOffice\PhpWord\Exception\Exception; /** * Reader for Word2007 */ -class Word2007 extends Reader implements IReader +class Word2007 extends AbstractReader implements ReaderInterface { /** - * Can the current IReader read the file? + * Can the current ReaderInterface read the file? * * @param string $pFilename * @return bool diff --git a/src/PhpWord/Style/Section.php b/src/PhpWord/Style/Section.php index 762d45c3..52352434 100644 --- a/src/PhpWord/Style/Section.php +++ b/src/PhpWord/Style/Section.php @@ -7,12 +7,12 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Container; +namespace PhpOffice\PhpWord\Style; /** * Section settings */ -class Settings +class Section { /** * Default Page Size Width diff --git a/src/PhpWord/Writer/Writer.php b/src/PhpWord/Writer/AbstractWriter.php similarity index 98% rename from src/PhpWord/Writer/Writer.php rename to src/PhpWord/Writer/AbstractWriter.php index e53d215a..b4f4b765 100644 --- a/src/PhpWord/Writer/Writer.php +++ b/src/PhpWord/Writer/AbstractWriter.php @@ -17,7 +17,7 @@ use PhpOffice\PhpWord\PhpWord; * * @since 0.9.2 */ -abstract class Writer implements IWriter +abstract class AbstractWriter implements WriterInterface { /** * PHPWord object diff --git a/src/PhpWord/Writer/ODText.php b/src/PhpWord/Writer/ODText.php index 10e90dd3..7e1da98c 100755 --- a/src/PhpWord/Writer/ODText.php +++ b/src/PhpWord/Writer/ODText.php @@ -21,7 +21,7 @@ use PhpOffice\PhpWord\Writer\ODText\Styles; /** * ODText writer */ -class ODText extends Writer implements IWriter +class ODText extends AbstractWriter implements WriterInterface { /** * Create new ODText writer diff --git a/src/PhpWord/Writer/ODText/WriterPart.php b/src/PhpWord/Writer/ODText/AbstractWriterPart.php similarity index 73% rename from src/PhpWord/Writer/ODText/WriterPart.php rename to src/PhpWord/Writer/ODText/AbstractWriterPart.php index a6fa93cb..1a5831e0 100644 --- a/src/PhpWord/Writer/ODText/WriterPart.php +++ b/src/PhpWord/Writer/ODText/AbstractWriterPart.php @@ -12,6 +12,6 @@ namespace PhpOffice\PhpWord\Writer\ODText; /** * ODText writer part abstract */ -abstract class WriterPart extends \PhpOffice\PhpWord\Writer\Word2007\WriterPart +abstract class AbstractWriterPart extends \PhpOffice\PhpWord\Writer\Word2007\AbstractWriterPart { } diff --git a/src/PhpWord/Writer/ODText/Content.php b/src/PhpWord/Writer/ODText/Content.php index 7fc0d2df..b902ffb3 100644 --- a/src/PhpWord/Writer/ODText/Content.php +++ b/src/PhpWord/Writer/ODText/Content.php @@ -29,7 +29,7 @@ use PhpOffice\PhpWord\TOC; /** * ODText content part writer */ -class Content extends WriterPart +class Content extends AbstractWriterPart { /** * Write content file to XML format diff --git a/src/PhpWord/Writer/ODText/Manifest.php b/src/PhpWord/Writer/ODText/Manifest.php index ecda1918..e94a1fd1 100755 --- a/src/PhpWord/Writer/ODText/Manifest.php +++ b/src/PhpWord/Writer/ODText/Manifest.php @@ -15,7 +15,7 @@ use PhpOffice\PhpWord\PhpWord; /** * ODText manifest part writer */ -class Manifest extends WriterPart +class Manifest extends AbstractWriterPart { /** * Write Manifest file to XML format diff --git a/src/PhpWord/Writer/ODText/Meta.php b/src/PhpWord/Writer/ODText/Meta.php index fc5314e9..0df77827 100644 --- a/src/PhpWord/Writer/ODText/Meta.php +++ b/src/PhpWord/Writer/ODText/Meta.php @@ -14,7 +14,7 @@ use PhpOffice\PhpWord\PhpWord; /** * ODText meta part writer */ -class Meta extends WriterPart +class Meta extends AbstractWriterPart { /** * Write Meta file to XML format diff --git a/src/PhpWord/Writer/ODText/Mimetype.php b/src/PhpWord/Writer/ODText/Mimetype.php index 1e713f2c..b8bc6539 100644 --- a/src/PhpWord/Writer/ODText/Mimetype.php +++ b/src/PhpWord/Writer/ODText/Mimetype.php @@ -14,7 +14,7 @@ use PhpOffice\PhpWord\PhpWord; /** * ODText mimetype part writer */ -class Mimetype extends WriterPart +class Mimetype extends AbstractWriterPart { /** * Write Mimetype to Text format diff --git a/src/PhpWord/Writer/ODText/Styles.php b/src/PhpWord/Writer/ODText/Styles.php index 8839f568..306ee5fc 100644 --- a/src/PhpWord/Writer/ODText/Styles.php +++ b/src/PhpWord/Writer/ODText/Styles.php @@ -18,7 +18,7 @@ use PhpOffice\PhpWord\Style\Table; /** * ODText styloes part writer */ -class Styles extends WriterPart +class Styles extends AbstractWriterPart { /** * Write Styles file to XML format diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index 8698365f..2505a76d 100755 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -29,7 +29,7 @@ use PhpOffice\PhpWord\TOC; /** * RTF writer */ -class RTF extends Writer implements IWriter +class RTF extends AbstractWriter implements WriterInterface { /** * Color register diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index 8fe90339..b426d570 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -26,7 +26,7 @@ use PhpOffice\PhpWord\Writer\Word2007\Styles; /** * Word2007 writer */ -class Word2007 extends Writer implements IWriter +class Word2007 extends AbstractWriter implements WriterInterface { /** * Content types values @@ -229,7 +229,7 @@ class Word2007 extends Writer implements IWriter /** * Add header/footer content * - * @param \PhpOffice\PhpWord\Container\Section $section + * @param \PhpOffice\PhpWord\Element\Section $section * @param mixed $objZip * @param string $elmType * @param integer $rID diff --git a/src/PhpWord/Writer/Word2007/WriterPart.php b/src/PhpWord/Writer/Word2007/AbstractWriterPart.php similarity index 80% rename from src/PhpWord/Writer/Word2007/WriterPart.php rename to src/PhpWord/Writer/Word2007/AbstractWriterPart.php index a61662e9..b58797db 100755 --- a/src/PhpWord/Writer/Word2007/WriterPart.php +++ b/src/PhpWord/Writer/Word2007/AbstractWriterPart.php @@ -10,27 +10,27 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\Exception\Exception; -use PhpOffice\PhpWord\Writer\IWriter; +use PhpOffice\PhpWord\Writer\WriterInterface; use PhpOffice\PhpWord\Shared\XMLWriter; /** * Word2007 writer part abstract class */ -abstract class WriterPart +abstract class AbstractWriterPart { /** * Parent writer * - * @var IWriter + * @var WriterInterface */ protected $parentWriter; /** * Set parent writer * - * @param IWriter $pWriter + * @param WriterInterface $pWriter */ - public function setParentWriter(IWriter $pWriter = null) + public function setParentWriter(WriterInterface $pWriter = null) { $this->parentWriter = $pWriter; } @@ -38,7 +38,7 @@ abstract class WriterPart /** * Get parent writer * - * @return IWriter + * @return WriterInterface * @throws Exception */ public function getParentWriter() @@ -46,7 +46,7 @@ abstract class WriterPart if (!is_null($this->parentWriter)) { return $this->parentWriter; } else { - throw new Exception("No parent IWriter assigned."); + throw new Exception("No parent WriterInterface assigned."); } } diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index 762bc7ea..a5f9a5e0 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -11,7 +11,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Container\Container; +use PhpOffice\PhpWord\Element\AbstractElement; use PhpOffice\PhpWord\Element\Text; use PhpOffice\PhpWord\Element\TextRun; use PhpOffice\PhpWord\Element\Link; @@ -37,7 +37,7 @@ use PhpOffice\PhpWord\Style\Image as ImageStyle; * * Write common parts of document.xml, headerx.xml, and footerx.xml */ -class Base extends WriterPart +class Base extends AbstractWriterPart { /** * Write text element @@ -1121,9 +1121,9 @@ class Base extends WriterPart * Write container elements * * @param XMLWriter $xmlWriter - * @param Container $container + * @param AbstractElement $container */ - protected function writeContainerElements(XMLWriter $xmlWriter, Container $container) + protected function writeContainerElements(XMLWriter $xmlWriter, AbstractElement $container) { // Check allowed elements $elmCommon = array('Text', 'Link', 'TextBreak', 'Image', 'Object'); diff --git a/src/PhpWord/Writer/Word2007/ContentTypes.php b/src/PhpWord/Writer/Word2007/ContentTypes.php index 8eb10bc1..a27ef43c 100755 --- a/src/PhpWord/Writer/Word2007/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/ContentTypes.php @@ -15,7 +15,7 @@ use PhpOffice\PhpWord\Shared\XMLWriter; /** * Word2007 contenttypes part writer */ -class ContentTypes extends WriterPart +class ContentTypes extends AbstractWriterPart { /** * Write [Content_Types].xml diff --git a/src/PhpWord/Writer/Word2007/DocProps.php b/src/PhpWord/Writer/Word2007/DocProps.php index cfa238e5..5818d511 100644 --- a/src/PhpWord/Writer/Word2007/DocProps.php +++ b/src/PhpWord/Writer/Word2007/DocProps.php @@ -14,7 +14,7 @@ use PhpOffice\PhpWord\PhpWord; /** * Word2007 contenttypes part writer */ -class DocProps extends WriterPart +class DocProps extends AbstractWriterPart { /** * Write docProps/app.xml diff --git a/src/PhpWord/Writer/Word2007/Document.php b/src/PhpWord/Writer/Word2007/Document.php index 2b5e6e95..9cc53375 100644 --- a/src/PhpWord/Writer/Word2007/Document.php +++ b/src/PhpWord/Writer/Word2007/Document.php @@ -11,7 +11,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\TOC; -use PhpOffice\PhpWord\Container\Section; +use PhpOffice\PhpWord\Element\Section; use PhpOffice\PhpWord\Element\PageBreak; use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Style\Font; diff --git a/src/PhpWord/Writer/Word2007/Footer.php b/src/PhpWord/Writer/Word2007/Footer.php index 4b075b97..dc726114 100644 --- a/src/PhpWord/Writer/Word2007/Footer.php +++ b/src/PhpWord/Writer/Word2007/Footer.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007; -use PhpOffice\PhpWord\Container\Footer as FooterElement; +use PhpOffice\PhpWord\Element\Footer as FooterElement; /** * Word2007 footer part writer diff --git a/src/PhpWord/Writer/Word2007/Header.php b/src/PhpWord/Writer/Word2007/Header.php index 3f649261..95e57340 100644 --- a/src/PhpWord/Writer/Word2007/Header.php +++ b/src/PhpWord/Writer/Word2007/Header.php @@ -9,7 +9,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007; -use PhpOffice\PhpWord\Container\Header as HeaderElement; +use PhpOffice\PhpWord\Element\Header as HeaderElement; /** * Word2007 header part writer diff --git a/src/PhpWord/Writer/Word2007/Rels.php b/src/PhpWord/Writer/Word2007/Rels.php index d7552410..c708eeaf 100755 --- a/src/PhpWord/Writer/Word2007/Rels.php +++ b/src/PhpWord/Writer/Word2007/Rels.php @@ -17,7 +17,7 @@ use PhpOffice\PhpWord\Shared\XMLWriter; * * @since 0.9.2 */ -class Rels extends WriterPart +class Rels extends AbstractWriterPart { /** * Base relationship URL diff --git a/src/PhpWord/Writer/IWriter.php b/src/PhpWord/Writer/WriterInterface.php similarity index 93% rename from src/PhpWord/Writer/IWriter.php rename to src/PhpWord/Writer/WriterInterface.php index ea69b830..2c225b2c 100755 --- a/src/PhpWord/Writer/IWriter.php +++ b/src/PhpWord/Writer/WriterInterface.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Writer; /** * Writer interface */ -interface IWriter +interface WriterInterface { /** * Save PhpWord to file diff --git a/tests/PhpWord/Tests/Element/FooterTest.php b/tests/PhpWord/Tests/Element/FooterTest.php index db982dc5..d4201149 100644 --- a/tests/PhpWord/Tests/Element/FooterTest.php +++ b/tests/PhpWord/Tests/Element/FooterTest.php @@ -7,12 +7,12 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Container; +namespace PhpOffice\PhpWord\Tests\Element; -use PhpOffice\PhpWord\Container\Footer; +use PhpOffice\PhpWord\Element\Footer; /** - * Test class for PhpOffice\PhpWord\Container\Footer + * Test class for PhpOffice\PhpWord\Element\Footer * * @runTestsInSeparateProcesses */ @@ -26,7 +26,7 @@ class FooterTest extends \PHPUnit_Framework_TestCase $iVal = rand(1, 1000); $oFooter = new Footer($iVal); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Container\\Footer', $oFooter); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Footer', $oFooter); $this->assertEquals($oFooter->getSectionId(), $iVal); } diff --git a/tests/PhpWord/Tests/Element/HeaderTest.php b/tests/PhpWord/Tests/Element/HeaderTest.php index ef1703f3..849dd220 100644 --- a/tests/PhpWord/Tests/Element/HeaderTest.php +++ b/tests/PhpWord/Tests/Element/HeaderTest.php @@ -7,12 +7,12 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Container; +namespace PhpOffice\PhpWord\Tests\Element; -use PhpOffice\PhpWord\Container\Header; +use PhpOffice\PhpWord\Element\Header; /** - * Test class for PhpOffice\PhpWord\Container\Header + * Test class for PhpOffice\PhpWord\Element\Header * * @runTestsInSeparateProcesses */ @@ -26,7 +26,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $iVal = rand(1, 1000); $oHeader = new Header($iVal); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Container\\Header', $oHeader); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Header', $oHeader); $this->assertEquals($oHeader->getSectionId(), $iVal); $this->assertEquals($oHeader->getType(), Header::AUTO); } diff --git a/tests/PhpWord/Tests/Element/PageBreakTest.php b/tests/PhpWord/Tests/Element/PageBreakTest.php index 3dbaad87..0c6379f5 100644 --- a/tests/PhpWord/Tests/Element/PageBreakTest.php +++ b/tests/PhpWord/Tests/Element/PageBreakTest.php @@ -24,7 +24,6 @@ class PageBreakTest extends \PHPUnit_Framework_TestCase */ public function testConstruct() { - // Section Settings $oPageBreak = new PageBreak(); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\PageBreak', $oPageBreak); diff --git a/tests/PhpWord/Tests/Element/SectionTest.php b/tests/PhpWord/Tests/Element/SectionTest.php index e077ff82..d41a1e1f 100644 --- a/tests/PhpWord/Tests/Element/SectionTest.php +++ b/tests/PhpWord/Tests/Element/SectionTest.php @@ -7,15 +7,15 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Container; +namespace PhpOffice\PhpWord\Tests\Element; use PhpOffice\PhpWord\Exception\Exception; -use PhpOffice\PhpWord\Container\Section; -use PhpOffice\PhpWord\Container\Header; +use PhpOffice\PhpWord\Element\Section; +use PhpOffice\PhpWord\Element\Header; use PhpOffice\PhpWord\Style; /** - * Test class for PhpOffice\PhpWord\Container\Section + * Test class for PhpOffice\PhpWord\Element\Section * * @runTestsInSeparateProcesses */ @@ -139,7 +139,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase foreach ($elements as $element) { $method = "create{$element}"; - $this->assertInstanceOf("PhpOffice\\PhpWord\\Container\\{$element}", $object->$method()); + $this->assertInstanceOf("PhpOffice\\PhpWord\\Element\\{$element}", $object->$method()); } $this->assertFalse($object->hasDifferentFirstPage()); } diff --git a/tests/PhpWord/Tests/MediaTest.php b/tests/PhpWord/Tests/MediaTest.php index 14a38c2d..93dfaf87 100644 --- a/tests/PhpWord/Tests/MediaTest.php +++ b/tests/PhpWord/Tests/MediaTest.php @@ -10,7 +10,7 @@ namespace PhpOffice\PhpWord\Tests; use PhpOffice\PhpWord\Media; -use PhpOffice\PhpWord\Container\Section; +use PhpOffice\PhpWord\Element\Section; use PhpOffice\PhpWord\Element\Image; /** diff --git a/tests/PhpWord/Tests/PhpWordTest.php b/tests/PhpWord/Tests/PhpWordTest.php index 829ddfff..efef7053 100644 --- a/tests/PhpWord/Tests/PhpWordTest.php +++ b/tests/PhpWord/Tests/PhpWordTest.php @@ -11,7 +11,7 @@ namespace PhpOffice\PhpWord\Tests; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\DocumentProperties; -use PhpOffice\PhpWord\Container\Section; +use PhpOffice\PhpWord\Element\Section; use PhpOffice\PhpWord\Style; /** diff --git a/tests/PhpWord/Tests/Style/SectionTest.php b/tests/PhpWord/Tests/Style/SectionTest.php index 3d5ae43a..542a0f25 100644 --- a/tests/PhpWord/Tests/Style/SectionTest.php +++ b/tests/PhpWord/Tests/Style/SectionTest.php @@ -7,14 +7,14 @@ * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ -namespace PhpOffice\PhpWord\Tests\Container; +namespace PhpOffice\PhpWord\Tests\Style; -use PhpOffice\PhpWord\Container\Settings; +use PhpOffice\PhpWord\Style\Section; /** - * Test class for PhpOffice\PhpWord\Container\Settings + * Test class for PhpOffice\PhpWord\Style\Section * - * @coversDefaultClass \PhpOffice\PhpWord\Container\Settings + * @coversDefaultClass \PhpOffice\PhpWord\Element\Section * @runTestsInSeparateProcesses */ class SettingsTest extends \PHPUnit_Framework_TestCase @@ -25,7 +25,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase public function testSettingValue() { // Section Settings - $oSettings = new Settings(); + $oSettings = new Section(); $oSettings->setSettingValue('_orientation', 'landscape'); $this->assertEquals('landscape', $oSettings->getOrientation()); @@ -63,7 +63,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase public function testMargin() { // Section Settings - $oSettings = new Settings(); + $oSettings = new Section(); $iVal = rand(1, 1000); $oSettings->setMarginTop($iVal); @@ -88,7 +88,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase public function testOrientationLandscape() { // Section Settings - $oSettings = new Settings(); + $oSettings = new Section(); $oSettings->setLandscape(); $this->assertEquals('landscape', $oSettings->getOrientation()); @@ -102,7 +102,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase public function testOrientationPortrait() { // Section Settings - $oSettings = new Settings(); + $oSettings = new Section(); $oSettings->setPortrait(); $this->assertNull($oSettings->getOrientation()); @@ -116,7 +116,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase public function testBorderSize() { // Section Settings - $oSettings = new Settings(); + $oSettings = new Section(); $iVal = rand(1, 1000); $oSettings->setBorderSize($iVal); @@ -149,7 +149,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase public function testBorderColor() { // Section Settings - $oSettings = new Settings(); + $oSettings = new Section(); $oSettings->setBorderColor('FF00AA'); $this->assertEquals(array('FF00AA', 'FF00AA', 'FF00AA', 'FF00AA'), $oSettings->getBorderColor()); @@ -177,7 +177,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase public function testNumberingStart() { // Section Settings - $oSettings = new Settings(); + $oSettings = new Section(); $this->assertNull($oSettings->getPageNumberingStart()); @@ -194,7 +194,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase */ public function testHeader() { - $oSettings = new Settings(); + $oSettings = new Section(); $this->assertEquals(720, $oSettings->getHeaderHeight()); @@ -212,7 +212,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase public function testFooter() { // Section Settings - $oSettings = new Settings(); + $oSettings = new Section(); $this->assertEquals(720, $oSettings->getFooterHeight()); @@ -230,7 +230,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase public function testColumnsNum() { // Section Settings - $oSettings = new Settings(); + $oSettings = new Section(); // Default $this->assertEquals(1, $oSettings->getColsNum()); @@ -249,16 +249,16 @@ class SettingsTest extends \PHPUnit_Framework_TestCase public function testColumnsSpace() { // Section Settings - $oSettings = new Settings(); + $oSettings = new Section(); // Default $this->assertEquals(720, $oSettings->getColsSpace()); $iVal = rand(1, 1000); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Container\\Settings', $oSettings->setColsSpace($iVal)); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Section', $oSettings->setColsSpace($iVal)); $this->assertEquals($iVal, $oSettings->getColsSpace()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Container\\Settings', $oSettings->setColsSpace()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Section', $oSettings->setColsSpace()); $this->assertEquals(720, $oSettings->getColsSpace()); } @@ -268,7 +268,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase public function testBreakType() { // Section Settings - $oSettings = new Settings(); + $oSettings = new Section(); $this->assertNull($oSettings->getBreakType()); diff --git a/tests/PhpWord/Tests/Writer/ODText/WriterPartTest.php b/tests/PhpWord/Tests/Writer/ODText/AbstractWriterPartTest.php similarity index 69% rename from tests/PhpWord/Tests/Writer/ODText/WriterPartTest.php rename to tests/PhpWord/Tests/Writer/ODText/AbstractWriterPartTest.php index 19f17ee5..a8863087 100644 --- a/tests/PhpWord/Tests/Writer/ODText/WriterPartTest.php +++ b/tests/PhpWord/Tests/Writer/ODText/AbstractWriterPartTest.php @@ -12,12 +12,12 @@ use PhpOffice\PhpWord\Writer\ODText; use PhpWord\Tests\TestHelperDOCX; /** - * Test class for PhpOffice\PhpWord\Writer\ODText\WriterPart + * Test class for PhpOffice\PhpWord\Writer\ODText\AbstractWriterPart * - * @coversDefaultClass \PhpOffice\PhpWord\Writer\ODText\WriterPart + * @coversDefaultClass \PhpOffice\PhpWord\Writer\ODText\AbstractWriterPart * @runTestsInSeparateProcesses */ -class WriterPartTest extends \PHPUnit_Framework_TestCase +class AbstractWriterPartTest extends \PHPUnit_Framework_TestCase { /** * covers ::setParentWriter @@ -26,7 +26,7 @@ class WriterPartTest extends \PHPUnit_Framework_TestCase public function testSetGetParentWriter() { $object = $this->getMockForAbstractClass( - 'PhpOffice\\PhpWord\\Writer\\ODText\\WriterPart' + 'PhpOffice\\PhpWord\\Writer\\ODText\\AbstractWriterPart' ); $object->setParentWriter(new ODText()); $this->assertEquals( @@ -38,12 +38,12 @@ class WriterPartTest extends \PHPUnit_Framework_TestCase /** * covers ::getParentWriter * @expectedException Exception - * @expectedExceptionMessage No parent IWriter assigned. + * @expectedExceptionMessage No parent WriterInterface assigned. */ public function testSetGetParentWriterNull() { $object = $this->getMockForAbstractClass( - 'PhpOffice\\PhpWord\\Writer\\ODText\\WriterPart' + 'PhpOffice\\PhpWord\\Writer\\ODText\\AbstractWriterPart' ); $object->getParentWriter(); } diff --git a/tests/PhpWord/Tests/Writer/Word2007/WriterPartTest.php b/tests/PhpWord/Tests/Writer/Word2007/AbstractWriterPartTest.php similarity index 66% rename from tests/PhpWord/Tests/Writer/Word2007/WriterPartTest.php rename to tests/PhpWord/Tests/Writer/Word2007/AbstractWriterPartTest.php index 5cbe9eb9..33051d6f 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/WriterPartTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/AbstractWriterPartTest.php @@ -8,17 +8,17 @@ */ namespace PhpOffice\PhpWord\Tests\Writer\Word2007; -use PhpOffice\PhpWord\Writer\Word2007\WriterPart; +use PhpOffice\PhpWord\Writer\Word2007\AbstractWriterPart; use PhpOffice\PhpWord\Writer\Word2007; use PhpWord\Tests\TestHelperDOCX; /** - * Test class for PhpOffice\PhpWord\Writer\Word2007\WriterPart + * Test class for PhpOffice\PhpWord\Writer\Word2007\AbstractWriterPart * - * @coversDefaultClass \PhpOffice\PhpWord\Writer\Word2007\WriterPart + * @coversDefaultClass \PhpOffice\PhpWord\Writer\Word2007\AbstractWriterPart * @runTestsInSeparateProcesses */ -class WriterPartTest extends \PHPUnit_Framework_TestCase +class AbstractWriterPartTest extends \PHPUnit_Framework_TestCase { /** * covers ::setParentWriter @@ -27,7 +27,7 @@ class WriterPartTest extends \PHPUnit_Framework_TestCase public function testSetGetParentWriter() { $object = $this->getMockForAbstractClass( - 'PhpOffice\\PhpWord\\Writer\\Word2007\\WriterPart' + 'PhpOffice\\PhpWord\\Writer\\Word2007\\AbstractWriterPart' ); $object->setParentWriter(new Word2007()); $this->assertEquals( @@ -39,12 +39,12 @@ class WriterPartTest extends \PHPUnit_Framework_TestCase /** * covers ::getParentWriter * @expectedException Exception - * @expectedExceptionMessage No parent IWriter assigned. + * @expectedExceptionMessage No parent WriterInterface assigned. */ public function testSetGetParentWriterNull() { $object = $this->getMockForAbstractClass( - 'PhpOffice\\PhpWord\\Writer\\Word2007\\WriterPart' + 'PhpOffice\\PhpWord\\Writer\\Word2007\\AbstractWriterPart' ); $object->getParentWriter(); } diff --git a/tests/PhpWord/Tests/Writer/Word2007/FooterTest.php b/tests/PhpWord/Tests/Writer/Word2007/FooterTest.php index 04e86832..b1bff02b 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/FooterTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/FooterTest.php @@ -28,7 +28,7 @@ class FooterTest extends \PHPUnit_Framework_TestCase public function testWriteFooter() { $imageSrc = __DIR__ . "/../../_files/images/PhpWord.png"; - $container = new \PhpOffice\PhpWord\Container\Footer(1); + $container = new \PhpOffice\PhpWord\Element\Footer(1); $container->addText(''); $container->addPreserveText(''); $container->addTextBreak(); diff --git a/tests/PhpWord/Tests/Writer/Word2007/HeaderTest.php b/tests/PhpWord/Tests/Writer/Word2007/HeaderTest.php index c468d7a7..b836b619 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/HeaderTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/HeaderTest.php @@ -26,7 +26,7 @@ class HeaderTest extends \PHPUnit_Framework_TestCase { $imageSrc = __DIR__ . "/../../_files/images/PhpWord.png"; - $container = new \PhpOffice\PhpWord\Container\Header(1); + $container = new \PhpOffice\PhpWord\Element\Header(1); $container->addText('Test'); $container->addPreserveText(''); $container->addTextBreak(); From 3ef0f41c252aa3a3a4591bfa5660aaae8bedc389 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Tue, 8 Apr 2014 03:03:14 +0700 Subject: [PATCH 26/27] New `Style\AbstractStyle` and 'ODText\Base` --- CHANGELOG.md | 14 ++-- src/PhpWord/Style/AbstractStyle.php | 43 ++++++++++++ src/PhpWord/Style/Cell.php | 2 +- src/PhpWord/Style/Font.php | 19 +----- src/PhpWord/Style/Image.php | 30 +++------ src/PhpWord/Style/ListItem.php | 16 +---- src/PhpWord/Style/Paragraph.php | 2 +- src/PhpWord/Style/Row.php | 16 +---- src/PhpWord/Style/Section.php | 2 +- src/PhpWord/Style/TOC.php | 2 +- src/PhpWord/Style/Tab.php | 2 +- src/PhpWord/Style/Table.php | 2 +- src/PhpWord/Style/Tabs.php | 2 +- src/PhpWord/Writer/AbstractWriter.php | 52 +++++++++++--- src/PhpWord/Writer/ODText.php | 31 ++------- src/PhpWord/Writer/ODText/Base.php | 93 ++++++++++++++++++++++++++ src/PhpWord/Writer/ODText/Content.php | 62 ++--------------- src/PhpWord/Writer/ODText/Manifest.php | 2 +- src/PhpWord/Writer/ODText/Meta.php | 2 +- src/PhpWord/Writer/ODText/Styles.php | 61 +---------------- src/PhpWord/Writer/Word2007.php | 32 ++------- 21 files changed, 225 insertions(+), 262 deletions(-) create mode 100644 src/PhpWord/Style/AbstractStyle.php create mode 100644 src/PhpWord/Writer/ODText/Base.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e159009..bc43822b 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ This is the changelog between releases of PHPWord. Releases are listed in revers ## 0.9.2 - Not yet released +This release marked heavy refactorings on internal code structure with the creation of some abstract classes to reduce code duplication. `Element` subnamespace is introduced in this release to replace `Section`. + ### Features - Image: Get image dimensions without EXIF extension - @andrew-kzoo GH-184 @@ -48,14 +50,16 @@ This is the changelog between releases of PHPWord. Releases are listed in revers ### Miscellaneous - Documentation: Simplify page level docblock - @ivanlanin GH-179 -- Writer: Refactor writer classes and make a new AbstractWriter abstract class - @ivanlanin GH-160 -- General: Refactor folders: Element and Exception - @ivanlanin GH-187 -- General: Remove legacy HashTable and ZipStreamWrapper and all related properties/methods - @ivanlanin GH-187 -- Element: Create new AbstractElement abstract class - @ivanlanin GH-187 +- Writer: Refactor writer classes and create a new `Write\AbstractWriter` abstract class - @ivanlanin GH-160 +- General: Refactor folders: `Element` and `Exception` - @ivanlanin GH-187 +- General: Remove legacy `HashTable` and `Shared\ZipStreamWrapper` and all related properties/methods - @ivanlanin GH-187 +- Element: New `AbstractElement` abstract class - @ivanlanin GH-187 - Media: Refactor media class to use one method for all docPart (section, header, footer, footnote) - @ivanlanin GH-187 - General: Remove underscore prefix from all private properties name - @ivanlanin GH-187 -- General: Move Section Settings to Style\Section - @ivanlanin GH-187 +- General: Move Section `Settings` to `Style\Section` - @ivanlanin GH-187 - General: Give `Abstract` prefix and `Interface` suffix for all abstract classes and interfaces as per [PHP-FIG recommendation](https://github.com/php-fig/fig-standards/blob/master/bylaws/002-psr-naming-conventions.md) - @ivanlanin GH-187 +- Style: New `Style\AbstractStyle` abstract class - @ivanlanin GH-187 +- Writer: New 'ODText\Base` class - @ivanlanin GH-187 ## 0.9.1 - 27 Mar 2014 diff --git a/src/PhpWord/Style/AbstractStyle.php b/src/PhpWord/Style/AbstractStyle.php new file mode 100644 index 00000000..c17ea6e2 --- /dev/null +++ b/src/PhpWord/Style/AbstractStyle.php @@ -0,0 +1,43 @@ +$method($value); + } + } +} diff --git a/src/PhpWord/Style/Cell.php b/src/PhpWord/Style/Cell.php index 1fbed6e7..2e1f9d79 100644 --- a/src/PhpWord/Style/Cell.php +++ b/src/PhpWord/Style/Cell.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Style; /** * Table cell style */ -class Cell +class Cell extends AbstractStyle { const TEXT_DIR_BTLR = 'btLr'; const TEXT_DIR_TBRL = 'tbRl'; diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index 999fc3fa..2fef3a66 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -15,7 +15,7 @@ use PhpOffice\PhpWord\Exception\InvalidStyleException; /** * Font style */ -class Font +class Font extends AbstractStyle { const UNDERLINE_NONE = 'none'; const UNDERLINE_DASH = 'dash'; @@ -202,23 +202,6 @@ class Font return $this; } - /** - * Set style value - * - * @param string $key - * @param mixed $value - */ - public function setStyleValue($key, $value) - { - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } - $method = 'set' . $key; - if (method_exists($this, $method)) { - $this->$method($value); - } - } - /** * Get font name * diff --git a/src/PhpWord/Style/Image.php b/src/PhpWord/Style/Image.php index e973f1ac..49060607 100644 --- a/src/PhpWord/Style/Image.php +++ b/src/PhpWord/Style/Image.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Style; /** * Image and memory image style */ -class Image +class Image extends AbstractStyle { const WRAPPING_STYLE_INLINE = 'inline'; const WRAPPING_STYLE_SQUARE = 'square'; @@ -41,13 +41,6 @@ class Image */ private $align; - /** - * Wrapping style - * - * @var string - */ - private $wrappingStyle; - /** * Margin Top * @@ -62,6 +55,13 @@ class Image */ private $marginLeft; + /** + * Wrapping style + * + * @var string + */ + private $wrappingStyle; + /** * Create new image style */ @@ -75,20 +75,6 @@ class Image $this->setWrappingStyle(self::WRAPPING_STYLE_INLINE); } - /** - * Set style value - * - * @param string $key - * @param mixed $value - */ - public function setStyleValue($key, $value) - { - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } - $this->$key = $value; - } - /** * Get width */ diff --git a/src/PhpWord/Style/ListItem.php b/src/PhpWord/Style/ListItem.php index 6681be16..ecc4274c 100644 --- a/src/PhpWord/Style/ListItem.php +++ b/src/PhpWord/Style/ListItem.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Style; /** * List item style */ -class ListItem +class ListItem extends AbstractStyle { const TYPE_NUMBER = 7; const TYPE_NUMBER_NESTED = 8; @@ -34,20 +34,6 @@ class ListItem $this->listType = self::TYPE_BULLET_FILLED; } - /** - * Set style value - * - * @param string $key - * @param string $value - */ - public function setStyleValue($key, $value) - { - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } - $this->$key = $value; - } - /** * Set List Type * diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index c55d9db1..8feb7924 100755 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -14,7 +14,7 @@ use PhpOffice\PhpWord\Exception\InvalidStyleException; /** * Paragraph style */ -class Paragraph +class Paragraph extends AbstractStyle { const LINE_HEIGHT = 240; diff --git a/src/PhpWord/Style/Row.php b/src/PhpWord/Style/Row.php index 113baaf2..d4dc642f 100644 --- a/src/PhpWord/Style/Row.php +++ b/src/PhpWord/Style/Row.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Style; /** * Table row style */ -class Row +class Row extends AbstractStyle { /** * Repeat table row on every new page @@ -42,20 +42,6 @@ class Row { } - /** - * Set style value - * - * @param string $key - * @param mixed $value - */ - public function setStyleValue($key, $value) - { - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } - $this->$key = $value; - } - /** * Set tblHeader * diff --git a/src/PhpWord/Style/Section.php b/src/PhpWord/Style/Section.php index 52352434..005ca1e5 100644 --- a/src/PhpWord/Style/Section.php +++ b/src/PhpWord/Style/Section.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Style; /** * Section settings */ -class Section +class Section extends AbstractStyle { /** * Default Page Size Width diff --git a/src/PhpWord/Style/TOC.php b/src/PhpWord/Style/TOC.php index f49d9847..f7a752a9 100644 --- a/src/PhpWord/Style/TOC.php +++ b/src/PhpWord/Style/TOC.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Style; /** * TOC style */ -class TOC +class TOC extends AbstractStyle { const TABLEADER_DOT = 'dot'; const TABLEADER_UNDERSCORE = 'underscore'; diff --git a/src/PhpWord/Style/Tab.php b/src/PhpWord/Style/Tab.php index 59757ea8..c28d8923 100644 --- a/src/PhpWord/Style/Tab.php +++ b/src/PhpWord/Style/Tab.php @@ -14,7 +14,7 @@ use PhpOffice\PhpWord\Shared\XMLWriter; /** * Tab style */ -class Tab +class Tab extends AbstractStyle { /** * Tab Stop Type diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index 178c2854..f7c98c36 100755 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Style; /** * Table style */ -class Table +class Table extends AbstractStyle { /** * Style for first row diff --git a/src/PhpWord/Style/Tabs.php b/src/PhpWord/Style/Tabs.php index 497e09f1..66137c0e 100755 --- a/src/PhpWord/Style/Tabs.php +++ b/src/PhpWord/Style/Tabs.php @@ -14,7 +14,7 @@ use PhpOffice\PhpWord\Shared\XMLWriter; /** * Tabs style */ -class Tabs +class Tabs extends AbstractStyle { /** * Tabs diff --git a/src/PhpWord/Writer/AbstractWriter.php b/src/PhpWord/Writer/AbstractWriter.php index b4f4b765..faa13ea6 100644 --- a/src/PhpWord/Writer/AbstractWriter.php +++ b/src/PhpWord/Writer/AbstractWriter.php @@ -11,6 +11,7 @@ namespace PhpOffice\PhpWord\Writer; use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\Settings; /** * Abstract writer class @@ -148,21 +149,21 @@ abstract class AbstractWriter implements WriterInterface /** * Get temporary file name * - * If $pFilename is php://output or php://stdout, make it a temporary file + * If $filename is php://output or php://stdout, make it a temporary file * - * @param string $pFilename + * @param string $filename * @return string */ - protected function getTempFile($pFilename) + protected function getTempFile($filename) { - $this->originalFilename = $pFilename; - if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { - $pFilename = @tempnam(sys_get_temp_dir(), 'phpword_'); - if ($pFilename == '') { - $pFilename = $this->originalFilename; + $this->originalFilename = $filename; + if (strtolower($filename) == 'php://output' || strtolower($filename) == 'php://stdout') { + $filename = @tempnam(sys_get_temp_dir(), 'phpword_'); + if ($filename == '') { + $filename = $this->originalFilename; } } - $this->tempFilename = $pFilename; + $this->tempFilename = $filename; return $this->tempFilename; } @@ -181,4 +182,37 @@ abstract class AbstractWriter implements WriterInterface @unlink($this->tempFilename); } } + + /** + * Get ZipArchive object + * + * @param string $filename + * @return mixed ZipArchive object + */ + protected function getZipArchive($filename) + { + // Create new ZIP file and open it for writing + $zipClass = Settings::getZipClass(); + $objZip = new $zipClass(); + + // Retrieve OVERWRITE and CREATE constants from the instantiated zip class + // This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP + $ro = new \ReflectionObject($objZip); + $zipOverWrite = $ro->getConstant('OVERWRITE'); + $zipCreate = $ro->getConstant('CREATE'); + + // Remove any existing file + if (file_exists($filename)) { + unlink($filename); + } + + // Try opening the ZIP file + if ($objZip->open($filename, $zipOverWrite) !== true) { + if ($objZip->open($filename, $zipCreate) !== true) { + throw new Exception("Could not open " . $filename . " for writing."); + } + } + + return $objZip; + } } diff --git a/src/PhpWord/Writer/ODText.php b/src/PhpWord/Writer/ODText.php index 7e1da98c..e3b82f4d 100755 --- a/src/PhpWord/Writer/ODText.php +++ b/src/PhpWord/Writer/ODText.php @@ -47,35 +47,14 @@ class ODText extends AbstractWriter implements WriterInterface /** * Save PhpWord to file * - * @param string $pFilename + * @param string $filename * @throws Exception */ - public function save($pFilename = null) + public function save($filename = null) { if (!is_null($this->phpWord)) { - $pFilename = $this->getTempFile($pFilename); - - // Create new ZIP file and open it for writing - $zipClass = Settings::getZipClass(); - $objZip = new $zipClass(); - - // Retrieve OVERWRITE and CREATE constants from the instantiated zip class - // This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP - $ro = new \ReflectionObject($objZip); - $zipOverWrite = $ro->getConstant('OVERWRITE'); - $zipCreate = $ro->getConstant('CREATE'); - - // Remove any existing file - if (file_exists($pFilename)) { - unlink($pFilename); - } - - // Try opening the ZIP file - if ($objZip->open($pFilename, $zipOverWrite) !== true) { - if ($objZip->open($pFilename, $zipCreate) !== true) { - throw new Exception("Could not open " . $pFilename . " for writing."); - } - } + $filename = $this->getTempFile($filename); + $objZip = $this->getZipArchive($filename); // Add mimetype to ZIP file //@todo Not in \ZipArchive::CM_STORE mode @@ -95,7 +74,7 @@ class ODText extends AbstractWriter implements WriterInterface // Close file if ($objZip->close() === false) { - throw new Exception("Could not close zip file $pFilename."); + throw new Exception("Could not close zip file $filename."); } $this->cleanupTempFile(); diff --git a/src/PhpWord/Writer/ODText/Base.php b/src/PhpWord/Writer/ODText/Base.php new file mode 100644 index 00000000..a1f0d315 --- /dev/null +++ b/src/PhpWord/Writer/ODText/Base.php @@ -0,0 +1,93 @@ +writeAttribute('office:version', '1.2'); + $xmlWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0'); + $xmlWriter->writeAttribute('xmlns:style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0'); + $xmlWriter->writeAttribute('xmlns:text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0'); + $xmlWriter->writeAttribute('xmlns:table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0'); + $xmlWriter->writeAttribute('xmlns:draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0'); + $xmlWriter->writeAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0'); + $xmlWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink'); + $xmlWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/'); + $xmlWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0'); + $xmlWriter->writeAttribute('xmlns:number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0'); + $xmlWriter->writeAttribute('xmlns:svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0'); + $xmlWriter->writeAttribute('xmlns:chart', 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0'); + $xmlWriter->writeAttribute('xmlns:dr3d', 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0'); + $xmlWriter->writeAttribute('xmlns:math', 'http://www.w3.org/1998/Math/MathML'); + $xmlWriter->writeAttribute('xmlns:form', 'urn:oasis:names:tc:opendocument:xmlns:form:1.0'); + $xmlWriter->writeAttribute('xmlns:script', 'urn:oasis:names:tc:opendocument:xmlns:script:1.0'); + $xmlWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office'); + $xmlWriter->writeAttribute('xmlns:ooow', 'http://openoffice.org/2004/writer'); + $xmlWriter->writeAttribute('xmlns:oooc', 'http://openoffice.org/2004/calc'); + $xmlWriter->writeAttribute('xmlns:dom', 'http://www.w3.org/2001/xml-events'); + $xmlWriter->writeAttribute('xmlns:rpt', 'http://openoffice.org/2005/report'); + $xmlWriter->writeAttribute('xmlns:of', 'urn:oasis:names:tc:opendocument:xmlns:of:1.2'); + $xmlWriter->writeAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml'); + $xmlWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#'); + $xmlWriter->writeAttribute('xmlns:tableooo', 'http://openoffice.org/2009/table'); + $xmlWriter->writeAttribute('xmlns:css3t', 'http://www.w3.org/TR/css3-text/'); + } + + /** + * Write font faces declaration + */ + protected function writeFontFaces(XMLWriter $xmlWriter) + { + $xmlWriter->startElement('office:font-face-decls'); + $arrFonts = array(); + $styles = Style::getStyles(); + $numFonts = 0; + if (count($styles) > 0) { + foreach ($styles as $styleName => $style) { + // Font + if ($style instanceof Font) { + $numFonts++; + $name = $style->getName(); + if (!in_array($name, $arrFonts)) { + $arrFonts[] = $name; + + // style:font-face + $xmlWriter->startElement('style:font-face'); + $xmlWriter->writeAttribute('style:name', $name); + $xmlWriter->writeAttribute('svg:font-family', $name); + $xmlWriter->endElement(); + } + } + } + } + if (!in_array(PhpWord::DEFAULT_FONT_NAME, $arrFonts)) { + $xmlWriter->startElement('style:font-face'); + $xmlWriter->writeAttribute('style:name', PhpWord::DEFAULT_FONT_NAME); + $xmlWriter->writeAttribute('svg:font-family', PhpWord::DEFAULT_FONT_NAME); + $xmlWriter->endElement(); + } + $xmlWriter->endElement(); + } +} diff --git a/src/PhpWord/Writer/ODText/Content.php b/src/PhpWord/Writer/ODText/Content.php index b902ffb3..327aed34 100644 --- a/src/PhpWord/Writer/ODText/Content.php +++ b/src/PhpWord/Writer/ODText/Content.php @@ -29,7 +29,7 @@ use PhpOffice\PhpWord\TOC; /** * ODText content part writer */ -class Content extends AbstractWriterPart +class Content extends Base { /** * Write content file to XML format @@ -51,38 +51,12 @@ class Content extends AbstractWriterPart // office:document-content $xmlWriter->startElement('office:document-content'); - $xmlWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0'); - $xmlWriter->writeAttribute('xmlns:style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0'); - $xmlWriter->writeAttribute('xmlns:text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0'); - $xmlWriter->writeAttribute('xmlns:table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0'); - $xmlWriter->writeAttribute('xmlns:draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0'); - $xmlWriter->writeAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0'); - $xmlWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink'); - $xmlWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/'); - $xmlWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0'); - $xmlWriter->writeAttribute('xmlns:number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0'); - $xmlWriter->writeAttribute('xmlns:svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0'); - $xmlWriter->writeAttribute('xmlns:chart', 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0'); - $xmlWriter->writeAttribute('xmlns:dr3d', 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0'); - $xmlWriter->writeAttribute('xmlns:math', 'http://www.w3.org/1998/Math/MathML'); - $xmlWriter->writeAttribute('xmlns:form', 'urn:oasis:names:tc:opendocument:xmlns:form:1.0'); - $xmlWriter->writeAttribute('xmlns:script', 'urn:oasis:names:tc:opendocument:xmlns:script:1.0'); - $xmlWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office'); - $xmlWriter->writeAttribute('xmlns:ooow', 'http://openoffice.org/2004/writer'); - $xmlWriter->writeAttribute('xmlns:oooc', 'http://openoffice.org/2004/calc'); - $xmlWriter->writeAttribute('xmlns:dom', 'http://www.w3.org/2001/xml-events'); + $this->writeCommonRootAttributes($xmlWriter); $xmlWriter->writeAttribute('xmlns:xforms', 'http://www.w3.org/2002/xforms'); $xmlWriter->writeAttribute('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema'); $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); - $xmlWriter->writeAttribute('xmlns:rpt', 'http://openoffice.org/2005/report'); - $xmlWriter->writeAttribute('xmlns:of', 'urn:oasis:names:tc:opendocument:xmlns:of:1.2'); - $xmlWriter->writeAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml'); - $xmlWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#'); - $xmlWriter->writeAttribute('xmlns:tableooo', 'http://openoffice.org/2009/table'); $xmlWriter->writeAttribute('xmlns:field', 'urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0'); $xmlWriter->writeAttribute('xmlns:formx', 'urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0'); - $xmlWriter->writeAttribute('xmlns:css3t', 'http://www.w3.org/TR/css3-text/'); - $xmlWriter->writeAttribute('office:version', '1.2'); // We firstly search all fonts used $sections = $phpWord->getSections(); @@ -122,37 +96,9 @@ class Content extends AbstractWriterPart } // office:font-face-decls - $xmlWriter->startElement('office:font-face-decls'); - $arrFonts = array(); - - $styles = Style::getStyles(); - $numFonts = 0; - if (count($styles) > 0) { - foreach ($styles as $styleName => $style) { - // Font - if ($style instanceof Font) { - $numFonts++; - $name = $style->getName(); - if (!in_array($name, $arrFonts)) { - $arrFonts[] = $name; - - // style:font-face - $xmlWriter->startElement('style:font-face'); - $xmlWriter->writeAttribute('style:name', $name); - $xmlWriter->writeAttribute('svg:font-family', $name); - $xmlWriter->endElement(); - } - } - } - if (!in_array(PhpWord::DEFAULT_FONT_NAME, $arrFonts)) { - $xmlWriter->startElement('style:font-face'); - $xmlWriter->writeAttribute('style:name', PhpWord::DEFAULT_FONT_NAME); - $xmlWriter->writeAttribute('svg:font-family', PhpWord::DEFAULT_FONT_NAME); - $xmlWriter->endElement(); - } - } - $xmlWriter->endElement(); + $this->writeFontFaces($xmlWriter); + // office:automatic-styles $xmlWriter->startElement('office:automatic-styles'); $styles = Style::getStyles(); $numPStyles = 0; diff --git a/src/PhpWord/Writer/ODText/Manifest.php b/src/PhpWord/Writer/ODText/Manifest.php index e94a1fd1..106ddfc5 100755 --- a/src/PhpWord/Writer/ODText/Manifest.php +++ b/src/PhpWord/Writer/ODText/Manifest.php @@ -33,8 +33,8 @@ class Manifest extends AbstractWriterPart // manifest:manifest $xmlWriter->startElement('manifest:manifest'); - $xmlWriter->writeAttribute('xmlns:manifest', 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0'); $xmlWriter->writeAttribute('manifest:version', '1.2'); + $xmlWriter->writeAttribute('xmlns:manifest', 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0'); // manifest:file-entry $xmlWriter->startElement('manifest:file-entry'); diff --git a/src/PhpWord/Writer/ODText/Meta.php b/src/PhpWord/Writer/ODText/Meta.php index 0df77827..8feb0d30 100644 --- a/src/PhpWord/Writer/ODText/Meta.php +++ b/src/PhpWord/Writer/ODText/Meta.php @@ -36,13 +36,13 @@ class Meta extends AbstractWriterPart // office:document-meta $xmlWriter->startElement('office:document-meta'); + $xmlWriter->writeAttribute('office:version', '1.2'); $xmlWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0'); $xmlWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink'); $xmlWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/'); $xmlWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0'); $xmlWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office'); $xmlWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#'); - $xmlWriter->writeAttribute('office:version', '1.2'); // office:meta $xmlWriter->startElement('office:meta'); diff --git a/src/PhpWord/Writer/ODText/Styles.php b/src/PhpWord/Writer/ODText/Styles.php index 306ee5fc..53175165 100644 --- a/src/PhpWord/Writer/ODText/Styles.php +++ b/src/PhpWord/Writer/ODText/Styles.php @@ -18,7 +18,7 @@ use PhpOffice\PhpWord\Style\Table; /** * ODText styloes part writer */ -class Styles extends AbstractWriterPart +class Styles extends Base { /** * Write Styles file to XML format @@ -40,65 +40,10 @@ class Styles extends AbstractWriterPart // Styles:Styles $xmlWriter->startElement('office:document-styles'); - $xmlWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0'); - $xmlWriter->writeAttribute('xmlns:style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0'); - $xmlWriter->writeAttribute('xmlns:text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0'); - $xmlWriter->writeAttribute('xmlns:table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0'); - $xmlWriter->writeAttribute('xmlns:draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0'); - $xmlWriter->writeAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0'); - $xmlWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink'); - $xmlWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/'); - $xmlWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0'); - $xmlWriter->writeAttribute('xmlns:number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0'); - $xmlWriter->writeAttribute('xmlns:svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0'); - $xmlWriter->writeAttribute('xmlns:chart', 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0'); - $xmlWriter->writeAttribute('xmlns:dr3d', 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0'); - $xmlWriter->writeAttribute('xmlns:math', 'http://www.w3.org/1998/Math/MathML'); - $xmlWriter->writeAttribute('xmlns:form', 'urn:oasis:names:tc:opendocument:xmlns:form:1.0'); - $xmlWriter->writeAttribute('xmlns:script', 'urn:oasis:names:tc:opendocument:xmlns:script:1.0'); - $xmlWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office'); - $xmlWriter->writeAttribute('xmlns:ooow', 'http://openoffice.org/2004/writer'); - $xmlWriter->writeAttribute('xmlns:oooc', 'http://openoffice.org/2004/calc'); - $xmlWriter->writeAttribute('xmlns:dom', 'http://www.w3.org/2001/xml-events'); - $xmlWriter->writeAttribute('xmlns:rpt', 'http://openoffice.org/2005/report'); - $xmlWriter->writeAttribute('xmlns:of', 'urn:oasis:names:tc:opendocument:xmlns:of:1.2'); - $xmlWriter->writeAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml'); - $xmlWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#'); - $xmlWriter->writeAttribute('xmlns:tableooo', 'http://openoffice.org/2009/table'); - $xmlWriter->writeAttribute('xmlns:css3t', 'http://www.w3.org/TR/css3-text/'); - $xmlWriter->writeAttribute('office:version', '1.2'); - + $this->writeCommonRootAttributes($xmlWriter); // office:font-face-decls - $xmlWriter->startElement('office:font-face-decls'); - $arrFonts = array(); - $styles = Style::getStyles(); - $numFonts = 0; - if (count($styles) > 0) { - foreach ($styles as $styleName => $style) { - // Font - if ($style instanceof Font) { - $numFonts++; - $name = $style->getName(); - if (!in_array($name, $arrFonts)) { - $arrFonts[] = $name; - - // style:font-face - $xmlWriter->startElement('style:font-face'); - $xmlWriter->writeAttribute('style:name', $name); - $xmlWriter->writeAttribute('svg:font-family', $name); - $xmlWriter->endElement(); - } - } - } - } - if (!in_array(PhpWord::DEFAULT_FONT_NAME, $arrFonts)) { - $xmlWriter->startElement('style:font-face'); - $xmlWriter->writeAttribute('style:name', PhpWord::DEFAULT_FONT_NAME); - $xmlWriter->writeAttribute('svg:font-family', PhpWord::DEFAULT_FONT_NAME); - $xmlWriter->endElement(); - } - $xmlWriter->endElement(); + $this->writeFontFaces($xmlWriter); // office:styles $xmlWriter->startElement('office:styles'); diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index b426d570..5a489542 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -13,7 +13,6 @@ use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Footnote; use PhpOffice\PhpWord\Media; -use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Writer\Word2007\ContentTypes; use PhpOffice\PhpWord\Writer\Word2007\Rels; use PhpOffice\PhpWord\Writer\Word2007\DocProps; @@ -69,34 +68,13 @@ class Word2007 extends AbstractWriter implements WriterInterface /** * Save document by name * - * @param string $pFilename + * @param string $filename */ - public function save($pFilename = null) + public function save($filename = null) { if (!is_null($this->phpWord)) { - $pFilename = $this->getTempFile($pFilename); - - // Create new ZIP file and open it for writing - $zipClass = Settings::getZipClass(); - $objZip = new $zipClass(); - - // Retrieve OVERWRITE and CREATE constants from the instantiated zip class - // This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP - $ro = new \ReflectionObject($objZip); - $zipOverWrite = $ro->getConstant('OVERWRITE'); - $zipCreate = $ro->getConstant('CREATE'); - - // Remove any existing file - if (file_exists($pFilename)) { - unlink($pFilename); - } - - // Try opening the ZIP file - if ($objZip->open($pFilename, $zipOverWrite) !== true) { - if ($objZip->open($pFilename, $zipCreate) !== true) { - throw new Exception("Could not open " . $pFilename . " for writing."); - } - } + $filename = $this->getTempFile($filename); + $objZip = $this->getZipArchive($filename); // Content types $this->cTypes['default'] = array( @@ -156,7 +134,7 @@ class Word2007 extends AbstractWriter implements WriterInterface // Close file if ($objZip->close() === false) { - throw new Exception("Could not close zip file $pFilename."); + throw new Exception("Could not close zip file $filename."); } $this->cleanupTempFile(); From cd2dba084891b3525db82a0663517cb942d4577e Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Tue, 8 Apr 2014 03:27:19 +0700 Subject: [PATCH 27/27] Element inheritance refinements --- src/PhpWord/Element/AbstractElement.php | 2 -- src/PhpWord/Element/Image.php | 27 ------------------------- src/PhpWord/Element/Link.php | 27 ------------------------- src/PhpWord/Element/Object.php | 27 ------------------------- src/PhpWord/Reader/AbstractReader.php | 5 ++++- src/PhpWord/Template.php | 17 +++++----------- src/PhpWord/Writer/ODText.php | 1 - src/PhpWord/Writer/ODText/Manifest.php | 1 - 8 files changed, 9 insertions(+), 98 deletions(-) diff --git a/src/PhpWord/Element/AbstractElement.php b/src/PhpWord/Element/AbstractElement.php index eabb1963..d9f9984d 100644 --- a/src/PhpWord/Element/AbstractElement.php +++ b/src/PhpWord/Element/AbstractElement.php @@ -9,7 +9,6 @@ namespace PhpOffice\PhpWord\Element; -use PhpOffice\PhpWord\Exception\InvalidImageException; use PhpOffice\PhpWord\Exception\InvalidObjectException; use PhpOffice\PhpWord\Media; use PhpOffice\PhpWord\Style; @@ -482,7 +481,6 @@ abstract class AbstractElement 'table' => array('section', 'header', 'footer'), 'footnote' => array('section', 'textrun', 'cell'), 'preservetext' => array('header', 'footer', 'cell'), - 'relationid' => array('header', 'footer', 'footnote'), 'title' => array('section'), ); // Special condition, e.g. preservetext can only exists in cell when diff --git a/src/PhpWord/Element/Image.php b/src/PhpWord/Element/Image.php index bf29220b..d49972d2 100755 --- a/src/PhpWord/Element/Image.php +++ b/src/PhpWord/Element/Image.php @@ -32,13 +32,6 @@ class Image extends AbstractElement */ private $style; - /** - * Image relation ID specific only for DOCX - * - * @var string - */ - private $rId; - /** * Is watermark * @@ -155,26 +148,6 @@ class Image extends AbstractElement return $this->style; } - /** - * Get image relation ID - * - * @return int - */ - public function getRelationId() - { - return $this->rId; - } - - /** - * Set image relation ID - * - * @param int $rId - */ - public function setRelationId($rId) - { - $this->rId = $rId; - } - /** * Get image source * diff --git a/src/PhpWord/Element/Link.php b/src/PhpWord/Element/Link.php index 96c66339..1cf35624 100644 --- a/src/PhpWord/Element/Link.php +++ b/src/PhpWord/Element/Link.php @@ -31,13 +31,6 @@ class Link extends AbstractElement */ private $name; - /** - * Link Relation ID - * - * @var string - */ - private $relationId; - /** * Font style * @@ -71,26 +64,6 @@ class Link extends AbstractElement return $this; } - /** - * Get Link Relation ID - * - * @return int - */ - public function getRelationId() - { - return $this->relationId; - } - - /** - * Set Link Relation ID - * - * @param int $rId - */ - public function setRelationId($rId) - { - $this->relationId = $rId; - } - /** * Get Link source * diff --git a/src/PhpWord/Element/Object.php b/src/PhpWord/Element/Object.php index f6b4161b..058117c9 100644 --- a/src/PhpWord/Element/Object.php +++ b/src/PhpWord/Element/Object.php @@ -30,13 +30,6 @@ class Object extends AbstractElement */ private $style; - /** - * Object Relation ID - * - * @var int - */ - private $relationId; - /** * Image Relation ID * @@ -84,26 +77,6 @@ class Object extends AbstractElement return $this->source; } - /** - * Get Object Relation ID - * - * @return int - */ - public function getRelationId() - { - return $this->relationId; - } - - /** - * Set Object Relation ID - * - * @param int $rId - */ - public function setRelationId($rId) - { - $this->relationId = $rId; - } - /** * Get Image Relation ID * diff --git a/src/PhpWord/Reader/AbstractReader.php b/src/PhpWord/Reader/AbstractReader.php index 10c864f3..cf43a858 100644 --- a/src/PhpWord/Reader/AbstractReader.php +++ b/src/PhpWord/Reader/AbstractReader.php @@ -90,7 +90,10 @@ abstract class AbstractReader implements ReaderInterface } catch (Exception $e) { return false; } - fclose($this->fileHandle); + if (is_resource($this->fileHandle)) { + fclose($this->fileHandle); + } + return true; } } diff --git a/src/PhpWord/Template.php b/src/PhpWord/Template.php index 30c2ee08..b0be592d 100644 --- a/src/PhpWord/Template.php +++ b/src/PhpWord/Template.php @@ -222,7 +222,7 @@ class Template * @param string $blockname * @param integer $clones * @param boolean $replace - * @return null + * @return string|null */ public function cloneBlock($blockname, $clones = 1, $replace = true) { @@ -263,7 +263,6 @@ class Template * Delete a block of text * * @param string $blockname - * @param string $replacement */ public function deleteBlock($blockname) { @@ -317,7 +316,7 @@ class Template * * @param string $documentPartXML * @param string $search - * @param mixed $replace + * @param string $replace * @param integer $limit * @return string */ @@ -335,16 +334,10 @@ class Template $search = '${' . $search . '}'; } - if (!is_array($replace)) { - if (!String::isUTF8($replace)) { - $replace = utf8_encode($replace); - } - $replace = htmlspecialchars($replace); - } else { - foreach ($replace as $key => $value) { - $replace[$key] = htmlspecialchars($value); - } + if (!String::isUTF8($replace)) { + $replace = utf8_encode($replace); } + $replace = htmlspecialchars($replace); $regExpDelim = '/'; $escapedSearch = preg_quote($search, $regExpDelim); diff --git a/src/PhpWord/Writer/ODText.php b/src/PhpWord/Writer/ODText.php index e3b82f4d..4a4b1cbb 100755 --- a/src/PhpWord/Writer/ODText.php +++ b/src/PhpWord/Writer/ODText.php @@ -11,7 +11,6 @@ namespace PhpOffice\PhpWord\Writer; use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Writer\ODText\Content; use PhpOffice\PhpWord\Writer\ODText\Manifest; use PhpOffice\PhpWord\Writer\ODText\Meta; diff --git a/src/PhpWord/Writer/ODText/Manifest.php b/src/PhpWord/Writer/ODText/Manifest.php index 106ddfc5..b82a6041 100755 --- a/src/PhpWord/Writer/ODText/Manifest.php +++ b/src/PhpWord/Writer/ODText/Manifest.php @@ -9,7 +9,6 @@ namespace PhpOffice\PhpWord\Writer\ODText; -use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; /**