diff --git a/CHANGELOG.md b/CHANGELOG.md index 30be78e6..f97c4c07 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,15 +7,22 @@ This is the changelog between releases of PHPWord. Releases are listed in revers ### Features - Image: Get image dimensions without EXIF extension - @andrew-kzoo GH-184 -- Table: Add tblGrid element for Libre/Open Office table sizing - @gianis6 GH-183 +- 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 +- Font: Add `bgColor` to font style to define background using HEX color - @jcarignan GH-168 +- Table: Add `exactHeight` to row style to define whether row height should be exact or atLeast - @jcarignan GH-168 +- Element: New `CheckBox` element for sections and table cells - @ozilion GH-156 ### Bugfixes -- +- Footnote: Footnote content doesn't show footnote reference number - @ivanlanin GH-170 ### 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/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/docs/elements.rst b/docs/elements.rst index d0f78d2e..bded72b8 100644 --- a/docs/elements.rst +++ b/docs/elements.rst @@ -67,6 +67,7 @@ Available font styles: - ``strikethrough`` Strikethrough, *true* or *false* - ``color`` Font color, e.g. *FF0000* - ``fgColor`` Font highlight color, e.g. *yellow*, *green*, *blue* +- ``bgColor`` Font background color, e.g. *FF0000* Paragraph style ^^^^^^^^^^^^^^^ @@ -201,27 +202,28 @@ Table, row, and cell styles Table styles: -- ``$width`` Table width in percent -- ``$bgColor`` Background color, e.g. '9966CC' -- ``$border(Top|Right|Bottom|Left)Size`` Border size in twips -- ``$border(Top|Right|Bottom|Left)Color`` Border color, e.g. '9966CC' -- ``$cellMargin(Top|Right|Bottom|Left)`` Cell margin in twips +- ``width`` Table width in percent +- ``bgColor`` Background color, e.g. '9966CC' +- ``border(Top|Right|Bottom|Left)Size`` Border size in twips +- ``border(Top|Right|Bottom|Left)Color`` Border color, e.g. '9966CC' +- ``cellMargin(Top|Right|Bottom|Left)`` Cell margin in twips Row styles: - ``tblHeader`` Repeat table row on every new page, *true* or *false* - ``cantSplit`` Table row cannot break across pages, *true* or *false* +- ``exactHeight`` Row height is exact or at least Cell styles: -- ``$width`` Cell width in twips -- ``$valign`` Vertical alignment, *top*, *center*, *both*, *bottom* -- ``$textDirection`` Direction of text -- ``$bgColor`` Background color, e.g. '9966CC' -- ``$border(Top|Right|Bottom|Left)Size`` Border size in twips -- ``$border(Top|Right|Bottom|Left)Color`` Border color, e.g. '9966CC' -- ``$gridSpan`` Number of columns spanned -- ``$vMerge`` *restart* or *continue* +- ``width`` Cell width in twips +- ``valign`` Vertical alignment, *top*, *center*, *both*, *bottom* +- ``textDirection`` Direction of text +- ``bgColor`` Background color, e.g. '9966CC' +- ``border(Top|Right|Bottom|Left)Size`` Border size in twips +- ``border(Top|Right|Bottom|Left)Color`` Border color, e.g. '9966CC' +- ``gridSpan`` Number of columns spanned +- ``vMerge`` *restart* or *continue* Cell span ~~~~~~~~~ @@ -326,7 +328,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 +338,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 +352,23 @@ 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)``; + +Checkboxes +---------- + +Checkbox elements can be added to sections or table cells by using +``addCheckBox``. + +.. code-block:: php + + $section->addCheckBox($name, $text, [$fontStyle], [$paragraphStyle]) + +- ``$name`` Name of the check box. +- ``$text`` Text following the check box +- ``$fontStyle`` See "Font style" section. +- ``$paragraphStyle`` See "Paragraph style" section. 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/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/samples/Sample_20_BGColor.php b/samples/Sample_20_BGColor.php new file mode 100644 index 00000000..46a232f7 --- /dev/null +++ b/samples/Sample_20_BGColor.php @@ -0,0 +1,23 @@ +createSection(); + +$section->addText("This is some text highlighted using fgColor (limited to 15 colors) ", array("fgColor" => \PhpOffice\PhpWord\Style\Font::FGCOLOR_YELLOW)); +$section->addText("This one uses bgColor and is using hex value (0xfbbb10)", array("bgColor" => "fbbb10")); +$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; + $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, $writer); + $xmlWriter->save("{$name}.{$extension}"); + rename("{$name}.{$extension}", "results/{$name}.{$extension}"); +} + +include_once 'Sample_Footer.php'; diff --git a/samples/Sample_21_TableRowRules.php b/samples/Sample_21_TableRowRules.php new file mode 100644 index 00000000..c02ac2ba --- /dev/null +++ b/samples/Sample_21_TableRowRules.php @@ -0,0 +1,40 @@ +createSection(); + +$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); +$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 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("exactHeight" => true)); +$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('exactHeight'=>true));"); + +// 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; + $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, $writer); + $xmlWriter->save("{$name}.{$extension}"); + rename("{$name}.{$extension}", "results/{$name}.{$extension}"); +} + +include_once 'Sample_Footer.php'; diff --git a/samples/Sample_22_CheckBox.php b/samples/Sample_22_CheckBox.php new file mode 100644 index 00000000..505d3518 --- /dev/null +++ b/samples/Sample_22_CheckBox.php @@ -0,0 +1,27 @@ +createSection(); +$section->addText('Check box in section'); +$section->addCheckBox('chkBox1', 'Checkbox 1'); +$section->addText('Check box in table cell'); +$table = $section->addTable(); +$table->addRow(); +$cell = $table->addCell(); +$cell->addCheckBox('chkBox2', 'Checkbox 2'); + +// 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; + $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, $writer); + $xmlWriter->save("{$name}.{$extension}"); + rename("{$name}.{$extension}", "results/{$name}.{$extension}"); +} + +include_once 'Sample_Footer.php'; 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/DocumentProperties.php b/src/PhpWord/DocumentProperties.php index cc24e35a..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(); @@ -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; @@ -551,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 @@ -585,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/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/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 d742f955..9161d162 100644 --- a/src/PhpWord/Reader/Word2007.php +++ b/src/PhpWord/Reader/Word2007.php @@ -17,7 +17,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/Section.php b/src/PhpWord/Section.php index e63ed8ab..090ca236 100644 --- a/src/PhpWord/Section.php +++ b/src/PhpWord/Section.php @@ -22,6 +22,7 @@ use PhpOffice\PhpWord\Section\Text; use PhpOffice\PhpWord\Section\TextBreak; use PhpOffice\PhpWord\Section\TextRun; use PhpOffice\PhpWord\Section\Title; +use PhpOffice\PhpWord\Section\CheckBox; use PhpOffice\PhpWord\Shared\String; /** @@ -115,7 +116,7 @@ class Section */ public function addText($text, $styleFont = null, $styleParagraph = null) { - if (!String::IsUTF8($text)) { + if (!String::isUTF8($text)) { $text = utf8_encode($text); } $text = new Text($text, $styleFont, $styleParagraph); @@ -134,11 +135,11 @@ class Section */ public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null) { - if (!String::IsUTF8($linkSrc)) { + if (!String::isUTF8($linkSrc)) { $linkSrc = utf8_encode($linkSrc); } if (!is_null($linkName)) { - if (!String::IsUTF8($linkName)) { + if (!String::isUTF8($linkName)) { $linkName = utf8_encode($linkName); } } @@ -413,4 +414,27 @@ class Section $this->_elementCollection[] = $footnote; return $footnote; } + + /** + * Add a CheckBox Element + * + * @param string $name + * @param string $text + * @param mixed $styleFont + * @param mixed $styleParagraph + * @return \PhpOffice\PhpWord\Section\CheckBox + */ + public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null) + { + if (!String::isUTF8($name)) { + $name = utf8_encode($name); + } + if (!String::isUTF8($text)) { + $text = utf8_encode($text); + } + $element = new CheckBox($name, $text, $styleFont, $styleParagraph); + $this->_elementCollection[] = $element; + + return $element; + } } diff --git a/src/PhpWord/Section/CheckBox.php b/src/PhpWord/Section/CheckBox.php new file mode 100644 index 00000000..5e101d9f --- /dev/null +++ b/src/PhpWord/Section/CheckBox.php @@ -0,0 +1,174 @@ +setName($name); + $this->setText($text); + $paragraphStyle = $this->setParagraphStyle($paragraphStyle); + $this->setFontStyle($fontStyle, $paragraphStyle); + + return $this; + } + + /** + * Set Text style + * + * @param mixed $style + * @param mixed $paragraphStyle + * @return string|Font + */ + public function setFontStyle($style = null, $paragraphStyle = null) + { + if ($style instanceof Font) { + $this->fontStyle = $style; + $this->setParagraphStyle($paragraphStyle); + } elseif (is_array($style)) { + $this->fontStyle = new Font('text', $paragraphStyle); + $this->fontStyle->setArrayStyle($style); + } elseif (null === $style) { + $this->fontStyle = new Font('text', $paragraphStyle); + } else { + $this->fontStyle = $style; + $this->setParagraphStyle($paragraphStyle); + } + return $this->fontStyle; + } + + /** + * Get Text style + * + * @return string|Font + */ + public function getFontStyle() + { + return $this->fontStyle; + } + + /** + * Set Paragraph style + * + * @param mixed $style + * @return string|Paragraph + */ + public function setParagraphStyle($style = null) + { + if (is_array($style)) { + $this->paragraphStyle = new Paragraph; + $this->paragraphStyle->setArrayStyle($style); + } elseif ($style instanceof Paragraph) { + $this->paragraphStyle = $style; + } elseif (null === $style) { + $this->paragraphStyle = new Paragraph; + } else { + $this->paragraphStyle = $style; + } + return $this->paragraphStyle; + } + + /** + * Get Paragraph style + * + * @return string|Paragraph + */ + public function getParagraphStyle() + { + return $this->paragraphStyle; + } + + /** + * Set name content + * + * @param string $name + * @return $this + */ + public function setName($name) + { + $this->name = $name; + return $this; + } + + /** + * Get name content + * + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * Set text content + * + * @param string $text + * @return $this + */ + public function setText($text) + { + $this->text = $text; + return $this; + } + + /** + * Get text content + * + * @return string + */ + public function getText() + { + return $this->text; + } +} 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/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/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/Table/Cell.php b/src/PhpWord/Section/Table/Cell.php index 601f6030..e0004350 100755 --- a/src/PhpWord/Section/Table/Cell.php +++ b/src/PhpWord/Section/Table/Cell.php @@ -21,6 +21,7 @@ use PhpOffice\PhpWord\Section\Object; use PhpOffice\PhpWord\Section\Text; use PhpOffice\PhpWord\Section\TextBreak; use PhpOffice\PhpWord\Section\TextRun; +use PhpOffice\PhpWord\Section\CheckBox; use PhpOffice\PhpWord\Shared\String; /** @@ -290,6 +291,28 @@ class Cell return $textRun; } + /** + * Add a CheckBox Element + * + * @param string $name + * @param string $text + * @param mixed $styleFont + * @param mixed $styleParagraph + * @return \PhpOffice\PhpWord\Section\CheckBox + */ + public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null) + { + if (!String::isUTF8($name)) { + $name = utf8_encode($name); + } + if (!String::isUTF8($text)) { + $text = utf8_encode($text); + } + $text = new CheckBox($name, $text, $styleFont, $styleParagraph); + $this->_elementCollection[] = $text; + return $text; + } + /** * Get all Elements * 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/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() diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index 3ab855d6..d1f5353a 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -135,6 +135,18 @@ class Font */ private $_fgColor = null; + /** + * Background color + * + * @var string + */ + private $_bgColor = null; + /** + * Text line height + * + * @var int + */ + /** * Text line height * @@ -454,6 +466,28 @@ class Font return $this; } + /** + * Get background color + * + * @return string + */ + public function getBgColor() + { + return $this->_bgColor; + } + + /** + * Set background color + * + * @param string $pValue + * @return $this + */ + public function setBgColor($pValue = null) + { + $this->_bgColor = $pValue; + return $this; + } + /** * Get style type * diff --git a/src/PhpWord/Style/Row.php b/src/PhpWord/Style/Row.php index 1ecb044b..2b714024 100644 --- a/src/PhpWord/Style/Row.php +++ b/src/PhpWord/Style/Row.php @@ -28,6 +28,13 @@ class Row */ private $_cantSplit = false; + /** + * Table row exact height + * + * @var bool + */ + private $_exactHeight = false; + /** * Create a new row style */ @@ -50,7 +57,7 @@ class Row * Set tblHeader * * @param boolean $pValue - * @return PHPWord_Style_Row + * @return $this */ public function setTblHeader($pValue = false) { @@ -75,7 +82,7 @@ class Row * Set cantSplit * * @param boolean $pValue - * @return PHPWord_Style_Row + * @return $this */ public function setCantSplit($pValue = false) { @@ -95,4 +102,29 @@ class Row { return $this->_cantSplit; } + + /** + * Set exactHeight + * + * @param bool $pValue + * @return $this + */ + public function setExactHeight($pValue = false) + { + if (!is_bool($pValue)) { + $pValue = false; + } + $this->_exactHeight = $pValue; + return $this; + } + + /** + * Get exactHeight + * + * @return boolean + */ + public function getExactHeight() + { + return $this->_exactHeight; + } } 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 101da77f..7ff31bf0 100755 --- a/src/PhpWord/Writer/ODText.php +++ b/src/PhpWord/Writer/ODText.php @@ -22,91 +22,48 @@ 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 + * @var 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 - * @param \PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord */ public function __construct(PhpWord $phpWord = null) { // 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(); } /** * Save PhpWord to file * * @param string $pFilename - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws Exception */ 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('./', 'phppttmp'); - 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 $zipClass = Settings::getZipClass(); @@ -132,19 +89,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 @@ -187,111 +144,19 @@ 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 * - * @return \PhpOffice\PhpWord\HashTable + * @return HashTable */ 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..2e9a61e6 100644 --- a/src/PhpWord/Writer/ODText/Content.php +++ b/src/PhpWord/Writer/ODText/Content.php @@ -35,18 +35,13 @@ 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) { // 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'); @@ -131,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(); @@ -163,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); @@ -249,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) { @@ -276,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); } } } @@ -293,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(); @@ -341,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); } } } @@ -362,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'); @@ -375,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 @@ -396,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 f1c652ca..31168e3d 100755 --- a/src/PhpWord/Writer/ODText/Manifest.php +++ b/src/PhpWord/Writer/ODText/Manifest.php @@ -21,18 +21,13 @@ 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) { // 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'); @@ -69,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); @@ -102,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 69cc5aea..51647173 100644 --- a/src/PhpWord/Writer/ODText/Meta.php +++ b/src/PhpWord/Writer/ODText/Meta.php @@ -20,18 +20,13 @@ 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) { // 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/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 f2d46c6a..4ae937e0 100644 --- a/src/PhpWord/Writer/ODText/Styles.php +++ b/src/PhpWord/Writer/ODText/Styles.php @@ -24,18 +24,13 @@ 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) { // 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'); @@ -78,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(); @@ -149,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'); @@ -173,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); @@ -188,7 +183,7 @@ class Styles extends WriterPart $xmlWriter->endElement(); } elseif ($style instanceof Table) { - // PhpOffice\PhpWord\Style\Table + // Table } } } 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 b9aa3aa3..e37e53b3 100755 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -31,46 +31,39 @@ 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 + * @var 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 - * @param \PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord */ public function __construct(PhpWord $phpWord = null) { @@ -78,79 +71,38 @@ class RTF implements IWriter $this->setPhpWord($phpWord); // Set HashTable variables - $this->_drawingHashTable = new HashTable(); + $this->drawingHashTable = new HashTable(); } /** * Save PhpWord to file * * @param string $pFilename - * @throws \PhpOffice\PhpWord\Exceptions\Exception + * @throws Exception */ 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('./', 'phppttmp'); - 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 * - * @return \PhpOffice\PhpWord\HashTable + * @return HashTable */ 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,19 +170,18 @@ 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(); - $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(); @@ -273,14 +224,13 @@ 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(); - $numPStyles = 0; if (count($styles) > 0) { foreach ($styles as $styleName => $style) { // Font @@ -334,7 +284,7 @@ class RTF implements IWriter */ private function getDataContent() { - $phpWord = $this->_document; + $phpWord = $this->phpWord; $sRTFBody = ''; $_sections = $phpWord->getSections(); @@ -400,7 +350,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 +360,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 +378,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; } @@ -438,14 +388,14 @@ class RTF implements IWriter if ($styleFont->getBold()) { $sRTFText .= '\b'; } - if ($styleFont->getBold()) { + if ($styleFont->getItalic()) { $sRTFText .= '\i'; } if ($styleFont->getSize()) { $sRTFText .= '\fs' . ($styleFont->getSize() * 2); } } - if ($this->_lastParagraphStyle != '' || $styleFont) { + if ($this->lastParagraphStyle != '' || $styleFont) { $sRTFText .= ' '; } $sRTFText .= $text->getText(); @@ -501,7 +451,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 689b0bb1..687bf8d2 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -28,73 +28,44 @@ 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 * * @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) { - $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); } } @@ -106,16 +77,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('./', 'phppttmp'); - if ($pFilename == '') { - $pFilename = $originalFilename; - } - } + if (!is_null($this->phpWord)) { + $pFilename = $this->getTempFile($pFilename); // Create new ZIP file and open it for writing $zipClass = Settings::getZipClass(); @@ -143,7 +106,7 @@ class Word2007 implements IWriter $_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; } @@ -153,7 +116,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); } } } @@ -163,7 +126,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); } } } @@ -178,7 +141,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) { @@ -217,18 +180,18 @@ class Word2007 implements IWriter $objZip->addFromString( '[Content_Types].xml', $this->getWriterPart('contenttypes')->writeContentTypes( - $this->_imageTypes, - $this->_objectTypes, + $this->imageTypes, + $this->objectTypes, $_cHdrs, $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'); @@ -237,19 +200,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."); } @@ -292,79 +248,23 @@ class Word2007 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; } } } - /** - * 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/Base.php b/src/PhpWord/Writer/Word2007/Base.php index 733b376a..3acf6c1d 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -10,22 +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 @@ -37,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 } @@ -94,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->_writeFootnoteReference($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 @@ -239,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(); @@ -413,6 +869,7 @@ class Base extends WriterPart $color = $style->getColor(); $size = $style->getSize(); $fgColor = $style->getFgColor(); + $bgColor = $style->getBgColor(); $strikethrough = $style->getStrikethrough(); $underline = $style->getUnderline(); $superscript = $style->getSuperScript(); @@ -483,6 +940,15 @@ class Base extends WriterPart $xmlWriter->endElement(); } + // Background-Color + if (!is_null($bgColor)) { + $xmlWriter->startElement('w:shd'); + $xmlWriter->writeAttribute('w:val', "clear"); + $xmlWriter->writeAttribute('w:color', "auto"); + $xmlWriter->writeAttribute('w:fill', $bgColor); + $xmlWriter->endElement(); + } + // Superscript/subscript if ($superscript || $subscript) { $xmlWriter->startElement('w:vertAlign'); @@ -493,203 +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'); - $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(); - - $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->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); - } - } - } 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(); @@ -805,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); } } } @@ -813,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(); @@ -830,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); @@ -883,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(); @@ -990,251 +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 + $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 $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'); - $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 - * - * @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); + 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(); } } } - - $xmlWriter->endElement(); // w:p - $xmlWriter->endElement(); // w:footnote } /** - * Write footnote reference element + * Write inline font style * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param PhpOffice\PhpWord\Section\Footnote $footnote - * @param boolean $withoutP + * @param XMLWriter $xmlWriter + * @param Font|string $styleFont */ - protected function _writeFootnoteReference(XMLWriter $xmlWriter, Footnote $footnote, $withoutP = false) - { - if (!$withoutP) { - $xmlWriter->startElement('w:p'); - } - - $xmlWriter->startElement('w:r'); - - $xmlWriter->startElement('w:footnoteReference'); - $xmlWriter->writeAttribute('w:id', $footnote->getReferenceId()); - $xmlWriter->endElement(); // w:footnoteReference - - $xmlWriter->endElement(); // w:r - - if (!$withoutP) { - $xmlWriter->endElement(); // w:p + 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(); + } } } } diff --git a/src/PhpWord/Writer/Word2007/ContentTypes.php b/src/PhpWord/Writer/Word2007/ContentTypes.php index ea97d138..65c75384 100755 --- a/src/PhpWord/Writer/Word2007/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/ContentTypes.php @@ -19,20 +19,15 @@ 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 = 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'); @@ -42,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' @@ -70,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' @@ -148,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' @@ -163,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 @@ -204,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 @@ -221,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 3c7d6977..92c2fab8 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'); @@ -115,17 +110,12 @@ class DocProps extends WriterPart /** * Write docProps/core.xml * - * @param PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord */ 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 ffce32d2..03184bd1 100644 --- a/src/PhpWord/Writer/Word2007/Document.php +++ b/src/PhpWord/Writer/Word2007/Document.php @@ -22,6 +22,7 @@ use PhpOffice\PhpWord\Section\Text; use PhpOffice\PhpWord\Section\TextBreak; use PhpOffice\PhpWord\Section\TextRun; use PhpOffice\PhpWord\Section\Title; +use PhpOffice\PhpWord\Section\CheckBox; use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; @@ -35,16 +36,12 @@ class Document extends Base /** * Write word/document.xml * - * @param PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord */ 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'); @@ -73,39 +70,40 @@ class Document extends Base $pSection++; $_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->_writeFootnoteReference($xmlWriter, $element); + $this->writeFootnote($xmlWriter, $element); + } elseif ($element instanceof CheckBox) { + $this->writeCheckBox($xmlWriter, $element); } } if ($pSection == $countSections) { - $this->_writeEndSection($xmlWriter, $section); + $this->writeEndSection($xmlWriter, $section); } else { - $this->_writeSection($xmlWriter, $section); + $this->writeSection($xmlWriter, $section); } } } @@ -120,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(); } @@ -135,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(); @@ -274,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'); @@ -287,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(); @@ -423,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) { @@ -481,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 218869b5..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 @@ -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'); @@ -40,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', @@ -48,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', @@ -56,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', @@ -64,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', @@ -72,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', @@ -80,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', @@ -94,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, @@ -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'); @@ -138,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, @@ -152,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 a2ff09b4..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,17 +26,12 @@ 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 = 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'); @@ -55,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 59d8cc1f..ce4aba60 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; /** @@ -25,21 +29,20 @@ 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'); - $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 +53,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 +64,61 @@ 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 XMLWriter $xmlWriter + * @param Footnote $footnote + * @param boolean $withoutP + */ + 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 + $styleParagraph = $footnote->getParagraphStyle(); + $this->writeInlineParagraphStyle($xmlWriter, $styleParagraph); + // 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/FootnotesRels.php b/src/PhpWord/Writer/Word2007/FootnotesRels.php index 7f87ce30..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 @@ -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'); @@ -46,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(); @@ -54,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 bdec8d45..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,16 +26,12 @@ 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 - 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'); @@ -55,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 a5f5084e..3e41033f 100755 --- a/src/PhpWord/Writer/Word2007/Rels.php +++ b/src/PhpWord/Writer/Word2007/Rels.php @@ -16,22 +16,17 @@ 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) { // 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'); @@ -43,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', @@ -51,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', @@ -59,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', @@ -70,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 dc665300..c7b35faf 100644 --- a/src/PhpWord/Writer/Word2007/Styles.php +++ b/src/PhpWord/Writer/Word2007/Styles.php @@ -14,67 +14,37 @@ 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 $_document; - /** * Write word/styles.xml * - * @param PhpOffice\PhpWord\PhpWord $phpWord + * @param PhpWord $phpWord */ 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->_document = $phpWord; + $xmlWriter = $this->getXmlWriter(); // 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, $phpWord, $styles); // Write other styles if (count($styles) > 0) { foreach ($styles as $styleName => $style) { @@ -116,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(); @@ -149,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'); @@ -166,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 } @@ -180,36 +150,65 @@ class Styles extends Base } /** - * Write document defaults + * Write default font and other default styles * - * @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param XMLWriter $xmlWriter + * @param array $styles */ - private function _writeDocDefaults(XMLWriter $xmlWriter) + private function writeDefaultStyles(XMLWriter $xmlWriter, PhpWord $phpWord, $styles) { - $fontName = $this->_document->getDefaultFontName(); - $fontSize = $this->_document->getDefaultFontSize(); + $fontName = $phpWord->getDefaultFontName(); + $fontSize = $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/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..bc749738 --- /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); + } + } +} 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/CheckBoxTest.php b/tests/PhpWord/Tests/Section/CheckBoxTest.php new file mode 100644 index 00000000..d07a0b69 --- /dev/null +++ b/tests/PhpWord/Tests/Section/CheckBoxTest.php @@ -0,0 +1,79 @@ +assertInstanceOf('PhpOffice\\PhpWord\\Section\\CheckBox', $oCheckBox); + $this->assertEquals(null, $oCheckBox->getText()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $oCheckBox->getFontStyle()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oCheckBox->getParagraphStyle()); + } + + /** + * Get name and text + */ + public function testCheckBox() + { + $oCheckBox = new CheckBox('chkBox', 'CheckBox'); + + $this->assertEquals($oCheckBox->getName(), 'chkBox'); + $this->assertEquals($oCheckBox->getText(), 'CheckBox'); + } + + /** + * Get font style + */ + public function testFont() + { + $oCheckBox = new CheckBox('chkBox', 'CheckBox', 'fontStyle'); + $this->assertEquals($oCheckBox->getFontStyle(), 'fontStyle'); + + $oCheckBox->setFontStyle(array('bold' => true, 'italic' => true, 'size' => 16)); + $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 + */ + public function testParagraph() + { + $oCheckBox = new CheckBox('chkBox', 'CheckBox', 'fontStyle', 'paragraphStyle'); + $this->assertEquals($oCheckBox->getParagraphStyle(), 'paragraphStyle'); + + $oCheckBox->setParagraphStyle(array('align' => 'center', 'spaceAfter' => 100)); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oCheckBox->getParagraphStyle()); + } +} 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/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 19cf58c7..fc537ab8 100644 --- a/tests/PhpWord/Tests/Section/FootnoteTest.php +++ b/tests/PhpWord/Tests/Section/FootnoteTest.php @@ -14,11 +14,13 @@ 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 + */ public function testConstruct() { $oFootnote = new Footnote(); @@ -28,6 +30,9 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oFootnote->getParagraphStyle(), null); } + /** + * New instance with string parameter + */ public function testConstructString() { $oFootnote = new Footnote('pStyle'); @@ -35,6 +40,9 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oFootnote->getParagraphStyle(), 'pStyle'); } + /** + * New instance with array parameter + */ public function testConstructArray() { $oFootnote = new Footnote(array('spacing' => 100)); @@ -45,6 +53,9 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase ); } + /** + * Add text element + */ public function testAddText() { $oFootnote = new Footnote(); @@ -54,6 +65,20 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Text', $element); } + /** + * Add text break element + */ + public function testAddTextBreak() + { + $oFootnote = new Footnote(); + $oFootnote->addTextBreak(2); + + $this->assertCount(2, $oFootnote->getElements()); + } + + /** + * Add link element + */ public function testAddLink() { $oFootnote = new Footnote(); @@ -63,6 +88,9 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Link', $element); } + /** + * Set/get reference Id + */ public function testReferenceId() { $oFootnote = new Footnote(); @@ -72,6 +100,9 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oFootnote->getReferenceId(), $iVal); } + /** + * Get elements + */ public function testGetElements() { $oFootnote = new Footnote(); 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/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/CellTest.php b/tests/PhpWord/Tests/Section/Table/CellTest.php index 9759995d..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,6 +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(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/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/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/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/SectionTest.php b/tests/PhpWord/Tests/SectionTest.php index 448361ab..48b5bb73 100644 --- a/tests/PhpWord/Tests/SectionTest.php +++ b/tests/PhpWord/Tests/SectionTest.php @@ -88,12 +88,13 @@ class SectionTest extends \PHPUnit_Framework_TestCase $section->addTitle(utf8_decode('ä'), 1); $section->createTextRun(); $section->createFootnote(); + $section->addCheckBox(utf8_decode('chkä'), utf8_decode('Contentä')); $section->addTOC(); $elementCollection = $section->getElements(); $elementTypes = array('Text', 'Link', 'TextBreak', 'PageBreak', 'Table', 'ListItem', 'Object', 'Image', 'Image', - 'Title', 'TextRun', 'Footnote'); + 'Title', 'TextRun', 'Footnote', 'CheckBox'); $i = 0; foreach ($elementTypes as $elementType) { $this->assertInstanceOf("PhpOffice\\PhpWord\\Section\\{$elementType}", $elementCollection[$i]); 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 482bfe91..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(); @@ -55,6 +57,7 @@ class FontTest extends \PHPUnit_Framework_TestCase 'strikethrough' => false, 'color' => PhpWord::DEFAULT_FONT_COLOR, 'fgColor' => null, + 'bgColor' => null, 'hint' => PhpWord::DEFAULT_FONT_CONTENT_TYPE, ); foreach ($attributes as $key => $default) { @@ -83,7 +86,8 @@ class FontTest extends \PHPUnit_Framework_TestCase 'underline' => Font::UNDERLINE_HEAVY, 'strikethrough' => true, 'color' => '999999', - 'fgColor' => '999999', + 'fgColor' => Font::FGCOLOR_YELLOW, + 'bgColor' => 'FFFF00', 'hint' => 'eastAsia', ); $object->setArrayStyle($attributes); 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/Style/RowTest.php b/tests/PhpWord/Tests/Style/RowTest.php index daf27efb..fefb7b0a 100644 --- a/tests/PhpWord/Tests/Style/RowTest.php +++ b/tests/PhpWord/Tests/Style/RowTest.php @@ -29,6 +29,7 @@ class RowTest extends \PHPUnit_Framework_TestCase $properties = array( 'tblHeader' => true, 'cantSplit' => false, + 'exactHeight' => true, ); foreach ($properties as $key => $value) { // set/get @@ -56,6 +57,7 @@ class RowTest extends \PHPUnit_Framework_TestCase $properties = array( 'tblHeader' => 'a', 'cantSplit' => 'b', + 'exactHeight' => 'c', ); foreach ($properties as $key => $value) { $set = "set{$key}"; 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/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 ac14fd17..18524f58 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"; @@ -79,9 +80,15 @@ class BaseTest extends \PHPUnit_Framework_TestCase { $phpWord = new PhpWord(); $section = $phpWord->createSection(); + $fontStyleArray = array('bold' => true); + $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, $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'); @@ -97,8 +104,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 +206,8 @@ class BaseTest extends \PHPUnit_Framework_TestCase $styles['superScript'] = true; $styles['color'] = 'FF0000'; $styles['fgColor'] = 'yellow'; + $styles['bgColor'] = 'FFFF00'; + $styles['hint'] = 'eastAsia'; $section = $phpWord->createSection(); $section->addText('Test', $styles); @@ -219,6 +234,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; @@ -236,6 +255,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); @@ -246,6 +266,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'); @@ -352,4 +374,23 @@ class BaseTest extends \PHPUnit_Framework_TestCase $element = "/w:document/w:body/w:p/w:r/w:fldChar"; $this->assertEquals('end', $doc->getElementAttribute($element, 'w:fldCharType')); } + + /** + * 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 = '/w:document/w:body/w:p/w:r/w:fldChar/w:ffData/w:name'; + $this->assertEquals('Check1', $doc->getElementAttribute($element, 'w:val')); + } } diff --git a/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php b/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php index 6c3335d3..5318ac81 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/DocumentTest.php @@ -9,12 +9,12 @@ namespace PhpOffice\PhpWord\Tests\Writer\Word2007; use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\Style\Font; 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,11 +27,18 @@ class DocumentTest extends \PHPUnit_Framework_TestCase TestHelperDOCX::clear(); } + /** + * Write end section page numbering + */ public function testWriteEndSectionPageNumbering() { $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'); @@ -40,11 +47,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase } /** - * covers ::_writeTOC - * covers ::_writePageBreak - * covers ::_writeListItem - * covers ::_writeTitle - * covers ::_writeObject + * Write elements */ public function testElements() { @@ -87,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/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 06906b41..6baba0ac 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/FootnotesTest.php @@ -27,14 +27,21 @@ class FootnotesTest extends \PHPUnit_Framework_TestCase TestHelperDOCX::clear(); } + /** + * Write footnotes + */ 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/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/Writer/Word2007Test.php b/tests/PhpWord/Tests/Writer/Word2007Test.php index 5fcf37e2..fa4e301c 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,57 @@ 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'); + $footnote = $section->createFootnote(); + $footnote->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 +155,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() { 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 @@