From f9cab8d02c94854155dd86bbf66b57ba3506b086 Mon Sep 17 00:00:00 2001 From: Andrey Tyshev Date: Tue, 21 Feb 2017 21:42:56 +0300 Subject: [PATCH 1/9] Fix name of style definitions --- src/PhpWord/Reader/Word2007/Document.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/PhpWord/Reader/Word2007/Document.php b/src/PhpWord/Reader/Word2007/Document.php index b89a99ad..e5063fd9 100644 --- a/src/PhpWord/Reader/Word2007/Document.php +++ b/src/PhpWord/Reader/Word2007/Document.php @@ -113,10 +113,10 @@ class Document extends AbstractPart 'orientation' => array(self::READ_VALUE, 'w:pgSz', 'w:orient'), 'colsNum' => array(self::READ_VALUE, 'w:cols', 'w:num'), 'colsSpace' => array(self::READ_VALUE, 'w:cols', 'w:space'), - 'topMargin' => array(self::READ_VALUE, 'w:pgMar', 'w:top'), - 'leftMargin' => array(self::READ_VALUE, 'w:pgMar', 'w:left'), - 'bottomMargin' => array(self::READ_VALUE, 'w:pgMar', 'w:bottom'), - 'rightMargin' => array(self::READ_VALUE, 'w:pgMar', 'w:right'), + 'marginTop' => array(self::READ_VALUE, 'w:pgMar', 'w:top'), + 'marginLeft' => array(self::READ_VALUE, 'w:pgMar', 'w:left'), + 'marginBottom' => array(self::READ_VALUE, 'w:pgMar', 'w:bottom'), + 'marginRight' => array(self::READ_VALUE, 'w:pgMar', 'w:right'), 'headerHeight' => array(self::READ_VALUE, 'w:pgMar', 'w:header'), 'footerHeight' => array(self::READ_VALUE, 'w:pgMar', 'w:footer'), 'gutter' => array(self::READ_VALUE, 'w:pgMar', 'w:gutter'), From e22ece8c003b428a0c08339dd9ac668d7d3c2c37 Mon Sep 17 00:00:00 2001 From: Hubert Miazek Date: Mon, 19 Jun 2017 11:05:59 +0200 Subject: [PATCH 2/9] #431 --- src/PhpWord/Writer/HTML/Element/Image.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/PhpWord/Writer/HTML/Element/Image.php b/src/PhpWord/Writer/HTML/Element/Image.php index 9c69d41f..646cb9a9 100644 --- a/src/PhpWord/Writer/HTML/Element/Image.php +++ b/src/PhpWord/Writer/HTML/Element/Image.php @@ -41,17 +41,15 @@ class Image extends Text $parentWriter = $this->parentWriter; $content = ''; - if (!$parentWriter->isPdf()) { - $imageData = $this->element->getImageStringData(true); - if ($imageData !== null) { - $styleWriter = new ImageStyleWriter($this->element->getStyle()); - $style = $styleWriter->write(); - $imageData = 'data:' . $this->element->getImageType() . ';base64,' . $imageData; + $imageData = $this->element->getImageStringData(true); + if ($imageData !== null) { + $styleWriter = new ImageStyleWriter($this->element->getStyle()); + $style = $styleWriter->write(); + $imageData = 'data:' . $this->element->getImageType() . ';base64,' . $imageData; - $content .= $this->writeOpening(); - $content .= ""; - $content .= $this->writeClosing(); - } + $content .= $this->writeOpening(); + $content .= ""; + $content .= $this->writeClosing(); } return $content; From 8c3efa4a514759ac958213d7779c788a8ee4a49f Mon Sep 17 00:00:00 2001 From: troosan Date: Sat, 1 Jul 2017 16:56:33 +0200 Subject: [PATCH 3/9] remove unused variable --- src/PhpWord/Writer/HTML/Element/Image.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/PhpWord/Writer/HTML/Element/Image.php b/src/PhpWord/Writer/HTML/Element/Image.php index 646cb9a9..24e35957 100644 --- a/src/PhpWord/Writer/HTML/Element/Image.php +++ b/src/PhpWord/Writer/HTML/Element/Image.php @@ -37,9 +37,6 @@ class Image extends Text if (!$this->element instanceof ImageElement) { return ''; } - /** @var \PhpOffice\PhpWord\Writer\HTML $parentWriter Type hint */ - $parentWriter = $this->parentWriter; - $content = ''; $imageData = $this->element->getImageStringData(true); if ($imageData !== null) { From fd418e074621d035fe7b32664c6590b6156af949 Mon Sep 17 00:00:00 2001 From: troosan Date: Sat, 1 Jul 2017 17:15:50 +0200 Subject: [PATCH 4/9] Make sample php 5.3 compliant --- samples/Sample_09_Tables.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/samples/Sample_09_Tables.php b/samples/Sample_09_Tables.php index 53d32e08..e3edc605 100644 --- a/samples/Sample_09_Tables.php +++ b/samples/Sample_09_Tables.php @@ -107,22 +107,22 @@ $table->addCell(null, $cellRowContinue); $section->addPageBreak(); $section->addText('Table with colspan and rowspan', $header); -$styleTable = ['borderSize' => 6, 'borderColor' => '999999']; +$styleTable = array('borderSize' => 6, 'borderColor' => '999999'); $phpWord->addTableStyle('Colspan Rowspan', $styleTable); $table = $section->addTable('Colspan Rowspan'); $row = $table->addRow(); -$row->addCell(null, ['vMerge' => 'restart'])->addText('A'); -$row->addCell(null, ['gridSpan' => 2, 'vMerge' => 'restart',])->addText('B'); +$row->addCell(null, array('vMerge' => 'restart'))->addText('A'); +$row->addCell(null, array('gridSpan' => 2, 'vMerge' => 'restart',))->addText('B'); $row->addCell()->addText('1'); $row = $table->addRow(); -$row->addCell(null, ['vMerge' => 'continue']); -$row->addCell(null, ['vMerge' => 'continue','gridSpan' => 2,]); +$row->addCell(null, array('vMerge' => 'continue')); +$row->addCell(null, array('vMerge' => 'continue','gridSpan' => 2,)); $row->addCell()->addText('2'); $row = $table->addRow(); -$row->addCell(null, ['vMerge' => 'continue']); +$row->addCell(null, array('vMerge' => 'continue')); $row->addCell()->addText('C'); $row->addCell()->addText('D'); $row->addCell()->addText('3'); From d5dbfb98b031b2ddaabaedad2dca97634483a28f Mon Sep 17 00:00:00 2001 From: Maxim Barulin Date: Tue, 4 Apr 2017 11:35:48 +0300 Subject: [PATCH 5/9] add support for contextualSpacing attribute issue #1037 --- src/PhpWord/Style/Paragraph.php | 26 +++++++++++++++++++ .../Writer/Word2007/Style/Paragraph.php | 3 +++ 2 files changed, 29 insertions(+) diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index c6e60efb..ec23dce3 100644 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -157,6 +157,13 @@ class Paragraph extends Border * @var \PhpOffice\PhpWord\Style\Shading */ private $shading; + + /** + * Do not add an interval between paragraphs of the same style + * + * @var bool + */ + private $contextualSpacing = true; /** * Set Style value @@ -208,6 +215,7 @@ class Paragraph extends Border ), 'tabs' => $this->getTabs(), 'shading' => $this->getShading(), + 'contextualSpacing' => $this->getContextualSpacing(), ); return $styles; @@ -731,4 +739,22 @@ class Paragraph extends Border return $this; } + + /** + * @return bool + */ + public function getContextualSpacing() + { + return $this->contextualSpacing; + } + + /** + * @param bool $contextualSpacing + */ + public function setContextualSpacing($contextualSpacing) + { + $this->contextualSpacing = $contextualSpacing; + + return $this; + } } diff --git a/src/PhpWord/Writer/Word2007/Style/Paragraph.php b/src/PhpWord/Writer/Word2007/Style/Paragraph.php index 2cb08bee..787f2a5f 100644 --- a/src/PhpWord/Writer/Word2007/Style/Paragraph.php +++ b/src/PhpWord/Writer/Word2007/Style/Paragraph.php @@ -106,6 +106,9 @@ class Paragraph extends AbstractStyle } $xmlWriter->endElement(); } + + //Paragraph contextualSpacing + $xmlWriter->writeElementIf($styles['contextualSpacing'] === true, 'w:contextualSpacing'); // Child style: alignment, indentation, spacing, and shading $this->writeChildStyle($xmlWriter, 'Indentation', $styles['indentation']); From 0f649f3f376cbeff390e21945d6b03cfb0aca7e6 Mon Sep 17 00:00:00 2001 From: troosan Date: Sat, 1 Jul 2017 22:51:53 +0200 Subject: [PATCH 6/9] Add test & update documentation --- docs/styles.rst | 1 + src/PhpWord/Style/Paragraph.php | 13 +++++-- tests/PhpWord/Style/ParagraphTest.php | 54 +++++++++++++++++---------- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/docs/styles.rst b/docs/styles.rst index b71059a6..4f8a53fe 100644 --- a/docs/styles.rst +++ b/docs/styles.rst @@ -77,6 +77,7 @@ See ``\PhpOffice\PhpWord\SimpleType\Jc`` class for the details. - ``spaceAfter``. Space after paragraph. - ``tabs``. Set of custom tab stops. - ``widowControl``. Allow first/last line to display on a separate page, *true* or *false*. +- ``contextualSpacing``. Ignore Spacing Above and Below When Using Identical Styles, *true* or *false*. .. _table-style: diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index ec23dce3..a9b53b2b 100644 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -159,11 +159,11 @@ class Paragraph extends Border private $shading; /** - * Do not add an interval between paragraphs of the same style + * Ignore Spacing Above and Below When Using Identical Styles * * @var bool */ - private $contextualSpacing = true; + private $contextualSpacing = false; /** * Set Style value @@ -215,7 +215,7 @@ class Paragraph extends Border ), 'tabs' => $this->getTabs(), 'shading' => $this->getShading(), - 'contextualSpacing' => $this->getContextualSpacing(), + 'contextualSpacing' => $this->hasContextualSpacing(), ); return $styles; @@ -741,15 +741,20 @@ class Paragraph extends Border } /** + * Get contextualSpacing + * * @return bool */ - public function getContextualSpacing() + public function hasContextualSpacing() { return $this->contextualSpacing; } /** + * Set contextualSpacing + * * @param bool $contextualSpacing + * @return self */ public function setContextualSpacing($contextualSpacing) { diff --git a/tests/PhpWord/Style/ParagraphTest.php b/tests/PhpWord/Style/ParagraphTest.php index c0096b0b..86d6e896 100644 --- a/tests/PhpWord/Style/ParagraphTest.php +++ b/tests/PhpWord/Style/ParagraphTest.php @@ -43,13 +43,14 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase $object = new Paragraph(); $attributes = array( - 'widowControl' => true, - 'keepNext' => false, - 'keepLines' => false, - 'pageBreakBefore' => false, + 'widowControl' => true, + 'keepNext' => false, + 'keepLines' => false, + 'pageBreakBefore' => false, + 'contextualSpacing' => false, ); foreach ($attributes as $key => $default) { - $get = "get{$key}"; + $get = $this->findGetter($key, $default, $object); $object->setStyleValue($key, null); $this->assertEquals($default, $object->$get()); $object->setStyleValue($key, ''); @@ -65,22 +66,23 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase $object = new Paragraph(); $attributes = array( - 'spaceAfter' => 240, - 'spaceBefore' => 240, - 'indent' => 1, - 'hanging' => 1, - 'spacing' => 120, - 'basedOn' => 'Normal', - 'next' => 'Normal', - 'numStyle' => 'numStyle', - 'numLevel' => 1, - 'widowControl' => false, - 'keepNext' => true, - 'keepLines' => true, - 'pageBreakBefore' => true, + 'spaceAfter' => 240, + 'spaceBefore' => 240, + 'indent' => 1, + 'hanging' => 1, + 'spacing' => 120, + 'basedOn' => 'Normal', + 'next' => 'Normal', + 'numStyle' => 'numStyle', + 'numLevel' => 1, + 'widowControl' => false, + 'keepNext' => true, + 'keepLines' => true, + 'pageBreakBefore' => true, + 'contextualSpacing' => true, ); foreach ($attributes as $key => $value) { - $get = "get{$key}"; + $get = $this->findGetter($key, $value, $object); $object->setStyleValue("$key", $value); if ('indent' == $key || 'hanging' == $key) { $value = $value * 720; @@ -91,6 +93,18 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase } } + private function findGetter($key, $value, $object) + { + if (is_bool($value)) { + if (method_exists($object, "is{$key}")) { + return "is{$key}"; + } else if (method_exists($object, "has{$key}")) { + return "has{$key}"; + } + } + return "get{$key}"; + } + /** * Test get null style value */ @@ -100,7 +114,7 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase $attributes = array('spacing', 'indent', 'hanging', 'spaceBefore', 'spaceAfter'); foreach ($attributes as $key) { - $get = "get{$key}"; + $get = $this->findGetter($key, null, $object); $this->assertNull($object->$get()); } } From e7c551a0bfe2b92befebf40a86f4645e540e108e Mon Sep 17 00:00:00 2001 From: troosan Date: Sun, 2 Jul 2017 00:37:29 +0200 Subject: [PATCH 7/9] Add possibility to show/hide spelling and grammatical errors (#985) * Add possibility to show/hide spelling and grammatical errors --- .gitignore | 3 +- docs/general.rst | 28 +++++++++- src/PhpWord/Settings.php | 52 +++++++++++++++++++ src/PhpWord/Writer/Word2007/Part/Settings.php | 2 + tests/PhpWord/SettingsTest.php | 14 +++++ .../Writer/Word2007/Part/SettingsTest.php | 45 ++++++++++++++-- 6 files changed, 137 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index e5deb643..98f65dbf 100644 --- a/.gitignore +++ b/.gitignore @@ -11,9 +11,10 @@ composer.lock composer.phar vendor /report +/build /samples/resources /samples/results /.settings phpword.ini /.buildpath -/.project \ No newline at end of file +/.project diff --git a/docs/general.rst b/docs/general.rst index 27d0448a..95340125 100644 --- a/docs/general.rst +++ b/docs/general.rst @@ -109,8 +109,8 @@ Zip class By default, PHPWord uses `Zip extension `__ to deal with ZIP compressed archives and files inside them. If you can't have Zip extension installed on your server, you can use pure PHP library -alternative, `PclZip `__, which -included with PHPWord. +alternative, `PclZip `__, which is +included in PHPWord. .. code-block:: php @@ -130,6 +130,17 @@ To turn it on set ``outputEscapingEnabled`` option to ``true`` in your PHPWord c \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true); +Spelling and grammatical checks +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +By default spelling and grammatical errors are shown as soon as you open a word document. +For big documents this can slow down the opening of the document. You can hide the spelling and/or grammatical errors with: + +.. code-block:: php + + \PhpOffice\PhpWord\Settings::setSpellingErrorsHidden(true); + \PhpOffice\PhpWord\Settings::setGrammaticalErrorsHidden(true); + Default font ~~~~~~~~~~~~ @@ -183,3 +194,16 @@ points to twips. $sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5)); // 2 cm right margin $sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2)); + +Language +-------- + +You can hide spelling errors: + +.. code-block:: php + \PhpOffice\PhpWord\Settings::setSpellingErrorsHidden(true); + +And hide grammatical errors: + +.. code-block:: php + \PhpOffice\PhpWord\Settings::setGrammaticalErrorsHidden(true); diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php index 3fbbb0a6..3da32cbb 100644 --- a/src/PhpWord/Settings.php +++ b/src/PhpWord/Settings.php @@ -119,6 +119,18 @@ class Settings */ private static $defaultFontSize = self::DEFAULT_FONT_SIZE; + /** + * Hide spelling errors + * @var boolean + */ + private static $spellingErrorsHidden = false; + + /** + * Hide grammatical errors + * @var boolean + */ + private static $grammaticalErrorsHidden = false; + /** * The user defined temporary directory. * @@ -416,6 +428,46 @@ class Settings return false; } + /** + * Are spelling errors hidden + * + * @return boolean + */ + public static function isSpellingErrorsHidden() + { + return self::$spellingErrorsHidden; + } + + /** + * Hide spelling errors + * + * @param boolean $spellingErrorsHidden + */ + public static function setSpellingErrorsHidden($spellingErrorsHidden) + { + self::$spellingErrorsHidden = $spellingErrorsHidden; + } + + /** + * Are grammatical errors hidden + * + * @return boolean + */ + public static function isGrammaticalErrorsHidden() + { + return self::$grammaticalErrorsHidden; + } + + /** + * Hide grammatical errors + * + * @param boolean $grammaticalErrorsHidden + */ + public static function setGrammaticalErrorsHidden($grammaticalErrorsHidden) + { + self::$grammaticalErrorsHidden = $grammaticalErrorsHidden; + } + /** * Load setting from phpword.yml or phpword.yml.dist * diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index 82669f91..72406462 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -107,6 +107,8 @@ class Settings extends AbstractPart 'w:characterSpacingControl' => array('@attributes' => array('w:val' => 'doNotCompress')), 'w:evenAndOddHeaders' => array('@attributes' => array('w:val' => DocumentSettings::isEvenAndOddHeaders() ? 'true': 'false')), 'w:themeFontLang' => array('@attributes' => array('w:val' => 'en-US')), + 'w:hideSpellingErrors' => array('@attributes' => array('w:val' => DocumentSettings::isSpellingErrorsHidden() ? 'true' : 'false')), + 'w:hideGrammaticalErrors' => array('@attributes' => array('w:val' => DocumentSettings::isGrammaticalErrorsHidden() ? 'true' : 'false')), 'w:decimalSymbol' => array('@attributes' => array('w:val' => '.')), 'w:listSeparator' => array('@attributes' => array('w:val' => ';')), 'w:compat' => array(), diff --git a/tests/PhpWord/SettingsTest.php b/tests/PhpWord/SettingsTest.php index 764fccd4..6347f424 100644 --- a/tests/PhpWord/SettingsTest.php +++ b/tests/PhpWord/SettingsTest.php @@ -114,6 +114,20 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertFalse(Settings::setDefaultFontSize(null)); } + /** + * Test set/get spelling and grammar + */ + public function testSetGetSpellingGrammar() + { + $this->assertFalse(Settings::isSpellingErrorsHidden()); + Settings::setSpellingErrorsHidden(true); + $this->assertTrue(Settings::isSpellingErrorsHidden()); + + $this->assertFalse(Settings::isGrammaticalErrorsHidden()); + Settings::setGrammaticalErrorsHidden(true); + $this->assertTrue(Settings::isGrammaticalErrorsHidden()); + } + /** * Test set/get even and odd headers */ diff --git a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php index be018f8c..13210fb7 100644 --- a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php @@ -68,6 +68,43 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertEquals($phpWord->getCompatibility()->getOoxmlVersion(), 15); } + /** + * Test language + */ + public function testLanguage() + { + $phpWord = new PhpWord(); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $file = 'word/settings.xml'; + + $path = '/w:settings/w:themeFontLang'; + $this->assertTrue($doc->elementExists($path, $file)); + $element = $doc->getElement($path, $file); + + $this->assertEquals('en-US', $element->getAttribute('w:val')); + } + + /** + * Test spelling + */ + public function testSpelling() + { + $phpWord = new PhpWord(); + Settings::setSpellingErrorsHidden(true); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $file = 'word/settings.xml'; + + $path = '/w:settings/w:hideSpellingErrors'; + $this->assertTrue($doc->elementExists($path, $file)); + $element = $doc->getElement($path, $file); + + $this->assertEquals('true', $element->getAttribute('w:val')); + } + /** * Test even and odd headers */ @@ -75,14 +112,14 @@ class SettingsTest extends \PHPUnit_Framework_TestCase { $phpWord = new PhpWord(); Settings::setEvenAndOddHeaders(true); - + $doc = TestHelperDOCX::getDocument($phpWord); - + $file = 'word/settings.xml'; - + $path = '/w:settings/w:evenAndOddHeaders'; $this->assertTrue($doc->elementExists($path, $file)); - + $element = $doc->getElement($path, $file); $this->assertEquals('true', $element->getAttribute('w:val')); } From ef956112036456a7f3fb22e0a0aba56985706e47 Mon Sep 17 00:00:00 2001 From: troosan Date: Mon, 3 Jul 2017 12:54:18 +0200 Subject: [PATCH 8/9] Fix multiple paragraphs being created --- src/PhpWord/Reader/Word2007/AbstractPart.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/PhpWord/Reader/Word2007/AbstractPart.php b/src/PhpWord/Reader/Word2007/AbstractPart.php index f429ff75..51970a06 100644 --- a/src/PhpWord/Reader/Word2007/AbstractPart.php +++ b/src/PhpWord/Reader/Word2007/AbstractPart.php @@ -167,14 +167,11 @@ abstract class AbstractPart $parent->addTextBreak(null, $paragraphStyle); } else { $nodes = $xmlReader->getElements('*', $domNode); + if ($runLinkCount > 1) { + $parent = $parent->addTextRun($paragraphStyle); + } foreach ($nodes as $node) { - $this->readRun( - $xmlReader, - $node, - ($runLinkCount > 1) ? $parent->addTextRun($paragraphStyle) : $parent, - $docPart, - $paragraphStyle - ); + $this->readRun($xmlReader, $node, $parent, $docPart, $paragraphStyle); } } } From 8a5433e5df1f76d2bfa554b99e08e2bd79e05779 Mon Sep 17 00:00:00 2001 From: troosan Date: Tue, 4 Jul 2017 15:04:30 +0200 Subject: [PATCH 9/9] add new NumberFormat values and refactor other uses --- samples/Sample_06_Footnote.php | 5 +- .../FootnoteProperties.php | 89 ++++++---- src/PhpWord/Element/Section.php | 2 +- src/PhpWord/Shared/AbstractEnum.php | 35 ++++ src/PhpWord/SimpleType/NumberFormat.php | 153 ++++++++++++++++++ src/PhpWord/Style/NumberingLevel.php | 16 +- src/PhpWord/Writer/Word2007/Part/Document.php | 1 - .../FootnotePropertiesTest.php | 13 +- .../Writer/Word2007/Part/DocumentTest.php | 7 +- .../Writer/Word2007/Part/NumberingTest.php | 3 +- 10 files changed, 271 insertions(+), 53 deletions(-) rename src/PhpWord/{SimpleType => ComplexType}/FootnoteProperties.php (67%) create mode 100644 src/PhpWord/Shared/AbstractEnum.php create mode 100644 src/PhpWord/SimpleType/NumberFormat.php rename tests/PhpWord/{SimpleType => ComplexType}/FootnotePropertiesTest.php (83%) diff --git a/samples/Sample_06_Footnote.php b/samples/Sample_06_Footnote.php index f9c6b5f7..19d6a524 100644 --- a/samples/Sample_06_Footnote.php +++ b/samples/Sample_06_Footnote.php @@ -1,5 +1,6 @@ addFootnote(); $footnote->addText('The reference for this is wrapped in its own line'); $footnoteProperties = new FootnoteProperties(); -$footnoteProperties->setNumFmt(FootnoteProperties::NUMBER_FORMAT_UPPER_ROMAN); +$footnoteProperties->setNumFmt(NumberFormat::DECIMAL_ENCLOSED_CIRCLE); $section->setFootnoteProperties($footnoteProperties); // Save file diff --git a/src/PhpWord/SimpleType/FootnoteProperties.php b/src/PhpWord/ComplexType/FootnoteProperties.php similarity index 67% rename from src/PhpWord/SimpleType/FootnoteProperties.php rename to src/PhpWord/ComplexType/FootnoteProperties.php index f98f0a94..882834d5 100644 --- a/src/PhpWord/SimpleType/FootnoteProperties.php +++ b/src/PhpWord/ComplexType/FootnoteProperties.php @@ -14,7 +14,9 @@ * @copyright 2010-2016 PHPWord contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ -namespace PhpOffice\PhpWord\SimpleType; +namespace PhpOffice\PhpWord\ComplexType; + +use PhpOffice\PhpWord\SimpleType\NumberFormat; /** * Footnote properties @@ -28,17 +30,6 @@ final class FootnoteProperties const RESTART_NUMBER_EACH_SECTION = 'eachSect'; const RESTART_NUMBER_EACH_PAGE = 'eachPage'; - const NUMBER_FORMAT_DECIMAL = 'decimal'; - const NUMBER_FORMAT_UPPER_ROMAN = 'upperRoman'; - const NUMBER_FORMAT_LOWER_ROMAN = 'lowerRoman'; - const NUMBER_FORMAT_UPPER_LETTER = 'upperLetter'; - const NUMBER_FORMAT_LOWER_LETTER = 'lowerLetter'; - const NUMBER_FORMAT_ORDINAL = 'ordinal'; - const NUMBER_FORMAT_CARDINAL_TEXT = 'cardinalText'; - const NUMBER_FORMAT_ORDINAL_TEXT = 'ordinalText'; - const NUMBER_FORMAT_NONE = 'none'; - const NUMBER_FORMAT_BULLET = 'bullet'; - const POSITION_PAGE_BOTTOM = 'pageBottom'; const POSITION_BENEATH_TEXT = 'beneathText'; const POSITION_SECTION_END = 'sectEnd'; @@ -52,7 +43,7 @@ final class FootnoteProperties private $pos; /** - * Footnote Numbering Format + * Footnote Numbering Format w:numFmt, one of PhpOffice\PhpWord\SimpleType\NumberFormat * * @var string */ @@ -61,7 +52,7 @@ final class FootnoteProperties /** * Footnote and Endnote Numbering Starting Value * - * @var decimal + * @var double */ private $numStart; @@ -72,11 +63,23 @@ final class FootnoteProperties */ private $numRestart; + /** + * Get the Footnote Positioning Location + * + * @return string + */ public function getPos() { return $this->pos; } + /** + * Set the Footnote Positioning Location (pageBottom, beneathText, sectEnd, docEnd) + * + * @param string $pos + * @throws \InvalidArgumentException + * @return self + */ public function setPos($pos) { $position = array( @@ -91,50 +94,71 @@ final class FootnoteProperties } else { throw new \InvalidArgumentException("Invalid value, on of " . implode(', ', $position) . " possible"); } + return $this; } + /** + * Get the Footnote Numbering Format + * + * @return string + */ public function getNumFmt() { return $this->numFmt; } + /** + * Set the Footnote Numbering Format + * + * @param string $numFmt One of NumberFormat + * @return self + */ public function setNumFmt($numFmt) { - $numberFormat = array( - self::NUMBER_FORMAT_DECIMAL, - self::NUMBER_FORMAT_UPPER_ROMAN, - self::NUMBER_FORMAT_LOWER_ROMAN, - self::NUMBER_FORMAT_UPPER_LETTER, - self::NUMBER_FORMAT_LOWER_LETTER, - self::NUMBER_FORMAT_ORDINAL, - self::NUMBER_FORMAT_CARDINAL_TEXT, - self::NUMBER_FORMAT_ORDINAL_TEXT, - self::NUMBER_FORMAT_NONE, - self::NUMBER_FORMAT_BULLET - ); - - if (in_array($numFmt, $numberFormat)) { - $this->numFmt = $numFmt; - } else { - throw new \InvalidArgumentException("Invalid value, on of " . implode(', ', $numberFormat) . " possible"); - } + NumberFormat::validate($numFmt); + $this->numFmt = $numFmt; + return $this; } + /** + * Get the Footnote Numbering Format + * + * @return double + */ public function getNumStart() { return $this->numStart; } + /** + * Set the Footnote Numbering Format + * + * @param double $numStart + * @return self + */ public function setNumStart($numStart) { $this->numStart = $numStart; + return $this; } + /** + * Get the Footnote and Endnote Numbering Starting Value + * + * @return string + */ public function getNumRestart() { return $this->numRestart; } + /** + * Set the Footnote and Endnote Numbering Starting Value (continuous, eachSect, eachPage) + * + * @param string $numRestart + * @throws \InvalidArgumentException + * @return self + */ public function setNumRestart($numRestart) { $restartNumbers = array( @@ -148,5 +172,6 @@ final class FootnoteProperties } else { throw new \InvalidArgumentException("Invalid value, on of " . implode(', ', $restartNumbers) . " possible"); } + return $this; } } diff --git a/src/PhpWord/Element/Section.php b/src/PhpWord/Element/Section.php index c0bfc3cf..6e199bb9 100644 --- a/src/PhpWord/Element/Section.php +++ b/src/PhpWord/Element/Section.php @@ -17,8 +17,8 @@ namespace PhpOffice\PhpWord\Element; +use PhpOffice\PhpWord\ComplexType\FootnoteProperties; use PhpOffice\PhpWord\Style\Section as SectionStyle; -use PhpOffice\PhpWord\SimpleType\FootnoteProperties; class Section extends AbstractContainer { diff --git a/src/PhpWord/Shared/AbstractEnum.php b/src/PhpWord/Shared/AbstractEnum.php new file mode 100644 index 00000000..4584c2f9 --- /dev/null +++ b/src/PhpWord/Shared/AbstractEnum.php @@ -0,0 +1,35 @@ +getConstants(); + } + return self::$constCacheArray[$calledClass]; + } + + public static function values() + { + return array_values(self::getConstants()); + } + + public static function validate($value) + { + $values = array_values(self::getConstants()); + if (!in_array($value, $values, true)) { + $calledClass = get_called_class(); + throw new \InvalidArgumentException("$value is not a valid value for $calledClass, possible values are " . implode(', ', $values)); + } + } +} diff --git a/src/PhpWord/SimpleType/NumberFormat.php b/src/PhpWord/SimpleType/NumberFormat.php new file mode 100644 index 00000000..4c98dbef --- /dev/null +++ b/src/PhpWord/SimpleType/NumberFormat.php @@ -0,0 +1,153 @@ +format = $this->setEnumVal($value, $enum, $this->format); + $this->format = $this->setEnumVal($value, NumberFormat::values(), $this->format); return $this; } /** - * Get start + * Get restart * * @return integer */ @@ -201,7 +203,7 @@ class NumberingLevel extends AbstractStyle } /** - * Set start + * Set restart * * @param integer $value * @return self diff --git a/src/PhpWord/Writer/Word2007/Part/Document.php b/src/PhpWord/Writer/Word2007/Part/Document.php index a2dc999b..11e3f510 100644 --- a/src/PhpWord/Writer/Word2007/Part/Document.php +++ b/src/PhpWord/Writer/Word2007/Part/Document.php @@ -21,7 +21,6 @@ use PhpOffice\Common\XMLWriter; use PhpOffice\PhpWord\Element\Section; use PhpOffice\PhpWord\Writer\Word2007\Element\Container; use PhpOffice\PhpWord\Writer\Word2007\Style\Section as SectionStyleWriter; -use PhpOffice\PhpWord\SimpleType\FootnoteProperties; /** * Word2007 document part writer: word/document.xml diff --git a/tests/PhpWord/SimpleType/FootnotePropertiesTest.php b/tests/PhpWord/ComplexType/FootnotePropertiesTest.php similarity index 83% rename from tests/PhpWord/SimpleType/FootnotePropertiesTest.php rename to tests/PhpWord/ComplexType/FootnotePropertiesTest.php index f977b32a..025e8c91 100644 --- a/tests/PhpWord/SimpleType/FootnotePropertiesTest.php +++ b/tests/PhpWord/ComplexType/FootnotePropertiesTest.php @@ -15,9 +15,10 @@ * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ -namespace PhpOffice\PhpWord\SimpleType; +namespace PhpOffice\PhpWord\ComplexType; -use PhpOffice\PhpWord\SimpleType\FootnoteProperties; +use PhpOffice\PhpWord\ComplexType\FootnoteProperties; +use PhpOffice\PhpWord\SimpleType\NumberFormat; /** * Test class for PhpOffice\PhpWord\SimpleType\FootnoteProperties @@ -34,12 +35,12 @@ class FootnotePropertiesTest extends \PHPUnit_Framework_TestCase { $footnoteProp = new FootnoteProperties(); $footnoteProp->setPos(FootnoteProperties::POSITION_DOC_END); - $footnoteProp->setNumFmt(FootnoteProperties::NUMBER_FORMAT_LOWER_ROMAN); + $footnoteProp->setNumFmt(NumberFormat::LOWER_ROMAN); $footnoteProp->setNumStart(2); $footnoteProp->setNumRestart(FootnoteProperties::RESTART_NUMBER_EACH_PAGE); $this->assertEquals(FootnoteProperties::POSITION_DOC_END, $footnoteProp->getPos()); - $this->assertEquals(FootnoteProperties::NUMBER_FORMAT_LOWER_ROMAN, $footnoteProp->getNumFmt()); + $this->assertEquals(NumberFormat::LOWER_ROMAN, $footnoteProp->getNumFmt()); $this->assertEquals(2, $footnoteProp->getNumStart()); $this->assertEquals(FootnoteProperties::RESTART_NUMBER_EACH_PAGE, $footnoteProp->getNumRestart()); } @@ -52,7 +53,7 @@ class FootnotePropertiesTest extends \PHPUnit_Framework_TestCase public function testWrongPos() { $footnoteProp= new FootnoteProperties(); - $footnoteProp->setPos(FootnoteProperties::NUMBER_FORMAT_LOWER_ROMAN); + $footnoteProp->setPos(NumberFormat::LOWER_ROMAN); } /** @@ -74,6 +75,6 @@ class FootnotePropertiesTest extends \PHPUnit_Framework_TestCase public function testWrongNumRestart() { $footnoteProp= new FootnoteProperties(); - $footnoteProp->setNumRestart(FootnoteProperties::NUMBER_FORMAT_LOWER_ROMAN); + $footnoteProp->setNumRestart(NumberFormat::LOWER_ROMAN); } } diff --git a/tests/PhpWord/Writer/Word2007/Part/DocumentTest.php b/tests/PhpWord/Writer/Word2007/Part/DocumentTest.php index 0f64d7a7..d45cde6b 100644 --- a/tests/PhpWord/Writer/Word2007/Part/DocumentTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/DocumentTest.php @@ -16,11 +16,12 @@ */ namespace PhpOffice\PhpWord\Writer\Word2007\Part; +use PhpOffice\PhpWord\ComplexType\FootnoteProperties; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\SimpleType\Jc; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\TestHelperDOCX; -use PhpOffice\PhpWord\SimpleType\FootnoteProperties; +use PhpOffice\PhpWord\SimpleType\NumberFormat; /** * Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Document @@ -65,7 +66,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase { $properties = new FootnoteProperties(); $properties->setPos(FootnoteProperties::POSITION_DOC_END); - $properties->setNumFmt(FootnoteProperties::NUMBER_FORMAT_LOWER_ROMAN); + $properties->setNumFmt(NumberFormat::LOWER_ROMAN); $properties->setNumStart(1); $properties->setNumRestart(FootnoteProperties::RESTART_NUMBER_EACH_PAGE); @@ -79,7 +80,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase $this->assertEquals(FootnoteProperties::POSITION_DOC_END, $element->getAttribute('w:val')); $element = $doc->getElement('/w:document/w:body/w:sectPr/w:footnotePr/w:numFmt'); - $this->assertEquals(FootnoteProperties::NUMBER_FORMAT_LOWER_ROMAN, $element->getAttribute('w:val')); + $this->assertEquals(NumberFormat::LOWER_ROMAN, $element->getAttribute('w:val')); $element = $doc->getElement('/w:document/w:body/w:sectPr/w:footnotePr/w:numStart'); $this->assertEquals(1, $element->getAttribute('w:val')); diff --git a/tests/PhpWord/Writer/Word2007/Part/NumberingTest.php b/tests/PhpWord/Writer/Word2007/Part/NumberingTest.php index 9d11e5cb..bca4b562 100644 --- a/tests/PhpWord/Writer/Word2007/Part/NumberingTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/NumberingTest.php @@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\SimpleType\Jc; use PhpOffice\PhpWord\TestHelperDOCX; +use PhpOffice\PhpWord\SimpleType\NumberFormat; /** * Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Numbering @@ -52,7 +53,7 @@ class NumberingTest extends \PHPUnit_Framework_TestCase 'levels' => array( array( 'start' => 1, - 'format' => 'decimal', + 'format' => NumberFormat::DECIMAL, 'restart' => 1, 'suffix' => 'space', 'text' => '%1.',