diff --git a/CHANGELOG.md b/CHANGELOG.md index 84cc2cb1..fcaf1388 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r - `Writer\Word2007\Part`: `Numbering::writeNumbering()`, `Settings::writeSettings()`, `WebSettings::writeWebSettings()`, `ContentTypes::writeContentTypes()`, `Styles::writeStyles()`, `Document::writeDocument()` all changed into `write()` - `Writer\Word2007\Part\DocProps`: Split into `Writer\Word2007\Part\DocPropsCore` and `Writer\Word2007\Part\DocPropsApp` - `Element\Title::getBookmarkId()` replaced by `Element\Title::getRelationId()` +- `Writer\HTML::writeDocument`: Replaced by `Writer\HTML::getContent` ### Miscellaneous diff --git a/src/PhpWord/Autoloader.php b/src/PhpWord/Autoloader.php index c60ae9a6..c467f836 100644 --- a/src/PhpWord/Autoloader.php +++ b/src/PhpWord/Autoloader.php @@ -22,6 +22,7 @@ namespace PhpOffice\PhpWord; */ class Autoloader { + /** @const string */ const NAMESPACE_PREFIX = 'PhpOffice\\PhpWord\\'; /** diff --git a/src/PhpWord/DocumentProperties.php b/src/PhpWord/DocumentProperties.php index 95159ec0..5644c3d9 100644 --- a/src/PhpWord/DocumentProperties.php +++ b/src/PhpWord/DocumentProperties.php @@ -22,7 +22,7 @@ namespace PhpOffice\PhpWord; */ class DocumentProperties { - /** Constants */ + /** @const string Property type constants */ const PROPERTY_TYPE_BOOLEAN = 'b'; const PROPERTY_TYPE_INTEGER = 'i'; const PROPERTY_TYPE_FLOAT = 'f'; diff --git a/src/PhpWord/Shared/XMLWriter.php b/src/PhpWord/Shared/XMLWriter.php index dc85bfc1..81e8e286 100644 --- a/src/PhpWord/Shared/XMLWriter.php +++ b/src/PhpWord/Shared/XMLWriter.php @@ -68,9 +68,8 @@ class XMLWriter // Create temporary filename $this->tempFile = @tempnam($tempFolder, 'xml'); - // Open storage + // Fallback to memory when temporary file cannot be used if ($this->xmlWriter->openUri($this->tempFile) === false) { - // Fallback to memory... $this->xmlWriter->openMemory(); } } @@ -105,9 +104,16 @@ class XMLWriter * * @param mixed $function * @param mixed $args + * @throws \BadMethodCallException */ public function __call($function, $args) { + // Catch exception + if (method_exists($this->xmlWriter, $function) === false) { + throw new \BadMethodCallException("Method '{$function}' does not exists."); + } + + // Run method try { @call_user_func_array(array($this->xmlWriter, $function), $args); } catch (\Exception $ex) { diff --git a/src/PhpWord/Style/AbstractStyle.php b/src/PhpWord/Style/AbstractStyle.php index 1b916934..8167c4d2 100644 --- a/src/PhpWord/Style/AbstractStyle.php +++ b/src/PhpWord/Style/AbstractStyle.php @@ -265,7 +265,7 @@ abstract class AbstractStyle { if ($value != null && trim($value) != '' && !empty($enum) && !in_array($value, $enum)) { throw new \InvalidArgumentException('Invalid style value.'); - } elseif (is_null($value) || trim($value) == '') { + } elseif ($value === null || trim($value) == '') { $value = $default; } diff --git a/src/PhpWord/Style/Cell.php b/src/PhpWord/Style/Cell.php index 95ed13b4..2d1b88d0 100644 --- a/src/PhpWord/Style/Cell.php +++ b/src/PhpWord/Style/Cell.php @@ -144,7 +144,7 @@ class Cell extends Border */ public function getBgColor() { - if (!is_null($this->shading)) { + if ($this->shading !== null) { return $this->shading->getFill(); } else { return null; diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index 07eebdb2..0775b8b3 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -540,7 +540,7 @@ class Font extends AbstractStyle */ public function getBgColor() { - if (!is_null($this->shading)) { + if ($this->shading !== null) { return $this->shading->getFill(); } else { return null; diff --git a/src/PhpWord/Style/LineNumbering.php b/src/PhpWord/Style/LineNumbering.php index d49c7f4e..b93ce03f 100644 --- a/src/PhpWord/Style/LineNumbering.php +++ b/src/PhpWord/Style/LineNumbering.php @@ -20,11 +20,12 @@ namespace PhpOffice\PhpWord\Style; /** * Line numbering style * - * @link http://www.schemacentral.com/sc/ooxml/e-w_lnNumType-1.html + * @link http://www.schemacentral.com/sc/ooxml/t-w_CT_LineNumber.html * @since 0.10.0 */ class LineNumbering extends AbstractStyle { + /** @const string Line numbering restart setting http://www.schemacentral.com/sc/ooxml/a-w_restart-1.html */ const LINE_NUMBERING_CONTINUOUS = 'continuous'; const LINE_NUMBERING_NEW_PAGE = 'newPage'; const LINE_NUMBERING_NEW_SECTION = 'newSection'; diff --git a/src/PhpWord/Style/ListItem.php b/src/PhpWord/Style/ListItem.php index 554d75b0..a689c691 100644 --- a/src/PhpWord/Style/ListItem.php +++ b/src/PhpWord/Style/ListItem.php @@ -65,7 +65,7 @@ class ListItem extends AbstractStyle */ public function __construct($numStyle = null) { - if (!is_null($numStyle)) { + if ($numStyle !== null) { $this->setNumStyle($numStyle); } else { $this->setListType(); @@ -149,7 +149,7 @@ class ListItem extends AbstractStyle { // Check if legacy style already registered in global Style collection $numStyle = "PHPWordList{$this->listType}"; - if (!is_null(Style::getStyle($numStyle))) { + if (Style::getStyle($numStyle) !== null) { $this->setNumStyle($numStyle); return; } diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index 8609b5ab..e70833ca 100644 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -171,7 +171,7 @@ class Paragraph extends AbstractStyle */ public function getSpaceBefore() { - if (!is_null($this->spacing)) { + if ($this->spacing !== null) { return $this->spacing->getBefore(); } else { return null; @@ -196,7 +196,7 @@ class Paragraph extends AbstractStyle */ public function getSpaceAfter() { - if (!is_null($this->spacing)) { + if ($this->spacing !== null) { return $this->spacing->getAfter(); } else { return null; @@ -221,7 +221,7 @@ class Paragraph extends AbstractStyle */ public function getSpacing() { - if (!is_null($this->spacing)) { + if ($this->spacing !== null) { return $this->spacing->getLine(); } else { return null; @@ -278,7 +278,7 @@ class Paragraph extends AbstractStyle */ public function getIndent() { - if (!is_null($this->indentation)) { + if ($this->indentation !== null) { return $this->indentation->getLeft(); } else { return null; @@ -303,7 +303,7 @@ class Paragraph extends AbstractStyle */ public function getHanging() { - if (!is_null($this->indentation)) { + if ($this->indentation !== null) { return $this->indentation->getHanging(); } else { return null; diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index 9875cc26..24f50667 100644 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -169,7 +169,7 @@ class Table extends Border */ public function getBgColor() { - if (!is_null($this->shading)) { + if ($this->shading !== null) { return $this->shading->getFill(); } diff --git a/src/PhpWord/Style/TextBox.php b/src/PhpWord/Style/TextBox.php index eb215c06..9f0a1dde 100644 --- a/src/PhpWord/Style/TextBox.php +++ b/src/PhpWord/Style/TextBox.php @@ -179,7 +179,7 @@ class TextBox extends Image $hasInnerMargins = false; $margins = $this->getInnerMargin(); for ($i = 0; $i < count($margins); $i++) { - if (!is_null($margins[$i])) { + if ($margins[$i] !== null) { $hasInnerMargins = true; } } diff --git a/src/PhpWord/Writer/AbstractWriter.php b/src/PhpWord/Writer/AbstractWriter.php index 52a1a28c..367b7729 100644 --- a/src/PhpWord/Writer/AbstractWriter.php +++ b/src/PhpWord/Writer/AbstractWriter.php @@ -258,7 +258,7 @@ abstract class AbstractWriter implements WriterInterface * * @param string $filename * @return \PhpOffice\PhpWord\Shared\ZipArchive - * @throws \PhpOffice\PhpWord\Exception\Exception + * @throws \Exception */ protected function getZipArchive($filename) { @@ -271,13 +271,46 @@ abstract class AbstractWriter implements WriterInterface $zip = new ZipArchive(); if ($zip->open($filename, ZipArchive::OVERWRITE) !== true) { if ($zip->open($filename, ZipArchive::CREATE) !== true) { - throw new Exception("Could not open " . $filename . " for writing."); + throw new \Exception("Could not open '{$filename}' for writing."); } } return $zip; } + /** + * Open file for writing + * + * @param string $filename + * @return resource + * @throws \Exception + * @since 0.11.0 + */ + protected function openFile($filename) + { + $filename = $this->getTempFile($filename); + $fileHandle = fopen($filename, 'w'); + if ($fileHandle === false) { + throw new \Exception("Could not open '{$filename}' for writing."); + } + + return $fileHandle; + } + + /** + * Write content to file + * + * @param resource $fileHandle + * @param string $content + * @since 0.11.0 + */ + protected function writeFile(&$fileHandle, $content) + { + fwrite($fileHandle, $content); + fclose($fileHandle); + $this->cleanupTempFile(); + } + /** * Add files to package * diff --git a/src/PhpWord/Writer/HTML.php b/src/PhpWord/Writer/HTML.php index 88e7658d..29173ff2 100644 --- a/src/PhpWord/Writer/HTML.php +++ b/src/PhpWord/Writer/HTML.php @@ -17,7 +17,6 @@ namespace PhpOffice\PhpWord\Writer; -use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; /** @@ -69,25 +68,20 @@ class HTML extends AbstractWriter implements WriterInterface */ public function save($filename = null) { - $this->setTempDir(sys_get_temp_dir() . '/PHPWordWriter/'); - $hFile = fopen($filename, 'w'); - if ($hFile !== false) { - fwrite($hFile, $this->writeDocument()); - fclose($hFile); - } else { - throw new Exception("Can't open file"); - } - $this->clearTempDir(); + $fileHandle = $this->openFile($filename); + $this->writeFile($fileHandle, $this->getContent()); } /** - * Get phpWord data + * Get content * * @return string + * @since 0.11.0 */ - public function writeDocument() + public function getContent() { $content = ''; + $content .= '' . PHP_EOL; $content .= '' . PHP_EOL; $content .= '' . PHP_EOL; @@ -128,4 +122,16 @@ class HTML extends AbstractWriter implements WriterInterface { $this->notes[$noteId] = $noteMark; } + + /** + * Write document + * + * @return string + * @deprecated 0.11.0 + * @codeCoverageIgnore + */ + public function writeDocument() + { + return $this->getContent(); + } } diff --git a/src/PhpWord/Writer/PDF.php b/src/PhpWord/Writer/PDF.php index 98dc1220..17865563 100644 --- a/src/PhpWord/Writer/PDF.php +++ b/src/PhpWord/Writer/PDF.php @@ -65,13 +65,13 @@ class PDF * @param string $name Renderer library method name * @param mixed[] $arguments Array of arguments to pass to the renderer method * @return mixed Returned data from the PDF renderer wrapper method - * @throws \PhpOffice\PhpWord\Exception\Exception */ public function __call($name, $arguments) { - if ($this->renderer === null) { - throw new Exception("PDF Rendering library has not been defined."); - } + // Note: Commented because all exceptions should already be catched by `__construct` + // if ($this->renderer === null) { + // throw new Exception("PDF Rendering library has not been defined."); + // } return call_user_func_array(array($this->renderer, $name), $arguments); } diff --git a/src/PhpWord/Writer/PDF/DomPDF.php b/src/PhpWord/Writer/PDF/DomPDF.php index 4effc154..a40e2cea 100644 --- a/src/PhpWord/Writer/PDF/DomPDF.php +++ b/src/PhpWord/Writer/PDF/DomPDF.php @@ -50,7 +50,7 @@ class DomPDF extends AbstractRenderer implements WriterInterface // Create PDF $pdf = new \DOMPDF(); $pdf->set_paper(strtolower($paperSize), $orientation); - $pdf->load_html($this->writeDocument()); + $pdf->load_html($this->getContent()); $pdf->render(); // Write to file diff --git a/src/PhpWord/Writer/PDF/MPDF.php b/src/PhpWord/Writer/PDF/MPDF.php index d38d5b66..9d4e050c 100644 --- a/src/PhpWord/Writer/PDF/MPDF.php +++ b/src/PhpWord/Writer/PDF/MPDF.php @@ -61,7 +61,7 @@ class MPDF extends AbstractRenderer implements WriterInterface $pdf->setKeywords($docProps->getKeywords()); $pdf->setCreator($docProps->getCreator()); - $pdf->writeHTML($this->writeDocument()); + $pdf->writeHTML($this->getContent()); // Write to file fwrite($fileHandle, $pdf->output($filename, 'S')); diff --git a/src/PhpWord/Writer/PDF/TCPDF.php b/src/PhpWord/Writer/PDF/TCPDF.php index 05f02756..669a2cdd 100644 --- a/src/PhpWord/Writer/PDF/TCPDF.php +++ b/src/PhpWord/Writer/PDF/TCPDF.php @@ -54,7 +54,7 @@ class TCPDF extends AbstractRenderer implements WriterInterface $pdf->setPrintFooter(false); $pdf->addPage(); $pdf->setFont($this->getFont()); - $pdf->writeHTML($this->writeDocument()); + $pdf->writeHTML($this->getContent()); // Write document properties $phpWord = $this->getPhpWord(); diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index ef94b264..d7fed942 100644 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -17,7 +17,6 @@ namespace PhpOffice\PhpWord\Writer; -use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; /** @@ -56,29 +55,34 @@ class RTF extends AbstractWriter implements WriterInterface } /** - * Save PhpWord to file + * Save content to file * * @param string $filename * @throws \PhpOffice\PhpWord\Exception\Exception */ public function save($filename = null) { - $content = ''; - $filename = $this->getTempFile($filename); - $hFile = fopen($filename, 'w'); - if ($hFile !== false) { - $content .= '{'; - $content .= '\rtf1' . PHP_EOL; - $content .= $this->getWriterPart('Header')->write(); - $content .= $this->getWriterPart('Document')->write(); - $content .= '}'; + $fileHandle = $this->openFile($filename); + $this->writeFile($fileHandle, $this->getContent()); + } - fwrite($hFile, $content); - fclose($hFile); - } else { - throw new Exception("Can't open file"); - } - $this->cleanupTempFile(); + /** + * Get content + * + * @return string + * @since 0.11.0 + */ + private function getContent() + { + $content = ''; + + $content .= '{'; + $content .= '\rtf1' . PHP_EOL; + $content .= $this->getWriterPart('Header')->write(); + $content .= $this->getWriterPart('Document')->write(); + $content .= '}'; + + return $content; } /** diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index a0a6317a..0d688e36 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -20,9 +20,9 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; use PhpOffice\PhpWord\Settings as PhpWordSettings; use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Style; -use PhpOffice\PhpWord\Style\Font; -use PhpOffice\PhpWord\Style\Paragraph; -use PhpOffice\PhpWord\Style\Table; +use PhpOffice\PhpWord\Style\Font as FontStyle; +use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle; +use PhpOffice\PhpWord\Style\Table as TableStyle; use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter; use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter; use PhpOffice\PhpWord\Writer\Word2007\Style\Table as TableStyleWriter; @@ -31,6 +31,7 @@ use PhpOffice\PhpWord\Writer\Word2007\Style\Table as TableStyleWriter; * Word2007 styles part writer: word/styles.xml * * @todo Do something with the numbering style introduced in 0.10.0 + * @SuppressWarnings(PHPMD.UnusedPrivateMethod) For writeFontStyle, writeParagraphStyle, and writeTableStyle */ class Styles extends AbstractPart { @@ -59,88 +60,11 @@ class Styles extends AbstractPart continue; } - // Font style - if ($style instanceof Font) { - $paragraphStyle = $style->getParagraph(); - $styleType = $style->getStyleType(); - $type = ($styleType == 'title') ? 'paragraph' : 'character'; - if (!is_null($paragraphStyle)) { - $type = 'paragraph'; - } - - $xmlWriter->startElement('w:style'); - $xmlWriter->writeAttribute('w:type', $type); - if ($styleType == 'title') { - $arrStyle = explode('_', $styleName); - $styleId = 'Heading' . $arrStyle[1]; - $styleName = 'heading ' . $arrStyle[1]; - $styleLink = 'Heading' . $arrStyle[1] . 'Char'; - $xmlWriter->writeAttribute('w:styleId', $styleId); - - $xmlWriter->startElement('w:link'); - $xmlWriter->writeAttribute('w:val', $styleLink); - $xmlWriter->endElement(); - } - $xmlWriter->startElement('w:name'); - $xmlWriter->writeAttribute('w:val', $styleName); - $xmlWriter->endElement(); - - // Parent style - $xmlWriter->writeElementIf(!is_null($paragraphStyle), 'w:basedOn', 'w:val', 'Normal'); - - // w:pPr - if (!is_null($paragraphStyle)) { - $styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle); - $styleWriter->write(); - } - - // w:rPr - $styleWriter = new FontStyleWriter($xmlWriter, $style); - $styleWriter->write(); - - $xmlWriter->endElement(); - - // Paragraph style - } elseif ($style instanceof Paragraph) { - $xmlWriter->startElement('w:style'); - $xmlWriter->writeAttribute('w:type', 'paragraph'); - $xmlWriter->writeAttribute('w:customStyle', '1'); - $xmlWriter->writeAttribute('w:styleId', $styleName); - $xmlWriter->startElement('w:name'); - $xmlWriter->writeAttribute('w:val', $styleName); - $xmlWriter->endElement(); - - // Parent style - $basedOn = $style->getBasedOn(); - $xmlWriter->writeElementIf(!is_null($basedOn), 'w:basedOn', 'w:val', $basedOn); - - // Next paragraph style - $next = $style->getNext(); - $xmlWriter->writeElementIf(!is_null($next), 'w:next', 'w:val', $next); - - // w:pPr - $styleWriter = new ParagraphStyleWriter($xmlWriter, $style); - $styleWriter->write(); - - $xmlWriter->endElement(); - - // Table style - } elseif ($style instanceof Table) { - $xmlWriter->startElement('w:style'); - $xmlWriter->writeAttribute('w:type', 'table'); - $xmlWriter->writeAttribute('w:customStyle', '1'); - $xmlWriter->writeAttribute('w:styleId', $styleName); - $xmlWriter->startElement('w:name'); - $xmlWriter->writeAttribute('w:val', $styleName); - $xmlWriter->endElement(); - $xmlWriter->startElement('w:uiPriority'); - $xmlWriter->writeAttribute('w:val', '99'); - $xmlWriter->endElement(); - - $styleWriter = new TableStyleWriter($xmlWriter, $style); - $styleWriter->write(); - - $xmlWriter->endElement(); // w:style + // Get style class and execute if the private method exists + $styleClass = substr(get_class($style), strrpos(get_class($style), '\\') + 1); + $method = "write{$styleClass}Style"; + if (method_exists($this, $method)) { + $this->$method($xmlWriter, $styleName, $style); } } } @@ -213,4 +137,115 @@ class Styles extends AbstractPart $xmlWriter->endElement(); // w:style } } + + /** + * Write font style + * + * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param string $styleName + * @param \PhpOffice\PhpWord\Style\Font $style + */ + private function writeFontStyle(XMLWriter $xmlWriter, $styleName, FontStyle $style) + { + $paragraphStyle = $style->getParagraph(); + $styleType = $style->getStyleType(); + $type = ($styleType == 'title') ? 'paragraph' : 'character'; + if (!is_null($paragraphStyle)) { + $type = 'paragraph'; + } + + $xmlWriter->startElement('w:style'); + $xmlWriter->writeAttribute('w:type', $type); + + // Heading style + if ($styleType == 'title') { + $arrStyle = explode('_', $styleName); + $styleId = 'Heading' . $arrStyle[1]; + $styleName = 'heading ' . $arrStyle[1]; + $styleLink = 'Heading' . $arrStyle[1] . 'Char'; + $xmlWriter->writeAttribute('w:styleId', $styleId); + + $xmlWriter->startElement('w:link'); + $xmlWriter->writeAttribute('w:val', $styleLink); + $xmlWriter->endElement(); + } + + // Style name + $xmlWriter->startElement('w:name'); + $xmlWriter->writeAttribute('w:val', $styleName); + $xmlWriter->endElement(); + + // Parent style + $xmlWriter->writeElementIf(!is_null($paragraphStyle), 'w:basedOn', 'w:val', 'Normal'); + + // w:pPr + if (!is_null($paragraphStyle)) { + $styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle); + $styleWriter->write(); + } + + // w:rPr + $styleWriter = new FontStyleWriter($xmlWriter, $style); + $styleWriter->write(); + + $xmlWriter->endElement(); + } + + /** + * Write paragraph style + * + * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param string $styleName + * @param \PhpOffice\PhpWord\Style\Paragraph $style + */ + private function writeParagraphStyle(XMLWriter $xmlWriter, $styleName, ParagraphStyle $style) + { + $xmlWriter->startElement('w:style'); + $xmlWriter->writeAttribute('w:type', 'paragraph'); + $xmlWriter->writeAttribute('w:customStyle', '1'); + $xmlWriter->writeAttribute('w:styleId', $styleName); + $xmlWriter->startElement('w:name'); + $xmlWriter->writeAttribute('w:val', $styleName); + $xmlWriter->endElement(); + + // Parent style + $basedOn = $style->getBasedOn(); + $xmlWriter->writeElementIf(!is_null($basedOn), 'w:basedOn', 'w:val', $basedOn); + + // Next paragraph style + $next = $style->getNext(); + $xmlWriter->writeElementIf(!is_null($next), 'w:next', 'w:val', $next); + + // w:pPr + $styleWriter = new ParagraphStyleWriter($xmlWriter, $style); + $styleWriter->write(); + + $xmlWriter->endElement(); + } + + /** + * Write table style + * + * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param string $styleName + * @param \PhpOffice\PhpWord\Style\Table $style + */ + private function writeTableStyle(XMLWriter $xmlWriter, $styleName, TableStyle $style) + { + $xmlWriter->startElement('w:style'); + $xmlWriter->writeAttribute('w:type', 'table'); + $xmlWriter->writeAttribute('w:customStyle', '1'); + $xmlWriter->writeAttribute('w:styleId', $styleName); + $xmlWriter->startElement('w:name'); + $xmlWriter->writeAttribute('w:val', $styleName); + $xmlWriter->endElement(); + $xmlWriter->startElement('w:uiPriority'); + $xmlWriter->writeAttribute('w:val', '99'); + $xmlWriter->endElement(); + + $styleWriter = new TableStyleWriter($xmlWriter, $style); + $styleWriter->write(); + + $xmlWriter->endElement(); // w:style + } } diff --git a/tests/PhpWord/Tests/Shared/XMLWriterTest.php b/tests/PhpWord/Tests/Shared/XMLWriterTest.php new file mode 100644 index 00000000..08db3918 --- /dev/null +++ b/tests/PhpWord/Tests/Shared/XMLWriterTest.php @@ -0,0 +1,40 @@ +foo(); + } +} diff --git a/tests/PhpWord/Tests/Style/AbstractStyleTest.php b/tests/PhpWord/Tests/Style/AbstractStyleTest.php index 15ff8fac..d35e1090 100644 --- a/tests/PhpWord/Tests/Style/AbstractStyleTest.php +++ b/tests/PhpWord/Tests/Style/AbstractStyleTest.php @@ -45,6 +45,7 @@ class AbstractStyleTest extends \PHPUnit_Framework_TestCase $this->assertEquals(true, self::callProtectedMethod($stub, 'setBoolVal', array(true, false))); $this->assertEquals(12, self::callProtectedMethod($stub, 'setIntVal', array(12, 200))); $this->assertEquals(871.1, self::callProtectedMethod($stub, 'setFloatVal', array(871.1, 2.1))); + $this->assertEquals(871.1, self::callProtectedMethod($stub, 'setFloatVal', array('871.1', 2.1))); $this->assertEquals('a', self::callProtectedMethod($stub, 'setEnumVal', array('a', array('a', 'b'), 'b'))); } diff --git a/tests/PhpWord/Tests/Style/CellTest.php b/tests/PhpWord/Tests/Style/CellTest.php index 1a026710..f9131728 100644 --- a/tests/PhpWord/Tests/Style/CellTest.php +++ b/tests/PhpWord/Tests/Style/CellTest.php @@ -52,7 +52,11 @@ class CellTest extends \PHPUnit_Framework_TestCase foreach ($attributes as $key => $value) { $set = "set{$key}"; $get = "get{$key}"; + + $this->assertNull($object->$get()); // Init with null value + $object->$set($value); + $this->assertEquals($value, $object->$get()); } } diff --git a/tests/PhpWord/Tests/Style/FontTest.php b/tests/PhpWord/Tests/Style/FontTest.php index ca2105fb..432b29fb 100644 --- a/tests/PhpWord/Tests/Style/FontTest.php +++ b/tests/PhpWord/Tests/Style/FontTest.php @@ -74,6 +74,7 @@ class FontTest extends \PHPUnit_Framework_TestCase ); foreach ($attributes as $key => $default) { $get = is_bool($default) ? "is{$key}" : "get{$key}"; + $this->assertEquals($default, $object->$get()); $object->setStyleValue("$key", null); $this->assertEquals($default, $object->$get()); $object->setStyleValue("$key", ''); diff --git a/tests/PhpWord/Tests/Style/ParagraphTest.php b/tests/PhpWord/Tests/Style/ParagraphTest.php index 357371c6..32e46985 100644 --- a/tests/PhpWord/Tests/Style/ParagraphTest.php +++ b/tests/PhpWord/Tests/Style/ParagraphTest.php @@ -96,6 +96,20 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase } } + /** + * Test get null style value + */ + public function testGetNullStyleValue() + { + $object = new Paragraph(); + + $attributes = array('spacing', 'indent', 'hanging', 'spaceBefore', 'spaceAfter'); + foreach ($attributes as $key) { + $get = "get{$key}"; + $this->assertNull($object->$get()); + } + } + /** * Test tabs */ diff --git a/tests/PhpWord/Tests/Style/TableTest.php b/tests/PhpWord/Tests/Style/TableTest.php index a7b46c1f..2afbab74 100644 --- a/tests/PhpWord/Tests/Style/TableTest.php +++ b/tests/PhpWord/Tests/Style/TableTest.php @@ -74,6 +74,9 @@ class TableTest extends \PHPUnit_Framework_TestCase 'cellMarginLeft' => 240, 'cellMarginRight' => 240, 'cellMarginBottom' => 240, + 'align' => 'center', + 'width' => 100, + 'unit' => 'pct', ); foreach ($attributes as $key => $value) { $set = "set{$key}"; @@ -146,6 +149,7 @@ class TableTest extends \PHPUnit_Framework_TestCase $this->assertEquals($value, $object->$get()); } $this->assertEquals($values, $object->getCellMargin()); + $this->assertTrue($object->hasMargin()); } /** diff --git a/tests/PhpWord/Tests/_files/documents/reader.docx b/tests/PhpWord/Tests/_files/documents/reader.docx index 5f37ef4b..d09091b1 100644 Binary files a/tests/PhpWord/Tests/_files/documents/reader.docx and b/tests/PhpWord/Tests/_files/documents/reader.docx differ