From bf5eed4d26e6bde65e59987ff1efeccfb8bbbb9c Mon Sep 17 00:00:00 2001 From: Roman Syroeshko Date: Sun, 30 Mar 2014 05:27:25 -0700 Subject: [PATCH 1/5] List of authors has been updated in "composer.json". --- composer.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/composer.json b/composer.json index fcca56e7..6a2d9573 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,10 @@ { "name": "Ivan Lanin", "homepage": "http://ivan.lanin.org" + }, + { + "name": "Roman Syroeshko", + "homepage": "http://ru.linkedin.com/pub/roman-syroeshko/34/a53/994/" } ], "require": { From acba6b448ab8a61eb5b4a74d2215bbd22df6876c Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 30 Mar 2014 18:51:25 +0700 Subject: [PATCH 2/5] Refactoring & remove underscore prefix from method and property names --- src/PhpWord/DocumentProperties.php | 12 +- src/PhpWord/Section/CheckBox.php | 22 +- src/PhpWord/Section/Link.php | 8 +- src/PhpWord/Section/Text.php | 22 +- src/PhpWord/Section/TextBreak.php | 18 +- src/PhpWord/Section/TextRun.php | 9 +- src/PhpWord/Writer/ODText/Content.php | 22 +- src/PhpWord/Writer/ODText/Manifest.php | 4 +- src/PhpWord/Writer/RTF.php | 2 - src/PhpWord/Writer/Word2007.php | 16 +- src/PhpWord/Writer/Word2007/Base.php | 1473 +++++++++-------- src/PhpWord/Writer/Word2007/ContentTypes.php | 80 +- src/PhpWord/Writer/Word2007/Document.php | 154 +- src/PhpWord/Writer/Word2007/DocumentRels.php | 50 +- src/PhpWord/Writer/Word2007/Footer.php | 12 +- src/PhpWord/Writer/Word2007/Footnotes.php | 8 +- src/PhpWord/Writer/Word2007/FootnotesRels.php | 36 +- src/PhpWord/Writer/Word2007/Header.php | 14 +- src/PhpWord/Writer/Word2007/Rels.php | 41 +- src/PhpWord/Writer/Word2007/Styles.php | 27 +- 20 files changed, 934 insertions(+), 1096 deletions(-) diff --git a/src/PhpWord/DocumentProperties.php b/src/PhpWord/DocumentProperties.php index 5e982646..63ff0275 100644 --- a/src/PhpWord/DocumentProperties.php +++ b/src/PhpWord/DocumentProperties.php @@ -39,14 +39,14 @@ class DocumentProperties /** * Created * - * @var datetime + * @var datetime|int */ private $_created; /** * Modified * - * @var datetime + * @var datetime|int */ private $_modified; @@ -102,7 +102,7 @@ class DocumentProperties /** * Custom Properties * - * @var string + * @var array */ private $_customProperties = array(); @@ -542,26 +542,21 @@ class DocumentProperties case 'ui8': // 8-Byte Unsigned Integer case 'uint': // Unsigned Integer return self::PROPERTY_TYPE_INTEGER; - break; case 'r4': // 4-Byte Real Number case 'r8': // 8-Byte Real Number case 'decimal': // Decimal return self::PROPERTY_TYPE_FLOAT; - break; case 'empty': // Empty case 'null': // Null case 'lpstr': // LPSTR case 'lpwstr': // LPWSTR case 'bstr': // Basic String return self::PROPERTY_TYPE_STRING; - break; case 'date': // Date and Time case 'filetime': // File Time return self::PROPERTY_TYPE_DATE; - break; case 'bool': // Boolean return self::PROPERTY_TYPE_BOOLEAN; - break; case 'cy': // Currency case 'error': // Error Status Code case 'vector': // Vector @@ -576,7 +571,6 @@ class DocumentProperties case 'clsid': // Class ID case 'cf': // Clipboard Data return self::PROPERTY_TYPE_UNKNOWN; - break; } return self::PROPERTY_TYPE_UNKNOWN; } diff --git a/src/PhpWord/Section/CheckBox.php b/src/PhpWord/Section/CheckBox.php index 00f75e54..5e101d9f 100644 --- a/src/PhpWord/Section/CheckBox.php +++ b/src/PhpWord/Section/CheckBox.php @@ -34,14 +34,14 @@ class CheckBox /** * Text style * - * @var Font + * @var string|Font */ private $fontStyle; /** * Paragraph style * - * @var Paragraph + * @var string|Paragraph */ private $paragraphStyle; @@ -50,8 +50,8 @@ class CheckBox * * @param string $name * @param string $text - * @param Font $fontStyle - * @param Paragraph $paragraphStyle + * @param mixed $fontStyle + * @param mixed $paragraphStyle */ public function __construct($name = null, $text = null, $fontStyle = null, $paragraphStyle = null) { @@ -66,9 +66,9 @@ class CheckBox /** * Set Text style * - * @param Font $style - * @param Paragraph $paragraphStyle - * @return Font + * @param mixed $style + * @param mixed $paragraphStyle + * @return string|Font */ public function setFontStyle($style = null, $paragraphStyle = null) { @@ -90,7 +90,7 @@ class CheckBox /** * Get Text style * - * @return Font + * @return string|Font */ public function getFontStyle() { @@ -100,8 +100,8 @@ class CheckBox /** * Set Paragraph style * - * @param Paragraph $style - * @return Paragraph + * @param mixed $style + * @return string|Paragraph */ public function setParagraphStyle($style = null) { @@ -121,7 +121,7 @@ class CheckBox /** * Get Paragraph style * - * @return Paragraph + * @return string|Paragraph */ public function getParagraphStyle() { diff --git a/src/PhpWord/Section/Link.php b/src/PhpWord/Section/Link.php index 39b1c56d..240d0445 100644 --- a/src/PhpWord/Section/Link.php +++ b/src/PhpWord/Section/Link.php @@ -41,14 +41,14 @@ class Link /** * Link style * - * @var \PhpOffice\PhpWord\Style\Font + * @var string|Font */ private $_styleFont; /** * Paragraph style * - * @var \PhpOffice\PhpWord\Style\Paragraph + * @var string|Paragraph */ private $_styleParagraph; @@ -140,7 +140,7 @@ class Link /** * Get Text style * - * @return \PhpOffice\PhpWord\Style\Font + * @return string|Font */ public function getFontStyle() { @@ -150,7 +150,7 @@ class Link /** * Get Paragraph style * - * @return \PhpOffice\PhpWord\Style\Paragraph + * @return string|Paragraph */ public function getParagraphStyle() { diff --git a/src/PhpWord/Section/Text.php b/src/PhpWord/Section/Text.php index a99945fe..4e228cda 100644 --- a/src/PhpWord/Section/Text.php +++ b/src/PhpWord/Section/Text.php @@ -27,14 +27,14 @@ class Text /** * Text style * - * @var \PhpOffice\PhpWord\Style\Font + * @var string|Font */ private $fontStyle; /** * Paragraph style * - * @var \PhpOffice\PhpWord\Style\Paragraph + * @var string|Paragraph */ private $paragraphStyle; @@ -42,8 +42,8 @@ class Text * Create a new Text Element * * @param string $text - * @param null|array|\PhpOffice\PhpWord\Style\Font $fontStyle - * @param null|array|\PhpOffice\PhpWord\Style\Paragraph $paragraphStyle + * @param mixed $fontStyle + * @param mixed $paragraphStyle */ public function __construct($text = null, $fontStyle = null, $paragraphStyle = null) { @@ -55,9 +55,9 @@ class Text /** * Set Text style * - * @param null|array|\PhpOffice\PhpWord\Style\Font $style - * @param null|array|\PhpOffice\PhpWord\Style\Paragraph $paragraphStyle - * @return \PhpOffice\PhpWord\Style\Font + * @param string|array|Font $style + * @param string|array|Paragraph $paragraphStyle + * @return string|Font */ public function setFontStyle($style = null, $paragraphStyle = null) { @@ -79,7 +79,7 @@ class Text /** * Get Text style * - * @return \PhpOffice\PhpWord\Style\Font + * @return string|Font */ public function getFontStyle() { @@ -89,8 +89,8 @@ class Text /** * Set Paragraph style * - * @param null|array|\PhpOffice\PhpWord\Style\Paragraph $style - * @return null|\PhpOffice\PhpWord\Style\Paragraph + * @param string|array|Paragraph $style + * @return string|Paragraph */ public function setParagraphStyle($style = null) { @@ -110,7 +110,7 @@ class Text /** * Get Paragraph style * - * @return \PhpOffice\PhpWord\Style\Paragraph + * @return string|Paragraph */ public function getParagraphStyle() { diff --git a/src/PhpWord/Section/TextBreak.php b/src/PhpWord/Section/TextBreak.php index 97f6deca..4df4c780 100755 --- a/src/PhpWord/Section/TextBreak.php +++ b/src/PhpWord/Section/TextBreak.php @@ -20,14 +20,14 @@ class TextBreak /** * Paragraph style * - * @var \PhpOffice\PhpWord\Style\Pagaraph + * @var string|Paragraph */ private $paragraphStyle = null; /** * Text style * - * @var \PhpOffice\PhpWord\Style\Font + * @var string|Font */ private $fontStyle = null; @@ -50,9 +50,9 @@ class TextBreak /** * Set Text style * - * @param null|array|\PhpOffice\PhpWord\Style\Font $style - * @param null|array|\PhpOffice\PhpWord\Style\Paragraph $paragraphStyle - * @return \PhpOffice\PhpWord\Style\Font + * @param mixed $style + * @param mixed $paragraphStyle + * @return string|Font */ public function setFontStyle($style = null, $paragraphStyle = null) { @@ -72,7 +72,7 @@ class TextBreak /** * Get Text style * - * @return \PhpOffice\PhpWord\Style\Font + * @return string|Font */ public function getFontStyle() { @@ -82,8 +82,8 @@ class TextBreak /** * Set Paragraph style * - * @param null|array|\PhpOffice\PhpWord\Style\Paragraph $style - * @return null|\PhpOffice\PhpWord\Style\Paragraph + * @param string|array|Paragraph $style + * @return string|Paragraph */ public function setParagraphStyle($style = null) { @@ -101,7 +101,7 @@ class TextBreak /** * Get Paragraph style * - * @return \PhpOffice\PhpWord\Style\Paragraph + * @return string|Paragraph */ public function getParagraphStyle() { diff --git a/src/PhpWord/Section/TextRun.php b/src/PhpWord/Section/TextRun.php index 497f6f45..bcb5173a 100755 --- a/src/PhpWord/Section/TextRun.php +++ b/src/PhpWord/Section/TextRun.php @@ -12,6 +12,7 @@ namespace PhpOffice\PhpWord\Section; use PhpOffice\PhpWord\Exceptions\InvalidImageException; use PhpOffice\PhpWord\Media; use PhpOffice\PhpWord\Shared\String; +use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; /** @@ -22,7 +23,7 @@ class TextRun /** * Paragraph style * - * @var \PhpOffice\PhpWord\Style\Paragraph + * @var Paragraph */ private $_styleParagraph; @@ -123,8 +124,8 @@ class TextRun * Add TextBreak * * @param int $count - * @param null|string|array|\PhpOffice\PhpWord\Style\Font $fontStyle - * @param null|string|array|\PhpOffice\PhpWord\Style\Paragraph $paragraphStyle + * @param mixed $fontStyle + * @param mixed $paragraphStyle */ public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null) { @@ -161,7 +162,7 @@ class TextRun /** * Get Paragraph style * - * @return \PhpOffice\PhpWord\Style\Paragraph + * @return string|Paragraph */ public function getParagraphStyle() { diff --git a/src/PhpWord/Writer/ODText/Content.php b/src/PhpWord/Writer/ODText/Content.php index 4e75068d..8142d9dc 100644 --- a/src/PhpWord/Writer/ODText/Content.php +++ b/src/PhpWord/Writer/ODText/Content.php @@ -244,11 +244,11 @@ class Content extends WriterPart foreach ($_elements as $element) { if ($element instanceof Text) { - $this->_writeText($xmlWriter, $element); + $this->writeText($xmlWriter, $element); } elseif ($element instanceof TextRun) { - $this->_writeTextRun($xmlWriter, $element); + $this->writeTextRun($xmlWriter, $element); } elseif ($element instanceof TextBreak) { - $this->_writeTextBreak($xmlWriter); + $this->writeTextBreak($xmlWriter); } elseif ($element instanceof Link) { $this->writeUnsupportedElement($xmlWriter, 'Link'); } elseif ($element instanceof Title) { @@ -271,9 +271,9 @@ class Content extends WriterPart } if ($pSection == $countSections) { - $this->_writeEndSection($xmlWriter, $section); + $this->writeEndSection($xmlWriter, $section); } else { - $this->_writeSection($xmlWriter, $section); + $this->writeSection($xmlWriter, $section); } } } @@ -292,7 +292,7 @@ class Content extends WriterPart * @param \PhpOffice\PhpWord\Section\Text $text * @param bool $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(); @@ -340,14 +340,14 @@ class Content extends WriterPart * @param \PhpOffice\PhpWord\Section\TextRun $textrun * @todo Enable all other section types */ - protected function _writeTextRun(XMLWriter $xmlWriter, TextRun $textrun) + protected function writeTextRun(XMLWriter $xmlWriter, TextRun $textrun) { $elements = $textrun->getElements(); $xmlWriter->startElement('text:p'); if (count($elements) > 0) { foreach ($elements as $element) { if ($element instanceof Text) { - $this->_writeText($xmlWriter, $element, true); + $this->writeText($xmlWriter, $element, true); } } } @@ -359,7 +359,7 @@ class Content extends WriterPart * * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter */ - protected function _writeTextBreak(XMLWriter $xmlWriter = null) + protected function writeTextBreak(XMLWriter $xmlWriter = null) { $xmlWriter->startElement('text:p'); $xmlWriter->writeAttribute('text:style-name', 'Standard'); @@ -373,7 +373,7 @@ class Content extends WriterPart * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter * @param PhpOffice\PhpWord\Section $section */ - private function _writeEndSection(XMLWriter $xmlWriter = null, Section $section = null) + private function writeEndSection(XMLWriter $xmlWriter = null, Section $section = null) { } @@ -383,7 +383,7 @@ class Content extends WriterPart * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter * @param PhpOffice\PhpWord\Section $section */ - private function _writeSection(XMLWriter $xmlWriter = null, Section $section = null) + private function writeSection(XMLWriter $xmlWriter = null, Section $section = null) { } // @codeCoverageIgnoreEnd diff --git a/src/PhpWord/Writer/ODText/Manifest.php b/src/PhpWord/Writer/ODText/Manifest.php index cb92ddcc..25a17f2e 100755 --- a/src/PhpWord/Writer/ODText/Manifest.php +++ b/src/PhpWord/Writer/ODText/Manifest.php @@ -64,7 +64,7 @@ class Manifest extends WriterPart 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()); + $mimeType = $this->getImageMimeType($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getPath()); $xmlWriter->startElement('manifest:file-entry'); $xmlWriter->writeAttribute('manifest:media-type', $mimeType); @@ -99,7 +99,7 @@ class Manifest extends WriterPart * @return string Mime Type * @throws \PhpOffice\PhpWord\Exceptions\Exception */ - private function _getImageMimeType($pFile = '') + private function getImageMimeType($pFile = '') { if (file_exists($pFile)) { $image = getimagesize($pFile); diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index d7fc7101..cff6ca46 100755 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -179,7 +179,6 @@ class RTF extends Writer implements IWriter // Browse styles $styles = Style::getStyles(); - $numPStyles = 0; if (count($styles) > 0) { foreach ($styles as $styleName => $style) { // PhpOffice\PhpWord\Style\Font @@ -232,7 +231,6 @@ class RTF extends Writer implements IWriter // Browse styles $styles = Style::getStyles(); - $numPStyles = 0; if (count($styles) > 0) { foreach ($styles as $styleName => $style) { // Font diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index 6ceed4fb..cb5cfc72 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -34,14 +34,14 @@ class Word2007 extends Writer implements IWriter * * @var array */ - private $_imageTypes = array(); + private $imageTypes = array(); /** * Types of objects * * @var array */ - private $_objectTypes = array(); + private $objectTypes = array(); /** * Create new Word2007 writer @@ -167,8 +167,8 @@ class Word2007 extends Writer implements IWriter $objZip->addFromString( '[Content_Types].xml', $this->getWriterPart('contenttypes')->writeContentTypes( - $this->_imageTypes, - $this->_objectTypes, + $this->imageTypes, + $this->objectTypes, $_cHdrs, $footers ) @@ -235,12 +235,12 @@ class Word2007 extends Writer implements IWriter if ($imageExtension === 'jpeg') { $imageExtension = 'jpg'; } - if (!in_array($imageType, $this->_imageTypes)) { - $this->_imageTypes[$imageExtension] = $imageType; + if (!in_array($imageType, $this->imageTypes)) { + $this->imageTypes[$imageExtension] = $imageType; } } else { - if (!in_array($extension, $this->_objectTypes)) { - $this->_objectTypes[] = $extension; + if (!in_array($extension, $this->objectTypes)) { + $this->objectTypes[] = $extension; } } } diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index 644bf2a2..f813e586 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -10,23 +10,23 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Section\Footer\PreserveText; -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\Table; use PhpOffice\PhpWord\Section\Text; -use PhpOffice\PhpWord\Section\TextBreak; 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\Shared\String; use PhpOffice\PhpWord\Shared\XMLWriter; -use PhpOffice\PhpWord\Style\Cell; -use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; +use PhpOffice\PhpWord\Style\Font; +use PhpOffice\PhpWord\Style\Cell; /** * Word2007 base part writer @@ -38,55 +38,33 @@ class Base extends WriterPart /** * Write text element * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section\Text $text + * @param XMLWriter $xmlWriter + * @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(); - $sfIsObject = ($styleFont instanceof Font) ? true : false; - - if (!$withoutP) { - $xmlWriter->startElement('w:p'); - - $styleParagraph = $text->getParagraphStyle(); - $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; - - if ($spIsObject) { - $this->_writeParagraphStyle($xmlWriter, $styleParagraph); - } elseif (!$spIsObject && !is_null($styleParagraph)) { - $xmlWriter->startElement('w:pPr'); - $xmlWriter->startElement('w:pStyle'); - $xmlWriter->writeAttribute('w:val', $styleParagraph); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - } - } - + $styleParagraph = $text->getParagraphStyle(); + $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; $strText = htmlspecialchars($text->getText()); $strText = String::controlCharacterPHP2OOXML($strText); - $xmlWriter->startElement('w:r'); - - if ($sfIsObject) { - $this->_writeTextStyle($xmlWriter, $styleFont); - } elseif (!$sfIsObject && !is_null($styleFont)) { - $xmlWriter->startElement('w:rPr'); - $xmlWriter->startElement('w:rStyle'); - $xmlWriter->writeAttribute('w:val', $styleFont); - $xmlWriter->endElement(); - $xmlWriter->endElement(); + if (!$withoutP) { + $xmlWriter->startElement('w:p'); + $this->writeInlineParagraphStyle($xmlWriter, $styleParagraph); } - + $xmlWriter->startElement('w:r'); + $this->writeInlineFontStyle($xmlWriter, $styleFont); $xmlWriter->startElement('w:t'); - $xmlWriter->writeAttribute('xml:space', 'preserve'); // needed because of drawing spaces before and after text + $xmlWriter->writeAttribute('xml:space', 'preserve'); $xmlWriter->writeRaw($strText); $xmlWriter->endElement(); - $xmlWriter->endElement(); // w:r - if (!$withoutP) { $xmlWriter->endElement(); // w:p } @@ -95,57 +73,706 @@ class Base extends WriterPart /** * Write textrun element * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section $section + * @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(); - $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; - $xmlWriter->startElement('w:p'); - - if ($spIsObject) { - $this->_writeParagraphStyle($xmlWriter, $styleParagraph); - } elseif (!$spIsObject && !is_null($styleParagraph)) { - $xmlWriter->startElement('w:pPr'); - $xmlWriter->startElement('w:pStyle'); - $xmlWriter->writeAttribute('w:val', $styleParagraph); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - } - + $this->writeInlineParagraphStyle($xmlWriter, $styleParagraph); if (count($elements) > 0) { foreach ($elements as $element) { if ($element instanceof Text) { - $this->_writeText($xmlWriter, $element, true); + $this->writeText($xmlWriter, $element, true); } elseif ($element instanceof Link) { - $this->_writeLink($xmlWriter, $element, true); + $this->writeLink($xmlWriter, $element, true); } elseif ($element instanceof Image) { - $this->_writeImage($xmlWriter, $element, true); + $this->writeImage($xmlWriter, $element, true); } elseif ($element instanceof Footnote) { - $this->_writeFootnote($xmlWriter, $element, true); + $this->writeFootnote($xmlWriter, $element, true); } elseif ($element instanceof TextBreak) { $xmlWriter->writeElement('w:br'); } } } + $xmlWriter->endElement(); // w:p + } + + /** + * Write link element + * + * @param XMLWriter $xmlWriter + * @param Link $link + * @param boolean $withoutP + */ + protected function writeLink( + XMLWriter $xmlWriter, + Link $link, + $withoutP = false + ) { + $rID = $link->getRelationId(); + $linkName = $link->getLinkName(); + if (is_null($linkName)) { + $linkName = $link->getLinkSrc(); + } + $styleFont = $link->getFontStyle(); + $sfIsObject = ($styleFont instanceof Font) ? true : false; + $styleParagraph = $link->getParagraphStyle(); + $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; + + if (!$withoutP) { + $xmlWriter->startElement('w:p'); + $this->writeInlineParagraphStyle($xmlWriter, $styleParagraph); + } + $xmlWriter->startElement('w:hyperlink'); + $xmlWriter->writeAttribute('r:id', 'rId' . $rID); + $xmlWriter->writeAttribute('w:history', '1'); + $xmlWriter->startElement('w:r'); + $this->writeInlineFontStyle($xmlWriter, $styleFont); + $xmlWriter->startElement('w:t'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); // needed because of drawing spaces before and after text + $xmlWriter->writeRaw($linkName); + $xmlWriter->endElement(); // w:t + $xmlWriter->endElement(); // w:r + $xmlWriter->endElement(); // w:hyperlink + if (!$withoutP) { + $xmlWriter->endElement(); // w:p + } + } + + /** + * Write title element + * + * @param XMLWriter $xmlWriter + * @param Title $title + */ + protected function writeTitle(XMLWriter $xmlWriter, Title $title) + { + $text = htmlspecialchars($title->getText()); + $text = String::controlCharacterPHP2OOXML($text); + $anchor = $title->getAnchor(); + $bookmarkId = $title->getBookmarkId(); + $style = $title->getStyle(); + + $xmlWriter->startElement('w:p'); + + if (!empty($style)) { + $xmlWriter->startElement('w:pPr'); + $xmlWriter->startElement('w:pStyle'); + $xmlWriter->writeAttribute('w:val', $style); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + } + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'end'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + + $xmlWriter->startElement('w:bookmarkStart'); + $xmlWriter->writeAttribute('w:id', $bookmarkId); + $xmlWriter->writeAttribute('w:name', $anchor); + $xmlWriter->endElement(); + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:t'); + $xmlWriter->writeRaw($text); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + + $xmlWriter->startElement('w:bookmarkEnd'); + $xmlWriter->writeAttribute('w:id', $bookmarkId); + $xmlWriter->endElement(); $xmlWriter->endElement(); } + /** + * Write preserve text element + * + * @param XMLWriter $xmlWriter + * @param TextRun $textrun + */ + protected function writePreserveText( + XMLWriter $xmlWriter, + PreserveText $textrun + ) { + $styleFont = $textrun->getFontStyle(); + $sfIsObject = ($styleFont instanceof Font) ? true : false; + $styleParagraph = $textrun->getParagraphStyle(); + $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; + + $arrText = $textrun->getText(); + if (!is_array($arrText)) { + $arrText = array($arrText); + } + + $xmlWriter->startElement('w:p'); + $this->writeInlineParagraphStyle($xmlWriter, $styleParagraph); + foreach ($arrText as $text) { + if (substr($text, 0, 1) == '{') { + $text = substr($text, 1, -1); + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'begin'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + + $xmlWriter->startElement('w:r'); + $this->writeInlineFontStyle($xmlWriter, $styleFont); + $xmlWriter->startElement('w:instrText'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->writeRaw($text); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'separate'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'end'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + } else { + $text = htmlspecialchars($text); + $text = String::controlCharacterPHP2OOXML($text); + + $xmlWriter->startElement('w:r'); + $this->writeInlineFontStyle($xmlWriter, $styleFont); + $xmlWriter->startElement('w:t'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->writeRaw($text); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + } + } + + $xmlWriter->endElement(); // p + } + + /** + * Write text break element + * + * @param XMLWriter $xmlWriter + * @param TextBreak $element + */ + protected function writeTextBreak($xmlWriter, TextBreak $element = null) + { + $hasStyle = false; + if (!is_null($element)) { + $styleFont = $element->getFontStyle(); + $sfIsObject = ($styleFont instanceof Font) ? true : false; + $styleParagraph = $element->getParagraphStyle(); + $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; + $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 { + // Null element. No paragraph nor font style + $xmlWriter->writeElement('w:p', null); + } + } + + /** + * Write list item element + * + * @param XMLWriter $xmlWriter + * @param ListItem $listItem + */ + protected function writeListItem(XMLWriter $xmlWriter, ListItem $listItem) + { + $textObject = $listItem->getTextObject(); + $text = $textObject->getText(); + $depth = $listItem->getDepth(); + $listType = $listItem->getStyle()->getListType(); + $styleParagraph = $textObject->getParagraphStyle(); + $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; + + $xmlWriter->startElement('w:p'); + $xmlWriter->startElement('w:pPr'); + $this->writeInlineParagraphStyle($xmlWriter, $styleParagraph, true); + $xmlWriter->startElement('w:numPr'); + $xmlWriter->startElement('w:ilvl'); + $xmlWriter->writeAttribute('w:val', $depth); + $xmlWriter->endElement(); // w:ilvl + $xmlWriter->startElement('w:numId'); + $xmlWriter->writeAttribute('w:val', $listType); + $xmlWriter->endElement(); // w:numId + $xmlWriter->endElement(); // w:numPr + $xmlWriter->endElement(); // w:pPr + $this->writeText($xmlWriter, $textObject, true); + $xmlWriter->endElement(); // w:p + } + + /** + * Write footnote reference element + * + * @param XMLWriter $xmlWriter + * @param Table $table + */ + protected function writeTable(XMLWriter $xmlWriter, Table $table) + { + $_rows = $table->getRows(); + $_cRows = count($_rows); + + if ($_cRows > 0) { + $xmlWriter->startElement('w:tbl'); + + // Table grid + $cellWidths = array(); + for ($i = 0; $i < $_cRows; $i++) { + $row = $_rows[$i]; + $cells = $row->getCells(); + if (count($cells) <= count($cellWidths)) { + continue; + } + $cellWidths = array(); + foreach ($cells as $cell) { + $cellWidths[] = $cell->getWidth(); + } + } + $xmlWriter->startElement('w:tblGrid'); + foreach ($cellWidths as $width) { + $xmlWriter->startElement('w:gridCol'); + if (!is_null($width)) { + $xmlWriter->writeAttribute('w:w', $width); + $xmlWriter->writeAttribute('w:type', 'dxa'); + } + $xmlWriter->endElement(); + } + $xmlWriter->endElement(); // w:tblGrid + + // Table style + $tblStyle = $table->getStyle(); + $tblWidth = $table->getWidth(); + if ($tblStyle instanceof \PhpOffice\PhpWord\Style\Table) { + $this->writeTableStyle($xmlWriter, $tblStyle, false); + } else { + if (!empty($tblStyle)) { + $xmlWriter->startElement('w:tblPr'); + $xmlWriter->startElement('w:tblStyle'); + $xmlWriter->writeAttribute('w:val', $tblStyle); + $xmlWriter->endElement(); + if (!is_null($tblWidth)) { + $xmlWriter->startElement('w:tblW'); + $xmlWriter->writeAttribute('w:w', $tblWidth); + $xmlWriter->writeAttribute('w:type', 'pct'); + $xmlWriter->endElement(); + } + $xmlWriter->endElement(); + } + } + + // Table rows + for ($i = 0; $i < $_cRows; $i++) { + $row = $_rows[$i]; + $height = $row->getHeight(); + $rowStyle = $row->getStyle(); + $tblHeader = $rowStyle->getTblHeader(); + $cantSplit = $rowStyle->getCantSplit(); + $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)) { + $xmlWriter->startElement('w:trHeight'); + $xmlWriter->writeAttribute('w:val', $height); + $xmlWriter->writeAttribute('w:hRule', ($exactHeight ? 'exact' : 'atLeast')); + $xmlWriter->endElement(); + } + if ($tblHeader) { + $xmlWriter->startElement('w:tblHeader'); + $xmlWriter->writeAttribute('w:val', '1'); + $xmlWriter->endElement(); + } + if ($cantSplit) { + $xmlWriter->startElement('w:cantSplit'); + $xmlWriter->writeAttribute('w:val', '1'); + $xmlWriter->endElement(); + } + $xmlWriter->endElement(); + } + + foreach ($row->getCells() as $cell) { + $xmlWriter->startElement('w:tc'); + + $cellStyle = $cell->getStyle(); + $width = $cell->getWidth(); + + $xmlWriter->startElement('w:tcPr'); + $xmlWriter->startElement('w:tcW'); + $xmlWriter->writeAttribute('w:w', $width); + $xmlWriter->writeAttribute('w:type', 'dxa'); + $xmlWriter->endElement(); + + 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 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 PreserveText) { + $this->writePreserveText($xmlWriter, $element); + } elseif ($element instanceof CheckBox) { + $this->writeCheckBox($xmlWriter, $element); + } + } + } else { + $this->writeTextBreak($xmlWriter); + } + + $xmlWriter->endElement(); + } + $xmlWriter->endElement(); + } + $xmlWriter->endElement(); + } + } + + /** + * Write image element + * + * @param XMLWriter $xmlWriter + * @param Image $image + * @param boolean $withoutP + */ + protected function writeImage( + XMLWriter $xmlWriter, + Image $image, + $withoutP = false + ) { + $rId = $image->getRelationId(); + + $style = $image->getStyle(); + $width = $style->getWidth(); + $height = $style->getHeight(); + $align = $style->getAlign(); + $marginTop = $style->getMarginTop(); + $marginLeft = $style->getMarginLeft(); + $wrappingStyle = $style->getWrappingStyle(); + + if (!$withoutP) { + $xmlWriter->startElement('w:p'); + + if (!is_null($align)) { + $xmlWriter->startElement('w:pPr'); + $xmlWriter->startElement('w:jc'); + $xmlWriter->writeAttribute('w:val', $align); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + } + } + + $xmlWriter->startElement('w:r'); + + $xmlWriter->startElement('w:pict'); + + $xmlWriter->startElement('v:shape'); + $xmlWriter->writeAttribute('type', '#_x0000_t75'); + + $imgStyle = ''; + if (null !== $width) { + $imgStyle .= 'width:' . $width . 'px;'; + } + if (null !== $height) { + $imgStyle .= 'height:' . $height . 'px;'; + } + if (null !== $marginTop) { + $imgStyle .= 'margin-top:' . $marginTop . 'in;'; + } + if (null !== $marginLeft) { + $imgStyle .= 'margin-left:' . $marginLeft . 'in;'; + } + + switch ($wrappingStyle) { + case \PhpOffice\PhpWord\Style\Image::WRAPPING_STYLE_BEHIND: + $imgStyle .= 'position:absolute;z-index:-251658752;'; + break; + case \PhpOffice\PhpWord\Style\Image::WRAPPING_STYLE_SQUARE: + $imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; + break; + case \PhpOffice\PhpWord\Style\Image::WRAPPING_STYLE_TIGHT: + $imgStyle .= 'position:absolute;z-index:251659264;mso-wrap-edited:f;mso-position-horizontal:absolute;mso-position-vertical:absolute'; + break; + case \PhpOffice\PhpWord\Style\Image::WRAPPING_STYLE_INFRONT: + $imgStyle .= 'position:absolute;zz-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; + break; + } + + $xmlWriter->writeAttribute('style', $imgStyle); + + $xmlWriter->startElement('v:imagedata'); + $xmlWriter->writeAttribute('r:id', 'rId' . $rId); + $xmlWriter->writeAttribute('o:title', ''); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + + $xmlWriter->endElement(); + + $xmlWriter->endElement(); + + if (!$withoutP) { + $xmlWriter->endElement(); // w:p + } + } + + /** + * Write watermark element + * + * @param XMLWriter $xmlWriter + * @param Image $image + */ + protected function writeWatermark(XMLWriter $xmlWriter, Image $image) + { + $rId = $image->getRelationId(); + + $style = $image->getStyle(); + $width = $style->getWidth(); + $height = $style->getHeight(); + $marginLeft = $style->getMarginLeft(); + $marginTop = $style->getMarginTop(); + + $xmlWriter->startElement('w:p'); + + $xmlWriter->startElement('w:r'); + + $xmlWriter->startElement('w:pict'); + + $xmlWriter->startElement('v:shape'); + $xmlWriter->writeAttribute('type', '#_x0000_t75'); + + $strStyle = 'position:absolute;'; + $strStyle .= ' width:' . $width . 'px;'; + $strStyle .= ' height:' . $height . 'px;'; + if (!is_null($marginTop)) { + $strStyle .= ' margin-top:' . $marginTop . 'px;'; + } + if (!is_null($marginLeft)) { + $strStyle .= ' margin-left:' . $marginLeft . 'px;'; + } + + $xmlWriter->writeAttribute('style', $strStyle); + + $xmlWriter->startElement('v:imagedata'); + $xmlWriter->writeAttribute('r:id', 'rId' . $rId); + $xmlWriter->writeAttribute('o:title', ''); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + + $xmlWriter->endElement(); + + $xmlWriter->endElement(); + + $xmlWriter->endElement(); + } + + /** + * Write object element + * + * @param XMLWriter $xmlWriter + * @param Object $object + */ + protected function writeObject(XMLWriter $xmlWriter, Object $object) + { + $rIdObject = $object->getRelationId(); + $rIdImage = $object->getImageRelationId(); + $shapeId = md5($rIdObject . '_' . $rIdImage); + $objectId = $object->getObjectId(); + $style = $object->getStyle(); + $width = $style->getWidth(); + $height = $style->getHeight(); + $align = $style->getAlign(); + + $xmlWriter->startElement('w:p'); + if (!is_null($align)) { + $xmlWriter->startElement('w:pPr'); + $xmlWriter->startElement('w:jc'); + $xmlWriter->writeAttribute('w:val', $align); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + } + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:object'); + $xmlWriter->writeAttribute('w:dxaOrig', '249'); + $xmlWriter->writeAttribute('w:dyaOrig', '160'); + $xmlWriter->startElement('v:shape'); + $xmlWriter->writeAttribute('id', $shapeId); + $xmlWriter->writeAttribute('type', '#_x0000_t75'); + $xmlWriter->writeAttribute('style', 'width:104px;height:67px'); + $xmlWriter->writeAttribute('o:ole', ''); + $xmlWriter->startElement('v:imagedata'); + $xmlWriter->writeAttribute('r:id', 'rId' . $rIdImage); + $xmlWriter->writeAttribute('o:title', ''); + $xmlWriter->endElement(); // v:imagedata + $xmlWriter->endElement(); // v:shape + $xmlWriter->startElement('o:OLEObject'); + $xmlWriter->writeAttribute('Type', 'Embed'); + $xmlWriter->writeAttribute('ProgID', 'Package'); + $xmlWriter->writeAttribute('ShapeID', $shapeId); + $xmlWriter->writeAttribute('DrawAspect', 'Icon'); + $xmlWriter->writeAttribute('ObjectID', '_' . $objectId); + $xmlWriter->writeAttribute('r:id', 'rId' . $rIdObject); + $xmlWriter->endElement(); // o:OLEObject + $xmlWriter->endElement(); // w:object + $xmlWriter->endElement(); // w:r + $xmlWriter->endElement(); // w:p + } + + /** + * Write footnote element which links to the actual content in footnotes.xml + * + * @param XMLWriter $xmlWriter + * @param Footnote $footnote + * @param boolean $withoutP + */ + protected function writeFootnote( + XMLWriter $xmlWriter, + Footnote $footnote, + $withoutP = false + ) { + if (!$withoutP) { + $xmlWriter->startElement('w:p'); + } + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:rPr'); + $xmlWriter->startElement('w:rStyle'); + $xmlWriter->writeAttribute('w:val', 'FootnoteReference'); + $xmlWriter->endElement(); // w:rStyle + $xmlWriter->endElement(); // w:rPr + $xmlWriter->startElement('w:footnoteReference'); + $xmlWriter->writeAttribute('w:id', $footnote->getReferenceId()); + $xmlWriter->endElement(); // w:footnoteReference + $xmlWriter->endElement(); // w:r + if (!$withoutP) { + $xmlWriter->endElement(); // w:p + } + } + + /** + * Write CheckBox + * + * @param boolean $withoutP + * @param boolean $checkState + */ + protected function writeCheckBox( + XMLWriter $xmlWriter, + CheckBox $checkbox, + $withoutP = false, + $checkState = false + ) { + $name = htmlspecialchars($checkbox->getName()); + $name = String::controlCharacterPHP2OOXML($name); + $text = htmlspecialchars($checkbox->getText()); + $text = String::controlCharacterPHP2OOXML($text); + $styleFont = $checkbox->getFontStyle(); + $sfIsObject = ($styleFont instanceof Font) ? true : false; + $styleParagraph = $checkbox->getParagraphStyle(); + $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; + + if (!$withoutP) { + $xmlWriter->startElement('w:p'); + $this->writeInlineParagraphStyle($xmlWriter, $styleParagraph); + } + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'begin'); + $xmlWriter->startElement('w:ffData'); + $xmlWriter->startElement('w:name'); + $xmlWriter->writeAttribute('w:val', $name); + $xmlWriter->endElement(); //w:name + $xmlWriter->writeAttribute('w:enabled', ''); + $xmlWriter->startElement('w:calcOnExit'); + $xmlWriter->writeAttribute('w:val', '0'); + $xmlWriter->endElement(); //w:calcOnExit + $xmlWriter->startElement('w:checkBox'); + $xmlWriter->writeAttribute('w:sizeAuto', ''); + $xmlWriter->startElement('w:default'); + $xmlWriter->writeAttribute('w:val', ($checkState ? '1' : '0')); + $xmlWriter->endElement(); //w:default + $xmlWriter->endElement(); //w:checkBox + $xmlWriter->endElement(); // w:ffData + $xmlWriter->endElement(); // w:fldChar + $xmlWriter->endElement(); // w:r + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:instrText'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->writeRaw(' FORMCHECKBOX '); + $xmlWriter->endElement();// w:instrText + $xmlWriter->endElement(); // w:r + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'seperate'); + $xmlWriter->endElement();// w:fldChar + $xmlWriter->endElement(); // w:r + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'end'); + $xmlWriter->endElement();// w:fldChar + $xmlWriter->endElement(); // w:r + + $xmlWriter->startElement('w:r'); + $this->writeInlineFontStyle($xmlWriter, $styleFont); + $xmlWriter->startElement('w:t'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->writeRaw($text); + $xmlWriter->endElement(); // w:t + $xmlWriter->endElement(); // w:r + + if (!$withoutP) { + $xmlWriter->endElement(); // w:p + } + } + /** * Write paragraph style * - * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param XMLWriter $xmlWriter * @param \PhpOffice\PhpWord\Style\Paragraph $style * @param bool $withoutPPR */ - protected function _writeParagraphStyle( + protected function writeParagraphStyle( XMLWriter $xmlWriter, - Paragraph $style, + Paragraph $style = null, $withoutPPR = false ) { @@ -239,174 +866,13 @@ class Base extends WriterPart } } - /** - * Write link element - * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section\Link $link - * @param boolean $withoutP - */ - protected function _writeLink(XMLWriter $xmlWriter, Link $link, $withoutP = false) - { - $rID = $link->getRelationId(); - $linkName = $link->getLinkName(); - if (is_null($linkName)) { - $linkName = $link->getLinkSrc(); - } - - $styleFont = $link->getFontStyle(); - $sfIsObject = ($styleFont instanceof Font) ? true : false; - - if (!$withoutP) { - $xmlWriter->startElement('w:p'); - - $styleParagraph = $link->getParagraphStyle(); - $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; - - if ($spIsObject) { - $this->_writeParagraphStyle($xmlWriter, $styleParagraph); - } elseif (!$spIsObject && !is_null($styleParagraph)) { - $xmlWriter->startElement('w:pPr'); - $xmlWriter->startElement('w:pStyle'); - $xmlWriter->writeAttribute('w:val', $styleParagraph); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - } - } - - $xmlWriter->startElement('w:hyperlink'); - $xmlWriter->writeAttribute('r:id', 'rId' . $rID); - $xmlWriter->writeAttribute('w:history', '1'); - - $xmlWriter->startElement('w:r'); - if ($sfIsObject) { - $this->_writeTextStyle($xmlWriter, $styleFont); - } elseif (!$sfIsObject && !is_null($styleFont)) { - $xmlWriter->startElement('w:rPr'); - $xmlWriter->startElement('w:rStyle'); - $xmlWriter->writeAttribute('w:val', $styleFont); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - } - - $xmlWriter->startElement('w:t'); - $xmlWriter->writeAttribute('xml:space', 'preserve'); // needed because of drawing spaces before and after text - $xmlWriter->writeRaw($linkName); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - - $xmlWriter->endElement(); - - if (!$withoutP) { - $xmlWriter->endElement(); // w:p - } - } - - /** - * Write preserve text element - * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section\TextRun $textrun - */ - protected function _writePreserveText(XMLWriter $xmlWriter, PreserveText $textrun) - { - $styleFont = $textrun->getFontStyle(); - $styleParagraph = $textrun->getParagraphStyle(); - - $sfIsObject = ($styleFont instanceof Font) ? true : false; - $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; - - $arrText = $textrun->getText(); - if (!is_array($arrText)) { - $arrText = array($arrText); - } - - $xmlWriter->startElement('w:p'); - - if ($spIsObject) { - $this->_writeParagraphStyle($xmlWriter, $styleParagraph); - } elseif (!$spIsObject && !is_null($styleParagraph)) { - $xmlWriter->startElement('w:pPr'); - $xmlWriter->startElement('w:pStyle'); - $xmlWriter->writeAttribute('w:val', $styleParagraph); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - } - - foreach ($arrText as $text) { - - if (substr($text, 0, 1) == '{') { - $text = substr($text, 1, -1); - - $xmlWriter->startElement('w:r'); - $xmlWriter->startElement('w:fldChar'); - $xmlWriter->writeAttribute('w:fldCharType', 'begin'); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - - $xmlWriter->startElement('w:r'); - - if ($sfIsObject) { - $this->_writeTextStyle($xmlWriter, $styleFont); - } elseif (!$sfIsObject && !is_null($styleFont)) { - $xmlWriter->startElement('w:rPr'); - $xmlWriter->startElement('w:rStyle'); - $xmlWriter->writeAttribute('w:val', $styleFont); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - } - - $xmlWriter->startElement('w:instrText'); - $xmlWriter->writeAttribute('xml:space', 'preserve'); - $xmlWriter->writeRaw($text); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - - $xmlWriter->startElement('w:r'); - $xmlWriter->startElement('w:fldChar'); - $xmlWriter->writeAttribute('w:fldCharType', 'separate'); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - - $xmlWriter->startElement('w:r'); - $xmlWriter->startElement('w:fldChar'); - $xmlWriter->writeAttribute('w:fldCharType', 'end'); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - } else { - $text = htmlspecialchars($text); - $text = String::controlCharacterPHP2OOXML($text); - - $xmlWriter->startElement('w:r'); - - if ($sfIsObject) { - $this->_writeTextStyle($xmlWriter, $styleFont); - } elseif (!$sfIsObject && !is_null($styleFont)) { - $xmlWriter->startElement('w:rPr'); - $xmlWriter->startElement('w:rStyle'); - $xmlWriter->writeAttribute('w:val', $styleFont); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - } - - $xmlWriter->startElement('w:t'); - $xmlWriter->writeAttribute('xml:space', 'preserve'); - $xmlWriter->writeRaw($text); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - } - } - - $xmlWriter->endElement(); // p - } - /** * Write footnote reference element * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param XMLWriter $xmlWriter * @param PhpOffice\PhpWord\Section $section */ - protected function _writeTextStyle(XMLWriter $xmlWriter, Font $style) + protected function writeFontStyle(XMLWriter $xmlWriter, Font $style) { $font = $style->getName(); $bold = $style->getBold(); @@ -504,207 +970,14 @@ class Base extends WriterPart $xmlWriter->endElement(); } - /** - * Write text break element - * - * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param \PhpOffice\PhpWord\Section\TextBreak $element - */ - protected function _writeTextBreak($xmlWriter, $element = null) - { - $hasStyle = false; - if (!is_null($element)) { - $fontStyle = $element->getFontStyle(); - $sfIsObject = ($fontStyle instanceof Font) ? true : false; - $paragraphStyle = $element->getParagraphStyle(); - $spIsObject = ($paragraphStyle instanceof Paragraph) ? true : false; - $hasStyle = !is_null($fontStyle) || !is_null($paragraphStyle); - } - if ($hasStyle) { - // Paragraph style - $xmlWriter->startElement('w:p'); - if ($spIsObject) { - $this->_writeParagraphStyle($xmlWriter, $paragraphStyle); - } elseif (!$spIsObject && !is_null($paragraphStyle)) { - $xmlWriter->startElement('w:pPr'); - $xmlWriter->startElement('w:pStyle'); - $xmlWriter->writeAttribute('w:val', $paragraphStyle); - $xmlWriter->endElement(); // w:pStyle - $xmlWriter->endElement(); // w:pPr - } - // Font style - if (!is_null($fontStyle)) { - $xmlWriter->startElement('w:pPr'); - if ($sfIsObject) { - $this->_writeTextStyle($xmlWriter, $fontStyle); - } elseif (!$sfIsObject && !is_null($fontStyle)) { - $xmlWriter->startElement('w:rPr'); - $xmlWriter->startElement('w:rStyle'); - $xmlWriter->writeAttribute('w:val', $fontStyle); - $xmlWriter->endElement(); // w:rStyle - $xmlWriter->endElement(); // w:rPr - } - $xmlWriter->endElement(); // w:pPr - } - $xmlWriter->endElement(); // w:p - } else { - // Null element. No paragraph nor font style - $xmlWriter->writeElement('w:p', null); - } - } - - /** - * Write footnote reference element - * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section\Table $table - */ - protected function _writeTable(XMLWriter $xmlWriter, Table $table) - { - $_rows = $table->getRows(); - $_cRows = count($_rows); - - if ($_cRows > 0) { - $xmlWriter->startElement('w:tbl'); - - // Table grid - $cellWidths = array(); - for ($i = 0; $i < $_cRows; $i++) { - $row = $_rows[$i]; - $cells = $row->getCells(); - if (count($cells) <= count($cellWidths)) { - continue; - } - $cellWidths = array(); - foreach ($cells as $cell) { - $cellWidths[] = $cell->getWidth(); - } - } - $xmlWriter->startElement('w:tblGrid'); - foreach ($cellWidths as $width) { - $xmlWriter->startElement('w:gridCol'); - if (!is_null($width)) { - $xmlWriter->writeAttribute('w:w', $width); - $xmlWriter->writeAttribute('w:type', 'dxa'); - } - $xmlWriter->endElement(); - } - $xmlWriter->endElement(); // w:tblGrid - - // Table style - $tblStyle = $table->getStyle(); - $tblWidth = $table->getWidth(); - if ($tblStyle instanceof \PhpOffice\PhpWord\Style\Table) { - $this->_writeTableStyle($xmlWriter, $tblStyle, false); - } else { - if (!empty($tblStyle)) { - $xmlWriter->startElement('w:tblPr'); - $xmlWriter->startElement('w:tblStyle'); - $xmlWriter->writeAttribute('w:val', $tblStyle); - $xmlWriter->endElement(); - if (!is_null($tblWidth)) { - $xmlWriter->startElement('w:tblW'); - $xmlWriter->writeAttribute('w:w', $tblWidth); - $xmlWriter->writeAttribute('w:type', 'pct'); - $xmlWriter->endElement(); - } - $xmlWriter->endElement(); - } - } - - // Table rows - for ($i = 0; $i < $_cRows; $i++) { - $row = $_rows[$i]; - $height = $row->getHeight(); - $rowStyle = $row->getStyle(); - $tblHeader = $rowStyle->getTblHeader(); - $cantSplit = $rowStyle->getCantSplit(); - $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)) { - $xmlWriter->startElement('w:trHeight'); - $xmlWriter->writeAttribute('w:val', $height); - $xmlWriter->writeAttribute('w:hRule', ($exactHeight ? 'exact' : 'atLeast')); - $xmlWriter->endElement(); - } - if ($tblHeader) { - $xmlWriter->startElement('w:tblHeader'); - $xmlWriter->writeAttribute('w:val', '1'); - $xmlWriter->endElement(); - } - if ($cantSplit) { - $xmlWriter->startElement('w:cantSplit'); - $xmlWriter->writeAttribute('w:val', '1'); - $xmlWriter->endElement(); - } - $xmlWriter->endElement(); - } - - foreach ($row->getCells() as $cell) { - $xmlWriter->startElement('w:tc'); - - $cellStyle = $cell->getStyle(); - $width = $cell->getWidth(); - - $xmlWriter->startElement('w:tcPr'); - $xmlWriter->startElement('w:tcW'); - $xmlWriter->writeAttribute('w:w', $width); - $xmlWriter->writeAttribute('w:type', 'dxa'); - $xmlWriter->endElement(); - - 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 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 PreserveText) { - $this->_writePreserveText($xmlWriter, $element); - } elseif ($element instanceof CheckBox) { - $this->_writeCheckBox($xmlWriter, $element); - } - } - } else { - $this->_writeTextBreak($xmlWriter); - } - - $xmlWriter->endElement(); - } - $xmlWriter->endElement(); - } - $xmlWriter->endElement(); - } - } - /** * Write table style * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param XMLWriter $xmlWriter * @param PhpOffice\PhpWord\Style\Table $style * @param boolean $isFullStyle */ - protected function _writeTableStyle( + protected function writeTableStyle( XMLWriter $xmlWriter, \PhpOffice\PhpWord\Style\Table $style, $isFullStyle = true @@ -822,7 +1095,7 @@ class Base extends WriterPart // First Row $firstRow = $style->getFirstRow(); if (!is_null($firstRow)) { - $this->_writeRowStyle($xmlWriter, 'firstRow', $firstRow); + $this->writeRowStyle($xmlWriter, 'firstRow', $firstRow); } } } @@ -830,11 +1103,11 @@ class Base extends WriterPart /** * Write row style * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param XMLWriter $xmlWriter * @param string $type * @param PhpOffice\PhpWord\Style\Table $style */ - protected function _writeRowStyle( + protected function writeRowStyle( XMLWriter $xmlWriter, $type, \PhpOffice\PhpWord\Style\Table $style @@ -900,10 +1173,10 @@ class Base extends WriterPart /** * Write footnote reference element * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param XMLWriter $xmlWriter * @param PhpOffice\PhpWord\Style\Cell $style */ - protected function _writeCellStyle(XMLWriter $xmlWriter, Cell $style = null) + protected function writeCellStyle(XMLWriter $xmlWriter, Cell $style = null) { $bgColor = $style->getBgColor(); $valign = $style->getVAlign(); @@ -1007,288 +1280,85 @@ class Base extends WriterPart } /** - * Write image element + * Write individual rels entry * - * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param mixed $image - * @param boolean $withoutP + * @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 _writeImage(XMLWriter $xmlWriter, $image, $withoutP = false) - { - $rId = $image->getRelationId(); - - $style = $image->getStyle(); - $width = $style->getWidth(); - $height = $style->getHeight(); - $align = $style->getAlign(); - $marginTop = $style->getMarginTop(); - $marginLeft = $style->getMarginLeft(); - $wrappingStyle = $style->getWrappingStyle(); - - if (!$withoutP) { - $xmlWriter->startElement('w:p'); - - if (!is_null($align)) { - $xmlWriter->startElement('w:pPr'); - $xmlWriter->startElement('w:jc'); - $xmlWriter->writeAttribute('w:val', $align); - $xmlWriter->endElement(); - $xmlWriter->endElement(); + protected function writeRelationship( + XMLWriter $xmlWriter = null, + $pId = 1, + $pType = '', + $pTarget = '', + $pTargetMode = '' + ) { + if ($pType != '' && $pTarget != '') { + if (strpos($pId, 'rId') === false) { + $pId = 'rId' . $pId; } - } - $xmlWriter->startElement('w:r'); + // Write relationship + $xmlWriter->startElement('Relationship'); + $xmlWriter->writeAttribute('Id', $pId); + $xmlWriter->writeAttribute('Type', $pType); + $xmlWriter->writeAttribute('Target', $pTarget); - $xmlWriter->startElement('w:pict'); + if ($pTargetMode != '') { + $xmlWriter->writeAttribute('TargetMode', $pTargetMode); + } - $xmlWriter->startElement('v:shape'); - $xmlWriter->writeAttribute('type', '#_x0000_t75'); - - $imgStyle = ''; - if (null !== $width) { - $imgStyle .= 'width:' . $width . 'px;'; - } - if (null !== $height) { - $imgStyle .= 'height:' . $height . 'px;'; - } - if (null !== $marginTop) { - $imgStyle .= 'margin-top:' . $marginTop . 'in;'; - } - if (null !== $marginLeft) { - $imgStyle .= 'margin-left:' . $marginLeft . 'in;'; - } - - switch ($wrappingStyle) { - case \PhpOffice\PhpWord\Style\Image::WRAPPING_STYLE_BEHIND: - $imgStyle .= 'position:absolute;z-index:-251658752;'; - break; - case \PhpOffice\PhpWord\Style\Image::WRAPPING_STYLE_SQUARE: - $imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; - break; - case \PhpOffice\PhpWord\Style\Image::WRAPPING_STYLE_TIGHT: - $imgStyle .= 'position:absolute;z-index:251659264;mso-wrap-edited:f;mso-position-horizontal:absolute;mso-position-vertical:absolute'; - break; - case \PhpOffice\PhpWord\Style\Image::WRAPPING_STYLE_INFRONT: - $imgStyle .= 'position:absolute;zz-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; - break; - } - - $xmlWriter->writeAttribute('style', $imgStyle); - - $xmlWriter->startElement('v:imagedata'); - $xmlWriter->writeAttribute('r:id', 'rId' . $rId); - $xmlWriter->writeAttribute('o:title', ''); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - - $xmlWriter->endElement(); - - $xmlWriter->endElement(); - - if (!$withoutP) { - $xmlWriter->endElement(); // w:p + $xmlWriter->endElement(); + } else { + throw new Exception("Invalid parameters passed."); } } /** - * Write footnote reference element + * Write inline paragraph style * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param mixed $image + * @param XMLWriter $xmlWriter + * @param Paragraph|string $styleParagraph + * @param boolean $spIsObject + * @param boolean $withoutPPR */ - protected function _writeWatermark(XMLWriter $xmlWriter, $image) - { - $rId = $image->getRelationId(); - - $style = $image->getStyle(); - $width = $style->getWidth(); - $height = $style->getHeight(); - $marginLeft = $style->getMarginLeft(); - $marginTop = $style->getMarginTop(); - - $xmlWriter->startElement('w:p'); - - $xmlWriter->startElement('w:r'); - - $xmlWriter->startElement('w:pict'); - - $xmlWriter->startElement('v:shape'); - $xmlWriter->writeAttribute('type', '#_x0000_t75'); - - $strStyle = 'position:absolute;'; - $strStyle .= ' width:' . $width . 'px;'; - $strStyle .= ' height:' . $height . 'px;'; - if (!is_null($marginTop)) { - $strStyle .= ' margin-top:' . $marginTop . 'px;'; - } - if (!is_null($marginLeft)) { - $strStyle .= ' margin-left:' . $marginLeft . 'px;'; - } - - $xmlWriter->writeAttribute('style', $strStyle); - - $xmlWriter->startElement('v:imagedata'); - $xmlWriter->writeAttribute('r:id', 'rId' . $rId); - $xmlWriter->writeAttribute('o:title', ''); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - - $xmlWriter->endElement(); - - $xmlWriter->endElement(); - - $xmlWriter->endElement(); - } - - /** - * Write title element - * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section\Title $title - */ - protected function _writeTitle(XMLWriter $xmlWriter, Title $title) - { - $text = htmlspecialchars($title->getText()); - $text = String::controlCharacterPHP2OOXML($text); - $anchor = $title->getAnchor(); - $bookmarkId = $title->getBookmarkId(); - $style = $title->getStyle(); - - $xmlWriter->startElement('w:p'); - - if (!empty($style)) { - $xmlWriter->startElement('w:pPr'); + private function writeInlineParagraphStyle( + XMLWriter $xmlWriter, + $styleParagraph = null, + $withoutPPR = false + ) { + $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; + if ($spIsObject) { + $this->writeParagraphStyle($xmlWriter, $styleParagraph, $withoutPPR); + } elseif (!$spIsObject && !is_null($styleParagraph)) { + if (!$withoutPPR) { + $xmlWriter->startElement('w:pPr'); + } $xmlWriter->startElement('w:pStyle'); - $xmlWriter->writeAttribute('w:val', $style); + $xmlWriter->writeAttribute('w:val', $styleParagraph); $xmlWriter->endElement(); - $xmlWriter->endElement(); - } - - $xmlWriter->startElement('w:r'); - $xmlWriter->startElement('w:fldChar'); - $xmlWriter->writeAttribute('w:fldCharType', 'end'); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - - $xmlWriter->startElement('w:bookmarkStart'); - $xmlWriter->writeAttribute('w:id', $bookmarkId); - $xmlWriter->writeAttribute('w:name', $anchor); - $xmlWriter->endElement(); - - $xmlWriter->startElement('w:r'); - $xmlWriter->startElement('w:t'); - $xmlWriter->writeRaw($text); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - - $xmlWriter->startElement('w:bookmarkEnd'); - $xmlWriter->writeAttribute('w:id', $bookmarkId); - $xmlWriter->endElement(); - - $xmlWriter->endElement(); - } - - /** - * Write footnote element which links to the actual content in footnotes.xml - * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section\Footnote $footnote - * @param boolean $withoutP - */ - protected function _writeFootnote(XMLWriter $xmlWriter, Footnote $footnote, $withoutP = false) - { - if (!$withoutP) { - $xmlWriter->startElement('w:p'); - } - - $xmlWriter->startElement('w:r'); - $xmlWriter->startElement('w:rPr'); - $xmlWriter->startElement('w:rStyle'); - $xmlWriter->writeAttribute('w:val', 'FootnoteReference'); - $xmlWriter->endElement(); // w:rStyle - $xmlWriter->endElement(); // w:rPr - $xmlWriter->startElement('w:footnoteReference'); - $xmlWriter->writeAttribute('w:id', $footnote->getReferenceId()); - $xmlWriter->endElement(); // w:footnoteReference - - $xmlWriter->endElement(); // w:r - - if (!$withoutP) { - $xmlWriter->endElement(); // w:p - } - } - - /** - * Write CheckBox - * - * @param boolean $withoutP - * @param boolean $checkState - */ - protected function _writeCheckBox(XMLWriter $xmlWriter, CheckBox $checkbox, $withoutP = false, $checkState = false) - { - $name = htmlspecialchars($checkbox->getName()); - $name = String::controlCharacterPHP2OOXML($name); - $text = htmlspecialchars($checkbox->getText()); - $text = String::controlCharacterPHP2OOXML($text); - - $styleFont = $checkbox->getFontStyle(); - $sfIsObject = ($styleFont instanceof Font) ? true : false; - if (!$withoutP) { - $xmlWriter->startElement('w:p'); - $styleParagraph = $checkbox->getParagraphStyle(); - $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; - if ($spIsObject) { - $this->_writeParagraphStyle($xmlWriter, $styleParagraph); - } elseif (!$spIsObject && !is_null($styleParagraph)) { - $xmlWriter->startElement('w:pPr'); - $xmlWriter->startElement('w:pStyle'); - $xmlWriter->writeAttribute('w:val', $styleParagraph); - $xmlWriter->endElement(); + if (!$withoutPPR) { $xmlWriter->endElement(); } } + } - $xmlWriter->startElement('w:r'); - $xmlWriter->startElement('w:fldChar'); - $xmlWriter->writeAttribute('w:fldCharType', 'begin'); - $xmlWriter->startElement('w:ffData'); - $xmlWriter->startElement('w:name'); - $xmlWriter->writeAttribute('w:val', $name); - $xmlWriter->endElement(); //w:name - $xmlWriter->writeAttribute('w:enabled', ''); - $xmlWriter->startElement('w:calcOnExit'); - $xmlWriter->writeAttribute('w:val', '0'); - $xmlWriter->endElement(); //w:calcOnExit - $xmlWriter->startElement('w:checkBox'); - $xmlWriter->writeAttribute('w:sizeAuto', ''); - $xmlWriter->startElement('w:default'); - $xmlWriter->writeAttribute('w:val', ($checkState ? '1' : '0')); - $xmlWriter->endElement(); //w:default - $xmlWriter->endElement(); //w:checkBox - $xmlWriter->endElement(); // w:ffData - $xmlWriter->endElement(); // w:fldChar - $xmlWriter->endElement(); // w:r - - $xmlWriter->startElement('w:r'); - $xmlWriter->startElement('w:instrText'); - $xmlWriter->writeAttribute('xml:space', 'preserve'); - $xmlWriter->writeRaw(' FORMCHECKBOX '); - $xmlWriter->endElement();// w:instrText - $xmlWriter->endElement(); // w:r - $xmlWriter->startElement('w:r'); - $xmlWriter->startElement('w:fldChar'); - $xmlWriter->writeAttribute('w:fldCharType', 'seperate'); - $xmlWriter->endElement();// w:fldChar - $xmlWriter->endElement(); // w:r - $xmlWriter->startElement('w:r'); - $xmlWriter->startElement('w:fldChar'); - $xmlWriter->writeAttribute('w:fldCharType', 'end'); - $xmlWriter->endElement();// w:fldChar - $xmlWriter->endElement(); // w:r - - $xmlWriter->startElement('w:r'); + /** + * Write inline font style + * + * @param XMLWriter $xmlWriter + * @param Font|string $styleFont + * @param boolean $sfIsObject + */ + private function writeInlineFontStyle( + XMLWriter $xmlWriter, + $styleFont = null + ) { + $sfIsObject = ($styleFont instanceof Font) ? true : false; if ($sfIsObject) { - $this->_writeTextStyle($xmlWriter, $styleFont); + $this->writeFontStyle($xmlWriter, $styleFont); } elseif (!$sfIsObject && !is_null($styleFont)) { $xmlWriter->startElement('w:rPr'); $xmlWriter->startElement('w:rStyle'); @@ -1296,14 +1366,5 @@ class Base extends WriterPart $xmlWriter->endElement(); $xmlWriter->endElement(); } - $xmlWriter->startElement('w:t'); - $xmlWriter->writeAttribute('xml:space', 'preserve'); - $xmlWriter->writeRaw($text); - $xmlWriter->endElement(); // w:t - $xmlWriter->endElement(); // w:r - - if (!$withoutP) { - $xmlWriter->endElement(); // w:p - } } } diff --git a/src/PhpWord/Writer/Word2007/ContentTypes.php b/src/PhpWord/Writer/Word2007/ContentTypes.php index 9f8c3fb2..2e2f3def 100755 --- a/src/PhpWord/Writer/Word2007/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/ContentTypes.php @@ -19,12 +19,12 @@ class ContentTypes extends WriterPart { /** * Write [Content_Types].xml - * @param array $_imageTypes - * @param array $_objectTypes + * @param array $imageTypes + * @param array $objectTypes * @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(); @@ -37,27 +37,27 @@ class ContentTypes extends WriterPart $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types'); // Rels - $this->_writeDefaultContentType( + $this->writeDefaultContentType( $xmlWriter, 'rels', 'application/vnd.openxmlformats-package.relationships+xml' ); // XML - $this->_writeDefaultContentType( + $this->writeDefaultContentType( $xmlWriter, 'xml', 'application/xml' ); // Add media content-types - foreach ($_imageTypes as $key => $value) { - $this->_writeDefaultContentType($xmlWriter, $key, $value); + foreach ($imageTypes as $key => $value) { + $this->writeDefaultContentType($xmlWriter, $key, $value); } // Add embedding content-types - if (count($_objectTypes) > 0) { - $this->_writeDefaultContentType( + if (count($objectTypes) > 0) { + $this->writeDefaultContentType( $xmlWriter, 'bin', 'application/vnd.openxmlformats-officedocument.oleObject' @@ -65,76 +65,76 @@ class ContentTypes extends WriterPart } // DocProps - $this->_writeOverrideContentType( + $this->writeOverrideContentType( $xmlWriter, '/docProps/app.xml', 'application/vnd.openxmlformats-officedocument.extended-properties+xml' ); - $this->_writeOverrideContentType( + $this->writeOverrideContentType( $xmlWriter, '/docProps/core.xml', 'application/vnd.openxmlformats-package.core-properties+xml' ); // Document - $this->_writeOverrideContentType( + $this->writeOverrideContentType( $xmlWriter, '/word/document.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml' ); // Styles - $this->_writeOverrideContentType( + $this->writeOverrideContentType( $xmlWriter, '/word/styles.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml' ); // Numbering - $this->_writeOverrideContentType( + $this->writeOverrideContentType( $xmlWriter, '/word/numbering.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml' ); // Settings - $this->_writeOverrideContentType( + $this->writeOverrideContentType( $xmlWriter, '/word/settings.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml' ); // Theme1 - $this->_writeOverrideContentType( + $this->writeOverrideContentType( $xmlWriter, '/word/theme/theme1.xml', 'application/vnd.openxmlformats-officedocument.theme+xml' ); // WebSettings - $this->_writeOverrideContentType( + $this->writeOverrideContentType( $xmlWriter, '/word/webSettings.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml' ); // Font Table - $this->_writeOverrideContentType( + $this->writeOverrideContentType( $xmlWriter, '/word/fontTable.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml' ); // Footnotes - $this->_writeOverrideContentType( + $this->writeOverrideContentType( $xmlWriter, '/word/footnotes.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml' ); for ($i = 1; $i <= $_cHdrs; $i++) { - $this->_writeOverrideContentType( + $this->writeOverrideContentType( $xmlWriter, '/word/header' . $i . '.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml' @@ -143,7 +143,7 @@ class ContentTypes extends WriterPart for ($i = 1; $i <= count($footers); $i++) { if (!is_null($footers[$i])) { - $this->_writeOverrideContentType( + $this->writeOverrideContentType( $xmlWriter, '/word/footer' . $i . '.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml' @@ -158,23 +158,6 @@ class ContentTypes extends WriterPart return $xmlWriter->getData(); } - /** - * Get image mime type - * - * @param string $pFile Filename - * @return string Mime Type - * @throws \PhpOffice\PhpWord\Exceptions\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 * @@ -183,7 +166,7 @@ class ContentTypes extends WriterPart * @param string $pContentType Content type * @throws \PhpOffice\PhpWord\Exceptions\Exception */ - private function _writeDefaultContentType(XMLWriter $xmlWriter = null, $pPartname = '', $pContentType = '') + private function writeDefaultContentType(XMLWriter $xmlWriter = null, $pPartname = '', $pContentType = '') { if ($pPartname != '' && $pContentType != '') { // Write content type @@ -204,7 +187,7 @@ class ContentTypes extends WriterPart * @param string $pContentType Content type * @throws \PhpOffice\PhpWord\Exceptions\Exception */ - private function _writeOverrideContentType(XMLWriter $xmlWriter = null, $pPartname = '', $pContentType = '') + private function writeOverrideContentType(XMLWriter $xmlWriter = null, $pPartname = '', $pContentType = '') { if ($pPartname != '' && $pContentType != '') { // Write content type @@ -216,4 +199,21 @@ class ContentTypes extends WriterPart throw new Exception("Invalid parameters passed."); } } + + /** + * Get image mime type + * + * @param string $pFile Filename + * @return string Mime Type + * @throws \PhpOffice\PhpWord\Exceptions\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/Document.php b/src/PhpWord/Writer/Word2007/Document.php index 585050df..05fd145a 100644 --- a/src/PhpWord/Writer/Word2007/Document.php +++ b/src/PhpWord/Writer/Word2007/Document.php @@ -72,38 +72,38 @@ class Document extends Base $_elements = $section->getElements(); foreach ($_elements as $element) { if ($element instanceof Text) { - $this->_writeText($xmlWriter, $element); + $this->writeText($xmlWriter, $element); } elseif ($element instanceof TextRun) { - $this->_writeTextRun($xmlWriter, $element); + $this->writeTextRun($xmlWriter, $element); } elseif ($element instanceof Link) { - $this->_writeLink($xmlWriter, $element); + $this->writeLink($xmlWriter, $element); } elseif ($element instanceof Title) { - $this->_writeTitle($xmlWriter, $element); + $this->writeTitle($xmlWriter, $element); } elseif ($element instanceof TextBreak) { - $this->_writeTextBreak($xmlWriter, $element); + $this->writeTextBreak($xmlWriter, $element); } elseif ($element instanceof PageBreak) { - $this->_writePageBreak($xmlWriter); + $this->writePageBreak($xmlWriter); } elseif ($element instanceof Table) { - $this->_writeTable($xmlWriter, $element); + $this->writeTable($xmlWriter, $element); } elseif ($element instanceof ListItem) { - $this->_writeListItem($xmlWriter, $element); + $this->writeListItem($xmlWriter, $element); } elseif ($element instanceof Image) { - $this->_writeImage($xmlWriter, $element); + $this->writeImage($xmlWriter, $element); } elseif ($element instanceof Object) { - $this->_writeObject($xmlWriter, $element); + $this->writeObject($xmlWriter, $element); } elseif ($element instanceof TOC) { - $this->_writeTOC($xmlWriter); + $this->writeTOC($xmlWriter); } elseif ($element instanceof Footnote) { - $this->_writeFootnote($xmlWriter, $element); + $this->writeFootnote($xmlWriter, $element); } elseif ($element instanceof CheckBox) { - $this->_writeCheckBox($xmlWriter, $element); + $this->writeCheckBox($xmlWriter, $element); } } if ($pSection == $countSections) { - $this->_writeEndSection($xmlWriter, $section); + $this->writeEndSection($xmlWriter, $section); } else { - $this->_writeSection($xmlWriter, $section); + $this->writeSection($xmlWriter, $section); } } } @@ -121,11 +121,11 @@ class Document extends Base * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter * @param PhpOffice\PhpWord\Section $section */ - private function _writeSection(XMLWriter $xmlWriter, Section $section) + private function writeSection(XMLWriter $xmlWriter, Section $section) { $xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:pPr'); - $this->_writeEndSection($xmlWriter, $section, 3); + $this->writeEndSection($xmlWriter, $section, 3); $xmlWriter->endElement(); $xmlWriter->endElement(); } @@ -136,7 +136,7 @@ class Document extends Base * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter * @param PhpOffice\PhpWord\Section $section */ - private function _writeEndSection(XMLWriter $xmlWriter, Section $section) + private function writeEndSection(XMLWriter $xmlWriter, Section $section) { $settings = $section->getSettings(); $_headers = $section->getHeaders(); @@ -274,7 +274,7 @@ class Document extends Base * * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter */ - private function _writePageBreak(XMLWriter $xmlWriter) + private function writePageBreak(XMLWriter $xmlWriter) { $xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:r'); @@ -285,122 +285,12 @@ class Document extends Base $xmlWriter->endElement(); } - /** - * Write list item element - * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section\ListItem $listItem - */ - public function _writeListItem(XMLWriter $xmlWriter, ListItem $listItem) - { - $textObject = $listItem->getTextObject(); - $text = $textObject->getText(); - $styleParagraph = $textObject->getParagraphStyle(); - $SpIsObject = ($styleParagraph instanceof Paragraph) ? true : false; - - $depth = $listItem->getDepth(); - $listType = $listItem->getStyle()->getListType(); - - $xmlWriter->startElement('w:p'); - $xmlWriter->startElement('w:pPr'); - - if ($SpIsObject) { - $this->_writeParagraphStyle($xmlWriter, $styleParagraph, true); - } elseif (!$SpIsObject && !is_null($styleParagraph)) { - $xmlWriter->startElement('w:pStyle'); - $xmlWriter->writeAttribute('w:val', $styleParagraph); - $xmlWriter->endElement(); - } - - $xmlWriter->startElement('w:numPr'); - - $xmlWriter->startElement('w:ilvl'); - $xmlWriter->writeAttribute('w:val', $depth); - $xmlWriter->endElement(); - - $xmlWriter->startElement('w:numId'); - $xmlWriter->writeAttribute('w:val', $listType); - $xmlWriter->endElement(); - - $xmlWriter->endElement(); - $xmlWriter->endElement(); - - $this->_writeText($xmlWriter, $textObject, true); - - $xmlWriter->endElement(); - } - - /** - * Write object element - * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section\Object $object - */ - protected function _writeObject(XMLWriter $xmlWriter, Object $object) - { - $rIdObject = $object->getRelationId(); - $rIdImage = $object->getImageRelationId(); - $shapeId = md5($rIdObject . '_' . $rIdImage); - - $objectId = $object->getObjectId(); - - $style = $object->getStyle(); - $width = $style->getWidth(); - $height = $style->getHeight(); - $align = $style->getAlign(); - - - $xmlWriter->startElement('w:p'); - - if (!is_null($align)) { - $xmlWriter->startElement('w:pPr'); - $xmlWriter->startElement('w:jc'); - $xmlWriter->writeAttribute('w:val', $align); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - } - - $xmlWriter->startElement('w:r'); - - $xmlWriter->startElement('w:object'); - $xmlWriter->writeAttribute('w:dxaOrig', '249'); - $xmlWriter->writeAttribute('w:dyaOrig', '160'); - - $xmlWriter->startElement('v:shape'); - $xmlWriter->writeAttribute('id', $shapeId); - $xmlWriter->writeAttribute('type', '#_x0000_t75'); - $xmlWriter->writeAttribute('style', 'width:104px;height:67px'); - $xmlWriter->writeAttribute('o:ole', ''); - - $xmlWriter->startElement('v:imagedata'); - $xmlWriter->writeAttribute('r:id', 'rId' . $rIdImage); - $xmlWriter->writeAttribute('o:title', ''); - $xmlWriter->endElement(); - - $xmlWriter->endElement(); - - $xmlWriter->startElement('o:OLEObject'); - $xmlWriter->writeAttribute('Type', 'Embed'); - $xmlWriter->writeAttribute('ProgID', 'Package'); - $xmlWriter->writeAttribute('ShapeID', $shapeId); - $xmlWriter->writeAttribute('DrawAspect', 'Icon'); - $xmlWriter->writeAttribute('ObjectID', '_' . $objectId); - $xmlWriter->writeAttribute('r:id', 'rId' . $rIdObject); - $xmlWriter->endElement(); - - $xmlWriter->endElement(); - - $xmlWriter->endElement(); // w:r - - $xmlWriter->endElement(); // w:p - } - /** * Write TOC element * * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter */ - private function _writeTOC(XMLWriter $xmlWriter) + private function writeTOC(XMLWriter $xmlWriter) { $titles = TOC::getTitles(); $styleFont = TOC::getStyleFont(); @@ -421,7 +311,7 @@ class Document extends Base $xmlWriter->startElement('w:pPr'); if ($isObject && !is_null($styleFont->getParagraphStyle())) { - $this->_writeParagraphStyle($xmlWriter, $styleFont->getParagraphStyle()); + $this->writeParagraphStyle($xmlWriter, $styleFont->getParagraphStyle()); } if ($indent > 0) { @@ -479,7 +369,7 @@ class Document extends Base $xmlWriter->startElement('w:r'); if ($isObject) { - $this->_writeTextStyle($xmlWriter, $styleFont); + $this->writeFontStyle($xmlWriter, $styleFont); } $xmlWriter->startElement('w:t'); diff --git a/src/PhpWord/Writer/Word2007/DocumentRels.php b/src/PhpWord/Writer/Word2007/DocumentRels.php index 20a014f2..53a5ded5 100755 --- a/src/PhpWord/Writer/Word2007/DocumentRels.php +++ b/src/PhpWord/Writer/Word2007/DocumentRels.php @@ -15,7 +15,7 @@ use PhpOffice\PhpWord\Shared\XMLWriter; /** * Word2007 document rels part writer */ -class DocumentRels extends WriterPart +class DocumentRels extends Base { /** * Write word/_rels/document.xml.rels @@ -35,7 +35,7 @@ class DocumentRels extends WriterPart $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); // Relationship word/document.xml - $this->_writeRelationship( + $this->writeRelationship( $xmlWriter, 1, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles', @@ -43,7 +43,7 @@ class DocumentRels extends WriterPart ); // Relationship word/numbering.xml - $this->_writeRelationship( + $this->writeRelationship( $xmlWriter, 2, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering', @@ -51,7 +51,7 @@ class DocumentRels extends WriterPart ); // Relationship word/settings.xml - $this->_writeRelationship( + $this->writeRelationship( $xmlWriter, 3, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings', @@ -59,7 +59,7 @@ class DocumentRels extends WriterPart ); // Relationship word/settings.xml - $this->_writeRelationship( + $this->writeRelationship( $xmlWriter, 4, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme', @@ -67,7 +67,7 @@ class DocumentRels extends WriterPart ); // Relationship word/settings.xml - $this->_writeRelationship( + $this->writeRelationship( $xmlWriter, 5, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings', @@ -75,7 +75,7 @@ class DocumentRels extends WriterPart ); // Relationship word/settings.xml - $this->_writeRelationship( + $this->writeRelationship( $xmlWriter, 6, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable', @@ -89,7 +89,7 @@ class DocumentRels extends WriterPart $relationId = $relation['rID']; $targetMode = ($relationType == 'hyperlink') ? 'External' : ''; - $this->_writeRelationship( + $this->writeRelationship( $xmlWriter, $relationId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/' . $relationType, @@ -128,7 +128,7 @@ class DocumentRels extends WriterPart $relationName = $relation['target']; $relationId = $relation['rID']; - $this->_writeRelationship( + $this->writeRelationship( $xmlWriter, $relationId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/' . $relationType, @@ -142,36 +142,4 @@ class DocumentRels extends WriterPart // Return return $xmlWriter->getData(); } - - /** - * Write individual rels entry - * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param int $pId Relationship ID - * @param string $pType Relationship type - * @param string $pTarget Relationship target - * @param string $pTargetMode Relationship target mode - */ - private function _writeRelationship(XMLWriter $xmlWriter = null, $pId = 1, $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."); - } - } } diff --git a/src/PhpWord/Writer/Word2007/Footer.php b/src/PhpWord/Writer/Word2007/Footer.php index d4f68826..49fad838 100644 --- a/src/PhpWord/Writer/Word2007/Footer.php +++ b/src/PhpWord/Writer/Word2007/Footer.php @@ -50,17 +50,17 @@ class Footer extends Base foreach ($_elements as $element) { if ($element instanceof Text) { - $this->_writeText($xmlWriter, $element); + $this->writeText($xmlWriter, $element); } elseif ($element instanceof TextRun) { - $this->_writeTextRun($xmlWriter, $element); + $this->writeTextRun($xmlWriter, $element); } elseif ($element instanceof TextBreak) { - $this->_writeTextBreak($xmlWriter, $element); + $this->writeTextBreak($xmlWriter, $element); } elseif ($element instanceof Table) { - $this->_writeTable($xmlWriter, $element); + $this->writeTable($xmlWriter, $element); } elseif ($element instanceof Image) { - $this->_writeImage($xmlWriter, $element); + $this->writeImage($xmlWriter, $element); } elseif ($element instanceof PreserveText) { - $this->_writePreserveText($xmlWriter, $element); + $this->writePreserveText($xmlWriter, $element); } } diff --git a/src/PhpWord/Writer/Word2007/Footnotes.php b/src/PhpWord/Writer/Word2007/Footnotes.php index d69e0f0c..1d244b01 100644 --- a/src/PhpWord/Writer/Word2007/Footnotes.php +++ b/src/PhpWord/Writer/Word2007/Footnotes.php @@ -80,7 +80,7 @@ class Footnotes extends Base * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter * @param PhpOffice\PhpWord\Section\Footnote $footnote */ - private function writeFootnote(XMLWriter $xmlWriter, Footnote $footnote) + protected function writeFootnote(XMLWriter $xmlWriter, Footnote $footnote) { $xmlWriter->startElement('w:footnote'); $xmlWriter->writeAttribute('w:id', $footnote->getReferenceId()); @@ -89,7 +89,7 @@ class Footnotes extends Base $paragraphStyle = $footnote->getParagraphStyle(); $spIsObject = ($paragraphStyle instanceof Paragraph) ? true : false; if ($spIsObject) { - $this->_writeParagraphStyle($xmlWriter, $paragraphStyle); + $this->writeParagraphStyle($xmlWriter, $paragraphStyle); } elseif (!$spIsObject && !is_null($paragraphStyle)) { $xmlWriter->startElement('w:pPr'); $xmlWriter->startElement('w:pStyle'); @@ -118,9 +118,9 @@ class Footnotes extends Base if (count($elements) > 0) { foreach ($elements as $element) { if ($element instanceof Text) { - $this->_writeText($xmlWriter, $element, true); + $this->writeText($xmlWriter, $element, true); } elseif ($element instanceof Link) { - $this->_writeLink($xmlWriter, $element, true); + $this->writeLink($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 f9e5c015..d0665de3 100644 --- a/src/PhpWord/Writer/Word2007/FootnotesRels.php +++ b/src/PhpWord/Writer/Word2007/FootnotesRels.php @@ -15,7 +15,7 @@ use PhpOffice\PhpWord\Shared\XMLWriter; /** * Word2007 footnotes rel part writer */ -class FootnotesRels extends WriterPart +class FootnotesRels extends Base { /** * Write word/_rels/footnotes.xml.rels @@ -41,7 +41,7 @@ class FootnotesRels extends WriterPart $relationId = $relation['rID']; $targetMode = ($relationType == 'hyperlink') ? 'External' : ''; - $this->_writeRelationship($xmlWriter, $relationId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/' . $relationType, $relationName, $targetMode); + $this->writeRelationship($xmlWriter, $relationId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/' . $relationType, $relationName, $targetMode); } $xmlWriter->endElement(); @@ -49,36 +49,4 @@ class FootnotesRels extends WriterPart // Return return $xmlWriter->getData(); } - - /** - * Write individual rels entry - * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param int $pId Relationship ID - * @param string $pType Relationship type - * @param string $pTarget Relationship target - * @param string $pTargetMode Relationship target mode - */ - private function _writeRelationship(XMLWriter $xmlWriter = null, $pId = 1, $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."); - } - } } diff --git a/src/PhpWord/Writer/Word2007/Header.php b/src/PhpWord/Writer/Word2007/Header.php index 50d77e98..88400c50 100644 --- a/src/PhpWord/Writer/Word2007/Header.php +++ b/src/PhpWord/Writer/Word2007/Header.php @@ -51,21 +51,21 @@ class Header extends Base foreach ($_elements as $element) { if ($element instanceof Text) { - $this->_writeText($xmlWriter, $element); + $this->writeText($xmlWriter, $element); } elseif ($element instanceof TextRun) { - $this->_writeTextRun($xmlWriter, $element); + $this->writeTextRun($xmlWriter, $element); } elseif ($element instanceof TextBreak) { - $this->_writeTextBreak($xmlWriter, $element); + $this->writeTextBreak($xmlWriter, $element); } elseif ($element instanceof Table) { - $this->_writeTable($xmlWriter, $element); + $this->writeTable($xmlWriter, $element); } elseif ($element instanceof Image) { if (!$element->getIsWatermark()) { - $this->_writeImage($xmlWriter, $element); + $this->writeImage($xmlWriter, $element); } else { - $this->_writeWatermark($xmlWriter, $element); + $this->writeWatermark($xmlWriter, $element); } } elseif ($element instanceof PreserveText) { - $this->_writePreserveText($xmlWriter, $element); + $this->writePreserveText($xmlWriter, $element); } } diff --git a/src/PhpWord/Writer/Word2007/Rels.php b/src/PhpWord/Writer/Word2007/Rels.php index 19ce58b0..e7e26856 100755 --- a/src/PhpWord/Writer/Word2007/Rels.php +++ b/src/PhpWord/Writer/Word2007/Rels.php @@ -16,7 +16,7 @@ use PhpOffice\PhpWord\Shared\XMLWriter; /** * Word2007 rels part writer */ -class Rels extends WriterPart +class Rels extends Base { /** * Write _rels/.rels @@ -38,7 +38,7 @@ class Rels extends WriterPart $relationId = 1; // Relationship word/document.xml - $this->_writeRelationship( + $this->writeRelationship( $xmlWriter, $relationId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument', @@ -46,7 +46,7 @@ class Rels extends WriterPart ); // Relationship docProps/core.xml - $this->_writeRelationship( + $this->writeRelationship( $xmlWriter, ++$relationId, 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties', @@ -54,7 +54,7 @@ class Rels extends WriterPart ); // Relationship docProps/app.xml - $this->_writeRelationship( + $this->writeRelationship( $xmlWriter, ++$relationId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties', @@ -65,37 +65,4 @@ class Rels extends WriterPart return $xmlWriter->getData(); } - - /** - * Write Override content type - * - * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param int $pId Relationship ID. rId will be prepended! - * @param string $pType Relationship type - * @param string $pTarget Relationship target - * @param string $pTargetMode Relationship target mode - * @throws \PhpOffice\PhpWord\Exceptions\Exception - */ - private function _writeRelationship(XMLWriter $xmlWriter = null, $pId = 1, $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."); - } - } } diff --git a/src/PhpWord/Writer/Word2007/Styles.php b/src/PhpWord/Writer/Word2007/Styles.php index 1c7c05e2..ca0acfdb 100644 --- a/src/PhpWord/Writer/Word2007/Styles.php +++ b/src/PhpWord/Writer/Word2007/Styles.php @@ -20,13 +20,6 @@ use PhpOffice\PhpWord\Style\Paragraph; */ class Styles extends Base { - /** - * PhpWord object - * - * @var PhpWord - */ - private $phpWord; - /** * Write word/styles.xml * @@ -34,8 +27,6 @@ class Styles extends Base */ public function writeStyles(PhpWord $phpWord = null) { - $this->phpWord = $phpWord; - // Create XML writer $xmlWriter = $this->getXmlWriter(); @@ -52,7 +43,7 @@ class Styles extends Base ); // Write default styles $styles = Style::getStyles(); - $this->writeDefaultStyles($xmlWriter, $styles); + $this->writeDefaultStyles($xmlWriter, $phpWord, $styles); // Write other styles if (count($styles) > 0) { foreach ($styles as $styleName => $style) { @@ -94,10 +85,10 @@ class Styles extends Base $xmlWriter->startElement('w:basedOn'); $xmlWriter->writeAttribute('w:val', 'Normal'); $xmlWriter->endElement(); - $this->_writeParagraphStyle($xmlWriter, $paragraphStyle); + $this->writeParagraphStyle($xmlWriter, $paragraphStyle); } - $this->_writeTextStyle($xmlWriter, $style); + $this->writeFontStyle($xmlWriter, $style); $xmlWriter->endElement(); @@ -127,7 +118,7 @@ class Styles extends Base $xmlWriter->endElement(); } - $this->_writeParagraphStyle($xmlWriter, $style); + $this->writeParagraphStyle($xmlWriter, $style); $xmlWriter->endElement(); } elseif ($style instanceof \PhpOffice\PhpWord\Style\Table) { @@ -144,7 +135,7 @@ class Styles extends Base $xmlWriter->writeAttribute('w:val', '99'); $xmlWriter->endElement(); - $this->_writeTableStyle($xmlWriter, $style); + $this->writeTableStyle($xmlWriter, $style); $xmlWriter->endElement(); // w:style } @@ -163,10 +154,10 @@ class Styles extends Base * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter * @param array $styles */ - private function writeDefaultStyles(XMLWriter $xmlWriter, $styles) + private function writeDefaultStyles(XMLWriter $xmlWriter, PhpWord $phpWord, $styles) { - $fontName = $this->phpWord->getDefaultFontName(); - $fontSize = $this->phpWord->getDefaultFontSize(); + $fontName = $phpWord->getDefaultFontName(); + $fontSize = $phpWord->getDefaultFontSize(); // Default font $xmlWriter->startElement('w:docDefaults'); @@ -197,7 +188,7 @@ class Styles extends Base $xmlWriter->writeAttribute('w:val', 'Normal'); $xmlWriter->endElement(); // w:name if (array_key_exists('Normal', $styles)) { - $this->_writeParagraphStyle($xmlWriter, $styles['Normal']); + $this->writeParagraphStyle($xmlWriter, $styles['Normal']); } $xmlWriter->endElement(); // w:style From 41407825a2d998baabb2024ac65d8cbc202de223 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 30 Mar 2014 22:23:12 +0700 Subject: [PATCH 3/5] Namespace adjustments on writer classes --- src/PhpWord/Writer/ODText.php | 8 ++--- src/PhpWord/Writer/ODText/Content.php | 26 +++++++------- src/PhpWord/Writer/ODText/Manifest.php | 4 +-- src/PhpWord/Writer/ODText/Meta.php | 2 +- src/PhpWord/Writer/ODText/Mimetype.php | 2 +- src/PhpWord/Writer/ODText/Styles.php | 10 +++--- src/PhpWord/Writer/RTF.php | 10 +++--- src/PhpWord/Writer/Word2007.php | 2 +- src/PhpWord/Writer/Word2007/Base.php | 36 ++++++++++---------- src/PhpWord/Writer/Word2007/ContentTypes.php | 10 +++--- src/PhpWord/Writer/Word2007/DocProps.php | 2 +- src/PhpWord/Writer/Word2007/Document.php | 14 ++++---- src/PhpWord/Writer/Word2007/Footer.php | 5 +-- src/PhpWord/Writer/Word2007/Footnotes.php | 20 ++++------- src/PhpWord/Writer/Word2007/Header.php | 5 +-- src/PhpWord/Writer/Word2007/Rels.php | 2 +- src/PhpWord/Writer/Word2007/Styles.php | 7 ++-- src/PhpWord/Writer/Writer.php | 2 +- 18 files changed, 81 insertions(+), 86 deletions(-) diff --git a/src/PhpWord/Writer/ODText.php b/src/PhpWord/Writer/ODText.php index 8c58df60..3aebf2a4 100755 --- a/src/PhpWord/Writer/ODText.php +++ b/src/PhpWord/Writer/ODText.php @@ -26,13 +26,13 @@ class ODText extends Writer implements IWriter /** * Private unique PHPWord_Worksheet_BaseDrawing HashTable * - * @var \PhpOffice\PhpWord\HashTable + * @var HashTable */ private $drawingHashTable; /** * Create new ODText writer - * @param \PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord */ public function __construct(PhpWord $phpWord = null) { @@ -57,7 +57,7 @@ class ODText extends Writer implements IWriter * Save PhpWord to file * * @param string $pFilename - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws Exception */ public function save($pFilename = null) { @@ -139,7 +139,7 @@ class ODText extends Writer implements IWriter /** * Get PHPWord_Worksheet_BaseDrawing HashTable * - * @return \PhpOffice\PhpWord\HashTable + * @return HashTable */ public function getDrawingHashTable() { diff --git a/src/PhpWord/Writer/ODText/Content.php b/src/PhpWord/Writer/ODText/Content.php index 8142d9dc..2e9a61e6 100644 --- a/src/PhpWord/Writer/ODText/Content.php +++ b/src/PhpWord/Writer/ODText/Content.php @@ -35,7 +35,7 @@ class Content extends WriterPart /** * Write content file to XML format * - * @param \PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord * @return string XML Output */ public function writeContent(PhpWord $phpWord = null) @@ -126,7 +126,7 @@ class Content extends WriterPart $numFonts = 0; if (count($styles) > 0) { foreach ($styles as $styleName => $style) { - // PhpOffice\PhpWord\Style\Font + // Font if ($style instanceof Font) { $numFonts++; $name = $style->getName(); @@ -158,7 +158,7 @@ class Content extends WriterPart if (preg_match('#^T[0-9]+$#', $styleName) != 0 || preg_match('#^P[0-9]+$#', $styleName) != 0 ) { - // PhpOffice\PhpWord\Style\Font + // Font if ($style instanceof Font) { $xmlWriter->startElement('style:style'); $xmlWriter->writeAttribute('style:name', $styleName); @@ -288,8 +288,8 @@ class Content extends WriterPart /** * Write text * - * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param \PhpOffice\PhpWord\Section\Text $text + * @param XMLWriter $xmlWriter + * @param Text $text * @param bool $withoutP */ protected function writeText(XMLWriter $xmlWriter, Text $text, $withoutP = false) @@ -336,8 +336,8 @@ class Content extends WriterPart /** * Write TextRun section * - * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param \PhpOffice\PhpWord\Section\TextRun $textrun + * @param XMLWriter $xmlWriter + * @param TextRun $textrun * @todo Enable all other section types */ protected function writeTextRun(XMLWriter $xmlWriter, TextRun $textrun) @@ -357,7 +357,7 @@ class Content extends WriterPart /** * Write TextBreak * - * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param XMLWriter $xmlWriter */ protected function writeTextBreak(XMLWriter $xmlWriter = null) { @@ -370,8 +370,8 @@ class Content extends WriterPart /** * Write end section * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section $section + * @param XMLWriter $xmlWriter + * @param Section $section */ private function writeEndSection(XMLWriter $xmlWriter = null, Section $section = null) { @@ -380,8 +380,8 @@ class Content extends WriterPart /** * Write section * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section $section + * @param XMLWriter $xmlWriter + * @param Section $section */ private function writeSection(XMLWriter $xmlWriter = null, Section $section = null) { @@ -391,7 +391,7 @@ class Content extends WriterPart /** * Write unsupported element * - * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param XMLWriter $xmlWriter * @param string $element */ private function writeUnsupportedElement($xmlWriter, $element) diff --git a/src/PhpWord/Writer/ODText/Manifest.php b/src/PhpWord/Writer/ODText/Manifest.php index 25a17f2e..31168e3d 100755 --- a/src/PhpWord/Writer/ODText/Manifest.php +++ b/src/PhpWord/Writer/ODText/Manifest.php @@ -21,7 +21,7 @@ class Manifest extends WriterPart /** * Write Manifest file to XML format * - * @param \PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord * @return string XML Output */ public function writeManifest(PhpWord $phpWord = null) @@ -97,7 +97,7 @@ class Manifest extends WriterPart * * @param string $pFile Filename * @return string Mime Type - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws Exception */ private function getImageMimeType($pFile = '') { diff --git a/src/PhpWord/Writer/ODText/Meta.php b/src/PhpWord/Writer/ODText/Meta.php index fdd0ec7c..51647173 100644 --- a/src/PhpWord/Writer/ODText/Meta.php +++ b/src/PhpWord/Writer/ODText/Meta.php @@ -20,7 +20,7 @@ class Meta extends WriterPart /** * Write Meta file to XML format * - * @param \PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord * @return string XML Output */ public function writeMeta(PhpWord $phpWord = null) diff --git a/src/PhpWord/Writer/ODText/Mimetype.php b/src/PhpWord/Writer/ODText/Mimetype.php index 0d4e598e..1e713f2c 100644 --- a/src/PhpWord/Writer/ODText/Mimetype.php +++ b/src/PhpWord/Writer/ODText/Mimetype.php @@ -19,7 +19,7 @@ class Mimetype extends WriterPart /** * Write Mimetype to Text format * - * @param \PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord * @return string Text Output */ public function writeMimetype(PhpWord $phpWord = null) diff --git a/src/PhpWord/Writer/ODText/Styles.php b/src/PhpWord/Writer/ODText/Styles.php index 6a91a7e2..4ae937e0 100644 --- a/src/PhpWord/Writer/ODText/Styles.php +++ b/src/PhpWord/Writer/ODText/Styles.php @@ -24,7 +24,7 @@ class Styles extends WriterPart /** * Write Styles file to XML format * - * @param \PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord * @return string XML Output */ public function writeStyles(PhpWord $phpWord = null) @@ -73,7 +73,7 @@ class Styles extends WriterPart $numFonts = 0; if (count($styles) > 0) { foreach ($styles as $styleName => $style) { - // PhpOffice\PhpWord\Style\Font + // Font if ($style instanceof Font) { $numFonts++; $name = $style->getName(); @@ -144,7 +144,7 @@ class Styles extends WriterPart if (preg_match('#^T[0-9]+$#', $styleName) == 0 && preg_match('#^P[0-9]+$#', $styleName) == 0 ) { - // PhpOffice\PhpWord\Style\Font + // Font if ($style instanceof Font) { // style:style $xmlWriter->startElement('style:style'); @@ -168,7 +168,7 @@ class Styles extends WriterPart $xmlWriter->endElement(); $xmlWriter->endElement(); } elseif ($style instanceof Paragraph) { - // PhpOffice\PhpWord\Style\Paragraph + // Paragraph // style:style $xmlWriter->startElement('style:style'); $xmlWriter->writeAttribute('style:name', $styleName); @@ -183,7 +183,7 @@ class Styles extends WriterPart $xmlWriter->endElement(); } elseif ($style instanceof Table) { - // PhpOffice\PhpWord\Style\Table + // Table } } } diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index cff6ca46..e37e53b3 100755 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -36,7 +36,7 @@ class RTF extends Writer implements IWriter /** * Private unique PHPWord_Worksheet_BaseDrawing HashTable * - * @var \PhpOffice\PhpWord\HashTable + * @var HashTable */ private $drawingHashTable; @@ -63,7 +63,7 @@ class RTF extends Writer implements IWriter /** * Create new RTF writer - * @param \PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord */ public function __construct(PhpWord $phpWord = null) { @@ -78,7 +78,7 @@ class RTF extends Writer implements IWriter * Save PhpWord to file * * @param string $pFilename - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws Exception */ public function save($pFilename = null) { @@ -98,7 +98,7 @@ class RTF extends Writer implements IWriter /** * Get PHPWord_Worksheet_BaseDrawing HashTable * - * @return \PhpOffice\PhpWord\HashTable + * @return HashTable */ public function getDrawingHashTable() { @@ -181,7 +181,7 @@ class RTF extends Writer implements IWriter $styles = Style::getStyles(); if (count($styles) > 0) { foreach ($styles as $styleName => $style) { - // PhpOffice\PhpWord\Style\Font + // Font if ($style instanceof Font) { if (in_array($style->getName(), $arrFonts) == false) { $arrFonts[] = $style->getName(); diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index cb5cfc72..4c55006f 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -46,7 +46,7 @@ class Word2007 extends Writer implements IWriter /** * Create new Word2007 writer * - * @param PhpOffice\PhpWord\PhpWord + * @param PhpWord */ public function __construct(PhpWord $phpWord = null) { diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index f813e586..2a1e01f5 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -27,6 +27,8 @@ use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Cell; +use PhpOffice\PhpWord\Style\Table as TableStyle; +use PhpOffice\PhpWord\Style\Image as ImageStyle; /** * Word2007 base part writer @@ -197,7 +199,7 @@ class Base extends WriterPart * Write preserve text element * * @param XMLWriter $xmlWriter - * @param TextRun $textrun + * @param PreserveText $textrun */ protected function writePreserveText( XMLWriter $xmlWriter, @@ -364,7 +366,7 @@ class Base extends WriterPart // Table style $tblStyle = $table->getStyle(); $tblWidth = $table->getWidth(); - if ($tblStyle instanceof \PhpOffice\PhpWord\Style\Table) { + if ($tblStyle instanceof TableStyle) { $this->writeTableStyle($xmlWriter, $tblStyle, false); } else { if (!empty($tblStyle)) { @@ -523,16 +525,16 @@ class Base extends WriterPart } switch ($wrappingStyle) { - case \PhpOffice\PhpWord\Style\Image::WRAPPING_STYLE_BEHIND: + case ImageStyle::WRAPPING_STYLE_BEHIND: $imgStyle .= 'position:absolute;z-index:-251658752;'; break; - case \PhpOffice\PhpWord\Style\Image::WRAPPING_STYLE_SQUARE: + case ImageStyle::WRAPPING_STYLE_SQUARE: $imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; break; - case \PhpOffice\PhpWord\Style\Image::WRAPPING_STYLE_TIGHT: + case ImageStyle::WRAPPING_STYLE_TIGHT: $imgStyle .= 'position:absolute;z-index:251659264;mso-wrap-edited:f;mso-position-horizontal:absolute;mso-position-vertical:absolute'; break; - case \PhpOffice\PhpWord\Style\Image::WRAPPING_STYLE_INFRONT: + case ImageStyle::WRAPPING_STYLE_INFRONT: $imgStyle .= 'position:absolute;zz-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; break; } @@ -767,7 +769,7 @@ class Base extends WriterPart * Write paragraph style * * @param XMLWriter $xmlWriter - * @param \PhpOffice\PhpWord\Style\Paragraph $style + * @param Paragraph $style * @param bool $withoutPPR */ protected function writeParagraphStyle( @@ -867,10 +869,10 @@ class Base extends WriterPart } /** - * Write footnote reference element + * Write font style * * @param XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section $section + * @param Font $style */ protected function writeFontStyle(XMLWriter $xmlWriter, Font $style) { @@ -974,12 +976,12 @@ class Base extends WriterPart * Write table style * * @param XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Style\Table $style + * @param TableStyle $style * @param boolean $isFullStyle */ protected function writeTableStyle( XMLWriter $xmlWriter, - \PhpOffice\PhpWord\Style\Table $style, + TableStyle $style, $isFullStyle = true ) { $bgColor = $style->getBgColor(); @@ -1105,12 +1107,12 @@ class Base extends WriterPart * * @param XMLWriter $xmlWriter * @param string $type - * @param PhpOffice\PhpWord\Style\Table $style + * @param TableStyle $style */ protected function writeRowStyle( XMLWriter $xmlWriter, $type, - \PhpOffice\PhpWord\Style\Table $style + TableStyle $style ) { $brdSz = $style->getBorderSize(); $brdCol = $style->getBorderColor(); @@ -1174,7 +1176,7 @@ class Base extends WriterPart * Write footnote reference element * * @param XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Style\Cell $style + * @param Cell $style */ protected function writeCellStyle(XMLWriter $xmlWriter, Cell $style = null) { @@ -1321,10 +1323,9 @@ class Base extends WriterPart * * @param XMLWriter $xmlWriter * @param Paragraph|string $styleParagraph - * @param boolean $spIsObject * @param boolean $withoutPPR */ - private function writeInlineParagraphStyle( + protected function writeInlineParagraphStyle( XMLWriter $xmlWriter, $styleParagraph = null, $withoutPPR = false @@ -1350,9 +1351,8 @@ class Base extends WriterPart * * @param XMLWriter $xmlWriter * @param Font|string $styleFont - * @param boolean $sfIsObject */ - private function writeInlineFontStyle( + protected function writeInlineFontStyle( XMLWriter $xmlWriter, $styleFont = null ) { diff --git a/src/PhpWord/Writer/Word2007/ContentTypes.php b/src/PhpWord/Writer/Word2007/ContentTypes.php index 2e2f3def..65c75384 100755 --- a/src/PhpWord/Writer/Word2007/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/ContentTypes.php @@ -161,10 +161,10 @@ class ContentTypes extends WriterPart /** * Write Default XML element * - * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter XML Writer + * @param XMLWriter $xmlWriter XML Writer * @param string $pPartname Part name * @param string $pContentType Content type - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws Exception */ private function writeDefaultContentType(XMLWriter $xmlWriter = null, $pPartname = '', $pContentType = '') { @@ -182,10 +182,10 @@ class ContentTypes extends WriterPart /** * Write Override XML element * - * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param XMLWriter $xmlWriter * @param string $pPartname Part name * @param string $pContentType Content type - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws Exception */ private function writeOverrideContentType(XMLWriter $xmlWriter = null, $pPartname = '', $pContentType = '') { @@ -205,7 +205,7 @@ class ContentTypes extends WriterPart * * @param string $pFile Filename * @return string Mime Type - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws Exception */ private function getImageMimeType($pFile = '') { diff --git a/src/PhpWord/Writer/Word2007/DocProps.php b/src/PhpWord/Writer/Word2007/DocProps.php index 591dfc0e..92c2fab8 100644 --- a/src/PhpWord/Writer/Word2007/DocProps.php +++ b/src/PhpWord/Writer/Word2007/DocProps.php @@ -110,7 +110,7 @@ class DocProps extends WriterPart /** * Write docProps/core.xml * - * @param PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord */ public function writeDocPropsCore(PhpWord $phpWord = null) { diff --git a/src/PhpWord/Writer/Word2007/Document.php b/src/PhpWord/Writer/Word2007/Document.php index 05fd145a..03184bd1 100644 --- a/src/PhpWord/Writer/Word2007/Document.php +++ b/src/PhpWord/Writer/Word2007/Document.php @@ -36,7 +36,7 @@ class Document extends Base /** * Write word/document.xml * - * @param PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord */ public function writeDocument(PhpWord $phpWord = null) { @@ -118,8 +118,8 @@ class Document extends Base /** * Write begin section * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section $section + * @param XMLWriter $xmlWriter + * @param Section $section */ private function writeSection(XMLWriter $xmlWriter, Section $section) { @@ -133,8 +133,8 @@ class Document extends Base /** * Write end section * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section $section + * @param XMLWriter $xmlWriter + * @param Section $section */ private function writeEndSection(XMLWriter $xmlWriter, Section $section) { @@ -272,7 +272,7 @@ class Document extends Base /** * Write page break element * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param XMLWriter $xmlWriter */ private function writePageBreak(XMLWriter $xmlWriter) { @@ -288,7 +288,7 @@ class Document extends Base /** * Write TOC element * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param XMLWriter $xmlWriter */ private function writeTOC(XMLWriter $xmlWriter) { diff --git a/src/PhpWord/Writer/Word2007/Footer.php b/src/PhpWord/Writer/Word2007/Footer.php index 49fad838..4a3b97f6 100644 --- a/src/PhpWord/Writer/Word2007/Footer.php +++ b/src/PhpWord/Writer/Word2007/Footer.php @@ -15,6 +15,7 @@ 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\Shared\XMLWriter; /** @@ -25,9 +26,9 @@ class Footer extends Base /** * Write word/footnotes.xml * - * @param PhpOffice\PhpWord\Section\Footer $footer + * @param FooterElement $footer */ - public function writeFooter(\PhpOffice\PhpWord\Section\Footer $footer) + public function writeFooter(FooterElement $footer) { // Create XML writer $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/Word2007/Footnotes.php b/src/PhpWord/Writer/Word2007/Footnotes.php index 1d244b01..ce4aba60 100644 --- a/src/PhpWord/Writer/Word2007/Footnotes.php +++ b/src/PhpWord/Writer/Word2007/Footnotes.php @@ -77,26 +77,18 @@ class Footnotes extends Base /** * Write footnote content, overrides method in parent class * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section\Footnote $footnote + * @param XMLWriter $xmlWriter + * @param Footnote $footnote + * @param boolean $withoutP */ - protected function writeFootnote(XMLWriter $xmlWriter, Footnote $footnote) + protected function writeFootnote(XMLWriter $xmlWriter, Footnote $footnote, $withoutP = false) { $xmlWriter->startElement('w:footnote'); $xmlWriter->writeAttribute('w:id', $footnote->getReferenceId()); $xmlWriter->startElement('w:p'); // Paragraph style - $paragraphStyle = $footnote->getParagraphStyle(); - $spIsObject = ($paragraphStyle instanceof Paragraph) ? true : false; - if ($spIsObject) { - $this->writeParagraphStyle($xmlWriter, $paragraphStyle); - } elseif (!$spIsObject && !is_null($paragraphStyle)) { - $xmlWriter->startElement('w:pPr'); - $xmlWriter->startElement('w:pStyle'); - $xmlWriter->writeAttribute('w:val', $paragraphStyle); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - } + $styleParagraph = $footnote->getParagraphStyle(); + $this->writeInlineParagraphStyle($xmlWriter, $styleParagraph); // Reference symbol $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:rPr'); diff --git a/src/PhpWord/Writer/Word2007/Header.php b/src/PhpWord/Writer/Word2007/Header.php index 88400c50..03757693 100644 --- a/src/PhpWord/Writer/Word2007/Header.php +++ b/src/PhpWord/Writer/Word2007/Header.php @@ -15,6 +15,7 @@ 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\Shared\XMLWriter; /** @@ -25,9 +26,9 @@ class Header extends Base /** * Write word/headerx.xml * - * @param PhpOffice\PhpWord\Section\Header $header + * @param HeaderElement $header */ - public function writeHeader(\PhpOffice\PhpWord\Section\Header $header) + public function writeHeader(HeaderElement $header) { // Create XML writer $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/Word2007/Rels.php b/src/PhpWord/Writer/Word2007/Rels.php index e7e26856..3e41033f 100755 --- a/src/PhpWord/Writer/Word2007/Rels.php +++ b/src/PhpWord/Writer/Word2007/Rels.php @@ -21,7 +21,7 @@ class Rels extends Base /** * Write _rels/.rels * - * @param PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord */ public function writeRelationships(PhpWord $phpWord = null) { diff --git a/src/PhpWord/Writer/Word2007/Styles.php b/src/PhpWord/Writer/Word2007/Styles.php index ca0acfdb..c7b35faf 100644 --- a/src/PhpWord/Writer/Word2007/Styles.php +++ b/src/PhpWord/Writer/Word2007/Styles.php @@ -14,6 +14,7 @@ use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; +use PhpOffice\PhpWord\Style\Table; /** * Word2007 styles part writer @@ -23,7 +24,7 @@ class Styles extends Base /** * Write word/styles.xml * - * @param PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord */ public function writeStyles(PhpWord $phpWord = null) { @@ -121,7 +122,7 @@ class Styles extends Base $this->writeParagraphStyle($xmlWriter, $style); $xmlWriter->endElement(); - } elseif ($style instanceof \PhpOffice\PhpWord\Style\Table) { + } elseif ($style instanceof Table) { $xmlWriter->startElement('w:style'); $xmlWriter->writeAttribute('w:type', 'table'); $xmlWriter->writeAttribute('w:customStyle', '1'); @@ -151,7 +152,7 @@ class Styles extends Base /** * Write default font and other default styles * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param XMLWriter $xmlWriter * @param array $styles */ private function writeDefaultStyles(XMLWriter $xmlWriter, PhpWord $phpWord, $styles) diff --git a/src/PhpWord/Writer/Writer.php b/src/PhpWord/Writer/Writer.php index eeba55ca..bc749738 100644 --- a/src/PhpWord/Writer/Writer.php +++ b/src/PhpWord/Writer/Writer.php @@ -22,7 +22,7 @@ abstract class Writer implements IWriter /** * PHPWord object * - * @var PhpOffice\PhpWord\PhpWord + * @var PhpWord */ protected $phpWord = null; From f911f5ccbe26feef737b37cdbe52911e5078e9de Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 30 Mar 2014 22:53:47 +0700 Subject: [PATCH 4/5] Namespace alias --- src/PhpWord/TOC.php | 11 ++--- src/PhpWord/Writer/Word2007/Base.php | 62 +++++++++++----------------- 2 files changed, 31 insertions(+), 42 deletions(-) diff --git a/src/PhpWord/TOC.php b/src/PhpWord/TOC.php index e6a24234..bd1c3f38 100644 --- a/src/PhpWord/TOC.php +++ b/src/PhpWord/TOC.php @@ -10,6 +10,7 @@ namespace PhpOffice\PhpWord; use PhpOffice\PhpWord\Style\Font; +use PhpOffice\PhpWord\Style\TOC as TOCStyle; /** * Table of contents @@ -26,14 +27,14 @@ class TOC /** * TOC style * - * @var PhpOffice\PhpWord\Style\TOC + * @var TOCStyle */ private static $_styleTOC; /** * Font style * - * @var PhpOffice\PhpWord\Style\Font|array|string + * @var Font|array|string */ private static $_styleFont; @@ -60,7 +61,7 @@ class TOC */ public function __construct($styleFont = null, $styleTOC = null) { - self::$_styleTOC = new \PhpOffice\PhpWord\Style\TOC(); + self::$_styleTOC = new TOCStyle(); if (!is_null($styleTOC) && is_array($styleTOC)) { foreach ($styleTOC as $key => $value) { @@ -122,7 +123,7 @@ class TOC /** * Get TOC Style * - * @return \PhpOffice\PhpWord\Style\TOC + * @return TOCStyle */ public static function getStyleTOC() { @@ -132,7 +133,7 @@ class TOC /** * Get Font Style * - * @return \PhpOffice\PhpWord\Style\Font + * @return Font */ public static function getStyleFont() { diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index 2a1e01f5..3acf6c1d 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -50,9 +50,7 @@ class Base extends WriterPart $withoutP = false ) { $styleFont = $text->getFontStyle(); - $sfIsObject = ($styleFont instanceof Font) ? true : false; $styleParagraph = $text->getParagraphStyle(); - $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; $strText = htmlspecialchars($text->getText()); $strText = String::controlCharacterPHP2OOXML($strText); @@ -84,7 +82,6 @@ class Base extends WriterPart ) { $elements = $textrun->getElements(); $styleParagraph = $textrun->getParagraphStyle(); - $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; $xmlWriter->startElement('w:p'); $this->writeInlineParagraphStyle($xmlWriter, $styleParagraph); if (count($elements) > 0) { @@ -123,9 +120,7 @@ class Base extends WriterPart $linkName = $link->getLinkSrc(); } $styleFont = $link->getFontStyle(); - $sfIsObject = ($styleFont instanceof Font) ? true : false; $styleParagraph = $link->getParagraphStyle(); - $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; if (!$withoutP) { $xmlWriter->startElement('w:p'); @@ -206,9 +201,7 @@ class Base extends WriterPart PreserveText $textrun ) { $styleFont = $textrun->getFontStyle(); - $sfIsObject = ($styleFont instanceof Font) ? true : false; $styleParagraph = $textrun->getParagraphStyle(); - $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; $arrText = $textrun->getText(); if (!is_array($arrText)) { @@ -272,11 +265,11 @@ class Base extends WriterPart protected function writeTextBreak($xmlWriter, TextBreak $element = null) { $hasStyle = false; + $styleFont = null; + $styleParagraph = null; if (!is_null($element)) { $styleFont = $element->getFontStyle(); - $sfIsObject = ($styleFont instanceof Font) ? true : false; $styleParagraph = $element->getParagraphStyle(); - $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; $hasStyle = !is_null($styleFont) || !is_null($styleParagraph); } if ($hasStyle) { @@ -303,11 +296,9 @@ class Base extends WriterPart protected function writeListItem(XMLWriter $xmlWriter, ListItem $listItem) { $textObject = $listItem->getTextObject(); - $text = $textObject->getText(); $depth = $listItem->getDepth(); $listType = $listItem->getStyle()->getListType(); $styleParagraph = $textObject->getParagraphStyle(); - $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; $xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:pPr'); @@ -619,8 +610,6 @@ class Base extends WriterPart $shapeId = md5($rIdObject . '_' . $rIdImage); $objectId = $object->getObjectId(); $style = $object->getStyle(); - $width = $style->getWidth(); - $height = $style->getHeight(); $align = $style->getAlign(); $xmlWriter->startElement('w:p'); @@ -705,9 +694,7 @@ class Base extends WriterPart $text = htmlspecialchars($checkbox->getText()); $text = String::controlCharacterPHP2OOXML($text); $styleFont = $checkbox->getFontStyle(); - $sfIsObject = ($styleFont instanceof Font) ? true : false; $styleParagraph = $checkbox->getParagraphStyle(); - $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; if (!$withoutP) { $xmlWriter->startElement('w:p'); @@ -774,7 +761,7 @@ class Base extends WriterPart */ protected function writeParagraphStyle( XMLWriter $xmlWriter, - Paragraph $style = null, + Paragraph $style, $withoutPPR = false ) { @@ -1122,7 +1109,6 @@ class Base extends WriterPart $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; $xmlWriter->startElement('w:tblStylePr'); $xmlWriter->writeAttribute('w:type', $type); @@ -1178,7 +1164,7 @@ class Base extends WriterPart * @param XMLWriter $xmlWriter * @param Cell $style */ - protected function writeCellStyle(XMLWriter $xmlWriter, Cell $style = null) + protected function writeCellStyle(XMLWriter $xmlWriter, Cell $style) { $bgColor = $style->getBgColor(); $valign = $style->getVAlign(); @@ -1291,7 +1277,7 @@ class Base extends WriterPart * @param string $pTargetMode Relationship target mode */ protected function writeRelationship( - XMLWriter $xmlWriter = null, + XMLWriter $xmlWriter, $pId = 1, $pType = '', $pTarget = '', @@ -1330,18 +1316,19 @@ class Base extends WriterPart $styleParagraph = null, $withoutPPR = false ) { - $spIsObject = ($styleParagraph instanceof Paragraph) ? true : false; - if ($spIsObject) { + if ($styleParagraph instanceof Paragraph) { $this->writeParagraphStyle($xmlWriter, $styleParagraph, $withoutPPR); - } elseif (!$spIsObject && !is_null($styleParagraph)) { - if (!$withoutPPR) { - $xmlWriter->startElement('w:pPr'); - } - $xmlWriter->startElement('w:pStyle'); - $xmlWriter->writeAttribute('w:val', $styleParagraph); - $xmlWriter->endElement(); - if (!$withoutPPR) { + } else { + if (!is_null($styleParagraph)) { + if (!$withoutPPR) { + $xmlWriter->startElement('w:pPr'); + } + $xmlWriter->startElement('w:pStyle'); + $xmlWriter->writeAttribute('w:val', $styleParagraph); $xmlWriter->endElement(); + if (!$withoutPPR) { + $xmlWriter->endElement(); + } } } } @@ -1356,15 +1343,16 @@ class Base extends WriterPart XMLWriter $xmlWriter, $styleFont = null ) { - $sfIsObject = ($styleFont instanceof Font) ? true : false; - if ($sfIsObject) { + if ($styleFont instanceof Font) { $this->writeFontStyle($xmlWriter, $styleFont); - } elseif (!$sfIsObject && !is_null($styleFont)) { - $xmlWriter->startElement('w:rPr'); - $xmlWriter->startElement('w:rStyle'); - $xmlWriter->writeAttribute('w:val', $styleFont); - $xmlWriter->endElement(); - $xmlWriter->endElement(); + } else { + if (!is_null($styleFont)) { + $xmlWriter->startElement('w:rPr'); + $xmlWriter->startElement('w:rStyle'); + $xmlWriter->writeAttribute('w:val', $styleFont); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + } } } } From 99c25c3abcfcc16bc909d6694ec96031d8bbeddb Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 30 Mar 2014 23:04:48 +0700 Subject: [PATCH 5/5] Namespace updates --- src/PhpWord/Section/Footer/PreserveText.php | 12 ++++++------ src/PhpWord/Style/Font.php | 4 ++-- src/PhpWord/Style/Row.php | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/PhpWord/Section/Footer/PreserveText.php b/src/PhpWord/Section/Footer/PreserveText.php index 579ca271..fbeb16be 100644 --- a/src/PhpWord/Section/Footer/PreserveText.php +++ b/src/PhpWord/Section/Footer/PreserveText.php @@ -27,16 +27,16 @@ class PreserveText /** * Text style * - * @var \PhpOffice\PhpWord\Style\Font + * @var string|Font */ private $_styleFont; /** * Paragraph style * - * @var \PhpOffice\PhpWord\Style\Paragraph + * @var Paragraph */ - private $_styleParagraph; + private string|$_styleParagraph; /** @@ -45,7 +45,7 @@ class PreserveText * @param string $text * @param mixed $styleFont * @param mixed $styleParagraph - * @return PHPWord_Section_Footer_PreserveText + * @return $this */ public function __construct($text = null, $styleFont = null, $styleParagraph = null) { @@ -88,7 +88,7 @@ class PreserveText /** * Get Text style * - * @return \PhpOffice\PhpWord\Style\Font + * @return string|Font */ public function getFontStyle() { @@ -98,7 +98,7 @@ class PreserveText /** * Get Paragraph style * - * @return \PhpOffice\PhpWord\Style\Paragraph + * @return string|Paragraph */ public function getParagraphStyle() { diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index 3d235343..d1f5353a 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -479,8 +479,8 @@ class Font /** * Set background color * - * @param string $pValue - * @return PHPWord_Style_Font + * @param string $pValue + * @return $this */ public function setBgColor($pValue = null) { diff --git a/src/PhpWord/Style/Row.php b/src/PhpWord/Style/Row.php index 74b4d966..2b714024 100644 --- a/src/PhpWord/Style/Row.php +++ b/src/PhpWord/Style/Row.php @@ -57,7 +57,7 @@ class Row * Set tblHeader * * @param boolean $pValue - * @return PHPWord_Style_Row + * @return $this */ public function setTblHeader($pValue = false) { @@ -82,7 +82,7 @@ class Row * Set cantSplit * * @param boolean $pValue - * @return PHPWord_Style_Row + * @return $this */ public function setCantSplit($pValue = false) { @@ -107,7 +107,7 @@ class Row * Set exactHeight * * @param bool $pValue - * @return PHPWord_Style_Row + * @return $this */ public function setExactHeight($pValue = false) {