From 258c9c6b9e24c872947a91ce769af5fa478d69c3 Mon Sep 17 00:00:00 2001 From: ozilion Date: Thu, 20 Mar 2014 16:27:19 +0200 Subject: [PATCH 01/22] Added addCheckBox() function Created new file named CheckBox.php under Section folder. Added addCheckBox() function to Section.php and \Table\Cell.php files and _writeCheckbox(..) function to Base.php file. --- Classes/PHPWord/Section.php | 20 ++ Classes/PHPWord/Section/CheckBox.php | 198 +++++++++++++++++++ Classes/PHPWord/Section/Table/Cell.php | 20 ++ Classes/PHPWord/Writer/Word2007/Base.php | 114 +++++++++++ Classes/PHPWord/Writer/Word2007/Document.php | 12 +- 5 files changed, 359 insertions(+), 5 deletions(-) create mode 100644 Classes/PHPWord/Section/CheckBox.php diff --git a/Classes/PHPWord/Section.php b/Classes/PHPWord/Section.php index 07a17a31..f20cfd95 100755 --- a/Classes/PHPWord/Section.php +++ b/Classes/PHPWord/Section.php @@ -125,6 +125,26 @@ class PHPWord_Section return $text; } + /** + * Add a CheckBox Element + * + * @param string $text + * @param mixed $style + * @return PHPWord_Section_CheckBox + */ + public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null) + { + if (!PHPWord_Shared_String::IsUTF8($name)) { + $name = utf8_encode($name); + } + if (!PHPWord_Shared_String::IsUTF8($text)) { + $text = utf8_encode($text); + } + $text = new PHPWord_Section_CheckBox($name, $text, $styleFont, $styleParagraph); + $this->_elementCollection[] = $text; + return $text; + } + /** * Add a Link Element * diff --git a/Classes/PHPWord/Section/CheckBox.php b/Classes/PHPWord/Section/CheckBox.php new file mode 100644 index 00000000..af02356a --- /dev/null +++ b/Classes/PHPWord/Section/CheckBox.php @@ -0,0 +1,198 @@ +setName($name); + $this->setText($text); + $paragraphStyle = $this->setParagraphStyle($paragraphStyle); + $this->setFontStyle($fontStyle, $paragraphStyle); + } + + /** + * Set Text style + * + * @param null|array|\PHPWord_Style_Font $style + * @param null|array|\PHPWord_Style_Paragraph $paragraphStyle + * @return PHPWord_Style_Font + */ + public function setFontStyle($style = null, $paragraphStyle = null) + { + if ($style instanceof PHPWord_Style_Font) { + $this->fontStyle = $style; + $this->setParagraphStyle($paragraphStyle); + } elseif (is_array($style)) { + $this->fontStyle = new PHPWord_Style_Font('text', $paragraphStyle); + $this->fontStyle->setArrayStyle($style); + } elseif (null === $style) { + $this->fontStyle = new PHPWord_Style_Font('text', $paragraphStyle); + } else { + $this->fontStyle = $style; + $this->setParagraphStyle($paragraphStyle); + } + return $this->fontStyle; + } + + /** + * Get Text style + * + * @return PHPWord_Style_Font + */ + public function getFontStyle() + { + return $this->fontStyle; + } + + /** + * Set Paragraph style + * + * @param null|array|\PHPWord_Style_Paragraph $style + * @return null|\PHPWord_Style_Paragraph + */ + public function setParagraphStyle($style = null) + { + if (is_array($style)) { + $this->paragraphStyle = new PHPWord_Style_Paragraph; + $this->paragraphStyle->setArrayStyle($style); + } elseif ($style instanceof PHPWord_Style_Paragraph) { + $this->paragraphStyle = $style; + } elseif (null === $style) { + $this->paragraphStyle = new PHPWord_Style_Paragraph; + } else { + $this->paragraphStyle = $style; + } + return $this->paragraphStyle; + } + + /** + * Get Paragraph style + * + * @return PHPWord_Style_Paragraph + */ + public function getParagraphStyle() + { + return $this->paragraphStyle; + } + + /** + * @param string $name + * @return $this + */ + public function setName($text) + { + $this->name = $text; + return $this; + } + + /** + * @param string $text + * @return $this + */ + public function setText($text) + { + $this->text = $text; + return $this; + } + + /** + * Get name content + * + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * Get Text content + * + * @return string + */ + public function getText() + { + return $this->text; + } + + /** + * Get all Elements + * + * @return array + */ + public function getElements() + { + return $this->_elementCollection; + } + +} diff --git a/Classes/PHPWord/Section/Table/Cell.php b/Classes/PHPWord/Section/Table/Cell.php index f8d6d340..1a0d1c6a 100755 --- a/Classes/PHPWord/Section/Table/Cell.php +++ b/Classes/PHPWord/Section/Table/Cell.php @@ -113,6 +113,26 @@ class PHPWord_Section_Table_Cell return $text; } + /** + * Add a CheckBox Element + * + * @param string $text + * @param mixed $style + * @return PHPWord_Section_CheckBox + */ + public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null) + { + if (!PHPWord_Shared_String::IsUTF8($name)) { + $name = utf8_encode($name); + } + if (!PHPWord_Shared_String::IsUTF8($text)) { + $text = utf8_encode($text); + } + $text = new PHPWord_Section_CheckBox($name, $text, $styleFont, $styleParagraph); + $this->_elementCollection[] = $text; + return $text; + } + /** * Add a Link Element * diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index e9249120..9ae5602b 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -289,6 +289,118 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart } } + /** + * Write CheckBox + */ + protected function _writeCheckbox(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_CheckBox $Checkbox, $withoutP = false, $checkState = false) + { + $cbCount = 1; + $_elements = $Checkbox->getElements(); + if (count($_elements) > 1) { + $cbCount = $cbCount + 1; + } + + $styleFont = $Checkbox->getFontStyle(); + $SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false; + + if (!$withoutP) { + $objWriter->startElement('w:p'); + + $styleParagraph = $Checkbox->getParagraphStyle(); + $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false; + + if ($SpIsObject) { + $this->_writeParagraphStyle($objWriter, $styleParagraph); + } elseif (!$SpIsObject && !is_null($styleParagraph)) { + $objWriter->startElement('w:pPr'); + $objWriter->startElement('w:pStyle'); + $objWriter->writeAttribute('w:val', $styleParagraph); + $objWriter->endElement(); + $objWriter->endElement(); + } + } + + $strName = htmlspecialchars($Checkbox->getName()); + $strName = PHPWord_Shared_String::ControlCharacterPHP2OOXML($strName); + + $strText = htmlspecialchars($Checkbox->getText()); + $strText = PHPWord_Shared_String::ControlCharacterPHP2OOXML($strText); + + $objWriter->startElement('w:r'); + $objWriter->startElement('w:fldChar'); + $objWriter->writeAttribute('w:fldCharType', 'begin'); + $objWriter->startElement('w:ffData'); + $objWriter->startElement('w:name'); + $objWriter->writeAttribute('w:val', $strName); + $objWriter->endElement(); //w:name + + $objWriter->writeAttribute('w:enabled', ''); + $objWriter->startElement('w:calcOnExit'); + $objWriter->writeAttribute('w:val', '0'); + $objWriter->endElement(); //w:calcOnExit + + $objWriter->startElement('w:checkBox'); + $objWriter->writeAttribute('w:sizeAuto', ''); + $objWriter->startElement('w:default'); + if($checkState){ + $objWriter->writeAttribute('w:val', '1'); + }else{ + $objWriter->writeAttribute('w:val', '0'); + } + $objWriter->endElement(); //w:default + $objWriter->endElement(); //w:checkbox + $objWriter->endElement(); // w:ffData + $objWriter->endElement(); // w:fldChar + $objWriter->endElement(); // w:r + + $objWriter->startElement('w:bookmarkStart'); + $objWriter->writeAttribute('w:name', $strName); + $objWriter->writeAttribute('w:id', $cbCount); + $objWriter->startElement('w:r'); + $objWriter->startElement('w:instrText'); + $objWriter->writeAttribute('xml:space', 'preserve'); // needed because of drawing spaces before and after text + $objWriter->writeRaw(' FORMCHECKBOX '); + $objWriter->endElement();// w:instrText + $objWriter->endElement(); // w:r + $objWriter->startElement('w:r'); + $objWriter->startElement('w:fldChar'); + $objWriter->writeAttribute('w:fldCharType', 'seperate'); + $objWriter->endElement();// w:fldChar + $objWriter->endElement(); // w:r + $objWriter->startElement('w:r'); + $objWriter->startElement('w:fldChar'); + $objWriter->writeAttribute('w:fldCharType', 'end'); + $objWriter->endElement();// w:fldChar + $objWriter->endElement(); // w:r + $objWriter->endElement(); // w:bookmarkStart + $objWriter->startElement('w:bookmarkEnd'); + $objWriter->writeAttribute('w:id', $cbCount); + $objWriter->endElement();// w:bookmarkEnd + + $objWriter->startElement('w:r'); + + if ($SfIsObject) { + $this->_writeTextStyle($objWriter, $styleFont); + } elseif (!$SfIsObject && !is_null($styleFont)) { + $objWriter->startElement('w:rPr'); + $objWriter->startElement('w:rStyle'); + $objWriter->writeAttribute('w:val', $styleFont); + $objWriter->endElement(); + $objWriter->endElement(); + } + + $objWriter->startElement('w:t'); + $objWriter->writeAttribute('xml:space', 'preserve'); // needed because of drawing spaces before and after text + $objWriter->writeRaw($strText); + $objWriter->endElement(); + + $objWriter->endElement(); // w:r + + if (!$withoutP) { + $objWriter->endElement(); // w:p + } + } + /** * Write preserve text */ @@ -610,6 +722,8 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $this->_writeTextRun($objWriter, $element); } elseif ($element instanceof PHPWord_Section_Link) { $this->_writeLink($objWriter, $element); + } elseif ($element instanceof PHPWord_Section_CheckBox) { + $this->_writeCheckbox($objWriter, $element); } elseif ($element instanceof PHPWord_Section_TextBreak) { $this->_writeTextBreak($objWriter, $element); } elseif ($element instanceof PHPWord_Section_ListItem) { diff --git a/Classes/PHPWord/Writer/Word2007/Document.php b/Classes/PHPWord/Writer/Word2007/Document.php index abdd92cc..b6764b40 100755 --- a/Classes/PHPWord/Writer/Word2007/Document.php +++ b/Classes/PHPWord/Writer/Word2007/Document.php @@ -76,6 +76,8 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base $this->_writeTextRun($objWriter, $element); } elseif ($element instanceof PHPWord_Section_Link) { $this->_writeLink($objWriter, $element); + } elseif ($element instanceof PHPWord_Section_CheckBox) { + $this->_writeCheckbox($objWriter, $element); } elseif ($element instanceof PHPWord_Section_Title) { $this->_writeTitle($objWriter, $element); } elseif ($element instanceof PHPWord_Section_TextBreak) { @@ -259,11 +261,11 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base private function _writePageBreak(PHPWord_Shared_XMLWriter $objWriter = null) { $objWriter->startElement('w:p'); - $objWriter->startElement('w:r'); - $objWriter->startElement('w:br'); - $objWriter->writeAttribute('w:type', 'page'); - $objWriter->endElement(); - $objWriter->endElement(); + $objWriter->startElement('w:r'); + $objWriter->startElement('w:br'); + $objWriter->writeAttribute('w:type', 'page'); + $objWriter->endElement(); + $objWriter->endElement(); $objWriter->endElement(); } From 27840ab7103f4bd46190891c5d80b9f5a2393077 Mon Sep 17 00:00:00 2001 From: Julien Carignan Date: Mon, 24 Mar 2014 13:43:20 -0400 Subject: [PATCH 02/22] Font-style addition: bgColor Signed-off-by: Julien Carignan --- Classes/PHPWord/Style/Font.php | 34 ++++++++++++++++++++++++ Classes/PHPWord/Writer/Word2007/Base.php | 10 +++++++ 2 files changed, 44 insertions(+) diff --git a/Classes/PHPWord/Style/Font.php b/Classes/PHPWord/Style/Font.php index 2aa6cbc6..66d82832 100755 --- a/Classes/PHPWord/Style/Font.php +++ b/Classes/PHPWord/Style/Font.php @@ -149,6 +149,18 @@ class PHPWord_Style_Font * @var string */ private $_fgColor = null; + + /** + * Background color + * + * @var string + */ + private $_bgColor = null; + /** + * Text line height + * + * @var int + */ /** * Text line height @@ -468,6 +480,28 @@ class PHPWord_Style_Font $this->_fgColor = $pValue; return $this; } + + /** + * Get background color + * + * @return string + */ + public function getBgColor() + { + return $this->_bgColor; + } + + /** + * Set background color + * + * @param string $pValue + * @return PHPWord_Style_Font + */ + public function setBgColor($pValue = null) + { + $this->_bgColor = $pValue; + return $this; + } /** * Get style type diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index d829bf92..0102979d 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -398,6 +398,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $color = $style->getColor(); $size = $style->getSize(); $fgColor = $style->getFgColor(); + $bgColor = $style->getBgColor(); $strikethrough = $style->getStrikethrough(); $underline = $style->getUnderline(); $superscript = $style->getSuperScript(); @@ -467,6 +468,15 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->writeAttribute('w:val', $fgColor); $objWriter->endElement(); } + + // Background-Color + if (!is_null($bgColor)) { + $objWriter->startElement('w:shd'); + $objWriter->writeAttribute('w:val', "clear"); + $objWriter->writeAttribute('w:color', "auto"); + $objWriter->writeAttribute('w:fill', $bgColor); + $objWriter->endElement(); + } // Superscript/subscript if ($superscript || $subscript) { From e990c77b474aa39bdf6e883a9065d4794e6a2379 Mon Sep 17 00:00:00 2001 From: Julien Carignan Date: Mon, 24 Mar 2014 13:50:37 -0400 Subject: [PATCH 03/22] Added height rules for table row - mostly for removing the added space after a table - Code mostly come from this discussion: https://phpword.codeplex.com/discussions/440933 - Usage: $table->addRow(5000, $rowStyle, "exact"); Signed-off-by: Julien Carignan --- Classes/PHPWord/Section/Table.php | 4 ++-- Classes/PHPWord/Section/Table/Row.php | 22 ++++++++++++++++++++-- Classes/PHPWord/Writer/Word2007/Base.php | 6 ++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Classes/PHPWord/Section/Table.php b/Classes/PHPWord/Section/Table.php index 80126cba..7efdf0b8 100755 --- a/Classes/PHPWord/Section/Table.php +++ b/Classes/PHPWord/Section/Table.php @@ -101,9 +101,9 @@ class PHPWord_Section_Table * @param int $height * @param mixed $style */ - public function addRow($height = null, $style = null) + public function addRow($height = null, $style = null, $hRules = null) { - $row = new PHPWord_Section_Table_Row($this->_insideOf, $this->_pCount, $height, $style); + $row = new PHPWord_Section_Table_Row($this->_insideOf, $this->_pCount, $height, $style, $hRules); $this->_rows[] = $row; return $row; } diff --git a/Classes/PHPWord/Section/Table/Row.php b/Classes/PHPWord/Section/Table/Row.php index dd8ea65c..5b239c89 100644 --- a/Classes/PHPWord/Section/Table/Row.php +++ b/Classes/PHPWord/Section/Table/Row.php @@ -37,7 +37,14 @@ class PHPWord_Section_Table_Row * @var int */ private $_height = null; - + + /** + * Row heightRules + * + * @var array + */ + private $_heightRules = array(); + /** * Row style * @@ -75,11 +82,12 @@ class PHPWord_Section_Table_Row * @param int $height * @param mixed $style */ - public function __construct($insideOf, $pCount, $height = null, $style = null) + public function __construct($insideOf, $pCount, $height = null, $style = null, $hRules = null) { $this->_insideOf = $insideOf; $this->_pCount = $pCount; $this->_height = $height; + $this->_heightRules = $hRules; $this->_style = new PHPWord_Style_Row(); if (!is_null($style)) { @@ -138,4 +146,14 @@ class PHPWord_Section_Table_Row { return $this->_height; } + + /** + * Get all row height rules + * + * @return array + */ + public function getHeightRules() + { + return $this->_heightRules; + } } diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index 0102979d..57bb7266 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -570,6 +570,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart for ($i = 0; $i < $_cRows; $i++) { $row = $_rows[$i]; $height = $row->getHeight(); + $heightRules = $row->getHeightRules(); $rowStyle = $row->getStyle(); $tblHeader = $rowStyle->getTblHeader(); $cantSplit = $rowStyle->getCantSplit(); @@ -580,6 +581,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->startElement('w:trPr'); if (!is_null($height)) { $objWriter->startElement('w:trHeight'); + if(!is_null($heightRules)) { + $objWriter->startAttribute('w:hRule'); + $objWriter->text($heightRules); + $objWriter->endAttribute(); + } $objWriter->writeAttribute('w:val', $height); $objWriter->endElement(); } From e2cdf434ba315009b3eb5f32509477020d814209 Mon Sep 17 00:00:00 2001 From: Julien Carignan Date: Mon, 24 Mar 2014 13:55:01 -0400 Subject: [PATCH 04/22] "php://output" content-length + tmpfile location - added content-length header to know the total file size during the download - Tmp file should go into sys_get_temp_dir() instead of "./" Signed-off-by: Julien Carignan --- Classes/PHPWord/Writer/Word2007.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Classes/PHPWord/Writer/Word2007.php b/Classes/PHPWord/Writer/Word2007.php index 16d66c66..2589e9ef 100755 --- a/Classes/PHPWord/Writer/Word2007.php +++ b/Classes/PHPWord/Writer/Word2007.php @@ -113,7 +113,7 @@ class PHPWord_Writer_Word2007 implements PHPWord_Writer_IWriter // If $pFilename is php://output or php://stdout, make it a temporary file... $originalFilename = $pFilename; if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { - $pFilename = @tempnam('./', 'phppttmp'); + $pFilename = @tempnam(sys_get_temp_dir(), 'phpword_');// temp files should go to system temp directory (if a user cancels a download, the file stays) if ($pFilename == '') { $pFilename = $originalFilename; } @@ -236,6 +236,7 @@ class PHPWord_Writer_Word2007 implements PHPWord_Writer_IWriter // If a temporary file was used, copy it to the correct file stream if ($originalFilename != $pFilename) { + header('Content-Length: '.filesize($pFilename));// if php://output, we want to know the total file size when downloading if (copy($pFilename, $originalFilename) === false) { throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename."); } From 6796883d43f5cecd43844204926ab048bb4be6ef Mon Sep 17 00:00:00 2001 From: Julien Carignan Date: Mon, 24 Mar 2014 14:03:02 -0400 Subject: [PATCH 05/22] added samples for bgColor and tableRowHeightRules Signed-off-by: Julien Carignan --- samples/Sample_20_BGColor.php | 30 ++++++++++++++++++++ samples/Sample_21_TableRowRules.php | 44 +++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 samples/Sample_20_BGColor.php create mode 100644 samples/Sample_21_TableRowRules.php diff --git a/samples/Sample_20_BGColor.php b/samples/Sample_20_BGColor.php new file mode 100644 index 00000000..1122ba3d --- /dev/null +++ b/samples/Sample_20_BGColor.php @@ -0,0 +1,30 @@ +'); +require_once '../Classes/PHPWord.php'; + +// New Word Document +echo date('H:i:s') , ' Create new PHPWord object' , EOL; +$PHPWord = new PHPWord(); +$section = $PHPWord->createSection(); + +$section->addText("This is some text highlighted using fgColor (limited to 15 colors) ", array("fgColor" => PHPWord_Style_Font::FGCOLOR_YELLOW)); +$section->addText("This one uses bgColor and is using hex value (0xfbbb10)", array("bgColor" => "fbbb10")); +$section->addText("Compatible with font colors", array("color"=>"0000ff", "bgColor" => "fbbb10")); + + +// Save file +$name = basename(__FILE__, '.php'); +$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf'); +foreach ($writers as $writer => $extension) { + echo date('H:i:s'), " Write to {$writer} format", EOL; + $objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer); + $objWriter->save("{$name}.{$extension}"); + rename("{$name}.{$extension}", "results/{$name}.{$extension}"); +} + + +// Done +echo date('H:i:s'), " Done writing file(s)", EOL; +echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL; diff --git a/samples/Sample_21_TableRowRules.php b/samples/Sample_21_TableRowRules.php new file mode 100644 index 00000000..83d03e0c --- /dev/null +++ b/samples/Sample_21_TableRowRules.php @@ -0,0 +1,44 @@ +'); +require_once '../Classes/PHPWord.php'; + +// New Word Document +echo date('H:i:s') , ' Create new PHPWord object' , EOL; +$PHPWord = new PHPWord(); +$section = $PHPWord->createSection(); + +$section->addText("By default, a table row adds a textbreak after its content (notice the red border), even if the row height is <= height of the content"); + +$table1 = $section->addTable(array("cellMargin"=> 0, "cellMarginRight"=> 0, "cellMarginBottom"=> 0, "cellMarginLeft"=> 0)); +$table1->addRow(3750); +$cell1 = $table1->addCell(null, array("valign" => "top", "borderSize" => 30, "borderColor" => "ff0000")); +$cell1->addImage("./resources/_earth.jpg", array("width" => 250, "height" => 250, "align" => "center")); + +$section->addTextBreak(); +$section->addText("But if we set the row rule \"exact\", we get rid of the textbreak!"); + +$table2 = $section->addTable(array("cellMargin"=> 0, "cellMarginRight"=> 0, "cellMarginBottom"=> 0, "cellMarginLeft"=> 0)); +$table2->addRow(3750, null, "exact"); +$cell2 = $table2->addCell(null, array("valign" => "top", "borderSize" => 30, "borderColor" => "00ff00")); +$cell2->addImage("./resources/_earth.jpg", array("width" => 250, "height" => 250, "align" => "center")); + +$section->addTextBreak(); +$section->addText("In this example, image is 250px height. Rows are calculated in twips, and 1px = 15twips."); +$section->addText("So: $"."table2->addRow(3750, null, 'exact');"); + +// Save file +$name = basename(__FILE__, '.php'); +$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf'); +foreach ($writers as $writer => $extension) { + echo date('H:i:s'), " Write to {$writer} format", EOL; + $objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer); + $objWriter->save("{$name}.{$extension}"); + rename("{$name}.{$extension}", "results/{$name}.{$extension}"); +} + + +// Done +echo date('H:i:s'), " Done writing file(s)", EOL; +echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL; From ab6503eb0dcd7305c5e9895f095672440b2d39ef Mon Sep 17 00:00:00 2001 From: ozilion Date: Thu, 27 Mar 2014 10:17:07 +0200 Subject: [PATCH 06/22] Test folder updated Test folder updated for new file CheckBoxText --- Classes/PHPWord/Section.php | 1 + Classes/PHPWord/Section/CheckBox.php | 5 +-- Classes/PHPWord/Section/Table/Cell.php | 1 + Classes/PHPWord/Section/Text.php | 2 +- Tests/PHPWord/AutoloaderTest.php | 1 + Tests/PHPWord/Section/CheckBoxTest.php | 42 ++++++++++++++++++++++ Tests/PHPWord/Section/Table/CellTest.php | 9 +++++ Tests/PHPWord/SectionTest.php | 2 ++ Tests/PHPWord/Writer/Word2007/BaseTest.php | 20 +++++++++++ 9 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 Tests/PHPWord/Section/CheckBoxTest.php diff --git a/Classes/PHPWord/Section.php b/Classes/PHPWord/Section.php index f20cfd95..e65267df 100755 --- a/Classes/PHPWord/Section.php +++ b/Classes/PHPWord/Section.php @@ -128,6 +128,7 @@ class PHPWord_Section /** * Add a CheckBox Element * + * @param string $name * @param string $text * @param mixed $style * @return PHPWord_Section_CheckBox diff --git a/Classes/PHPWord/Section/CheckBox.php b/Classes/PHPWord/Section/CheckBox.php index af02356a..03f8228a 100644 --- a/Classes/PHPWord/Section/CheckBox.php +++ b/Classes/PHPWord/Section/CheckBox.php @@ -68,6 +68,7 @@ class PHPWord_Section_CheckBox /** * Create a new Text Element * + * @param string $name * @param string $text * @param null|array|\PHPWord_Style_Font $fontStyle * @param null|array|\PHPWord_Style_Paragraph $paragraphStyle @@ -149,9 +150,9 @@ class PHPWord_Section_CheckBox * @param string $name * @return $this */ - public function setName($text) + public function setName($name) { - $this->name = $text; + $this->name = $name; return $this; } diff --git a/Classes/PHPWord/Section/Table/Cell.php b/Classes/PHPWord/Section/Table/Cell.php index 1a0d1c6a..e18dd829 100755 --- a/Classes/PHPWord/Section/Table/Cell.php +++ b/Classes/PHPWord/Section/Table/Cell.php @@ -116,6 +116,7 @@ class PHPWord_Section_Table_Cell /** * Add a CheckBox Element * + * @param string $name * @param string $text * @param mixed $style * @return PHPWord_Section_CheckBox diff --git a/Classes/PHPWord/Section/Text.php b/Classes/PHPWord/Section/Text.php index 8631b66e..2a96a2d9 100755 --- a/Classes/PHPWord/Section/Text.php +++ b/Classes/PHPWord/Section/Text.php @@ -139,7 +139,7 @@ class PHPWord_Section_Text $this->text = $text; return $this; } - + /** * Get Text content * diff --git a/Tests/PHPWord/AutoloaderTest.php b/Tests/PHPWord/AutoloaderTest.php index 03e16db0..b1899770 100644 --- a/Tests/PHPWord/AutoloaderTest.php +++ b/Tests/PHPWord/AutoloaderTest.php @@ -1,6 +1,7 @@ assertInstanceOf('PHPWord_Section_CheckBox', $oCheckBox); + $this->assertEquals(null, $oCheckBox->getText()); + $this->assertInstanceOf('PHPWord_Style_Font', $oCheckBox->getFontStyle()); + $this->assertInstanceOf('PHPWord_Style_Paragraph', $oCheckBox->getParagraphStyle()); + } + + public function testCheckBox() + { + $oCheckBox = new PHPWord_Section_CheckBox('CheckBox'); + + $this->assertEquals($oCheckBox->getText(), 'CheckBox'); + } + + public function testFont() + { + $oCheckBox = new PHPWord_Section_CheckBox('CheckBox', 'fontStyle'); + $this->assertEquals($oCheckBox->getFontStyle(), 'fontStyle'); + + $oCheckBox->setFontStyle(array('bold' => true, 'italic' => true, 'size' => 16)); + $this->assertInstanceOf('PHPWord_Style_Font', $oCheckBox->getFontStyle()); + } + + public function testParagraph() + { + $oCheckBox = new PHPWord_Section_CheckBox('CheckBox', 'fontStyle', 'paragraphStyle'); + $this->assertEquals($oCheckBox->getParagraphStyle(), 'paragraphStyle'); + + $oCheckBox->setParagraphStyle(array('align' => 'center', 'spaceAfter' => 100)); + $this->assertInstanceOf('PHPWord_Style_Paragraph', $oCheckBox->getParagraphStyle()); + } +} diff --git a/Tests/PHPWord/Section/Table/CellTest.php b/Tests/PHPWord/Section/Table/CellTest.php index 30a7fcdb..be2878f1 100644 --- a/Tests/PHPWord/Section/Table/CellTest.php +++ b/Tests/PHPWord/Section/Table/CellTest.php @@ -40,6 +40,15 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PHPWord_Section_Text', $element); } + public function testaddCheckBox() + { + $oCell = new PHPWord_Section_Table_Cell('section', 1); + $element = $oCell->addCheckBox('check1', 'text'); + + $this->assertCount(1, $oCell->getElements()); + $this->assertInstanceOf('PHPWord_Section_CheckBox', $element); + } + public function testAddTextNotUTF8() { $oCell = new PHPWord_Section_Table_Cell('section', 1); diff --git a/Tests/PHPWord/SectionTest.php b/Tests/PHPWord/SectionTest.php index ac540d06..1bcd108c 100644 --- a/Tests/PHPWord/SectionTest.php +++ b/Tests/PHPWord/SectionTest.php @@ -61,6 +61,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase /** * @covers PHPWord_Section::addText + * @covers PHPWord_Section::addCheckBox * @covers PHPWord_Section::addLink * @covers PHPWord_Section::addTextBreak * @covers PHPWord_Section::addPageBreak @@ -88,6 +89,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase $section = new PHPWord_Section(0); $section->addText(utf8_decode('ä')); + $section->addCheckBox('check1', utf8_decode('ä')); $section->addLink(utf8_decode('http://äää.com'), utf8_decode('ä')); $section->addTextBreak(); $section->addPageBreak(); diff --git a/Tests/PHPWord/Writer/Word2007/BaseTest.php b/Tests/PHPWord/Writer/Word2007/BaseTest.php index fde6e30b..1ede88f4 100644 --- a/Tests/PHPWord/Writer/Word2007/BaseTest.php +++ b/Tests/PHPWord/Writer/Word2007/BaseTest.php @@ -43,6 +43,26 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->assertEquals($pStyle, $doc->getElementAttribute($element, 'w:val')); } + /** + * covers ::_writeCheckbox + */ + public function testWriteCheckbox() + { + $rStyle = 'rStyle'; + $pStyle = 'pStyle'; + + $PHPWord = new PHPWord(); + $PHPWord->addFontStyle($rStyle, array('bold' => true)); + $PHPWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120)); + $section = $PHPWord->createSection(); + $section->addCheckbox('Check1', 'Test', $rStyle, $pStyle); + $doc = TestHelperDOCX::getDocument($PHPWord); + + $element = $doc->getElement('/w:document/w:body/w:p/w:checkbox/w:r/w:t'); + + $this->assertEquals($expected, $element->nodeValue); + } + /** * covers ::_writeTextRun */ From 8363ee27d1461177dd625974f43feb1c160eb6ba Mon Sep 17 00:00:00 2001 From: Julien Carignan Date: Fri, 28 Mar 2014 13:26:36 -0400 Subject: [PATCH 07/22] table row height rule moved to rowStyle (ivanlanin) new usage: $table->addRow($height, array("heightRule"=>"exact")); Signed-off-by: Julien Carignan --- Classes/PHPWord/Section/Table.php | 4 +-- Classes/PHPWord/Section/Table/Row.php | 20 +-------------- Classes/PHPWord/Style/Row.php | 32 ++++++++++++++++++++++++ Classes/PHPWord/Writer/Word2007/Base.php | 8 +++--- samples/Sample_21_TableRowRules.php | 11 +++++--- 5 files changed, 46 insertions(+), 29 deletions(-) diff --git a/Classes/PHPWord/Section/Table.php b/Classes/PHPWord/Section/Table.php index 7efdf0b8..80126cba 100755 --- a/Classes/PHPWord/Section/Table.php +++ b/Classes/PHPWord/Section/Table.php @@ -101,9 +101,9 @@ class PHPWord_Section_Table * @param int $height * @param mixed $style */ - public function addRow($height = null, $style = null, $hRules = null) + public function addRow($height = null, $style = null) { - $row = new PHPWord_Section_Table_Row($this->_insideOf, $this->_pCount, $height, $style, $hRules); + $row = new PHPWord_Section_Table_Row($this->_insideOf, $this->_pCount, $height, $style); $this->_rows[] = $row; return $row; } diff --git a/Classes/PHPWord/Section/Table/Row.php b/Classes/PHPWord/Section/Table/Row.php index 5b239c89..7ca99823 100644 --- a/Classes/PHPWord/Section/Table/Row.php +++ b/Classes/PHPWord/Section/Table/Row.php @@ -38,13 +38,6 @@ class PHPWord_Section_Table_Row */ private $_height = null; - /** - * Row heightRules - * - * @var array - */ - private $_heightRules = array(); - /** * Row style * @@ -82,12 +75,11 @@ class PHPWord_Section_Table_Row * @param int $height * @param mixed $style */ - public function __construct($insideOf, $pCount, $height = null, $style = null, $hRules = null) + public function __construct($insideOf, $pCount, $height = null, $style = null) { $this->_insideOf = $insideOf; $this->_pCount = $pCount; $this->_height = $height; - $this->_heightRules = $hRules; $this->_style = new PHPWord_Style_Row(); if (!is_null($style)) { @@ -146,14 +138,4 @@ class PHPWord_Section_Table_Row { return $this->_height; } - - /** - * Get all row height rules - * - * @return array - */ - public function getHeightRules() - { - return $this->_heightRules; - } } diff --git a/Classes/PHPWord/Style/Row.php b/Classes/PHPWord/Style/Row.php index da039d84..aae7e851 100644 --- a/Classes/PHPWord/Style/Row.php +++ b/Classes/PHPWord/Style/Row.php @@ -45,6 +45,13 @@ class PHPWord_Style_Row */ private $_cantSplit = false; + /** + * Table row height rule (auto, exact, atLeast) + * + * @var String + */ + private $_heightRule = null; + /** * Create a new row style */ @@ -112,4 +119,29 @@ class PHPWord_Style_Row { return $this->_cantSplit; } + + /** + * Set heightRule + * + * @param String $pValue + * @return PHPWord_Style_Row + */ + public function setHeightRule($pValue = false) + { + if (!is_string($pValue)) { + $pValue = null; + } + $this->_heightRule = $pValue; + return $this; + } + + /** + * Get heightRule + * + * @return heightRule + */ + public function getHeightRule() + { + return $this->_heightRule; + } } diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index 57bb7266..0a45fb20 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -569,11 +569,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart for ($i = 0; $i < $_cRows; $i++) { $row = $_rows[$i]; - $height = $row->getHeight(); - $heightRules = $row->getHeightRules(); + $height = $row->getHeight(); $rowStyle = $row->getStyle(); $tblHeader = $rowStyle->getTblHeader(); $cantSplit = $rowStyle->getCantSplit(); + $heightRule = $rowStyle->getHeightRule(); $objWriter->startElement('w:tr'); @@ -581,9 +581,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->startElement('w:trPr'); if (!is_null($height)) { $objWriter->startElement('w:trHeight'); - if(!is_null($heightRules)) { + if(!is_null($heightRule)) { $objWriter->startAttribute('w:hRule'); - $objWriter->text($heightRules); + $objWriter->text($heightRule); $objWriter->endAttribute(); } $objWriter->writeAttribute('w:val', $height); diff --git a/samples/Sample_21_TableRowRules.php b/samples/Sample_21_TableRowRules.php index 83d03e0c..18c67be3 100644 --- a/samples/Sample_21_TableRowRules.php +++ b/samples/Sample_21_TableRowRules.php @@ -9,7 +9,9 @@ echo date('H:i:s') , ' Create new PHPWord object' , EOL; $PHPWord = new PHPWord(); $section = $PHPWord->createSection(); -$section->addText("By default, a table row adds a textbreak after its content (notice the red border), even if the row height is <= height of the content"); +$section->addText("By default, when you insert an image, it adds a textbreak after its content."); +$section->addText("If we want a simple border around an image, we wrap the image inside a table->row->cell"); +$section->addText("On the image with the red border, even if we set the row height to the height of the image, the textbreak is still there:"); $table1 = $section->addTable(array("cellMargin"=> 0, "cellMarginRight"=> 0, "cellMarginBottom"=> 0, "cellMarginLeft"=> 0)); $table1->addRow(3750); @@ -17,16 +19,17 @@ $cell1 = $table1->addCell(null, array("valign" => "top", "borderSize" => 30, "bo $cell1->addImage("./resources/_earth.jpg", array("width" => 250, "height" => 250, "align" => "center")); $section->addTextBreak(); -$section->addText("But if we set the row rule \"exact\", we get rid of the textbreak!"); +$section->addText("But if we set the rowStyle hRule \"exact\", the real row height is used, removing the textbreak:"); $table2 = $section->addTable(array("cellMargin"=> 0, "cellMarginRight"=> 0, "cellMarginBottom"=> 0, "cellMarginLeft"=> 0)); -$table2->addRow(3750, null, "exact"); +$table2->addRow(3750, array("heightRule"=>"exact")); $cell2 = $table2->addCell(null, array("valign" => "top", "borderSize" => 30, "borderColor" => "00ff00")); $cell2->addImage("./resources/_earth.jpg", array("width" => 250, "height" => 250, "align" => "center")); $section->addTextBreak(); $section->addText("In this example, image is 250px height. Rows are calculated in twips, and 1px = 15twips."); -$section->addText("So: $"."table2->addRow(3750, null, 'exact');"); +$section->addText("So: $"."table2->addRow(3750, array('heightRule'=>'exact'));"); +$section->addText("heightRule defaults to 'atLeast' when the row has an height set, and default to 'auto' otherwise"); // Save file $name = basename(__FILE__, '.php'); From 9faaa1b566b418577d56b4fe15cb461d4d234a35 Mon Sep 17 00:00:00 2001 From: Julien Carignan Date: Fri, 28 Mar 2014 13:31:30 -0400 Subject: [PATCH 08/22] reverted table/row Signed-off-by: Julien Carignan --- Classes/PHPWord/Section/Table/Row.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/PHPWord/Section/Table/Row.php b/Classes/PHPWord/Section/Table/Row.php index 7ca99823..dd8ea65c 100644 --- a/Classes/PHPWord/Section/Table/Row.php +++ b/Classes/PHPWord/Section/Table/Row.php @@ -37,7 +37,7 @@ class PHPWord_Section_Table_Row * @var int */ private $_height = null; - + /** * Row style * From 772017d8e3ede9c308104d237ba5b7e41b57d2e1 Mon Sep 17 00:00:00 2001 From: Julien Carignan Date: Fri, 28 Mar 2014 13:33:49 -0400 Subject: [PATCH 09/22] empty space remove --- Classes/PHPWord/Writer/Word2007/Base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index 0a45fb20..16680ae2 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -569,7 +569,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart for ($i = 0; $i < $_cRows; $i++) { $row = $_rows[$i]; - $height = $row->getHeight(); + $height = $row->getHeight(); $rowStyle = $row->getStyle(); $tblHeader = $rowStyle->getTblHeader(); $cantSplit = $rowStyle->getCantSplit(); From 255af437f2ccbbb03e87f218a919c521cfdcc233 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 29 Mar 2014 00:57:23 +0700 Subject: [PATCH 10/22] Bugfix for footnote reference number and additional feature to insert text break and style the reference number --- CHANGELOG.md | 4 +- docs/elements.rst | 14 ++- samples/Sample_06_Footnote.php | 3 +- src/PhpWord/Section/Footnote.php | 14 +++ src/PhpWord/Writer/Word2007/Base.php | 53 ++------- src/PhpWord/Writer/Word2007/Document.php | 2 +- src/PhpWord/Writer/Word2007/Footnotes.php | 87 +++++++++++++-- src/PhpWord/Writer/Word2007/Styles.php | 105 ++++++++++-------- tests/PhpWord/Tests/Section/FootnoteTest.php | 49 ++++++++ .../Tests/Writer/Word2007/BaseTest.php | 13 ++- 10 files changed, 235 insertions(+), 109 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30be78e6..25689dbc 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,10 +8,12 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - Image: Get image dimensions without EXIF extension - @andrew-kzoo GH-184 - Table: Add tblGrid element for Libre/Open Office table sizing - @gianis6 GH-183 +- Footnote: Ability to insert textbreak in footnote `$footnote->addTextBreak()` - @ivanlanin +- Footnote: Ability to style footnote reference mark by using `FootnoteReference` style - @ivanlanin ### Bugfixes -- +- Footnote: Footnote content doesn't show footnote reference number - @ivanlanin GH-170 ### Miscellaneous diff --git a/docs/elements.rst b/docs/elements.rst index d0f78d2e..f7ce0319 100644 --- a/docs/elements.rst +++ b/docs/elements.rst @@ -326,7 +326,8 @@ Footnotes --------- You can create footnotes in texts or textruns, but it's recommended to -use textrun to have better layout. +use textrun to have better layout. You can use ``addText``, ``addLink``, +and ``addTextBreak`` on a footnote. On textrun: @@ -335,7 +336,11 @@ On textrun: $textrun = $section->createTextRun(); $textrun->addText('Lead text.'); $footnote = $textrun->createFootnote(); - $footnote->addText('Footnote text.'); + $footnote->addText('Footnote text can have '); + $footnote->addLink('http://test.com', 'links'); + $footnote->addText('.'); + $footnote->addTextBreak(); + $footnote->addText('And text break.'); $textrun->addText('Trailing text.'); On text: @@ -345,3 +350,8 @@ On text: $section->addText('Lead text.'); $footnote = $section->createFootnote(); $footnote->addText('Footnote text.'); + +The footnote reference number will be displayed with decimal number starting +from 1. This number use ``FooterReference`` style which you can redefine by +``addFontStyle`` method. Default value for this style is +``array('superScript' => true)``; diff --git a/samples/Sample_06_Footnote.php b/samples/Sample_06_Footnote.php index c430e548..081d5918 100755 --- a/samples/Sample_06_Footnote.php +++ b/samples/Sample_06_Footnote.php @@ -24,7 +24,8 @@ $footnote->addText(' No break is placed after adding an element.', 'BoldText'); $footnote->addText(' All elements are placed inside a paragraph.', 'ColoredText'); $footnote->addText(' The best search engine: '); $footnote->addLink('http://www.google.com', null, 'NLink'); -$footnote->addText('. Also not bad: '); +$footnote->addText('. Also not bad:'); +$footnote->addTextBreak(); $footnote->addLink('http://www.bing.com', null, 'NLink'); $textrun->addText('The trailing text in the paragraph.'); diff --git a/src/PhpWord/Section/Footnote.php b/src/PhpWord/Section/Footnote.php index 857c4e20..c86d8e44 100644 --- a/src/PhpWord/Section/Footnote.php +++ b/src/PhpWord/Section/Footnote.php @@ -77,6 +77,20 @@ class Footnote return $text; } + /** + * Add TextBreak + * + * @param int $count + * @param mixed $fontStyle + * @param mixed $paragraphStyle + */ + public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null) + { + for ($i = 1; $i <= $count; $i++) { + $this->_elementCollection[] = new TextBreak($fontStyle, $paragraphStyle); + } + } + /** * Add a Link Element * diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index 733b376a..7234aed9 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -125,7 +125,7 @@ class Base extends WriterPart } elseif ($element instanceof Image) { $this->_writeImage($xmlWriter, $element, true); } elseif ($element instanceof Footnote) { - $this->_writeFootnoteReference($xmlWriter, $element, true); + $this->_writeFootnote($xmlWriter, $element, true); } elseif ($element instanceof TextBreak) { $xmlWriter->writeElement('w:br'); } @@ -1172,61 +1172,24 @@ class Base extends WriterPart } /** - * Write footnote element - * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section\Footnote $footnote - */ - protected function _writeFootnote(XMLWriter $xmlWriter, Footnote $footnote) - { - $xmlWriter->startElement('w:footnote'); - $xmlWriter->writeAttribute('w:id', $footnote->getReferenceId()); - - $styleParagraph = $footnote->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(); - } - - $elements = $footnote->getElements(); - if (count($elements) > 0) { - foreach ($elements as $element) { - if ($element instanceof Text) { - $this->_writeText($xmlWriter, $element, true); - } elseif ($element instanceof Link) { - $this->_writeLink($xmlWriter, $element, true); - } - } - } - - $xmlWriter->endElement(); // w:p - $xmlWriter->endElement(); // w:footnote - } - - /** - * Write footnote reference element + * 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 _writeFootnoteReference(XMLWriter $xmlWriter, Footnote $footnote, $withoutP = false) + 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 diff --git a/src/PhpWord/Writer/Word2007/Document.php b/src/PhpWord/Writer/Word2007/Document.php index ffce32d2..368d1b31 100644 --- a/src/PhpWord/Writer/Word2007/Document.php +++ b/src/PhpWord/Writer/Word2007/Document.php @@ -98,7 +98,7 @@ class Document extends Base } elseif ($element instanceof TOC) { $this->_writeTOC($xmlWriter); } elseif ($element instanceof Footnote) { - $this->_writeFootnoteReference($xmlWriter, $element); + $this->_writeFootnote($xmlWriter, $element); } } diff --git a/src/PhpWord/Writer/Word2007/Footnotes.php b/src/PhpWord/Writer/Word2007/Footnotes.php index 59d8cc1f..eb410393 100644 --- a/src/PhpWord/Writer/Word2007/Footnotes.php +++ b/src/PhpWord/Writer/Word2007/Footnotes.php @@ -10,6 +10,10 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\Section\Footnote; +use PhpOffice\PhpWord\Section\Text; +use PhpOffice\PhpWord\Section\Link; +use PhpOffice\PhpWord\Section\TextBreak; +use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Shared\XMLWriter; /** @@ -27,19 +31,25 @@ class Footnotes extends Base // Create XML writer $xmlWriter = null; if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); + $xmlWriter = new XMLWriter( + XMLWriter::STORAGE_DISK, + $this->getParentWriter()->getDiskCachingDirectory() + ); } else { $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); } - // XML header $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); - $xmlWriter->startElement('w:footnotes'); - $xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'); - $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); - - // write separator and continuation separator + $xmlWriter->writeAttribute( + 'xmlns:r', + 'http://schemas.openxmlformats.org/officeDocument/2006/relationships' + ); + $xmlWriter->writeAttribute( + 'xmlns:w', + 'http://schemas.openxmlformats.org/wordprocessingml/2006/main' + ); + // Separator and continuation separator $xmlWriter->startElement('w:footnote'); $xmlWriter->writeAttribute('w:id', 0); $xmlWriter->writeAttribute('w:type', 'separator'); @@ -50,7 +60,7 @@ class Footnotes extends Base $xmlWriter->endElement(); // w:r $xmlWriter->endElement(); // w:p $xmlWriter->endElement(); // w:footnote - + // Content $xmlWriter->startElement('w:footnote'); $xmlWriter->writeAttribute('w:id', 1); $xmlWriter->writeAttribute('w:type', 'continuationSeparator'); @@ -61,16 +71,69 @@ class Footnotes extends Base $xmlWriter->endElement(); // w:r $xmlWriter->endElement(); // w:p $xmlWriter->endElement(); // w:footnote - foreach ($allFootnotesCollection as $footnote) { if ($footnote instanceof Footnote) { - $this->_writeFootnote($xmlWriter, $footnote); + $this->writeFootnote($xmlWriter, $footnote); } } - $xmlWriter->endElement(); - // Return return $xmlWriter->getData(); } + + /** + * Write footnote content, overrides method in parent class + * + * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param PhpOffice\PhpWord\Section\Footnote $footnote + */ + private function writeFootnote(XMLWriter $xmlWriter, Footnote $footnote) + { + $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(); + } + // Reference symbol + $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->writeElement('w:footnoteRef'); + $xmlWriter->endElement(); // w:r + // Empty space after refence symbol + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:t'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->writeRaw(' '); + $xmlWriter->endElement(); // w:t + $xmlWriter->endElement(); // w:r + // Actual footnote contents + $elements = $footnote->getElements(); + if (count($elements) > 0) { + foreach ($elements as $element) { + if ($element instanceof Text) { + $this->_writeText($xmlWriter, $element, true); + } elseif ($element instanceof Link) { + $this->_writeLink($xmlWriter, $element, true); + } elseif ($element instanceof TextBreak) { + $xmlWriter->writeElement('w:br'); + } + } + } + $xmlWriter->endElement(); // w:p + $xmlWriter->endElement(); // w:footnote + } } diff --git a/src/PhpWord/Writer/Word2007/Styles.php b/src/PhpWord/Writer/Word2007/Styles.php index dc665300..a24ac516 100644 --- a/src/PhpWord/Writer/Word2007/Styles.php +++ b/src/PhpWord/Writer/Word2007/Styles.php @@ -21,11 +21,11 @@ use PhpOffice\PhpWord\Style\Paragraph; class Styles extends Base { /** - * PHPWord object + * PhpWord object * * @var PhpWord */ - private $_document; + private $phpWord; /** * Write word/styles.xml @@ -37,44 +37,28 @@ class Styles extends Base // Create XML writer $xmlWriter = null; if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); + $xmlWriter = new XMLWriter( + XMLWriter::STORAGE_DISK, + $this->getParentWriter()->getDiskCachingDirectory() + ); } else { $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); } - - $this->_document = $phpWord; - + $this->phpWord = $phpWord; // XML header $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); - $xmlWriter->startElement('w:styles'); - - $xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'); - $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); - - // Write DocDefaults - $this->_writeDocDefaults($xmlWriter); - - // Write Style Definitions + $xmlWriter->writeAttribute( + 'xmlns:r', + 'http://schemas.openxmlformats.org/officeDocument/2006/relationships' + ); + $xmlWriter->writeAttribute( + 'xmlns:w', + 'http://schemas.openxmlformats.org/wordprocessingml/2006/main' + ); + // Write default styles $styles = Style::getStyles(); - - // Write normal paragraph style - $normalStyle = null; - if (array_key_exists('Normal', $styles)) { - $normalStyle = $styles['Normal']; - } - $xmlWriter->startElement('w:style'); - $xmlWriter->writeAttribute('w:type', 'paragraph'); - $xmlWriter->writeAttribute('w:default', '1'); - $xmlWriter->writeAttribute('w:styleId', 'Normal'); - $xmlWriter->startElement('w:name'); - $xmlWriter->writeAttribute('w:val', 'Normal'); - $xmlWriter->endElement(); - if (!is_null($normalStyle)) { - $this->_writeParagraphStyle($xmlWriter, $normalStyle); - } - $xmlWriter->endElement(); - + $this->writeDefaultStyles($xmlWriter, $styles); // Write other styles if (count($styles) > 0) { foreach ($styles as $styleName => $style) { @@ -180,36 +164,65 @@ class Styles extends Base } /** - * Write document defaults + * Write default font and other default styles * * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param array $styles */ - private function _writeDocDefaults(XMLWriter $xmlWriter) + private function writeDefaultStyles(XMLWriter $xmlWriter, $styles) { - $fontName = $this->_document->getDefaultFontName(); - $fontSize = $this->_document->getDefaultFontSize(); + $fontName = $this->phpWord->getDefaultFontName(); + $fontSize = $this->phpWord->getDefaultFontSize(); + // Default font $xmlWriter->startElement('w:docDefaults'); $xmlWriter->startElement('w:rPrDefault'); $xmlWriter->startElement('w:rPr'); - $xmlWriter->startElement('w:rFonts'); $xmlWriter->writeAttribute('w:ascii', $fontName); $xmlWriter->writeAttribute('w:hAnsi', $fontName); $xmlWriter->writeAttribute('w:eastAsia', $fontName); $xmlWriter->writeAttribute('w:cs', $fontName); - $xmlWriter->endElement(); - + $xmlWriter->endElement(); // w:rFonts $xmlWriter->startElement('w:sz'); $xmlWriter->writeAttribute('w:val', $fontSize * 2); - $xmlWriter->endElement(); - + $xmlWriter->endElement(); // w:sz $xmlWriter->startElement('w:szCs'); $xmlWriter->writeAttribute('w:val', $fontSize * 2); - $xmlWriter->endElement(); + $xmlWriter->endElement(); // w:szCs + $xmlWriter->endElement(); // w:rPr + $xmlWriter->endElement(); // w:rPrDefault + $xmlWriter->endElement(); // w:docDefaults - $xmlWriter->endElement(); - $xmlWriter->endElement(); - $xmlWriter->endElement(); + // Normal style + $xmlWriter->startElement('w:style'); + $xmlWriter->writeAttribute('w:type', 'paragraph'); + $xmlWriter->writeAttribute('w:default', '1'); + $xmlWriter->writeAttribute('w:styleId', 'Normal'); + $xmlWriter->startElement('w:name'); + $xmlWriter->writeAttribute('w:val', 'Normal'); + $xmlWriter->endElement(); // w:name + if (array_key_exists('Normal', $styles)) { + $this->_writeParagraphStyle($xmlWriter, $styles['Normal']); + } + $xmlWriter->endElement(); // w:style + + // FootnoteReference style + if (!array_key_exists('FootnoteReference', $styles)) { + $xmlWriter->startElement('w:style'); + $xmlWriter->writeAttribute('w:type', 'character'); + $xmlWriter->writeAttribute('w:styleId', 'FootnoteReference'); + $xmlWriter->startElement('w:name'); + $xmlWriter->writeAttribute('w:val', 'Footnote Reference'); + $xmlWriter->endElement(); // w:name + $xmlWriter->writeElement('w:semiHidden'); + $xmlWriter->writeElement('w:unhideWhenUsed'); + $xmlWriter->startElement('w:rPr'); + $xmlWriter->startElement('w:vertAlign'); + $xmlWriter->writeAttribute('w:val', 'superscript'); + $xmlWriter->endElement(); // w:vertAlign + $xmlWriter->endElement(); // w:rPr + $xmlWriter->endElement(); // w:style + } } } diff --git a/tests/PhpWord/Tests/Section/FootnoteTest.php b/tests/PhpWord/Tests/Section/FootnoteTest.php index 19cf58c7..ed2e36de 100644 --- a/tests/PhpWord/Tests/Section/FootnoteTest.php +++ b/tests/PhpWord/Tests/Section/FootnoteTest.php @@ -19,6 +19,11 @@ use PhpOffice\PhpWord\Section\Footnote; */ class FootnoteTest extends \PHPUnit_Framework_TestCase { + /** + * New instance without parameter + * + * @covers ::__construct + */ public function testConstruct() { $oFootnote = new Footnote(); @@ -28,6 +33,11 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oFootnote->getParagraphStyle(), null); } + /** + * New instance with string parameter + * + * @covers ::__construct + */ public function testConstructString() { $oFootnote = new Footnote('pStyle'); @@ -35,6 +45,11 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oFootnote->getParagraphStyle(), 'pStyle'); } + /** + * New instance with array parameter + * + * @covers ::__construct + */ public function testConstructArray() { $oFootnote = new Footnote(array('spacing' => 100)); @@ -45,6 +60,11 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase ); } + /** + * Add text element + * + * @covers ::addText + */ public function testAddText() { $oFootnote = new Footnote(); @@ -54,6 +74,24 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $element); } + /** + * Add text break element + * + * @covers ::addTextBreak + */ + public function testAddTextBreak() + { + $oFootnote = new Footnote(); + $oFootnote->addTextBreak(2); + + $this->assertCount(2, $oFootnote->getElements()); + } + + /** + * Add link element + * + * @covers ::addLink + */ public function testAddLink() { $oFootnote = new Footnote(); @@ -63,6 +101,12 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Link', $element); } + /** + * Set/get reference Id + * + * @covers ::setReferenceId + * @covers ::getReferenceId + */ public function testReferenceId() { $oFootnote = new Footnote(); @@ -72,6 +116,11 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oFootnote->getReferenceId(), $iVal); } + /** + * Get elements + * + * @covers ::getElements + */ public function testGetElements() { $oFootnote = new Footnote(); diff --git a/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php b/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php index ac14fd17..e4abb7a1 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php @@ -79,9 +79,13 @@ class BaseTest extends \PHPUnit_Framework_TestCase { $phpWord = new PhpWord(); $section = $phpWord->createSection(); + $fontStyleArray = array('bold' => true); + $fontStyleName = 'Test'; $expected = 'PhpWord'; $section->addLink('http://github.com/phpoffice/phpword', $expected); + $section->addLink('http://github.com/phpoffice/phpword', 'Test', $fontStyleArray); + $section->addLink('http://github.com/phpoffice/phpword', 'Test', $fontStyleName); $doc = TestHelperDOCX::getDocument($phpWord); $element = $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t'); @@ -97,8 +101,14 @@ class BaseTest extends \PHPUnit_Framework_TestCase $phpWord = new PhpWord(); $section = $phpWord->createSection(); $footer = $section->createFooter(); + $fontStyleArray = array('bold' => true); + $fontStyleName = 'Font'; + $paragraphStyleArray = array('align' => 'right'); + $paragraphStyleName = 'Paragraph'; - $footer->addPreserveText('{PAGE}'); + $footer->addPreserveText('Page {PAGE}'); + $footer->addPreserveText('{PAGE}', $fontStyleArray, $paragraphStyleArray); + $footer->addPreserveText('{PAGE}', $fontStyleName, $paragraphStyleName); $doc = TestHelperDOCX::getDocument($phpWord); $preserve = $doc->getElement("w:p/w:r[2]/w:instrText", 'word/footer1.xml'); @@ -193,6 +203,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase $styles['superScript'] = true; $styles['color'] = 'FF0000'; $styles['fgColor'] = 'yellow'; + $styles['hint'] = 'eastAsia'; $section = $phpWord->createSection(); $section->addText('Test', $styles); From 612ad857739ff7cdb6e26fbfa8585588e33a0596 Mon Sep 17 00:00:00 2001 From: Julien Carignan Date: Fri, 28 Mar 2014 15:21:01 -0400 Subject: [PATCH 11/22] changed heightRule string to exactHeight bool --- Classes/PHPWord/Style/Row.php | 26 ++++++++++++------------ Classes/PHPWord/Writer/Word2007/Base.php | 6 +++--- samples/Sample_21_TableRowRules.php | 7 +++---- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/Classes/PHPWord/Style/Row.php b/Classes/PHPWord/Style/Row.php index aae7e851..d779d3cc 100644 --- a/Classes/PHPWord/Style/Row.php +++ b/Classes/PHPWord/Style/Row.php @@ -46,11 +46,11 @@ class PHPWord_Style_Row private $_cantSplit = false; /** - * Table row height rule (auto, exact, atLeast) + * Table row exact height * - * @var String + * @var bool */ - private $_heightRule = null; + private $_exactHeight = false; /** * Create a new row style @@ -121,27 +121,27 @@ class PHPWord_Style_Row } /** - * Set heightRule + * Set exactHeight * - * @param String $pValue + * @param bool $pValue * @return PHPWord_Style_Row */ - public function setHeightRule($pValue = false) + public function setExactHeight($pValue = false) { - if (!is_string($pValue)) { - $pValue = null; + if (!is_bool($pValue)) { + $pValue = false; } - $this->_heightRule = $pValue; + $this->_exactHeight = $pValue; return $this; } /** - * Get heightRule + * Get exactHeight * - * @return heightRule + * @return boolean */ - public function getHeightRule() + public function getExactHeight() { - return $this->_heightRule; + return $this->_exactHeight; } } diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index 16680ae2..1cf3bad3 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -573,7 +573,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $rowStyle = $row->getStyle(); $tblHeader = $rowStyle->getTblHeader(); $cantSplit = $rowStyle->getCantSplit(); - $heightRule = $rowStyle->getHeightRule(); + $exactHeight = $rowStyle->getExactHeight(); $objWriter->startElement('w:tr'); @@ -581,9 +581,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->startElement('w:trPr'); if (!is_null($height)) { $objWriter->startElement('w:trHeight'); - if(!is_null($heightRule)) { + if($exactHeight) { $objWriter->startAttribute('w:hRule'); - $objWriter->text($heightRule); + $objWriter->text("exact"); $objWriter->endAttribute(); } $objWriter->writeAttribute('w:val', $height); diff --git a/samples/Sample_21_TableRowRules.php b/samples/Sample_21_TableRowRules.php index 18c67be3..1082f99f 100644 --- a/samples/Sample_21_TableRowRules.php +++ b/samples/Sample_21_TableRowRules.php @@ -19,17 +19,16 @@ $cell1 = $table1->addCell(null, array("valign" => "top", "borderSize" => 30, "bo $cell1->addImage("./resources/_earth.jpg", array("width" => 250, "height" => 250, "align" => "center")); $section->addTextBreak(); -$section->addText("But if we set the rowStyle hRule \"exact\", the real row height is used, removing the textbreak:"); +$section->addText("But if we set the rowStyle 'exactHeight' to true, the real row height is used, removing the textbreak:"); $table2 = $section->addTable(array("cellMargin"=> 0, "cellMarginRight"=> 0, "cellMarginBottom"=> 0, "cellMarginLeft"=> 0)); -$table2->addRow(3750, array("heightRule"=>"exact")); +$table2->addRow(3750, array("exactHeight"=>)); $cell2 = $table2->addCell(null, array("valign" => "top", "borderSize" => 30, "borderColor" => "00ff00")); $cell2->addImage("./resources/_earth.jpg", array("width" => 250, "height" => 250, "align" => "center")); $section->addTextBreak(); $section->addText("In this example, image is 250px height. Rows are calculated in twips, and 1px = 15twips."); -$section->addText("So: $"."table2->addRow(3750, array('heightRule'=>'exact'));"); -$section->addText("heightRule defaults to 'atLeast' when the row has an height set, and default to 'auto' otherwise"); +$section->addText("So: $"."table2->addRow(3750, array('exactHeight'=>true));"); // Save file $name = basename(__FILE__, '.php'); From c91d6b61c9af18f182070b59b24a9a7a31532afd Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 29 Mar 2014 22:26:00 +0700 Subject: [PATCH 12/22] Writer unit test enhancements --- src/PhpWord/Writer/RTF.php | 2 +- tests/PhpWord/Tests/Writer/ODTextTest.php | 28 +++---- tests/PhpWord/Tests/Writer/RTFTest.php | 67 ++++++++-------- .../Tests/Writer/Word2007/BaseTest.php | 18 ++++- .../Tests/Writer/Word2007/DocumentTest.php | 10 +-- tests/PhpWord/Tests/Writer/Word2007Test.php | 76 ++++++++++++++++--- 6 files changed, 134 insertions(+), 67 deletions(-) diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index b9aa3aa3..970d91ad 100755 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -438,7 +438,7 @@ class RTF implements IWriter if ($styleFont->getBold()) { $sRTFText .= '\b'; } - if ($styleFont->getBold()) { + if ($styleFont->getItalic()) { $sRTFText .= '\i'; } if ($styleFont->getSize()) { diff --git a/tests/PhpWord/Tests/Writer/ODTextTest.php b/tests/PhpWord/Tests/Writer/ODTextTest.php index f65ce79f..8c345f55 100644 --- a/tests/PhpWord/Tests/Writer/ODTextTest.php +++ b/tests/PhpWord/Tests/Writer/ODTextTest.php @@ -14,13 +14,12 @@ use PhpOffice\PhpWord\Writer\ODText; /** * Test class for PhpOffice\PhpWord\Writer\ODText * - * @coversDefaultClass \PhpOffice\PhpWord\Writer\ODText * @runTestsInSeparateProcesses */ class ODTextTest extends \PHPUnit_Framework_TestCase { /** - * Test construct + * Construct */ public function testConstruct() { @@ -43,9 +42,10 @@ class ODTextTest extends \PHPUnit_Framework_TestCase } /** - * @covers ::getPhpWord - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception - * @expectedExceptionMessage No PhpWord assigned. + * Construct with null + * + * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedExceptionMessage No PhpWord assigned. */ public function testConstructWithNull() { @@ -54,7 +54,7 @@ class ODTextTest extends \PHPUnit_Framework_TestCase } /** - * @covers ::save + * Save */ public function testSave() { @@ -89,7 +89,8 @@ class ODTextTest extends \PHPUnit_Framework_TestCase } /** - * @covers ::save + * Save php output + * * @todo Haven't got any method to test this */ public function testSavePhpOutput() @@ -102,8 +103,9 @@ class ODTextTest extends \PHPUnit_Framework_TestCase } /** - * @covers ::save - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * Save with no PhpWord object assigned + * + * @expectedException \PhpOffice\PhpWord\Exceptions\Exception * @expectedExceptionMessage PhpWord object unassigned. */ public function testSaveException() @@ -113,7 +115,7 @@ class ODTextTest extends \PHPUnit_Framework_TestCase } /** - * @covers ::getWriterPart + * Get writer part return null value */ public function testGetWriterPartNull() { @@ -122,8 +124,7 @@ class ODTextTest extends \PHPUnit_Framework_TestCase } /** - * @covers ::setUseDiskCaching - * @covers ::getUseDiskCaching + * Set/get use disk caching */ public function testSetGetUseDiskCaching() { @@ -134,7 +135,8 @@ class ODTextTest extends \PHPUnit_Framework_TestCase } /** - * @covers ::setUseDiskCaching + * Use disk caching exception + * * @expectedException \PhpOffice\PhpWord\Exceptions\Exception */ public function testSetUseDiskCachingException() diff --git a/tests/PhpWord/Tests/Writer/RTFTest.php b/tests/PhpWord/Tests/Writer/RTFTest.php index 9707c05f..7b546438 100644 --- a/tests/PhpWord/Tests/Writer/RTFTest.php +++ b/tests/PhpWord/Tests/Writer/RTFTest.php @@ -14,13 +14,12 @@ use PhpOffice\PhpWord\Writer\RTF; /** * Test class for PhpOffice\PhpWord\Writer\RTF * - * @coversDefaultClass \PhpOffice\PhpWord\Writer\RTF * @runTestsInSeparateProcesses */ class RTFTest extends \PHPUnit_Framework_TestCase { /** - * covers ::construct + * Construct */ public function testConstruct() { @@ -31,8 +30,9 @@ class RTFTest extends \PHPUnit_Framework_TestCase } /** - * covers ::__construct - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * Construct with null + * + * @expectedException \PhpOffice\PhpWord\Exceptions\Exception * @expectedExceptionMessage No PhpWord assigned. */ public function testConstructWithNull() @@ -42,32 +42,7 @@ class RTFTest extends \PHPUnit_Framework_TestCase } /** - * @covers ::save - * @todo Haven't got any method to test this - */ - public function testSavePhpOutput() - { - $phpWord = new PhpWord(); - $section = $phpWord->createSection(); - $section->addText('Test'); - $writer = new RTF($phpWord); - $writer->save('php://output'); - } - - /** - * @covers ::save - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception - * @expectedExceptionMessage PhpWord object unassigned. - */ - public function testSaveException() - { - $writer = new RTF(); - $writer->save(); - } - - /** - * @covers ::save - * @covers :: + * Save */ public function testSave() { @@ -76,12 +51,12 @@ class RTFTest extends \PHPUnit_Framework_TestCase $file = __DIR__ . "/../_files/temp.rtf"; $phpWord = new PhpWord(); - $phpWord->addFontStyle('Font', array('size' => 11)); + $phpWord->addFontStyle('Font', array('name' => 'Verdana', 'size' => 11, 'color' => 'FF0000', 'fgColor' => 'FF0000')); $phpWord->addParagraphStyle('Paragraph', array('align' => 'center')); $section = $phpWord->createSection(); - $section->addText('Test 1', 'Font'); + $section->addText('Test 1', 'Font', 'Paragraph'); $section->addTextBreak(); - $section->addText('Test 2', null, 'Paragraph'); + $section->addText('Test 2', array('name' => 'Tahoma', 'bold' => true, 'italic' => true)); $section->addLink('http://test.com'); $section->addTitle('Test', 1); $section->addPageBreak(); @@ -101,4 +76,30 @@ class RTFTest extends \PHPUnit_Framework_TestCase unlink($file); } + + /** + * Save + * + * @todo Haven't got any method to test this + */ + public function testSavePhpOutput() + { + $phpWord = new PhpWord(); + $section = $phpWord->createSection(); + $section->addText('Test'); + $writer = new RTF($phpWord); + $writer->save('php://output'); + } + + /** + * Save with no PhpWord object assigned + * + * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedExceptionMessage PhpWord object unassigned. + */ + public function testSaveException() + { + $writer = new RTF(); + $writer->save(); + } } diff --git a/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php b/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php index e4abb7a1..68baeb2b 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php @@ -65,7 +65,8 @@ class BaseTest extends \PHPUnit_Framework_TestCase $textrun->addTextBreak(); $textrun = $section->createTextRun($aStyle); $textrun->addLink('http://test.com'); - $textrun->addImage($imageSrc); + $textrun->addImage($imageSrc, array('align' => 'top')); + $textrun->createFootnote(); $doc = TestHelperDOCX::getDocument($phpWord); $parent = "/w:document/w:body/w:p"; @@ -80,12 +81,14 @@ class BaseTest extends \PHPUnit_Framework_TestCase $phpWord = new PhpWord(); $section = $phpWord->createSection(); $fontStyleArray = array('bold' => true); - $fontStyleName = 'Test'; + $fontStyleName = 'Font Style'; + $paragraphStyleArray = array('align' => 'center'); + $paragraphStyleName = 'Paragraph Style'; $expected = 'PhpWord'; $section->addLink('http://github.com/phpoffice/phpword', $expected); - $section->addLink('http://github.com/phpoffice/phpword', 'Test', $fontStyleArray); - $section->addLink('http://github.com/phpoffice/phpword', 'Test', $fontStyleName); + $section->addLink('http://github.com/phpoffice/phpword', 'Test', $fontStyleArray, $paragraphStyleArray); + $section->addLink('http://github.com/phpoffice/phpword', 'Test', $fontStyleName, $paragraphStyleName); $doc = TestHelperDOCX::getDocument($phpWord); $element = $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t'); @@ -230,6 +233,10 @@ class BaseTest extends \PHPUnit_Framework_TestCase $tWidth = 120; $rHeight = 120; $cWidth = 120; + $imageSrc = __DIR__ . "/../../_files/images/earth.jpg"; + $objectSrc = __DIR__ . "/../../_files/documents/sheet.xls"; + + $tStyles["width"] = 50; $tStyles["cellMarginTop"] = 120; $tStyles["cellMarginRight"] = 120; $tStyles["cellMarginBottom"] = 120; @@ -247,6 +254,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase $cStyles["borderBottomColor"] = 'FF0000'; $cStyles["borderLeftColor"] = 'FF0000'; $cStyles["borderRightColor"] = 'FF0000'; + $cStyles["vMerge"] = 'restart'; $section = $phpWord->createSection(); $table = $section->addTable($tStyles); @@ -257,6 +265,8 @@ class BaseTest extends \PHPUnit_Framework_TestCase $cell->addTextBreak(); $cell->addLink('http://google.com'); $cell->addListItem('Test'); + $cell->addImage($imageSrc); + $cell->addObject($objectSrc); $textrun = $cell->createTextRun(); $textrun->addText('Test'); diff --git a/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php b/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php index 6c3335d3..f0b44e0b 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php @@ -14,7 +14,6 @@ use PhpOffice\PhpWord\Tests\TestHelperDOCX; /** * Test class for PhpOffice\PhpWord\Writer\Word2007\Document * - * @coversDefaultClass \PhpOffice\PhpWord\Writer\Word2007\Document * @runTestsInSeparateProcesses */ class DocumentTest extends \PHPUnit_Framework_TestCase @@ -27,6 +26,9 @@ class DocumentTest extends \PHPUnit_Framework_TestCase TestHelperDOCX::clear(); } + /** + * Write end section page numbering + */ public function testWriteEndSectionPageNumbering() { $phpWord = new PhpWord(); @@ -40,11 +42,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase } /** - * covers ::_writeTOC - * covers ::_writePageBreak - * covers ::_writeListItem - * covers ::_writeTitle - * covers ::_writeObject + * Write elements */ public function testElements() { diff --git a/tests/PhpWord/Tests/Writer/Word2007Test.php b/tests/PhpWord/Tests/Writer/Word2007Test.php index 5fcf37e2..22a1c0df 100644 --- a/tests/PhpWord/Tests/Writer/Word2007Test.php +++ b/tests/PhpWord/Tests/Writer/Word2007Test.php @@ -15,18 +15,20 @@ use PhpOffice\PhpWord\Tests\TestHelperDOCX; /** * Test class for PhpOffice\PhpWord\Writer\Word2007 * - * @coversDefaultClass \PhpOffice\PhpWord\Writer\Word2007 * @runTestsInSeparateProcesses */ class Word2007Test extends \PHPUnit_Framework_TestCase { + /** + * Tear down after each test + */ public function tearDown() { TestHelperDOCX::clear(); } /** - * covers ::__construct + * Construct */ public function testConstruct() { @@ -57,10 +59,12 @@ class Word2007Test extends \PHPUnit_Framework_TestCase } /** - * @covers ::save + * Save */ public function testSave() { + $localImage = __DIR__ . '/../_files/images/earth.jpg'; + $remoteImage = 'http://php.net//images/logos/php-med-trans-light.gif'; $phpWord = new PhpWord(); $phpWord->addFontStyle('Font', array('size' => 11)); $phpWord->addParagraphStyle('Paragraph', array('align' => 'center')); @@ -71,16 +75,55 @@ class Word2007Test extends \PHPUnit_Framework_TestCase $section = $phpWord->createSection(); $textrun = $section->createTextRun(); $textrun->addText('Test 3'); + $footnote = $textrun->createFootnote(); + $footnote->addLink('http://test.com'); + $header = $section->createHeader(); + $header->addImage($localImage); + $footer = $section->createFooter(); + $footer->addImage($remoteImage); $writer = new Word2007($phpWord); $file = __DIR__ . "/../_files/temp.docx"; $writer->save($file); - $this->assertTrue(\file_exists($file)); + + $this->assertTrue(file_exists($file)); + unlink($file); } /** - * @covers ::checkContentTypes + * Save using disk caching + */ + public function testSaveUseDiskCaching() + { + $phpWord = new PhpWord(); + $section = $phpWord->createSection(); + $section->addText('Test'); + + $writer = new Word2007($phpWord); + $writer->setUseDiskCaching(true); + $file = __DIR__ . "/../_files/temp.docx"; + $writer->save($file); + + $this->assertTrue(file_exists($file)); + + unlink($file); + } + + /** + * Save with no PhpWord object assigned + * + * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * @expectedExceptionMessage PhpWord object unassigned. + */ + public function testSaveException() + { + $writer = new Word2007(); + $writer->save(); + } + + /** + * Check content types */ public function testCheckContentTypes() { @@ -110,20 +153,33 @@ class Word2007Test extends \PHPUnit_Framework_TestCase } /** - * @covers ::setUseDiskCaching - * @covers ::getUseDiskCaching + * Get writer part return null value + */ + public function testGetWriterPartNull() + { + $object = new Word2007(); + $this->assertNull($object->getWriterPart()); + } + + /** + * Set/get use disk caching */ public function testSetGetUseDiskCaching() { - $object = new Word2007(); + $phpWord = new PhpWord(); + $section = $phpWord->createSection(); + $object = new Word2007($phpWord); $object->setUseDiskCaching(true, \PHPWORD_TESTS_BASE_DIR); + $writer = new Word2007($phpWord); + $writer->save('php://output'); $this->assertTrue($object->getUseDiskCaching()); } /** - * @covers ::setUseDiskCaching - * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + * Use disk caching exception + * + * @expectedException \PhpOffice\PhpWord\Exceptions\Exception */ public function testSetUseDiskCachingException() { From 970cb32b4566c08b0ed5b57ae200fb9299f1f0d9 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 30 Mar 2014 01:17:22 +0700 Subject: [PATCH 13/22] Fix deprecated method, unused parts, initial definition, and @method annotation --- samples/Sample_13_Images.php | 2 +- src/PhpWord/DocumentProperties.php | 9 --------- src/PhpWord/Media.php | 1 + src/PhpWord/Shared/XMLWriter.php | 1 + 4 files changed, 3 insertions(+), 10 deletions(-) diff --git a/samples/Sample_13_Images.php b/samples/Sample_13_Images.php index 6841ed64..cf467464 100644 --- a/samples/Sample_13_Images.php +++ b/samples/Sample_13_Images.php @@ -17,7 +17,7 @@ $section->addTextBreak(2); $source = 'http://php.net/images/logos/php-med-trans-light.gif'; $section->addText("Remote image from: {$source}"); -$section->addMemoryImage($source); +$section->addImage($source); // End code // Save file diff --git a/src/PhpWord/DocumentProperties.php b/src/PhpWord/DocumentProperties.php index cc24e35a..5e982646 100644 --- a/src/PhpWord/DocumentProperties.php +++ b/src/PhpWord/DocumentProperties.php @@ -476,41 +476,33 @@ class DocumentProperties switch ($propertyType) { case 'empty': // Empty return ''; - break; case 'null': // Null return null; - break; case 'i1': // 1-Byte Signed Integer case 'i2': // 2-Byte Signed Integer case 'i4': // 4-Byte Signed Integer case 'i8': // 8-Byte Signed Integer case 'int': // Integer return (int) $propertyValue; - break; case 'ui1': // 1-Byte Unsigned Integer case 'ui2': // 2-Byte Unsigned Integer case 'ui4': // 4-Byte Unsigned Integer case 'ui8': // 8-Byte Unsigned Integer case 'uint': // Unsigned Integer return abs((int) $propertyValue); - break; case 'r4': // 4-Byte Real Number case 'r8': // 8-Byte Real Number case 'decimal': // Decimal return (float) $propertyValue; - break; case 'lpstr': // LPSTR case 'lpwstr': // LPWSTR case 'bstr': // Basic String return $propertyValue; - break; case 'date': // Date and Time case 'filetime': // File Time return strtotime($propertyValue); - break; case 'bool': // Boolean return ($propertyValue == 'true') ? true : false; - break; case 'cy': // Currency case 'error': // Error Status Code case 'vector': // Vector @@ -525,7 +517,6 @@ class DocumentProperties case 'clsid': // Class ID case 'cf': // Clipboard Data return $propertyValue; - break; } return $propertyValue; diff --git a/src/PhpWord/Media.php b/src/PhpWord/Media.php index 80cde281..a9a9a6af 100755 --- a/src/PhpWord/Media.php +++ b/src/PhpWord/Media.php @@ -242,6 +242,7 @@ class Media $cImg = self::countFooterMediaElements($key); $rID = $cImg + 1; $cImg++; + $media = array(); $isMemImage = false; if (!is_null($image)) { $isMemImage = $image->getIsMemImage(); diff --git a/src/PhpWord/Shared/XMLWriter.php b/src/PhpWord/Shared/XMLWriter.php index 5b21e7b1..e27013d5 100644 --- a/src/PhpWord/Shared/XMLWriter.php +++ b/src/PhpWord/Shared/XMLWriter.php @@ -20,6 +20,7 @@ if (!defined('DATE_W3C')) { /** * XMLWriter wrapper * + * @method bool writeElement(string $name, string $content = null) * @method bool startElement(string $name) * @method bool writeAttribute(string $name, string $value) * @method bool endElement() From 05a4b952557f4d6fedda95362474f8a8d8c95715 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 30 Mar 2014 11:09:14 +0700 Subject: [PATCH 14/22] Unit test enhancements --- src/PhpWord/Section/CheckBox.php | 17 --- src/PhpWord/Writer/Word2007/Base.php | 12 -- tests/PhpWord/Tests/Section/CheckBoxTest.php | 11 ++ tests/PhpWord/Tests/Section/FooterTest.php | 62 ++++++++-- tests/PhpWord/Tests/Section/FootnoteTest.php | 18 --- tests/PhpWord/Tests/Section/HeaderTest.php | 59 ++++++++- tests/PhpWord/Tests/Section/ImageTest.php | 50 +++++++- .../PhpWord/Tests/Section/Table/CellTest.php | 115 ++++++++++++++++-- tests/PhpWord/Tests/Section/TextRunTest.php | 39 +++++- tests/PhpWord/Tests/Section/TextTest.php | 24 +++- tests/PhpWord/Tests/SectionTest.php | 2 +- .../Tests/Writer/Word2007/DocumentTest.php | 42 ++++++- .../Tests/Writer/Word2007/FootnotesTest.php | 10 +- tests/PhpWord/Tests/Writer/Word2007Test.php | 2 + 14 files changed, 378 insertions(+), 85 deletions(-) diff --git a/src/PhpWord/Section/CheckBox.php b/src/PhpWord/Section/CheckBox.php index f39ad841..57671b84 100644 --- a/src/PhpWord/Section/CheckBox.php +++ b/src/PhpWord/Section/CheckBox.php @@ -45,13 +45,6 @@ class CheckBox */ private $paragraphStyle; - /** - * Cell Element Collection - * - * @var array - */ - private $_elementCollection = array(); - /** * Create a new Text Element * @@ -174,14 +167,4 @@ class CheckBox { return $this->text; } - - /** - * Get all Elements - * - * @return array - */ - public function getElements() - { - return $this->_elementCollection; - } } diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index 5703a5a9..c32eede3 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -1223,11 +1223,6 @@ class Base extends WriterPart */ protected function _writeCheckBox(XMLWriter $xmlWriter, CheckBox $checkbox, $withoutP = false, $checkState = false) { - $count = 1; - $_elements = $checkbox->getElements(); - if (count($_elements) > 1) { - $count = $count + 1; - } $name = htmlspecialchars($checkbox->getName()); $name = String::controlCharacterPHP2OOXML($name); $text = htmlspecialchars($checkbox->getText()); @@ -1271,9 +1266,6 @@ class Base extends WriterPart $xmlWriter->endElement(); // w:fldChar $xmlWriter->endElement(); // w:r - $xmlWriter->startElement('w:bookmarkStart'); - $xmlWriter->writeAttribute('w:name', $name); - $xmlWriter->writeAttribute('w:id', $count); $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:instrText'); $xmlWriter->writeAttribute('xml:space', 'preserve'); @@ -1290,10 +1282,6 @@ class Base extends WriterPart $xmlWriter->writeAttribute('w:fldCharType', 'end'); $xmlWriter->endElement();// w:fldChar $xmlWriter->endElement(); // w:r - $xmlWriter->endElement(); // w:bookmarkStart - $xmlWriter->startElement('w:bookmarkEnd'); - $xmlWriter->writeAttribute('w:id', $count); - $xmlWriter->endElement();// w:bookmarkEnd $xmlWriter->startElement('w:r'); if ($sfIsObject) { diff --git a/tests/PhpWord/Tests/Section/CheckBoxTest.php b/tests/PhpWord/Tests/Section/CheckBoxTest.php index b69217a5..d07a0b69 100644 --- a/tests/PhpWord/Tests/Section/CheckBoxTest.php +++ b/tests/PhpWord/Tests/Section/CheckBoxTest.php @@ -10,6 +10,7 @@ namespace PhpOffice\PhpWord\Tests\Section; use PhpOffice\PhpWord\Section\CheckBox; +use PhpOffice\PhpWord\Style\Font; /** * Test class for PhpOffice\PhpWord\Section\CheckBox @@ -54,6 +55,16 @@ class CheckBoxTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $oCheckBox->getFontStyle()); } + /** + * Font style as object + */ + public function testFontObject() + { + $font = new Font(); + $oCheckBox = new CheckBox('chkBox', 'CheckBox', $font); + $this->assertEquals($oCheckBox->getFontStyle(), $font); + } + /** * Get paragraph style */ diff --git a/tests/PhpWord/Tests/Section/FooterTest.php b/tests/PhpWord/Tests/Section/FooterTest.php index 6ae3bc60..d14f125c 100644 --- a/tests/PhpWord/Tests/Section/FooterTest.php +++ b/tests/PhpWord/Tests/Section/FooterTest.php @@ -14,11 +14,13 @@ use PhpOffice\PhpWord\Section\Footer; /** * Test class for PhpOffice\PhpWord\Section\Footer * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Footer * @runTestsInSeparateProcesses */ class FooterTest extends \PHPUnit_Framework_TestCase { + /** + * New instance + */ public function testConstruct() { $iVal = rand(1, 1000); @@ -28,15 +30,9 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oFooter->getFooterCount(), $iVal); } - public function testRelationID() - { - $oFooter = new Footer(0); - - $iVal = rand(1, 1000); - $oFooter->setRelationId($iVal); - $this->assertEquals($oFooter->getRelationId(), $iVal); - } - + /** + * Add text + */ public function testAddText() { $oFooter = new Footer(1); @@ -46,6 +42,9 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $element); } + /** + * Add text non-UTF8 + */ public function testAddTextNotUTF8() { $oFooter = new Footer(1); @@ -56,6 +55,9 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), 'ééé'); } + /** + * Add text break + */ public function testAddTextBreak() { $oFooter = new Footer(1); @@ -65,6 +67,9 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertCount($iVal, $oFooter->getElements()); } + /** + * Add text run + */ public function testCreateTextRun() { $oFooter = new Footer(1); @@ -74,6 +79,9 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\TextRun', $element); } + /** + * Add table + */ public function testAddTable() { $oFooter = new Footer(1); @@ -83,16 +91,23 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Table', $element); } + /** + * Add image + */ public function testAddImage() { $src = __DIR__ . "/../_files/images/earth.jpg"; $oFooter = new Footer(1); - $element = $oFooter->addImage($src); + $element1 = $oFooter->addImage($src); + $element2 = $oFooter->addMemoryImage($src); // @deprecated - $this->assertCount(1, $oFooter->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); + $this->assertCount(2, $oFooter->getElements()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element1); } + /** + * Add image by URL + */ public function testAddImageByUrl() { $oFooter = new Footer(1); @@ -104,6 +119,9 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); } + /** + * Add preserve text + */ public function testAddPreserveText() { $oFooter = new Footer(1); @@ -113,6 +131,9 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Footer\\PreserveText', $element); } + /** + * Add preserve text non-UTF8 + */ public function testAddPreserveTextNotUTF8() { $oFooter = new Footer(1); @@ -123,10 +144,25 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), array('ééé')); } + /** + * Get elements + */ public function testGetElements() { $oFooter = new Footer(1); $this->assertInternalType('array', $oFooter->getElements()); } + + /** + * Set/get relation Id + */ + public function testRelationID() + { + $oFooter = new Footer(0); + + $iVal = rand(1, 1000); + $oFooter->setRelationId($iVal); + $this->assertEquals($oFooter->getRelationId(), $iVal); + } } diff --git a/tests/PhpWord/Tests/Section/FootnoteTest.php b/tests/PhpWord/Tests/Section/FootnoteTest.php index ed2e36de..fc537ab8 100644 --- a/tests/PhpWord/Tests/Section/FootnoteTest.php +++ b/tests/PhpWord/Tests/Section/FootnoteTest.php @@ -14,15 +14,12 @@ use PhpOffice\PhpWord\Section\Footnote; /** * Test class for PhpOffice\PhpWord\Section\Footnote * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Footnote * @runTestsInSeparateProcesses */ class FootnoteTest extends \PHPUnit_Framework_TestCase { /** * New instance without parameter - * - * @covers ::__construct */ public function testConstruct() { @@ -35,8 +32,6 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase /** * New instance with string parameter - * - * @covers ::__construct */ public function testConstructString() { @@ -47,8 +42,6 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase /** * New instance with array parameter - * - * @covers ::__construct */ public function testConstructArray() { @@ -62,8 +55,6 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase /** * Add text element - * - * @covers ::addText */ public function testAddText() { @@ -76,8 +67,6 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase /** * Add text break element - * - * @covers ::addTextBreak */ public function testAddTextBreak() { @@ -89,8 +78,6 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase /** * Add link element - * - * @covers ::addLink */ public function testAddLink() { @@ -103,9 +90,6 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase /** * Set/get reference Id - * - * @covers ::setReferenceId - * @covers ::getReferenceId */ public function testReferenceId() { @@ -118,8 +102,6 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase /** * Get elements - * - * @covers ::getElements */ public function testGetElements() { diff --git a/tests/PhpWord/Tests/Section/HeaderTest.php b/tests/PhpWord/Tests/Section/HeaderTest.php index 795b49c3..edc5d2c6 100644 --- a/tests/PhpWord/Tests/Section/HeaderTest.php +++ b/tests/PhpWord/Tests/Section/HeaderTest.php @@ -14,11 +14,13 @@ use PhpOffice\PhpWord\Section\Header; /** * Test class for PhpOffice\PhpWord\Section\Header * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Header * @runTestsInSeparateProcesses */ class HeaderTest extends \PHPUnit_Framework_TestCase { + /** + * New instance + */ public function testConstructDefault() { $iVal = rand(1, 1000); @@ -29,6 +31,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oHeader->getType(), Header::AUTO); } + /** + * Add text + */ public function testAddText() { $oHeader = new Header(1); @@ -39,6 +44,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), 'text'); } + /** + * Add text non-UTF8 + */ public function testAddTextNotUTF8() { $oHeader = new Header(1); @@ -49,6 +57,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), 'ééé'); } + /** + * Add text break + */ public function testAddTextBreak() { $oHeader = new Header(1); @@ -56,6 +67,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertCount(1, $oHeader->getElements()); } + /** + * Add text break with params + */ public function testAddTextBreakWithParams() { $oHeader = new Header(1); @@ -64,6 +78,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertCount($iVal, $oHeader->getElements()); } + /** + * Add text run + */ public function testCreateTextRun() { $oHeader = new Header(1); @@ -72,6 +89,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertCount(1, $oHeader->getElements()); } + /** + * Add table + */ public function testAddTable() { $oHeader = new Header(1); @@ -80,16 +100,23 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertCount(1, $oHeader->getElements()); } + /** + * Add image + */ public function testAddImage() { $src = __DIR__ . "/../_files/images/earth.jpg"; $oHeader = new Header(1); - $element = $oHeader->addImage($src); + $element1 = $oHeader->addImage($src); + $element2 = $oHeader->addMemoryImage($src); // @deprecated - $this->assertCount(1, $oHeader->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); + $this->assertCount(2, $oHeader->getElements()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element1); } + /** + * Add image by URL + */ public function testAddImageByUrl() { $oHeader = new Header(1); @@ -101,6 +128,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); } + /** + * Add preserve text + */ public function testAddPreserveText() { $oHeader = new Header(1); @@ -110,6 +140,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Footer\\PreserveText', $element); } + /** + * Add preserve text non-UTF8 + */ public function testAddPreserveTextNotUTF8() { $oHeader = new Header(1); @@ -120,6 +153,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), array('ééé')); } + /** + * Add watermark + */ public function testAddWatermark() { $src = __DIR__ . "/../_files/images/earth.jpg"; @@ -130,6 +166,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); } + /** + * Get elements + */ public function testGetElements() { $oHeader = new Header(1); @@ -137,6 +176,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertInternalType('array', $oHeader->getElements()); } + /** + * Set/get relation Id + */ public function testRelationId() { $oHeader = new Header(1); @@ -146,6 +188,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oHeader->getRelationId(), $iVal); } + /** + * Reset type + */ public function testResetType() { $oHeader = new Header(1); @@ -155,6 +200,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oHeader->getType(), Header::AUTO); } + /** + * First page + */ public function testFirstPage() { $oHeader = new Header(1); @@ -163,6 +211,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oHeader->getType(), Header::FIRST); } + /** + * Even page + */ public function testEvenPage() { $oHeader = new Header(1); diff --git a/tests/PhpWord/Tests/Section/ImageTest.php b/tests/PhpWord/Tests/Section/ImageTest.php index 7e4a25b4..bd4bf394 100644 --- a/tests/PhpWord/Tests/Section/ImageTest.php +++ b/tests/PhpWord/Tests/Section/ImageTest.php @@ -14,11 +14,13 @@ use PhpOffice\PhpWord\Section\Image; /** * Test class for PhpOffice\PhpWord\Section\Image * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Image * @runTestsInSeparateProcesses */ class ImageTest extends \PHPUnit_Framework_TestCase { + /** + * New instance + */ public function testConstruct() { $src = __DIR__ . "/../_files/images/firefox.png"; @@ -31,6 +33,9 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oImage->getStyle()); } + /** + * New instance with style + */ public function testConstructWithStyle() { $src = __DIR__ . "/../_files/images/firefox.png"; @@ -44,7 +49,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase } /** - * @covers ::__construct + * Valid image types */ public function testValidImageTypes() { @@ -57,8 +62,9 @@ class ImageTest extends \PHPUnit_Framework_TestCase } /** + * Image not found + * * @expectedException \PhpOffice\PhpWord\Exceptions\InvalidImageException - * @covers ::__construct */ public function testImageNotFound() { @@ -66,14 +72,18 @@ class ImageTest extends \PHPUnit_Framework_TestCase } /** + * Invalid image types + * * @expectedException \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException - * @covers ::__construct */ public function testInvalidImageTypes() { new Image(__DIR__ . "/../_files/images/alexz-johnson.pcx"); } + /** + * Get style + */ public function testStyle() { $oImage = new Image( @@ -84,6 +94,9 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oImage->getStyle()); } + /** + * Get relation Id + */ public function testRelationID() { $oImage = new Image(__DIR__ . "/../_files/images/earth.jpg"); @@ -92,12 +105,19 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oImage->getRelationId(), $iVal); } + /** + * Get is watermark + */ public function testWatermark() { $oImage = new Image(__DIR__ . "/../_files/images/earth.jpg"); $oImage->setIsWatermark(true); $this->assertEquals($oImage->getIsWatermark(), true); } + + /** + * Test PNG + */ public function testPNG() { $src = __DIR__ . "/../_files/images/firefox.png"; @@ -112,6 +132,9 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oImage->getImageType(), 'image/png'); } + /** + * Test GIF + */ public function testGIF() { $src = __DIR__ . "/../_files/images/mario.gif"; @@ -126,6 +149,9 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oImage->getImageType(), 'image/gif'); } + /** + * Test JPG + */ public function testJPG() { $src = __DIR__ . "/../_files/images/earth.jpg"; @@ -140,6 +166,9 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oImage->getImageType(), 'image/jpeg'); } + /** + * Test BMP + */ public function testBMP() { $oImage = new Image(__DIR__ . "/../_files/images/duke_nukem.bmp"); @@ -150,4 +179,17 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oImage->getImageExtension(), 'bmp'); $this->assertEquals($oImage->getImageType(), 'image/bmp'); } + + /** + * Test TIFF + */ + public function testTIFF() + { + $oImage = new Image(__DIR__ . "/../_files/images/angela_merkel.tif"); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $oImage); + $this->assertEquals($oImage->getImageCreateFunction(), null); + $this->assertEquals($oImage->getImageFunction(), null); + $this->assertEquals($oImage->getImageType(), 'image/tiff'); + } } diff --git a/tests/PhpWord/Tests/Section/Table/CellTest.php b/tests/PhpWord/Tests/Section/Table/CellTest.php index fc0c0a97..6f38d986 100644 --- a/tests/PhpWord/Tests/Section/Table/CellTest.php +++ b/tests/PhpWord/Tests/Section/Table/CellTest.php @@ -14,11 +14,13 @@ use PhpOffice\PhpWord\Section\Table\Cell; /** * Test class for PhpOffice\PhpWord\Section\Table\Cell * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Table\Cell * @runTestsInSeparateProcesses */ class CellTest extends \PHPUnit_Framework_TestCase { + /** + * New instance + */ public function testConstruct() { $iVal = rand(1, 1000); @@ -28,6 +30,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oCell->getWidth(), null); } + /** + * New instance with array + */ public function testConstructWithStyleArray() { $iVal = rand(1, 1000); @@ -37,6 +42,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oCell->getWidth(), null); } + /** + * New instance with string + */ public function testConstructWithStyleString() { $iVal = rand(1, 1000); @@ -45,6 +53,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oCell->getStyle(), 'cellStyle'); } + /** + * Add text + */ public function testAddText() { $oCell = new Cell('section', 1); @@ -54,6 +65,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $element); } + /** + * Add non-UTF8 + */ public function testAddTextNotUTF8() { $oCell = new Cell('section', 1); @@ -64,15 +78,31 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), 'ééé'); } + /** + * Add link + */ public function testAddLink() { $oCell = new Cell('section', 1); - $element = $oCell->addLink('http://www.google.fr', 'Nom'); + $element = $oCell->addLink(utf8_decode('ééé'), utf8_decode('ééé')); $this->assertCount(1, $oCell->getElements()); $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Link', $element); } + /** + * Add link exception + * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + */ + public function testAddLinkException() + { + $oCell = new Cell('header', 1); + $element = $oCell->addLink('http://google.com', 'Google'); + } + + /** + * Add text break + */ public function testAddTextBreak() { $oCell = new Cell('section', 1); @@ -81,6 +111,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertCount(1, $oCell->getElements()); } + /** + * Add list item + */ public function testAddListItem() { $oCell = new Cell('section', 1); @@ -91,6 +124,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getTextObject()->getText(), 'text'); } + /** + * Add list item non-UTF8 + */ public function testAddListItemNotUTF8() { $oCell = new Cell('section', 1); @@ -101,16 +137,23 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getTextObject()->getText(), 'ééé'); } + /** + * Add image section + */ public function testAddImageSection() { $src = __DIR__ . "/../../_files/images/earth.jpg"; $oCell = new Cell('section', 1); - $element = $oCell->addImage($src); + $element1 = $oCell->addImage($src); + $element2 = $oCell->addMemoryImage($src); // @deprecated - $this->assertCount(1, $oCell->getElements()); - $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); + $this->assertCount(2, $oCell->getElements()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element1); } + /** + * Add image header + */ public function testAddImageHeader() { $src = __DIR__ . "/../../_files/images/earth.jpg"; @@ -121,6 +164,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); } + /** + * Add image footer + */ public function testAddImageFooter() { $src = __DIR__ . "/../../_files/images/earth.jpg"; @@ -131,7 +177,10 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); } - public function testAddSectionImageByUrl() + /** + * Add image section by URL + */ + public function testAddImageSectionByUrl() { $oCell = new Cell('section', 1); $element = $oCell->addImage( @@ -142,7 +191,10 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); } - public function testAddHeaderImageByUrl() + /** + * Add image header by URL + */ + public function testAddImageHeaderByUrl() { $oCell = new Cell('header', 1); $element = $oCell->addImage( @@ -153,7 +205,10 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); } - public function testAddFooterImageByUrl() + /** + * Add image footer by URL + */ + public function testAddImageFooterByUrl() { $oCell = new Cell('footer', 1); $element = $oCell->addImage( @@ -164,6 +219,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element); } + /** + * Add object + */ public function testAddObjectXLS() { $src = __DIR__ . "/../../_files/documents/sheet.xls"; @@ -174,6 +232,21 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Object', $element); } + /** + * Test add object exception + * + * @expectedException \PhpOffice\PhpWord\Exceptions\InvalidObjectException + */ + public function testAddObjectException() + { + $src = __DIR__ . "/_files/xsl/passthrough.xsl"; + $oCell = new Cell('section', 1); + $element = $oCell->addObject($src); + } + + /** + * Add preserve text + */ public function testAddPreserveText() { $oCell = new Cell('header', 1); @@ -183,6 +256,9 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Footer\\PreserveText', $element); } + /** + * Add preserve text non-UTF8 + */ public function testAddPreserveTextNotUTF8() { $oCell = new Cell('header', 1); @@ -193,6 +269,20 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), array('ééé')); } + /** + * Add preserve text exception + * + * @expectedException \PhpOffice\PhpWord\Exceptions\Exception + */ + public function testAddPreserveTextException() + { + $oCell = new Cell('section', 1); + $element = $oCell->addPreserveText('text'); + } + + /** + * Add text run + */ public function testCreateTextRun() { $oCell = new Cell('section', 1); @@ -202,16 +292,21 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\TextRun', $element); } + /** + * Add check box + */ public function testAddCheckBox() { $oCell = new Cell('section', 1); - $element = $oCell->addCheckBox('check1', 'text'); + $element = $oCell->addCheckBox(utf8_decode('ééé'), utf8_decode('ééé')); $this->assertCount(1, $oCell->getElements()); $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\CheckBox', $element); } - + /** + * Get elements + */ public function testGetElements() { $oCell = new Cell('section', 1); diff --git a/tests/PhpWord/Tests/Section/TextRunTest.php b/tests/PhpWord/Tests/Section/TextRunTest.php index dea3fcdd..32b6d4dc 100644 --- a/tests/PhpWord/Tests/Section/TextRunTest.php +++ b/tests/PhpWord/Tests/Section/TextRunTest.php @@ -14,11 +14,13 @@ use PhpOffice\PhpWord\Section\TextRun; /** * Test class for PhpOffice\PhpWord\Section\TextRun * - * @coversDefaultClass \PhpOffice\PhpWord\Section\TextRun * @runTestsInSeparateProcesses */ class TextRunTest extends \PHPUnit_Framework_TestCase { + /** + * New instance + */ public function testConstructNull() { $oTextRun = new TextRun(); @@ -28,6 +30,9 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oTextRun->getParagraphStyle(), null); } + /** + * New instance with string + */ public function testConstructString() { $oTextRun = new TextRun('pStyle'); @@ -37,6 +42,9 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oTextRun->getParagraphStyle(), 'pStyle'); } + /** + * New instance with array + */ public function testConstructArray() { $oTextRun = new TextRun(array('spacing' => 100)); @@ -46,6 +54,9 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle()); } + /** + * Add text + */ public function testAddText() { $oTextRun = new TextRun(); @@ -56,6 +67,9 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), 'text'); } + /** + * Add text non-UTF8 + */ public function testAddTextNotUTF8() { $oTextRun = new TextRun(); @@ -66,6 +80,9 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getText(), 'ééé'); } + /** + * Add link + */ public function testAddLink() { $oTextRun = new TextRun(); @@ -76,6 +93,9 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getLinkSrc(), 'http://www.google.fr'); } + /** + * Add link with name + */ public function testAddLinkWithName() { $oTextRun = new TextRun(); @@ -87,6 +107,20 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertEquals($element->getLinkName(), 'ééé'); } + /** + * Add text break + */ + public function testAddTextBreak() + { + $oTextRun = new TextRun(); + $element = $oTextRun->addTextBreak(2); + + $this->assertCount(2, $oTextRun->getElements()); + } + + /** + * Add image + */ public function testAddImage() { $src = __DIR__ . "/../_files/images/earth.jpg"; @@ -98,6 +132,9 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertCount(1, $oTextRun->getElements()); } + /** + * Add footnote + */ public function testCreateFootnote() { $oTextRun = new TextRun(); diff --git a/tests/PhpWord/Tests/Section/TextTest.php b/tests/PhpWord/Tests/Section/TextTest.php index 4124f27a..5811c735 100644 --- a/tests/PhpWord/Tests/Section/TextTest.php +++ b/tests/PhpWord/Tests/Section/TextTest.php @@ -10,15 +10,18 @@ namespace PhpOffice\PhpWord\Tests\Section; use PhpOffice\PhpWord\Section\Text; +use PhpOffice\PhpWord\Style\Font; /** * Test class for PhpOffice\PhpWord\Section\Text * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Text * @runTestsInSeparateProcesses */ class TextTest extends \PHPUnit_Framework_TestCase { + /** + * New instance + */ public function testConstruct() { $oText = new Text(); @@ -29,6 +32,9 @@ class TextTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oText->getParagraphStyle()); } + /** + * Get text + */ public function testText() { $oText = new Text('text'); @@ -36,6 +42,9 @@ class TextTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oText->getText(), 'text'); } + /** + * Get font style + */ public function testFont() { $oText = new Text('text', 'fontStyle'); @@ -45,6 +54,19 @@ class TextTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $oText->getFontStyle()); } + /** + * Get font style as object + */ + public function testFontObject() + { + $font = new Font(); + $oText = new Text('text', $font); + $this->assertEquals($oText->getFontStyle(), $font); + } + + /** + * Get paragraph style + */ public function testParagraph() { $oText = new Text('text', 'fontStyle', 'paragraphStyle'); diff --git a/tests/PhpWord/Tests/SectionTest.php b/tests/PhpWord/Tests/SectionTest.php index e6205db3..48b5bb73 100644 --- a/tests/PhpWord/Tests/SectionTest.php +++ b/tests/PhpWord/Tests/SectionTest.php @@ -88,7 +88,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase $section->addTitle(utf8_decode('ä'), 1); $section->createTextRun(); $section->createFootnote(); - $section->addCheckBox('check1', utf8_decode('ä')); + $section->addCheckBox(utf8_decode('chkä'), utf8_decode('Contentä')); $section->addTOC(); $elementCollection = $section->getElements(); diff --git a/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php b/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php index f0b44e0b..5318ac81 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php @@ -9,6 +9,7 @@ namespace PhpOffice\PhpWord\Tests\Writer\Word2007; use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Tests\TestHelperDOCX; /** @@ -33,7 +34,11 @@ class DocumentTest extends \PHPUnit_Framework_TestCase { $phpWord = new PhpWord(); $section = $phpWord->createSection(); - $section->getSettings()->setPageNumberingStart(2); + $settings = $section->getSettings(); + $settings->setLandscape(); + $settings->setPageNumberingStart(2); + $settings->setBorderSize(240); + $settings->setBreakType('nextPage'); $doc = TestHelperDOCX::getDocument($phpWord); $element = $doc->getElement('/w:document/w:body/w:sectPr/w:pgNumType'); @@ -85,4 +90,39 @@ class DocumentTest extends \PHPUnit_Framework_TestCase $element = $doc->getElement('/w:document/w:body/w:p[11]/w:r/w:object/o:OLEObject'); $this->assertEquals('Embed', $element->getAttribute('Type')); } + + /** + * Write element with some styles + */ + public function testElementStyles() + { + $objectSrc = __DIR__ . "/../../_files/documents/sheet.xls"; + + $phpWord = new PhpWord(); + $phpWord->addParagraphStyle('pStyle', array('align' => 'center')); + $phpWord->addFontStyle('fStyle', array('size' => '20')); + $phpWord->addTitleStyle(1, array('color' => '333333', 'bold' => true)); + $fontStyle = new Font('text', array('align' => 'center')); + $section = $phpWord->createSection(); + $section->addListItem('List Item', 0, null, null, 'pStyle'); + $section->addObject($objectSrc, array('align' => 'center')); + $section->addTOC($fontStyle); + $section->addTitle('Title 1', 1); + $section->addTOC('fStyle'); + $doc = TestHelperDOCX::getDocument($phpWord); + + // List item + $element = $doc->getElement('/w:document/w:body/w:p[1]/w:pPr/w:numPr/w:numId'); + $this->assertEquals(3, $element->getAttribute('w:val')); + + // Object + $element = $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:object/o:OLEObject'); + $this->assertEquals('Embed', $element->getAttribute('Type')); + + // TOC + $element = $doc->getElement('/w:document/w:body/w:p[3]/w:pPr/w:tabs/w:tab'); + $this->assertEquals('right', $element->getAttribute('w:val')); + $this->assertEquals('dot', $element->getAttribute('w:leader')); + $this->assertEquals(9062, $element->getAttribute('w:pos')); + } } diff --git a/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php b/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php index 06906b41..a1220d26 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php @@ -30,11 +30,15 @@ class FootnotesTest extends \PHPUnit_Framework_TestCase public function testWriteFootnotes() { $phpWord = new PhpWord(); + $phpWord->addParagraphStyle('pStyle', array('align' => 'left')); $section = $phpWord->createSection(); $section->addText('Text'); - $footnote = $section->createFootnote(); - $footnote->addText('Footnote'); - $footnote->addLink('http://google.com'); + $footnote1 = $section->createFootnote('pStyle'); + $footnote1->addText('Footnote'); + $footnote1->addTextBreak(); + $footnote1->addLink('http://google.com'); + $footnote2 = $section->createFootnote(array('align' => 'left')); + $footnote2->addText('Footnote'); $doc = TestHelperDOCX::getDocument($phpWord); $this->assertTrue($doc->elementExists("/w:document/w:body/w:p/w:r/w:footnoteReference")); diff --git a/tests/PhpWord/Tests/Writer/Word2007Test.php b/tests/PhpWord/Tests/Writer/Word2007Test.php index 22a1c0df..fa4e301c 100644 --- a/tests/PhpWord/Tests/Writer/Word2007Test.php +++ b/tests/PhpWord/Tests/Writer/Word2007Test.php @@ -99,6 +99,8 @@ class Word2007Test extends \PHPUnit_Framework_TestCase $phpWord = new PhpWord(); $section = $phpWord->createSection(); $section->addText('Test'); + $footnote = $section->createFootnote(); + $footnote->addText('Test'); $writer = new Word2007($phpWord); $writer->setUseDiskCaching(true); From 0e2f476cc2818aaad12be1076efd2ed792d9d3be Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 30 Mar 2014 14:15:23 +0700 Subject: [PATCH 15/22] Docblock updates --- samples/Sample_Footer.php | 6 +- samples/Sample_Header.php | 10 +-- src/PhpWord/Section.php | 5 +- src/PhpWord/Section/CheckBox.php | 6 +- src/PhpWord/Section/Table/Cell.php | 5 +- src/PhpWord/Writer/Word2007/Base.php | 3 + .../Tests/Exceptions/ExceptionTest.php | 2 + .../Exceptions/InvalidImageExceptionTest.php | 2 + .../Exceptions/InvalidStyleExceptionTest.php | 2 + .../UnsupportedImageTypeExceptionTest.php | 2 + tests/PhpWord/Tests/Reader/Word2007Test.php | 5 ++ .../Tests/Section/Footer/PreserveTextTest.php | 10 ++- tests/PhpWord/Tests/Section/LinkTest.php | 12 ++++ tests/PhpWord/Tests/Section/ListItemTest.php | 9 +++ tests/PhpWord/Tests/Section/ObjectTest.php | 18 ++++++ tests/PhpWord/Tests/Section/SettingsTest.php | 34 +++++++++- tests/PhpWord/Tests/Section/Table/RowTest.php | 9 +++ tests/PhpWord/Tests/Section/TableTest.php | 18 ++++++ tests/PhpWord/Tests/Section/TitleTest.php | 15 +++++ tests/PhpWord/Tests/Shared/StringTest.php | 9 +++ tests/PhpWord/Tests/Style/FontTest.php | 4 +- tests/PhpWord/Tests/Style/ParagraphTest.php | 7 ++- tests/PhpWord/Tests/TemplateTest.php | 1 + .../Tests/Writer/Word2007/FooterTest.php | 2 + .../Tests/Writer/Word2007/FootnotesTest.php | 3 + .../Tests/Writer/Word2007/HeaderTest.php | 3 +- .../Tests/_includes/TestHelperDOCX.php | 26 +++++++- tests/PhpWord/Tests/_includes/XmlDocument.php | 63 ++++++++++++++++--- tests/bootstrap.php | 11 +++- 29 files changed, 271 insertions(+), 31 deletions(-) diff --git a/samples/Sample_Footer.php b/samples/Sample_Footer.php index 5c7d7da1..4d5777c2 100644 --- a/samples/Sample_Footer.php +++ b/samples/Sample_Footer.php @@ -3,7 +3,7 @@ * Footer file */ // Do not show execution time for index -if (!$isIndexFile) { +if (!IS_INDEX) { echo date('H:i:s'), " Done writing file(s)", EOL; echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL; } @@ -11,12 +11,12 @@ if (!$isIndexFile) { if (CLI) { echo 'The results are stored in the "results" subdirectory.', EOL; } else { - if (!$isIndexFile) { + if (!IS_INDEX) { $types = array('docx', 'odt', 'rtf'); echo '

 

'; echo '

Results: '; foreach ($types as $type) { - $result = "results/{$sampleFile}.{$type}"; + $result = 'results/' . SCRIPT_FILENAME . '.' . $type; if (file_exists($result)) { echo "{$type} "; } diff --git a/samples/Sample_Header.php b/samples/Sample_Header.php index 28ae61e7..b61e415a 100644 --- a/samples/Sample_Header.php +++ b/samples/Sample_Header.php @@ -5,6 +5,8 @@ error_reporting(E_ALL); define('CLI', (PHP_SAPI == 'cli') ? true : false); define('EOL', CLI ? PHP_EOL : '
'); +define('SCRIPT_FILENAME', basename($_SERVER['SCRIPT_FILENAME'], '.php')); +define('IS_INDEX', SCRIPT_FILENAME == 'index'); require_once '../src/PhpWord/Autoloader.php'; PhpOffice\PhpWord\Autoloader::register(); @@ -15,12 +17,10 @@ if (CLI) { } // Set titles and names -$sampleFile = basename($_SERVER['SCRIPT_FILENAME'], '.php'); -$isIndexFile = ($sampleFile == 'index'); -$pageHeading = str_replace('_', ' ', $sampleFile); -$pageTitle = $isIndexFile ? 'Welcome to ' : "{$pageHeading} - "; +$pageHeading = str_replace('_', ' ', SCRIPT_FILENAME); +$pageTitle = IS_INDEX ? 'Welcome to ' : "{$pageHeading} - "; $pageTitle .= 'PHPWord'; -$pageHeading = $isIndexFile ? '' : "

{$pageHeading}

"; +$pageHeading = IS_INDEX ? '' : "

{$pageHeading}

"; // Populate samples $files = ''; if ($handle = opendir('.')) { diff --git a/src/PhpWord/Section.php b/src/PhpWord/Section.php index 1649ce96..e350f7f8 100644 --- a/src/PhpWord/Section.php +++ b/src/PhpWord/Section.php @@ -421,8 +421,9 @@ class Section * * @param string $name * @param string $text - * @param mixed $style - * @return PHPWord_Section_CheckBox + * @param mixed $styleFont + * @param mixed $styleParagraph + * @return \PhpOffice\PhpWord\Section\CheckBox */ public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null) { diff --git a/src/PhpWord/Section/CheckBox.php b/src/PhpWord/Section/CheckBox.php index 57671b84..00f75e54 100644 --- a/src/PhpWord/Section/CheckBox.php +++ b/src/PhpWord/Section/CheckBox.php @@ -129,6 +129,8 @@ class CheckBox } /** + * Set name content + * * @param string $name * @return $this */ @@ -149,6 +151,8 @@ class CheckBox } /** + * Set text content + * * @param string $text * @return $this */ @@ -159,7 +163,7 @@ class CheckBox } /** - * Get Text content + * Get text content * * @return string */ diff --git a/src/PhpWord/Section/Table/Cell.php b/src/PhpWord/Section/Table/Cell.php index 45d43ece..e0004350 100755 --- a/src/PhpWord/Section/Table/Cell.php +++ b/src/PhpWord/Section/Table/Cell.php @@ -296,8 +296,9 @@ class Cell * * @param string $name * @param string $text - * @param mixed $style - * @return PHPWord_Section_CheckBox + * @param mixed $styleFont + * @param mixed $styleParagraph + * @return \PhpOffice\PhpWord\Section\CheckBox */ public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null) { diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index c32eede3..644bf2a2 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -1220,6 +1220,9 @@ class Base extends WriterPart /** * Write CheckBox + * + * @param boolean $withoutP + * @param boolean $checkState */ protected function _writeCheckBox(XMLWriter $xmlWriter, CheckBox $checkbox, $withoutP = false, $checkState = false) { diff --git a/tests/PhpWord/Tests/Exceptions/ExceptionTest.php b/tests/PhpWord/Tests/Exceptions/ExceptionTest.php index 2810c054..81732c97 100644 --- a/tests/PhpWord/Tests/Exceptions/ExceptionTest.php +++ b/tests/PhpWord/Tests/Exceptions/ExceptionTest.php @@ -20,6 +20,8 @@ use PhpOffice\PhpWord\Exceptions\Exception; class ExceptionTest extends \PHPUnit_Framework_TestCase { /** + * Throw new exception + * * @expectedException \PhpOffice\PhpWord\Exceptions\Exception * @covers \PhpOffice\PhpWord\Exceptions\Exception */ diff --git a/tests/PhpWord/Tests/Exceptions/InvalidImageExceptionTest.php b/tests/PhpWord/Tests/Exceptions/InvalidImageExceptionTest.php index 4ae7c7f0..7db70993 100644 --- a/tests/PhpWord/Tests/Exceptions/InvalidImageExceptionTest.php +++ b/tests/PhpWord/Tests/Exceptions/InvalidImageExceptionTest.php @@ -20,6 +20,8 @@ use PhpOffice\PhpWord\Exceptions\InvalidImageException; class InvalidImageExceptionTest extends \PHPUnit_Framework_TestCase { /** + * Throw new exception + * * @expectedException \PhpOffice\PhpWord\Exceptions\InvalidImageException * @covers \PhpOffice\PhpWord\Exceptions\InvalidImageException */ diff --git a/tests/PhpWord/Tests/Exceptions/InvalidStyleExceptionTest.php b/tests/PhpWord/Tests/Exceptions/InvalidStyleExceptionTest.php index bb782edc..174e07ac 100644 --- a/tests/PhpWord/Tests/Exceptions/InvalidStyleExceptionTest.php +++ b/tests/PhpWord/Tests/Exceptions/InvalidStyleExceptionTest.php @@ -20,6 +20,8 @@ use PhpOffice\PhpWord\Exceptions\InvalidStyleException; class InvalidStyleExceptionTest extends \PHPUnit_Framework_TestCase { /** + * Throw new exception + * * @expectedException \PhpOffice\PhpWord\Exceptions\InvalidStyleException * @covers \PhpOffice\PhpWord\Exceptions\InvalidStyleException */ diff --git a/tests/PhpWord/Tests/Exceptions/UnsupportedImageTypeExceptionTest.php b/tests/PhpWord/Tests/Exceptions/UnsupportedImageTypeExceptionTest.php index ab0d25cc..027ec3a9 100644 --- a/tests/PhpWord/Tests/Exceptions/UnsupportedImageTypeExceptionTest.php +++ b/tests/PhpWord/Tests/Exceptions/UnsupportedImageTypeExceptionTest.php @@ -20,6 +20,8 @@ use PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException; class UnsupportedImageTypeExceptionTest extends \PHPUnit_Framework_TestCase { /** + * Throw new exception + * * @expectedException \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException * @covers \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException */ diff --git a/tests/PhpWord/Tests/Reader/Word2007Test.php b/tests/PhpWord/Tests/Reader/Word2007Test.php index 768f6b6e..5aee8144 100644 --- a/tests/PhpWord/Tests/Reader/Word2007Test.php +++ b/tests/PhpWord/Tests/Reader/Word2007Test.php @@ -41,6 +41,8 @@ class Word2007Test extends \PHPUnit_Framework_TestCase } /** + * Can read exception + * * @expectedException \PhpOffice\PhpWord\Exceptions\Exception */ public function testCanReadFailed() @@ -54,6 +56,9 @@ class Word2007Test extends \PHPUnit_Framework_TestCase $object = IOFactory::load($fqFilename); } + /** + * Load + */ public function testLoad() { $fqFilename = join( diff --git a/tests/PhpWord/Tests/Section/Footer/PreserveTextTest.php b/tests/PhpWord/Tests/Section/Footer/PreserveTextTest.php index e78d2100..82ded881 100644 --- a/tests/PhpWord/Tests/Section/Footer/PreserveTextTest.php +++ b/tests/PhpWord/Tests/Section/Footer/PreserveTextTest.php @@ -14,11 +14,13 @@ use PhpOffice\PhpWord\Section\Footer\PreserveText; /** * Test class for PhpOffice\PhpWord\Section\Footer\PreserveText * - * @coversDefaultClass \PhpOffice\PhpWord\Section\Footer\PreserveText * @runTestsInSeparateProcesses */ class PreserveTextTest extends \PHPUnit_Framework_TestCase { + /** + * Create new instance + */ public function testConstruct() { $oPreserveText = new PreserveText(); @@ -29,6 +31,9 @@ class PreserveTextTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oPreserveText->getParagraphStyle(), null); } + /** + * Create new instance with style name + */ public function testConstructWithString() { $oPreserveText = new PreserveText('text', 'styleFont', 'styleParagraph'); @@ -37,6 +42,9 @@ class PreserveTextTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oPreserveText->getParagraphStyle(), 'styleParagraph'); } + /** + * Create new instance with array + */ public function testConstructWithArray() { $oPreserveText = new PreserveText( diff --git a/tests/PhpWord/Tests/Section/LinkTest.php b/tests/PhpWord/Tests/Section/LinkTest.php index 2e7c05b0..ae1a3e09 100644 --- a/tests/PhpWord/Tests/Section/LinkTest.php +++ b/tests/PhpWord/Tests/Section/LinkTest.php @@ -20,6 +20,9 @@ use PhpOffice\PhpWord\Style\Font; */ class LinkTest extends \PHPUnit_Framework_TestCase { + /** + * Create new instance + */ public function testConstructDefault() { $oLink = new Link('http://www.google.com'); @@ -31,6 +34,9 @@ class LinkTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oLink->getParagraphStyle(), null); } + /** + * Create new instance with array + */ public function testConstructWithParamsArray() { $oLink = new Link( @@ -47,6 +53,9 @@ class LinkTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oLink->getParagraphStyle()); } + /** + * Create new instance with style name string + */ public function testConstructWithParamsString() { $oLink = new Link('http://www.google.com', null, 'fontStyle', 'paragraphStyle'); @@ -55,6 +64,9 @@ class LinkTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oLink->getParagraphStyle(), 'paragraphStyle'); } + /** + * Set/get relation Id + */ public function testRelationId() { $oLink = new Link('http://www.google.com'); diff --git a/tests/PhpWord/Tests/Section/ListItemTest.php b/tests/PhpWord/Tests/Section/ListItemTest.php index 42af449a..1964cdac 100644 --- a/tests/PhpWord/Tests/Section/ListItemTest.php +++ b/tests/PhpWord/Tests/Section/ListItemTest.php @@ -19,6 +19,9 @@ use PhpOffice\PhpWord\Section\ListItem; */ class ListItemTest extends \PHPUnit_Framework_TestCase { + /** + * Get text object + */ public function testText() { $oListItem = new ListItem('text'); @@ -26,6 +29,9 @@ class ListItemTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $oListItem->getTextObject()); } + /** + * Get style + */ public function testStyle() { $oListItem = new ListItem( @@ -42,6 +48,9 @@ class ListItemTest extends \PHPUnit_Framework_TestCase ); } + /** + * Get depth + */ public function testDepth() { $iVal = rand(1, 1000); diff --git a/tests/PhpWord/Tests/Section/ObjectTest.php b/tests/PhpWord/Tests/Section/ObjectTest.php index a68b6a0f..d6094de1 100644 --- a/tests/PhpWord/Tests/Section/ObjectTest.php +++ b/tests/PhpWord/Tests/Section/ObjectTest.php @@ -19,6 +19,9 @@ use PhpOffice\PhpWord\Section\Object; */ class ObjectTest extends \PHPUnit_Framework_TestCase { + /** + * Create new instance with supported files + */ public function testConstructWithSupportedFiles() { $src = __DIR__ . "/../_files/documents/sheet.xls"; @@ -29,6 +32,9 @@ class ObjectTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oObject->getSource(), $src); } + /** + * Create new instance with non-supported files + */ public function testConstructWithNotSupportedFiles() { $src = __DIR__ . "/../_files/xsl/passthrough.xsl"; @@ -39,6 +45,9 @@ class ObjectTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oObject->getStyle(), null); } + /** + * Create with style + */ public function testConstructWithSupportedFilesAndStyle() { $src = __DIR__ . "/../_files/documents/sheet.xls"; @@ -49,6 +58,9 @@ class ObjectTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oObject->getSource(), $src); } + /** + * Set/get relation Id + */ public function testRelationId() { $src = __DIR__ . "/../_files/documents/sheet.xls"; @@ -59,6 +71,9 @@ class ObjectTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oObject->getRelationId(), $iVal); } + /** + * Set/get image relation Id + */ public function testImageRelationId() { $src = __DIR__ . "/../_files/documents/sheet.xls"; @@ -69,6 +84,9 @@ class ObjectTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oObject->getImageRelationId(), $iVal); } + /** + * Set/get object relation Id + */ public function testObjectId() { $src = __DIR__ . "/../_files/documents/sheet.xls"; diff --git a/tests/PhpWord/Tests/Section/SettingsTest.php b/tests/PhpWord/Tests/Section/SettingsTest.php index 7c66b67a..8fb62f1c 100644 --- a/tests/PhpWord/Tests/Section/SettingsTest.php +++ b/tests/PhpWord/Tests/Section/SettingsTest.php @@ -57,6 +57,9 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertEquals($iVal, $oSettings->getHeaderHeight()); } + /** + * Set/get margin + */ public function testMargin() { // Section Settings @@ -79,6 +82,9 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertEquals($iVal, $oSettings->getMarginRight()); } + /** + * Set/get landscape orientation + */ public function testOrientationLandscape() { // Section Settings @@ -90,6 +96,9 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertEquals(11906, $oSettings->getPageSizeH()); } + /** + * Set/get portrait orientation + */ public function testOrientationPortrait() { // Section Settings @@ -101,6 +110,9 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertEquals(16838, $oSettings->getPageSizeH()); } + /** + * Set/get border size + */ public function testBorderSize() { // Section Settings @@ -131,6 +143,9 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertEquals($iVal, $oSettings->getBorderTopSize()); } + /** + * Set/get border color + */ public function testBorderColor() { // Section Settings @@ -156,6 +171,9 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertEquals('22FF33', $oSettings->getBorderTopColor()); } + /** + * Set/get page numbering start + */ public function testNumberingStart() { // Section Settings @@ -171,9 +189,11 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertNull($oSettings->getPageNumberingStart()); } + /** + * Set/get header height + */ public function testHeader() { - // Section Settings $oSettings = new Settings(); $this->assertEquals(720, $oSettings->getHeaderHeight()); @@ -186,6 +206,9 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertEquals(720, $oSettings->getHeaderHeight()); } + /** + * Set/get footer height + */ public function testFooter() { // Section Settings @@ -201,6 +224,9 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertEquals(720, $oSettings->getFooterHeight()); } + /** + * Set/get column number + */ public function testColumnsNum() { // Section Settings @@ -217,6 +243,9 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertEquals(1, $oSettings->getColsNum()); } + /** + * Set/get column spacing + */ public function testColumnsSpace() { // Section Settings @@ -233,6 +262,9 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertEquals(720, $oSettings->getColsSpace()); } + /** + * Set/get break type + */ public function testBreakType() { // Section Settings diff --git a/tests/PhpWord/Tests/Section/Table/RowTest.php b/tests/PhpWord/Tests/Section/Table/RowTest.php index 991d8a47..10d53915 100644 --- a/tests/PhpWord/Tests/Section/Table/RowTest.php +++ b/tests/PhpWord/Tests/Section/Table/RowTest.php @@ -19,6 +19,9 @@ use PhpOffice\PhpWord\Section\Table\Row; */ class RowTest extends \PHPUnit_Framework_TestCase { + /** + * Create new instance + */ public function testConstruct() { $iVal = rand(1, 1000); @@ -31,6 +34,9 @@ class RowTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Row', $oRow->getStyle()); } + /** + * Create new instance with parameters + */ public function testConstructWithParams() { $iVal = rand(1, 1000); @@ -46,6 +52,9 @@ class RowTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Row', $oRow->getStyle()); } + /** + * Add cell + */ public function testAddCell() { $oRow = new Row('section', 1); diff --git a/tests/PhpWord/Tests/Section/TableTest.php b/tests/PhpWord/Tests/Section/TableTest.php index c6d3463d..9808485d 100644 --- a/tests/PhpWord/Tests/Section/TableTest.php +++ b/tests/PhpWord/Tests/Section/TableTest.php @@ -19,6 +19,9 @@ use PhpOffice\PhpWord\Section\Table; */ class TableTest extends \PHPUnit_Framework_TestCase { + /** + * Create new instance + */ public function testConstruct() { $oTable = new Table('section', 1); @@ -30,6 +33,9 @@ class TableTest extends \PHPUnit_Framework_TestCase $this->assertCount(0, $oTable->getRows()); } + /** + * Get style name + */ public function testStyleText() { $oTable = new Table('section', 1, 'tableStyle'); @@ -37,6 +43,9 @@ class TableTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oTable->getStyle(), 'tableStyle'); } + /** + * Get style array + */ public function testStyleArray() { $oTable = new Table( @@ -48,6 +57,9 @@ class TableTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Table', $oTable->getStyle()); } + /** + * Set/get width + */ public function testWidth() { $oTable = new Table('section', 1); @@ -56,6 +68,9 @@ class TableTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oTable->getWidth(), $iVal); } + /** + * Add/get row + */ public function testRow() { $oTable = new Table('section', 1); @@ -64,6 +79,9 @@ class TableTest extends \PHPUnit_Framework_TestCase $this->assertCount(1, $oTable->getRows()); } + /** + * Add cell + */ public function testCell() { $oTable = new Table('section', 1); diff --git a/tests/PhpWord/Tests/Section/TitleTest.php b/tests/PhpWord/Tests/Section/TitleTest.php index e1b25e3a..a63d184c 100644 --- a/tests/PhpWord/Tests/Section/TitleTest.php +++ b/tests/PhpWord/Tests/Section/TitleTest.php @@ -19,6 +19,9 @@ use PhpOffice\PhpWord\Section\Title; */ class TitleTest extends \PHPUnit_Framework_TestCase { + /** + * Create new instance + */ public function testConstruct() { $oTitle = new Title('text'); @@ -27,6 +30,9 @@ class TitleTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oTitle->getText(), 'text'); } + /** + * Get style null + */ public function testStyleNull() { $oTitle = new Title('text'); @@ -34,6 +40,9 @@ class TitleTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oTitle->getStyle(), null); } + /** + * Get style not null + */ public function testStyleNotNull() { $oTitle = new Title('text', 1, 'style'); @@ -41,6 +50,9 @@ class TitleTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oTitle->getStyle(), 'style'); } + /** + * Get anchor + */ public function testAnchor() { $oTitle = new Title('text'); @@ -50,6 +62,9 @@ class TitleTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oTitle->getAnchor(), $iVal); } + /** + * Get bookmark Id + */ public function testBookmarkID() { $oTitle = new Title('text'); diff --git a/tests/PhpWord/Tests/Shared/StringTest.php b/tests/PhpWord/Tests/Shared/StringTest.php index bb7a36ad..02b4898a 100644 --- a/tests/PhpWord/Tests/Shared/StringTest.php +++ b/tests/PhpWord/Tests/Shared/StringTest.php @@ -19,6 +19,9 @@ use PhpOffice\PhpWord\Shared\String; */ class StringTest extends \PHPUnit_Framework_TestCase { + /** + * Is UTF8 + */ public function testIsUTF8() { $this->assertTrue(String::isUTF8('')); @@ -26,12 +29,18 @@ class StringTest extends \PHPUnit_Framework_TestCase $this->assertFalse(String::isUTF8(utf8_decode('éééé'))); } + /** + * OOXML to PHP control character + */ public function testControlCharacterOOXML2PHP() { $this->assertEquals('', String::controlCharacterOOXML2PHP('')); $this->assertEquals(chr(0x08), String::controlCharacterOOXML2PHP('_x0008_')); } + /** + * PHP to OOXML control character + */ public function testControlCharacterPHP2OOXML() { $this->assertEquals('', String::controlCharacterPHP2OOXML('')); diff --git a/tests/PhpWord/Tests/Style/FontTest.php b/tests/PhpWord/Tests/Style/FontTest.php index c5e0664b..ed4d61db 100644 --- a/tests/PhpWord/Tests/Style/FontTest.php +++ b/tests/PhpWord/Tests/Style/FontTest.php @@ -16,11 +16,13 @@ use PhpOffice\PhpWord\Tests\TestHelperDOCX; /** * Test class for PhpOffice\PhpWord\Style\Font * - * @coversDefaultClass \PhpOffice\PhpWord\Style\Font * @runTestsInSeparateProcesses */ class FontTest extends \PHPUnit_Framework_TestCase { + /** + * Tear down after each test + */ public function tearDown() { TestHelperDOCX::clear(); diff --git a/tests/PhpWord/Tests/Style/ParagraphTest.php b/tests/PhpWord/Tests/Style/ParagraphTest.php index 856e2b13..619d5497 100644 --- a/tests/PhpWord/Tests/Style/ParagraphTest.php +++ b/tests/PhpWord/Tests/Style/ParagraphTest.php @@ -17,11 +17,13 @@ use PhpOffice\PhpWord\Tests\TestHelperDOCX; /** * Test class for PhpOffice\PhpWord\Style\Paragraph * - * @coversDefaultClass \PhpOffice\PhpWord\Style\Paragraph * @runTestsInSeparateProcesses */ class ParagraphTest extends \PHPUnit_Framework_TestCase { + /** + * Tear down after each test + */ public function tearDown() { TestHelperDOCX::clear(); @@ -97,6 +99,9 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Tabs', $object->getTabs()); } + /** + * Line height + */ public function testLineHeight() { $phpWord = new PhpWord(); diff --git a/tests/PhpWord/Tests/TemplateTest.php b/tests/PhpWord/Tests/TemplateTest.php index b9302f7e..669ae946 100644 --- a/tests/PhpWord/Tests/TemplateTest.php +++ b/tests/PhpWord/Tests/TemplateTest.php @@ -65,6 +65,7 @@ final class TemplateTest extends \PHPUnit_Framework_TestCase /** * XSL stylesheet can be applied * + * @param string $actualDocumentFqfn * @covers ::applyXslStyleSheet * @depends testTemplateCanBeSavedInTemporaryLocation * @test diff --git a/tests/PhpWord/Tests/Writer/Word2007/FooterTest.php b/tests/PhpWord/Tests/Writer/Word2007/FooterTest.php index de08ff9d..6d303a0a 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/FooterTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/FooterTest.php @@ -21,6 +21,8 @@ use PhpOffice\PhpWord\Tests\TestHelperDOCX; class FooterTest extends \PHPUnit_Framework_TestCase { /** + * Write footer + * * @covers ::writeFooter */ public function testWriteFooter() diff --git a/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php b/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php index a1220d26..6baba0ac 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php @@ -27,6 +27,9 @@ class FootnotesTest extends \PHPUnit_Framework_TestCase TestHelperDOCX::clear(); } + /** + * Write footnotes + */ public function testWriteFootnotes() { $phpWord = new PhpWord(); diff --git a/tests/PhpWord/Tests/Writer/Word2007/HeaderTest.php b/tests/PhpWord/Tests/Writer/Word2007/HeaderTest.php index ca997b65..de3ac010 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/HeaderTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/HeaderTest.php @@ -15,13 +15,12 @@ use PhpOffice\PhpWord\Tests\TestHelperDOCX; /** * Test class for PhpOffice\PhpWord\Writer\Word2007\Header * - * @coversDefaultClass \PhpOffice\PhpWord\Writer\Word2007\Header * @runTestsInSeparateProcesses */ class HeaderTest extends \PHPUnit_Framework_TestCase { /** - * @covers ::writeHeader + * Write header */ public function testWriteHeader() { diff --git a/tests/PhpWord/Tests/_includes/TestHelperDOCX.php b/tests/PhpWord/Tests/_includes/TestHelperDOCX.php index 97f84ac2..3e24c4cc 100644 --- a/tests/PhpWord/Tests/_includes/TestHelperDOCX.php +++ b/tests/PhpWord/Tests/_includes/TestHelperDOCX.php @@ -1,15 +1,32 @@ Date: Sun, 30 Mar 2014 16:31:01 +0700 Subject: [PATCH 16/22] Refactor writers and readers - Create Writer abstract class - Inherit writers class from Writer - Inherit ODText\WriterPart from Word2007\WriterPart - Rename AbstractReader > Reader --- CHANGELOG.md | 2 + .../Reader/{AbstractReader.php => Reader.php} | 2 +- src/PhpWord/Reader/Word2007.php | 2 +- src/PhpWord/Writer/ODText.php | 173 ++-------------- src/PhpWord/Writer/ODText/Content.php | 7 +- src/PhpWord/Writer/ODText/Manifest.php | 7 +- src/PhpWord/Writer/ODText/Meta.php | 7 +- src/PhpWord/Writer/ODText/Styles.php | 7 +- src/PhpWord/Writer/ODText/WriterPart.php | 36 +--- src/PhpWord/Writer/RTF.php | 104 +++------- src/PhpWord/Writer/Word2007.php | 157 +++------------ src/PhpWord/Writer/Word2007/ContentTypes.php | 7 +- src/PhpWord/Writer/Word2007/DocProps.php | 14 +- src/PhpWord/Writer/Word2007/Document.php | 6 +- src/PhpWord/Writer/Word2007/DocumentRels.php | 14 +- src/PhpWord/Writer/Word2007/Footer.php | 7 +- src/PhpWord/Writer/Word2007/Footnotes.php | 11 +- src/PhpWord/Writer/Word2007/FootnotesRels.php | 7 +- src/PhpWord/Writer/Word2007/Header.php | 6 +- src/PhpWord/Writer/Word2007/Rels.php | 7 +- src/PhpWord/Writer/Word2007/Styles.php | 14 +- src/PhpWord/Writer/Word2007/WriterPart.php | 30 ++- src/PhpWord/Writer/Writer.php | 184 ++++++++++++++++++ 23 files changed, 310 insertions(+), 501 deletions(-) rename src/PhpWord/Reader/{AbstractReader.php => Reader.php} (97%) create mode 100644 src/PhpWord/Writer/Writer.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 11e4cb23..f97c4c07 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ This is the changelog between releases of PHPWord. Releases are listed in revers ### Miscellaneous - Documentation: Simplify page level docblock - @ivanlanin GH-179 +- Writer: Refactor writer classes and make a new Writer abstract class - @ivanlanin GH-160 +- Reader: Rename AbstractReader > Reader - @ivanlanin ## 0.9.1 - 27 Mar 2014 diff --git a/src/PhpWord/Reader/AbstractReader.php b/src/PhpWord/Reader/Reader.php similarity index 97% rename from src/PhpWord/Reader/AbstractReader.php rename to src/PhpWord/Reader/Reader.php index e70bea94..153a4013 100644 --- a/src/PhpWord/Reader/AbstractReader.php +++ b/src/PhpWord/Reader/Reader.php @@ -16,7 +16,7 @@ use PhpOffice\PhpWord\Exceptions\Exception; * * @codeCoverageIgnore Abstract class */ -abstract class AbstractReader implements IReader +abstract class Reader implements IReader { /** * Read data only? diff --git a/src/PhpWord/Reader/Word2007.php b/src/PhpWord/Reader/Word2007.php index b5e679f2..78720237 100644 --- a/src/PhpWord/Reader/Word2007.php +++ b/src/PhpWord/Reader/Word2007.php @@ -16,7 +16,7 @@ use PhpOffice\PhpWord\Exceptions\Exception; /** * Reader for Word2007 */ -class Word2007 extends AbstractReader implements IReader +class Word2007 extends Reader implements IReader { /** * Can the current IReader read the file? diff --git a/src/PhpWord/Writer/ODText.php b/src/PhpWord/Writer/ODText.php index fe275838..8c58df60 100755 --- a/src/PhpWord/Writer/ODText.php +++ b/src/PhpWord/Writer/ODText.php @@ -21,42 +21,14 @@ use PhpOffice\PhpWord\Writer\ODText\Styles; /** * ODText writer */ -class ODText implements IWriter +class ODText extends Writer implements IWriter { - /** - * PHPWord object - * - * @var \PhpOffice\PhpWord\PhpWord - */ - private $_document; - - /** - * Individual writers - * - * @var \PhpOffice\PhpWord\Writer\ODText\WriterPart[] - */ - private $_writerParts; - /** * Private unique PHPWord_Worksheet_BaseDrawing HashTable * * @var \PhpOffice\PhpWord\HashTable */ - private $_drawingHashTable; - - /** - * Use disk caching where possible? - * - * @var boolean - */ - private $_useDiskCaching = false; - - /** - * Disk caching directory - * - * @var string - */ - private $_diskCachingDirectory; + private $drawingHashTable; /** * Create new ODText writer @@ -67,24 +39,18 @@ class ODText implements IWriter // Assign PhpWord $this->setPhpWord($phpWord); - // Set up disk caching location - $this->_diskCachingDirectory = './'; - - // Initialise writer parts - $this->_writerParts['content'] = new Content(); - $this->_writerParts['manifest'] = new Manifest(); - $this->_writerParts['meta'] = new Meta(); - $this->_writerParts['mimetype'] = new Mimetype(); - $this->_writerParts['styles'] = new Styles(); - - - // Assign parent IWriter - foreach ($this->_writerParts as $writer) { + // Set writer parts + $this->writerParts['content'] = new Content(); + $this->writerParts['manifest'] = new Manifest(); + $this->writerParts['meta'] = new Meta(); + $this->writerParts['mimetype'] = new Mimetype(); + $this->writerParts['styles'] = new Styles(); + foreach ($this->writerParts as $writer) { $writer->setParentWriter($this); } // Set HashTable variables - $this->_drawingHashTable = new HashTable(); + $this->drawingHashTable = new HashTable(); } /** @@ -95,17 +61,8 @@ class ODText implements IWriter */ public function save($pFilename = null) { - if (!is_null($this->_document)) { - // If $pFilename is php://output or php://stdout, make it a temporary file... - $originalFilename = $pFilename; - if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { - $pFilename = @tempnam(sys_get_temp_dir(), 'phpword_'); - if ($pFilename == '') { - $pFilename = $originalFilename; - } - } - - // Create drawing dictionary + if (!is_null($this->phpWord)) { + $pFilename = $this->getTempFile($pFilename); // Create new ZIP file and open it for writing $objZip = new \ZipArchive(); @@ -119,19 +76,19 @@ class ODText implements IWriter // Add mimetype to ZIP file //@todo Not in \ZipArchive::CM_STORE mode - $objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->writeMimetype($this->_document)); + $objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->writeMimetype($this->phpWord)); // Add content.xml to ZIP file - $objZip->addFromString('content.xml', $this->getWriterPart('content')->writeContent($this->_document)); + $objZip->addFromString('content.xml', $this->getWriterPart('content')->writeContent($this->phpWord)); // Add meta.xml to ZIP file - $objZip->addFromString('meta.xml', $this->getWriterPart('meta')->writeMeta($this->_document)); + $objZip->addFromString('meta.xml', $this->getWriterPart('meta')->writeMeta($this->phpWord)); // Add styles.xml to ZIP file - $objZip->addFromString('styles.xml', $this->getWriterPart('styles')->writeStyles($this->_document)); + $objZip->addFromString('styles.xml', $this->getWriterPart('styles')->writeStyles($this->phpWord)); // Add META-INF/manifest.xml - $objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('manifest')->writeManifest($this->_document)); + $objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('manifest')->writeManifest($this->phpWord)); // Add media. Has not used yet. Legacy from PHPExcel. // @codeCoverageIgnoreStart @@ -173,46 +130,12 @@ class ODText implements IWriter throw new Exception("Could not close zip file $pFilename."); } - // If a temporary file was used, copy it to the correct file stream - if ($originalFilename != $pFilename) { - if (copy($pFilename, $originalFilename) === false) { - throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename."); - } - @unlink($pFilename); - } - + $this->cleanupTempFile(); } else { throw new Exception("PhpWord object unassigned."); } } - /** - * Get PhpWord object - * - * @return \PhpOffice\PhpWord\PhpWord - * @throws \PhpOffice\PhpWord\Exceptions\Exception - */ - public function getPhpWord() - { - if (!is_null($this->_document)) { - return $this->_document; - } else { - throw new Exception("No PhpWord assigned."); - } - } - - /** - * Set PhpWord object - * - * @param \PhpOffice\PhpWord\PhpWord $phpWord - * @return \PhpOffice\PhpWord\Writer\ODText - */ - public function setPhpWord(PhpWord $phpWord = null) - { - $this->_document = $phpWord; - return $this; - } - /** * Get PHPWord_Worksheet_BaseDrawing HashTable * @@ -220,64 +143,6 @@ class ODText implements IWriter */ public function getDrawingHashTable() { - return $this->_drawingHashTable; - } - - /** - * Get writer part - * - * @param string $pPartName Writer part name - * @return \PhpOffice\PhpWord\Writer\ODText\WriterPart - */ - public function getWriterPart($pPartName = '') - { - if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) { - return $this->_writerParts[strtolower($pPartName)]; - } else { - return null; - } - } - - /** - * Get use disk caching where possible? - * - * @return boolean - */ - public function getUseDiskCaching() - { - return $this->_useDiskCaching; - } - - /** - * Set use disk caching where possible? - * - * @param boolean $pValue - * @param string $pDirectory Disk caching directory - * @throws \PhpOffice\PhpWord\Exceptions\Exception Exception when directory does not exist - * @return \PhpOffice\PhpWord\Writer\ODText - */ - public function setUseDiskCaching($pValue = false, $pDirectory = null) - { - $this->_useDiskCaching = $pValue; - - if (!is_null($pDirectory)) { - if (is_dir($pDirectory)) { - $this->_diskCachingDirectory = $pDirectory; - } else { - throw new Exception("Directory does not exist: $pDirectory"); - } - } - - return $this; - } - - /** - * Get disk caching directory - * - * @return string - */ - public function getDiskCachingDirectory() - { - return $this->_diskCachingDirectory; + return $this->drawingHashTable; } } diff --git a/src/PhpWord/Writer/ODText/Content.php b/src/PhpWord/Writer/ODText/Content.php index 44ac7bab..4e75068d 100644 --- a/src/PhpWord/Writer/ODText/Content.php +++ b/src/PhpWord/Writer/ODText/Content.php @@ -41,12 +41,7 @@ class Content extends WriterPart public function writeContent(PhpWord $phpWord = null) { // Create XML writer - $xmlWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); - } + $xmlWriter = $this->getXmlWriter(); // XML header $xmlWriter->startDocument('1.0', 'UTF-8'); diff --git a/src/PhpWord/Writer/ODText/Manifest.php b/src/PhpWord/Writer/ODText/Manifest.php index f1c652ca..cb92ddcc 100755 --- a/src/PhpWord/Writer/ODText/Manifest.php +++ b/src/PhpWord/Writer/ODText/Manifest.php @@ -27,12 +27,7 @@ class Manifest extends WriterPart public function writeManifest(PhpWord $phpWord = null) { // Create XML writer - $xmlWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); - } + $xmlWriter = $this->getXmlWriter(); // XML header $xmlWriter->startDocument('1.0', 'UTF-8'); diff --git a/src/PhpWord/Writer/ODText/Meta.php b/src/PhpWord/Writer/ODText/Meta.php index 69cc5aea..fdd0ec7c 100644 --- a/src/PhpWord/Writer/ODText/Meta.php +++ b/src/PhpWord/Writer/ODText/Meta.php @@ -26,12 +26,7 @@ class Meta extends WriterPart public function writeMeta(PhpWord $phpWord = null) { // Create XML writer - $xmlWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); - } + $xmlWriter = $this->getXmlWriter(); // XML header $xmlWriter->startDocument('1.0', 'UTF-8'); diff --git a/src/PhpWord/Writer/ODText/Styles.php b/src/PhpWord/Writer/ODText/Styles.php index f2d46c6a..6a91a7e2 100644 --- a/src/PhpWord/Writer/ODText/Styles.php +++ b/src/PhpWord/Writer/ODText/Styles.php @@ -30,12 +30,7 @@ class Styles extends WriterPart public function writeStyles(PhpWord $phpWord = null) { // Create XML writer - $xmlWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); - } + $xmlWriter = $this->getXmlWriter(); // XML header $xmlWriter->startDocument('1.0', 'UTF-8'); diff --git a/src/PhpWord/Writer/ODText/WriterPart.php b/src/PhpWord/Writer/ODText/WriterPart.php index 66531136..a6fa93cb 100644 --- a/src/PhpWord/Writer/ODText/WriterPart.php +++ b/src/PhpWord/Writer/ODText/WriterPart.php @@ -9,43 +9,9 @@ namespace PhpOffice\PhpWord\Writer\ODText; -use PhpOffice\PhpWord\Exceptions\Exception; -use PhpOffice\PhpWord\Writer\IWriter; - /** * ODText writer part abstract */ -abstract class WriterPart +abstract class WriterPart extends \PhpOffice\PhpWord\Writer\Word2007\WriterPart { - /** - * Parent IWriter object - * - * @var \PhpOffice\PhpWord\Writer\IWriter - */ - private $_parentWriter; - - /** - * Set parent IWriter object - * - * @param \PhpOffice\PhpWord\Writer\IWriter $pWriter - */ - public function setParentWriter(IWriter $pWriter = null) - { - $this->_parentWriter = $pWriter; - } - - /** - * Get parent IWriter object - * - * @return \PhpOffice\PhpWord\Writer\IWriter - * @throws \PhpOffice\PhpWord\Exceptions\Exception - */ - public function getParentWriter() - { - if (!is_null($this->_parentWriter)) { - return $this->_parentWriter; - } else { - throw new Exception("No parent IWriter assigned."); - } - } } diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index 017c89c1..d7fc7101 100755 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -31,42 +31,35 @@ use PhpOffice\PhpWord\TOC; /** * RTF writer */ -class RTF implements IWriter +class RTF extends Writer implements IWriter { - /** - * Private PhpWord - * - * @var \PhpOffice\PhpWord\PhpWord - */ - private $_document; - /** * Private unique PHPWord_Worksheet_BaseDrawing HashTable * * @var \PhpOffice\PhpWord\HashTable */ - private $_drawingHashTable; + private $drawingHashTable; /** * Color register * * @var array */ - private $_colorTable; + private $colorTable; /** * Font register * * @var array */ - private $_fontTable; + private $fontTable; /** * Last paragraph style * * @var mixed */ - private $_lastParagraphStyle; + private $lastParagraphStyle; /** * Create new RTF writer @@ -78,7 +71,7 @@ class RTF implements IWriter $this->setPhpWord($phpWord); // Set HashTable variables - $this->_drawingHashTable = new HashTable(); + $this->drawingHashTable = new HashTable(); } /** @@ -89,60 +82,19 @@ class RTF implements IWriter */ public function save($pFilename = null) { - if (!is_null($this->_document)) { - // If $pFilename is php://output or php://stdout, make it a temporary file... - $originalFilename = $pFilename; - if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { - $pFilename = @tempnam(sys_get_temp_dir(), 'phpword_'); - if ($pFilename == '') { - $pFilename = $originalFilename; - } - } + if (!is_null($this->phpWord)) { + $pFilename = $this->getTempFile($pFilename); $hFile = fopen($pFilename, 'w') or die("can't open file"); fwrite($hFile, $this->getData()); fclose($hFile); - // If a temporary file was used, copy it to the correct file stream - if ($originalFilename != $pFilename) { - if (copy($pFilename, $originalFilename) === false) { - throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename."); - } - @unlink($pFilename); - } - + $this->cleanupTempFile(); } else { throw new Exception("PhpWord object unassigned."); } } - /** - * Get PhpWord object - * - * @return \PhpOffice\PhpWord\PhpWord - * @throws \PhpOffice\PhpWord\Exceptions\Exception - */ - public function getPhpWord() - { - if (!is_null($this->_document)) { - return $this->_document; - } else { - throw new Exception("No PhpWord assigned."); - } - } - - /** - * Set PhpWord object - * - * @param \PhpOffice\PhpWord\PhpWord $phpWord - * @return \PhpOffice\PhpWord\Writer\RTF - */ - public function setPhpWord(PhpWord $phpWord = null) - { - $this->_document = $phpWord; - return $this; - } - /** * Get PHPWord_Worksheet_BaseDrawing HashTable * @@ -150,7 +102,7 @@ class RTF implements IWriter */ public function getDrawingHashTable() { - return $this->_drawingHashTable; + return $this->drawingHashTable; } /** @@ -160,9 +112,9 @@ class RTF implements IWriter */ private function getData() { - // PhpWord object : $this->_document - $this->_fontTable = $this->getDataFont(); - $this->_colorTable = $this->getDataColor(); + // PhpWord object : $this->phpWord + $this->fontTable = $this->getDataFont(); + $this->colorTable = $this->getDataColor(); $sRTFContent = '{\rtf1'; // Set the default character set @@ -174,13 +126,13 @@ class RTF implements IWriter $sRTFContent .= \PHP_EOL; // Set the font tbl group $sRTFContent .= '{\fonttbl'; - foreach ($this->_fontTable as $idx => $font) { + foreach ($this->fontTable as $idx => $font) { $sRTFContent .= '{\f' . $idx . '\fnil\fcharset0 ' . $font . ';}'; } $sRTFContent .= '}' . \PHP_EOL; // Set the color tbl group $sRTFContent .= '{\colortbl '; - foreach ($this->_colorTable as $idx => $color) { + foreach ($this->colorTable as $idx => $color) { $arrColor = Drawing::htmlToRGB($color); $sRTFContent .= ';\red' . $arrColor[0] . '\green' . $arrColor[1] . '\blue' . $arrColor[2] . ''; } @@ -218,12 +170,12 @@ class RTF implements IWriter */ private function getDataFont() { - $phpWord = $this->_document; + $phpWord = $this->phpWord; $arrFonts = array(); // Default font : PhpWord::DEFAULT_FONT_NAME $arrFonts[] = PhpWord::DEFAULT_FONT_NAME; - // PhpWord object : $this->_document + // PhpWord object : $this->phpWord // Browse styles $styles = Style::getStyles(); @@ -273,10 +225,10 @@ class RTF implements IWriter */ private function getDataColor() { - $phpWord = $this->_document; + $phpWord = $this->phpWord; $arrColors = array(); - // PhpWord object : $this->_document + // PhpWord object : $this->phpWord // Browse styles $styles = Style::getStyles(); @@ -334,7 +286,7 @@ class RTF implements IWriter */ private function getDataContent() { - $phpWord = $this->_document; + $phpWord = $this->phpWord; $sRTFBody = ''; $_sections = $phpWord->getSections(); @@ -400,7 +352,7 @@ class RTF implements IWriter } if ($styleParagraph && !$withoutP) { - if ($this->_lastParagraphStyle != $text->getParagraphStyle()) { + if ($this->lastParagraphStyle != $text->getParagraphStyle()) { $sRTFText .= '\pard\nowidctlpar'; if ($styleParagraph->getSpaceAfter() != null) { $sRTFText .= '\sa' . $styleParagraph->getSpaceAfter(); @@ -410,17 +362,17 @@ class RTF implements IWriter $sRTFText .= '\qc'; } } - $this->_lastParagraphStyle = $text->getParagraphStyle(); + $this->lastParagraphStyle = $text->getParagraphStyle(); } else { - $this->_lastParagraphStyle = ''; + $this->lastParagraphStyle = ''; } } else { - $this->_lastParagraphStyle = ''; + $this->lastParagraphStyle = ''; } if ($styleFont instanceof Font) { if ($styleFont->getColor() != null) { - $idxColor = array_search($styleFont->getColor(), $this->_colorTable); + $idxColor = array_search($styleFont->getColor(), $this->colorTable); if ($idxColor !== false) { $sRTFText .= '\cf' . ($idxColor + 1); } @@ -428,7 +380,7 @@ class RTF implements IWriter $sRTFText .= '\cf0'; } if ($styleFont->getName() != null) { - $idxFont = array_search($styleFont->getName(), $this->_fontTable); + $idxFont = array_search($styleFont->getName(), $this->fontTable); if ($idxFont !== false) { $sRTFText .= '\f' . $idxFont; } @@ -445,7 +397,7 @@ class RTF implements IWriter $sRTFText .= '\fs' . ($styleFont->getSize() * 2); } } - if ($this->_lastParagraphStyle != '' || $styleFont) { + if ($this->lastParagraphStyle != '' || $styleFont) { $sRTFText .= ' '; } $sRTFText .= $text->getText(); @@ -501,7 +453,7 @@ class RTF implements IWriter */ private function getDataContentTextBreak() { - $this->_lastParagraphStyle = ''; + $this->lastParagraphStyle = ''; return '\par' . \PHP_EOL; } diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index 5b4f9f85..6ceed4fb 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -27,36 +27,8 @@ use PhpOffice\PhpWord\Writer\Word2007\Styles; /** * Word2007 writer */ -class Word2007 implements IWriter +class Word2007 extends Writer implements IWriter { - /** - * PHPWord object - * - * @var PhpOffice\PhpWord\PhpWord - */ - private $_document; - - /** - * Individual writers - * - * @var PhpOffice\PhpWord\Writer\Word2007\WriterPart - */ - private $_writerParts; - - /** - * Disk caching directory - * - * @var string - */ - private $_diskCachingDirectory; - - /** - * Use disk caching - * - * @var boolean - */ - private $_useDiskCaching = false; - /** * Types of images * @@ -78,22 +50,21 @@ class Word2007 implements IWriter */ public function __construct(PhpWord $phpWord = null) { - $this->_document = $phpWord; + // Assign PhpWord + $this->setPhpWord($phpWord); - $this->_diskCachingDirectory = './'; - - $this->_writerParts['contenttypes'] = new ContentTypes(); - $this->_writerParts['rels'] = new Rels(); - $this->_writerParts['docprops'] = new DocProps(); - $this->_writerParts['documentrels'] = new DocumentRels(); - $this->_writerParts['document'] = new Document(); - $this->_writerParts['styles'] = new Styles(); - $this->_writerParts['header'] = new Header(); - $this->_writerParts['footer'] = new Footer(); - $this->_writerParts['footnotes'] = new Footnotes(); - $this->_writerParts['footnotesrels'] = new FootnotesRels(); - - foreach ($this->_writerParts as $writer) { + // Set writer parts + $this->writerParts['contenttypes'] = new ContentTypes(); + $this->writerParts['rels'] = new Rels(); + $this->writerParts['docprops'] = new DocProps(); + $this->writerParts['documentrels'] = new DocumentRels(); + $this->writerParts['document'] = new Document(); + $this->writerParts['styles'] = new Styles(); + $this->writerParts['header'] = new Header(); + $this->writerParts['footer'] = new Footer(); + $this->writerParts['footnotes'] = new Footnotes(); + $this->writerParts['footnotesrels'] = new FootnotesRels(); + foreach ($this->writerParts as $writer) { $writer->setParentWriter($this); } } @@ -105,16 +76,8 @@ class Word2007 implements IWriter */ public function save($pFilename = null) { - if (!is_null($this->_document)) { - - // If $pFilename is php://output or php://stdout, make it a temporary file... - $originalFilename = $pFilename; - if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { - $pFilename = @tempnam(sys_get_temp_dir(), 'phpword_'); - if ($pFilename == '') { - $pFilename = $originalFilename; - } - } + if (!is_null($this->phpWord)) { + $pFilename = $this->getTempFile($pFilename); // Create new ZIP file and open it for writing $objZip = new \ZipArchive(); @@ -126,12 +89,11 @@ class Word2007 implements IWriter } } - $sectionElements = array(); $_secElements = Media::getSectionMediaElements(); foreach ($_secElements as $element) { // loop through section media elements if ($element['type'] != 'hyperlink') { - $this->_addFileToPackage($objZip, $element); + $this->addFileToPackage($objZip, $element); } $sectionElements[] = $element; } @@ -141,7 +103,7 @@ class Word2007 implements IWriter if (count($_hdrMedia) > 0) { $objZip->addFromString('word/_rels/' . $_headerFile . '.xml.rels', $this->getWriterPart('documentrels')->writeHeaderFooterRels($_hdrMedia)); foreach ($_hdrMedia as $element) { // loop through header media elements - $this->_addFileToPackage($objZip, $element); + $this->addFileToPackage($objZip, $element); } } } @@ -151,7 +113,7 @@ class Word2007 implements IWriter if (count($_ftrMedia) > 0) { $objZip->addFromString('word/_rels/' . $_footerFile . '.xml.rels', $this->getWriterPart('documentrels')->writeHeaderFooterRels($_ftrMedia)); foreach ($_ftrMedia as $element) { // loop through footers media elements - $this->_addFileToPackage($objZip, $element); + $this->addFileToPackage($objZip, $element); } } } @@ -166,7 +128,7 @@ class Word2007 implements IWriter $_cHdrs = 0; $_cFtrs = 0; $rID = Media::countSectionMediaElements() + 6; - $_sections = $this->_document->getSections(); + $_sections = $this->phpWord->getSections(); $footers = array(); foreach ($_sections as $section) { @@ -211,12 +173,12 @@ class Word2007 implements IWriter $footers ) ); - $objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeRelationships($this->_document)); - $objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->_document)); - $objZip->addFromString('docProps/core.xml', $this->getWriterPart('docprops')->writeDocPropsCore($this->_document)); - $objZip->addFromString('word/document.xml', $this->getWriterPart('document')->writeDocument($this->_document)); + $objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeRelationships($this->phpWord)); + $objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->phpWord)); + $objZip->addFromString('docProps/core.xml', $this->getWriterPart('docprops')->writeDocPropsCore($this->phpWord)); + $objZip->addFromString('word/document.xml', $this->getWriterPart('document')->writeDocument($this->phpWord)); $objZip->addFromString('word/_rels/document.xml.rels', $this->getWriterPart('documentrels')->writeDocumentRels($sectionElements)); - $objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->_document)); + $objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->phpWord)); // Write static files $objZip->addFile(__DIR__ . '/../_staticDocParts/numbering.xml', 'word/numbering.xml'); @@ -225,19 +187,12 @@ class Word2007 implements IWriter $objZip->addFile(__DIR__ . '/../_staticDocParts/webSettings.xml', 'word/webSettings.xml'); $objZip->addFile(__DIR__ . '/../_staticDocParts/fontTable.xml', 'word/fontTable.xml'); - // Close file if ($objZip->close() === false) { throw new Exception("Could not close zip file $pFilename."); } - // If a temporary file was used, copy it to the correct file stream - if ($originalFilename != $pFilename) { - if (copy($pFilename, $originalFilename) === false) { - throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename."); - } - @unlink($pFilename); - } + $this->cleanupTempFile(); } else { throw new Exception("PhpWord object unassigned."); } @@ -290,69 +245,13 @@ class Word2007 implements IWriter } } - /** - * Get writer part - * - * @param string $pPartName Writer part name - * @return \PhpOffice\PhpWord\Writer\ODText\WriterPart - */ - public function getWriterPart($pPartName = '') - { - if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) { - return $this->_writerParts[strtolower($pPartName)]; - } else { - return null; - } - } - - /** - * Get use disk caching status - * - * @return boolean - */ - public function getUseDiskCaching() - { - return $this->_useDiskCaching; - } - - /** - * Set use disk caching status - * - * @param boolean $pValue - * @param string $pDirectory - */ - public function setUseDiskCaching($pValue = false, $pDirectory = null) - { - $this->_useDiskCaching = $pValue; - - if (!is_null($pDirectory)) { - if (is_dir($pDirectory)) { - $this->_diskCachingDirectory = $pDirectory; - } else { - throw new Exception("Directory does not exist: $pDirectory"); - } - } - - return $this; - } - - /** - * Get disk caching directory - * - * @return string - */ - public function getDiskCachingDirectory() - { - return $this->_diskCachingDirectory; - } - /** * Check content types * * @param mixed $objZip * @param mixed $element */ - private function _addFileToPackage($objZip, $element) + private function addFileToPackage($objZip, $element) { if (isset($element['isMemImage']) && $element['isMemImage']) { $image = call_user_func($element['createfunction'], $element['source']); diff --git a/src/PhpWord/Writer/Word2007/ContentTypes.php b/src/PhpWord/Writer/Word2007/ContentTypes.php index ea97d138..9f8c3fb2 100755 --- a/src/PhpWord/Writer/Word2007/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/ContentTypes.php @@ -27,12 +27,7 @@ class ContentTypes extends WriterPart public function writeContentTypes($_imageTypes, $_objectTypes, $_cHdrs, $footers) { // Create XML writer - $xmlWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); - } + $xmlWriter = $this->getXmlWriter(); // XML header $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); diff --git a/src/PhpWord/Writer/Word2007/DocProps.php b/src/PhpWord/Writer/Word2007/DocProps.php index 3c7d6977..591dfc0e 100644 --- a/src/PhpWord/Writer/Word2007/DocProps.php +++ b/src/PhpWord/Writer/Word2007/DocProps.php @@ -23,12 +23,7 @@ class DocProps extends WriterPart public function writeDocPropsApp(PhpWord $phpWord = null) { // Create XML writer - $xmlWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); - } + $xmlWriter = $this->getXmlWriter(); // XML header $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); @@ -120,12 +115,7 @@ class DocProps extends WriterPart public function writeDocPropsCore(PhpWord $phpWord = null) { // Create XML writer - $xmlWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); - } + $xmlWriter = $this->getXmlWriter(); // XML header $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); diff --git a/src/PhpWord/Writer/Word2007/Document.php b/src/PhpWord/Writer/Word2007/Document.php index ec7d2df0..585050df 100644 --- a/src/PhpWord/Writer/Word2007/Document.php +++ b/src/PhpWord/Writer/Word2007/Document.php @@ -41,11 +41,7 @@ class Document extends Base public function writeDocument(PhpWord $phpWord = null) { // Create XML writer - if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); - } + $xmlWriter = $this->getXmlWriter(); // XML header $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); diff --git a/src/PhpWord/Writer/Word2007/DocumentRels.php b/src/PhpWord/Writer/Word2007/DocumentRels.php index 218869b5..20a014f2 100755 --- a/src/PhpWord/Writer/Word2007/DocumentRels.php +++ b/src/PhpWord/Writer/Word2007/DocumentRels.php @@ -25,12 +25,7 @@ class DocumentRels extends WriterPart public function writeDocumentRels($_relsCollection) { // Create XML writer - $xmlWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); - } + $xmlWriter = $this->getXmlWriter(); // XML header $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); @@ -118,12 +113,7 @@ class DocumentRels extends WriterPart public function writeHeaderFooterRels($_relsCollection) { // Create XML writer - $xmlWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); - } + $xmlWriter = $this->getXmlWriter(); // XML header $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); diff --git a/src/PhpWord/Writer/Word2007/Footer.php b/src/PhpWord/Writer/Word2007/Footer.php index a2ff09b4..d4f68826 100644 --- a/src/PhpWord/Writer/Word2007/Footer.php +++ b/src/PhpWord/Writer/Word2007/Footer.php @@ -30,12 +30,7 @@ class Footer extends Base public function writeFooter(\PhpOffice\PhpWord\Section\Footer $footer) { // Create XML writer - $xmlWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); - } + $xmlWriter = $this->getXmlWriter(); // XML header $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); diff --git a/src/PhpWord/Writer/Word2007/Footnotes.php b/src/PhpWord/Writer/Word2007/Footnotes.php index eb410393..d69e0f0c 100644 --- a/src/PhpWord/Writer/Word2007/Footnotes.php +++ b/src/PhpWord/Writer/Word2007/Footnotes.php @@ -29,15 +29,8 @@ class Footnotes extends Base public function writeFootnotes($allFootnotesCollection) { // Create XML writer - $xmlWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter( - XMLWriter::STORAGE_DISK, - $this->getParentWriter()->getDiskCachingDirectory() - ); - } else { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); - } + $xmlWriter = $this->getXmlWriter(); + // XML header $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); $xmlWriter->startElement('w:footnotes'); diff --git a/src/PhpWord/Writer/Word2007/FootnotesRels.php b/src/PhpWord/Writer/Word2007/FootnotesRels.php index 7f87ce30..f9e5c015 100644 --- a/src/PhpWord/Writer/Word2007/FootnotesRels.php +++ b/src/PhpWord/Writer/Word2007/FootnotesRels.php @@ -25,12 +25,7 @@ class FootnotesRels extends WriterPart public function writeFootnotesRels($_relsCollection) { // Create XML writer - $xmlWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); - } + $xmlWriter = $this->getXmlWriter(); // XML header $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); diff --git a/src/PhpWord/Writer/Word2007/Header.php b/src/PhpWord/Writer/Word2007/Header.php index bdec8d45..50d77e98 100644 --- a/src/PhpWord/Writer/Word2007/Header.php +++ b/src/PhpWord/Writer/Word2007/Header.php @@ -30,11 +30,7 @@ class Header extends Base public function writeHeader(\PhpOffice\PhpWord\Section\Header $header) { // Create XML writer - if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); - } + $xmlWriter = $this->getXmlWriter(); // XML header $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); diff --git a/src/PhpWord/Writer/Word2007/Rels.php b/src/PhpWord/Writer/Word2007/Rels.php index a5f5084e..19ce58b0 100755 --- a/src/PhpWord/Writer/Word2007/Rels.php +++ b/src/PhpWord/Writer/Word2007/Rels.php @@ -26,12 +26,7 @@ class Rels extends WriterPart public function writeRelationships(PhpWord $phpWord = null) { // Create XML writer - $xmlWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); - } + $xmlWriter = $this->getXmlWriter(); // XML header $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); diff --git a/src/PhpWord/Writer/Word2007/Styles.php b/src/PhpWord/Writer/Word2007/Styles.php index a24ac516..1c7c05e2 100644 --- a/src/PhpWord/Writer/Word2007/Styles.php +++ b/src/PhpWord/Writer/Word2007/Styles.php @@ -34,17 +34,11 @@ class Styles extends Base */ public function writeStyles(PhpWord $phpWord = null) { - // Create XML writer - $xmlWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $xmlWriter = new XMLWriter( - XMLWriter::STORAGE_DISK, - $this->getParentWriter()->getDiskCachingDirectory() - ); - } else { - $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); - } $this->phpWord = $phpWord; + + // Create XML writer + $xmlWriter = $this->getXmlWriter(); + // XML header $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); $xmlWriter->startElement('w:styles'); diff --git a/src/PhpWord/Writer/Word2007/WriterPart.php b/src/PhpWord/Writer/Word2007/WriterPart.php index 2d7860e0..ead47239 100755 --- a/src/PhpWord/Writer/Word2007/WriterPart.php +++ b/src/PhpWord/Writer/Word2007/WriterPart.php @@ -11,6 +11,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\Exceptions\Exception; use PhpOffice\PhpWord\Writer\IWriter; +use PhpOffice\PhpWord\Shared\XMLWriter; /** * Word2007 writer part abstract class @@ -22,7 +23,7 @@ abstract class WriterPart * * @var IWriter */ - private $_parentWriter; + protected $parentWriter; /** * Set parent writer @@ -31,20 +32,41 @@ abstract class WriterPart */ public function setParentWriter(IWriter $pWriter = null) { - $this->_parentWriter = $pWriter; + $this->parentWriter = $pWriter; } /** * Get parent writer * * @return IWriter + * @throws Exception */ public function getParentWriter() { - if (!is_null($this->_parentWriter)) { - return $this->_parentWriter; + if (!is_null($this->parentWriter)) { + return $this->parentWriter; } else { throw new Exception("No parent IWriter assigned."); } } + + /** + * Get XML Writer + * + * @return XMLWriter + */ + protected function getXmlWriter() + { + $useDiskCaching = false; + if (!is_null($this->parentWriter)) { + if ($this->parentWriter->getUseDiskCaching()) { + $useDiskCaching = true; + } + } + if ($useDiskCaching) { + return new XMLWriter(XMLWriter::STORAGE_DISK, $this->parentWriter->getDiskCachingDirectory()); + } else { + return new XMLWriter(XMLWriter::STORAGE_MEMORY); + } + } } diff --git a/src/PhpWord/Writer/Writer.php b/src/PhpWord/Writer/Writer.php new file mode 100644 index 00000000..eeba55ca --- /dev/null +++ b/src/PhpWord/Writer/Writer.php @@ -0,0 +1,184 @@ +phpWord)) { + return $this->phpWord; + } else { + throw new Exception("No PhpWord assigned."); + } + } + + /** + * Set PhpWord object + * + * @param PhpWord + * @return $this + */ + public function setPhpWord(PhpWord $phpWord = null) + { + $this->phpWord = $phpWord; + return $this; + } + + /** + * Get writer part + * + * @param string $pPartName Writer part name + * @return mixed + */ + public function getWriterPart($pPartName = '') + { + if ($pPartName != '' && isset($this->writerParts[strtolower($pPartName)])) { + return $this->writerParts[strtolower($pPartName)]; + } else { + return null; + } + } + + /** + * Get use disk caching status + * + * @return boolean + */ + public function getUseDiskCaching() + { + return $this->useDiskCaching; + } + + /** + * Set use disk caching status + * + * @param boolean $pValue + * @param string $pDirectory + * @return $this + */ + public function setUseDiskCaching($pValue = false, $pDirectory = null) + { + $this->useDiskCaching = $pValue; + + if (!is_null($pDirectory)) { + if (is_dir($pDirectory)) { + $this->diskCachingDirectory = $pDirectory; + } else { + throw new Exception("Directory does not exist: $pDirectory"); + } + } + + return $this; + } + + /** + * Get disk caching directory + * + * @return string + */ + public function getDiskCachingDirectory() + { + return $this->diskCachingDirectory; + } + + /** + * Get temporary file name + * + * If $pFilename is php://output or php://stdout, make it a temporary file + * + * @param string $pFilename + * @return string + */ + protected function getTempFile($pFilename) + { + $this->originalFilename = $pFilename; + if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { + $pFilename = @tempnam(sys_get_temp_dir(), 'phpword_'); + if ($pFilename == '') { + $pFilename = $this->originalFilename; + } + } + $this->tempFilename = $pFilename; + + return $this->tempFilename; + } + + /** + * Cleanup temporary file + * + * If a temporary file was used, copy it to the correct file stream + */ + protected function cleanupTempFile() + { + if ($this->originalFilename != $this->tempFilename) { + if (copy($this->tempFilename, $this->originalFilename) === false) { + throw new Exception("Could not copy temporary zip file {$this->tempFilename} to {$this->originalFilename}."); + } + @unlink($this->tempFilename); + } + } +} From bf5eed4d26e6bde65e59987ff1efeccfb8bbbb9c Mon Sep 17 00:00:00 2001 From: Roman Syroeshko Date: Sun, 30 Mar 2014 05:27:25 -0700 Subject: [PATCH 17/22] 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 18/22] 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 19/22] 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 20/22] 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 21/22] 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) { From 45b0baf7bce599571780b2f9bda21fd32879f442 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 30 Mar 2014 18:51:25 +0700 Subject: [PATCH 22/22] Refactor writer classes --- composer.json | 4 + src/PhpWord/DocumentProperties.php | 12 +- src/PhpWord/Section/CheckBox.php | 22 +- src/PhpWord/Section/Footer/PreserveText.php | 10 +- 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/Style/Font.php | 4 +- src/PhpWord/Style/Row.php | 6 +- src/PhpWord/TOC.php | 11 +- src/PhpWord/Writer/ODText.php | 8 +- src/PhpWord/Writer/ODText/Content.php | 48 +- src/PhpWord/Writer/ODText/Manifest.php | 8 +- 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 | 12 +- src/PhpWord/Writer/Word2007.php | 18 +- src/PhpWord/Writer/Word2007/Base.php | 1495 +++++++++-------- src/PhpWord/Writer/Word2007/ContentTypes.php | 88 +- src/PhpWord/Writer/Word2007/DocProps.php | 2 +- src/PhpWord/Writer/Word2007/Document.php | 168 +- src/PhpWord/Writer/Word2007/DocumentRels.php | 50 +- src/PhpWord/Writer/Word2007/Footer.php | 17 +- src/PhpWord/Writer/Word2007/Footnotes.php | 24 +- src/PhpWord/Writer/Word2007/FootnotesRels.php | 36 +- src/PhpWord/Writer/Word2007/Header.php | 19 +- src/PhpWord/Writer/Word2007/Rels.php | 43 +- src/PhpWord/Writer/Word2007/Styles.php | 34 +- src/PhpWord/Writer/Writer.php | 2 +- 31 files changed, 1019 insertions(+), 1193 deletions(-) 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": { 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/Footer/PreserveText.php b/src/PhpWord/Section/Footer/PreserveText.php index 579ca271..6fbd7bba 100644 --- a/src/PhpWord/Section/Footer/PreserveText.php +++ b/src/PhpWord/Section/Footer/PreserveText.php @@ -27,14 +27,14 @@ class PreserveText /** * Text style * - * @var \PhpOffice\PhpWord\Style\Font + * @var string|Font */ private $_styleFont; /** * Paragraph style * - * @var \PhpOffice\PhpWord\Style\Paragraph + * @var string|Paragraph */ private $_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/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/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) { 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/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 4e75068d..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); @@ -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); } } } @@ -288,11 +288,11 @@ 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) + protected function writeText(XMLWriter $xmlWriter, Text $text, $withoutP = false) { $styleFont = $text->getFontStyle(); $styleParagraph = $text->getParagraphStyle(); @@ -336,18 +336,18 @@ 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) + 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); } } } @@ -357,9 +357,9 @@ class Content extends WriterPart /** * Write TextBreak * - * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param XMLWriter $xmlWriter */ - protected function _writeTextBreak(XMLWriter $xmlWriter = null) + protected function writeTextBreak(XMLWriter $xmlWriter = null) { $xmlWriter->startElement('text:p'); $xmlWriter->writeAttribute('text:style-name', 'Standard'); @@ -370,20 +370,20 @@ 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) + private function writeEndSection(XMLWriter $xmlWriter = null, Section $section = null) { } /** * 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) + private function writeSection(XMLWriter $xmlWriter = null, Section $section = null) { } // @codeCoverageIgnoreEnd @@ -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 cb92ddcc..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) @@ -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); @@ -97,9 +97,9 @@ class Manifest extends WriterPart * * @param string $pFile Filename * @return string Mime Type - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws Exception */ - private function _getImageMimeType($pFile = '') + private function getImageMimeType($pFile = '') { if (file_exists($pFile)) { $image = getimagesize($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 d7fc7101..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() { @@ -179,10 +179,9 @@ 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 + // Font if ($style instanceof Font) { if (in_array($style->getName(), $arrFonts) == false) { $arrFonts[] = $style->getName(); @@ -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..4c55006f 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -34,19 +34,19 @@ 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 * - * @param PhpOffice\PhpWord\PhpWord + * @param PhpWord */ public function __construct(PhpWord $phpWord = null) { @@ -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..3acf6c1d 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -10,23 +10,25 @@ 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; +use PhpOffice\PhpWord\Style\Table as TableStyle; +use PhpOffice\PhpWord\Style\Image as ImageStyle; /** * Word2007 base part writer @@ -38,55 +40,31 @@ 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(); $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,55 +73,693 @@ 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(); + $styleParagraph = $link->getParagraphStyle(); + + 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 PreserveText $textrun + */ + protected function writePreserveText( + XMLWriter $xmlWriter, + PreserveText $textrun + ) { + $styleFont = $textrun->getFontStyle(); + $styleParagraph = $textrun->getParagraphStyle(); + + $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; + $styleFont = null; + $styleParagraph = null; + if (!is_null($element)) { + $styleFont = $element->getFontStyle(); + $styleParagraph = $element->getParagraphStyle(); + $hasStyle = !is_null($styleFont) || !is_null($styleParagraph); + } + if ($hasStyle) { + $xmlWriter->startElement('w:p'); + $this->writeInlineParagraphStyle($xmlWriter, $styleParagraph); + if (!is_null($styleFont)) { + $xmlWriter->startElement('w:pPr'); + $this->writeInlineFontStyle($xmlWriter, $styleFont); + $xmlWriter->endElement(); // w:pPr + } + $xmlWriter->endElement(); // w:p + } else { + // 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(); + $depth = $listItem->getDepth(); + $listType = $listItem->getStyle()->getListType(); + $styleParagraph = $textObject->getParagraphStyle(); + + $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 TableStyle) { + $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 ImageStyle::WRAPPING_STYLE_BEHIND: + $imgStyle .= 'position:absolute;z-index:-251658752;'; + break; + case ImageStyle::WRAPPING_STYLE_SQUARE: + $imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; + break; + 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 ImageStyle::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(); + $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(); + $styleParagraph = $checkbox->getParagraphStyle(); + + 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 \PhpOffice\PhpWord\Style\Paragraph $style + * @param XMLWriter $xmlWriter + * @param Paragraph $style * @param bool $withoutPPR */ - protected function _writeParagraphStyle( + protected function writeParagraphStyle( XMLWriter $xmlWriter, Paragraph $style, $withoutPPR = false @@ -240,173 +856,12 @@ class Base extends WriterPart } /** - * Write link element + * Write font style * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section\Link $link - * @param boolean $withoutP + * @param XMLWriter $xmlWriter + * @param Font $style */ - 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 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,209 +959,16 @@ 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 PhpOffice\PhpWord\Style\Table $style + * @param XMLWriter $xmlWriter + * @param TableStyle $style * @param boolean $isFullStyle */ - protected function _writeTableStyle( + protected function writeTableStyle( XMLWriter $xmlWriter, - \PhpOffice\PhpWord\Style\Table $style, + TableStyle $style, $isFullStyle = true ) { $bgColor = $style->getBgColor(); @@ -822,7 +1084,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,14 +1092,14 @@ 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 + * @param TableStyle $style */ - protected function _writeRowStyle( + protected function writeRowStyle( XMLWriter $xmlWriter, $type, - \PhpOffice\PhpWord\Style\Table $style + TableStyle $style ) { $brdSz = $style->getBorderSize(); $brdCol = $style->getBorderColor(); @@ -847,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); @@ -900,10 +1161,10 @@ class Base extends WriterPart /** * Write footnote reference element * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Style\Cell $style + * @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(); @@ -1007,303 +1268,91 @@ 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, + $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 - } - } - - /** - * Write footnote reference element - * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param mixed $image - */ - 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'); - $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 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 + } else { + throw new Exception("Invalid parameters passed."); } } /** - * Write CheckBox + * Write inline paragraph style * - * @param boolean $withoutP - * @param boolean $checkState + * @param XMLWriter $xmlWriter + * @param Paragraph|string $styleParagraph + * @param boolean $withoutPPR */ - 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'); + protected function writeInlineParagraphStyle( + XMLWriter $xmlWriter, + $styleParagraph = null, + $withoutPPR = false + ) { + if ($styleParagraph instanceof Paragraph) { + $this->writeParagraphStyle($xmlWriter, $styleParagraph, $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(); + } + } + } + } + + /** + * Write inline font style + * + * @param XMLWriter $xmlWriter + * @param Font|string $styleFont + */ + protected function writeInlineFontStyle( + XMLWriter $xmlWriter, + $styleFont = null + ) { + if ($styleFont instanceof Font) { + $this->writeFontStyle($xmlWriter, $styleFont); + } else { + if (!is_null($styleFont)) { + $xmlWriter->startElement('w:rPr'); + $xmlWriter->startElement('w:rStyle'); + $xmlWriter->writeAttribute('w:val', $styleFont); + $xmlWriter->endElement(); $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'); - 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(); // 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..65c75384 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,32 +158,15 @@ 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 * - * @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 = '') + private function writeDefaultContentType(XMLWriter $xmlWriter = null, $pPartname = '', $pContentType = '') { if ($pPartname != '' && $pContentType != '') { // Write content type @@ -199,12 +182,12 @@ 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 = '') + 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 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/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 585050df..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) { @@ -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); } } } @@ -118,14 +118,14 @@ 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) + 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(); } @@ -133,10 +133,10 @@ 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) + private function writeEndSection(XMLWriter $xmlWriter, Section $section) { $settings = $section->getSettings(); $_headers = $section->getHeaders(); @@ -272,9 +272,9 @@ class Document extends Base /** * Write page break element * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param 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 + * @param 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..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(); @@ -50,17 +51,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..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 */ - private 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'); @@ -118,9 +110,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..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(); @@ -51,21 +52,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..3e41033f 100755 --- a/src/PhpWord/Writer/Word2007/Rels.php +++ b/src/PhpWord/Writer/Word2007/Rels.php @@ -16,12 +16,12 @@ use PhpOffice\PhpWord\Shared\XMLWriter; /** * Word2007 rels part writer */ -class Rels extends WriterPart +class Rels extends Base { /** * Write _rels/.rels * - * @param PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord */ public function writeRelationships(PhpWord $phpWord = null) { @@ -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..c7b35faf 100644 --- a/src/PhpWord/Writer/Word2007/Styles.php +++ b/src/PhpWord/Writer/Word2007/Styles.php @@ -14,28 +14,20 @@ 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 */ class Styles extends Base { - /** - * PhpWord object - * - * @var PhpWord - */ - private $phpWord; - /** * Write word/styles.xml * - * @param PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord */ public function writeStyles(PhpWord $phpWord = null) { - $this->phpWord = $phpWord; - // Create XML writer $xmlWriter = $this->getXmlWriter(); @@ -52,7 +44,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 +86,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,10 +119,10 @@ class Styles extends Base $xmlWriter->endElement(); } - $this->_writeParagraphStyle($xmlWriter, $style); + $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'); @@ -144,7 +136,7 @@ class Styles extends Base $xmlWriter->writeAttribute('w:val', '99'); $xmlWriter->endElement(); - $this->_writeTableStyle($xmlWriter, $style); + $this->writeTableStyle($xmlWriter, $style); $xmlWriter->endElement(); // w:style } @@ -160,13 +152,13 @@ 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, $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 +189,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 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;