From e9cc289243a8ebb1891779f7bd88a19affe0d125 Mon Sep 17 00:00:00 2001 From: troosan Date: Wed, 5 Jul 2017 21:42:38 +0200 Subject: [PATCH] refactor Settings to not mix PHPWord settings and document settings --- docs/containers.rst | 6 + docs/general.rst | 4 +- src/PhpWord/Element/Footer.php | 2 +- src/PhpWord/Metadata/Settings.php | 131 ++++++++++++++++++ src/PhpWord/PhpWord.php | 17 ++- src/PhpWord/Settings.php | 75 ---------- src/PhpWord/SimpleType/NumberFormat.php | 2 - src/PhpWord/Writer/Word2007/Part/Settings.php | 33 +++-- tests/PhpWord/Metadata/SettingsTest.php | 69 +++++++++ tests/PhpWord/SettingsTest.php | 24 ---- .../Writer/Word2007/Part/SettingsTest.php | 6 +- 11 files changed, 252 insertions(+), 117 deletions(-) create mode 100644 src/PhpWord/Metadata/Settings.php create mode 100644 tests/PhpWord/Metadata/SettingsTest.php diff --git a/docs/containers.rst b/docs/containers.rst index f165a589..3569cc50 100644 --- a/docs/containers.rst +++ b/docs/containers.rst @@ -98,6 +98,12 @@ that are available for the footer. See "Footer" section for detail. Additionally, only inside of the header reference you can add watermarks or background pictures. See "Watermarks" section. +You can pass an optional parameter to specify where the header/footer should be applied, it can be + +- ``Footer::AUTO`` default, all pages except if overridden by first or even +- ``Footer::FIRST`` each first page of the section +- ``Footer::EVEN`` each even page of the section. Will only be applied if the evenAndOddHeaders is set to true in phpWord->settings + Footers ------- diff --git a/docs/general.rst b/docs/general.rst index 95340125..8e347089 100644 --- a/docs/general.rst +++ b/docs/general.rst @@ -138,8 +138,8 @@ For big documents this can slow down the opening of the document. You can hide t .. code-block:: php - \PhpOffice\PhpWord\Settings::setSpellingErrorsHidden(true); - \PhpOffice\PhpWord\Settings::setGrammaticalErrorsHidden(true); + $phpWord->getSettings()->setHideGrammaticalErrors(true); + $phpWord->getSettings()->setHideSpellingErrors(true); Default font ~~~~~~~~~~~~ diff --git a/src/PhpWord/Element/Footer.php b/src/PhpWord/Element/Footer.php index 01c6d25c..b3196f3f 100644 --- a/src/PhpWord/Element/Footer.php +++ b/src/PhpWord/Element/Footer.php @@ -26,7 +26,7 @@ class Footer extends AbstractContainer * Header/footer types constants * * @var string - * @link http://www.schemacentral.com/sc/ooxml/a-wtype-4.html Header or Footer Type + * @link http://www.datypic.com/sc/ooxml/t-w_ST_HdrFtr.html Header or Footer Type */ const AUTO = 'default'; // default and odd pages const FIRST = 'first'; diff --git a/src/PhpWord/Metadata/Settings.php b/src/PhpWord/Metadata/Settings.php new file mode 100644 index 00000000..ba6931a0 --- /dev/null +++ b/src/PhpWord/Metadata/Settings.php @@ -0,0 +1,131 @@ +documentProtection == null) { + $this->documentProtection = new Protection(); + } + return $this->documentProtection; + } + + /** + * @param Protection $documentProtection + */ + public function setDocumentProtection($documentProtection) + { + $this->documentProtection = $documentProtection; + } + + /** + * Are spelling errors hidden + * + * @return boolean + */ + public function hasHideSpellingErrors() + { + return $this->hideSpellingErrors; + } + + /** + * Hide spelling errors + * + * @param boolean $hideSpellingErrors + */ + public function setHideSpellingErrors($hideSpellingErrors) + { + $this->hideSpellingErrors = $hideSpellingErrors; + } + + /** + * Are grammatical errors hidden + * + * @return boolean + */ + public function hasHideGrammaticalErrors() + { + return $this->hideGrammaticalErrors; + } + + /** + * Hide grammatical errors + * + * @param boolean $hideGrammaticalErrors + */ + public function setHideGrammaticalErrors($hideGrammaticalErrors) + { + $this->hideGrammaticalErrors = $hideGrammaticalErrors; + } + + /** + * @return boolean + */ + public function hasEvenAndOddHeaders() + { + return $this->evenAndOddHeaders; + } + + /** + * @param boolean $evenAndOddHeaders + */ + public function setEvenAndOddHeaders($evenAndOddHeaders) + { + $this->evenAndOddHeaders = $evenAndOddHeaders; + } +} diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index 0fa76b2f..bb5b4956 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -91,7 +91,7 @@ class PhpWord } // Metadata - $metadata = array('DocInfo', 'Protection', 'Compatibility'); + $metadata = array('DocInfo', 'Settings', 'Compatibility'); foreach ($metadata as $meta) { $class = 'PhpOffice\\PhpWord\\Metadata\\' . $meta; $this->metadata[$meta] = new $class(); @@ -170,10 +170,12 @@ class PhpWord * * @return \PhpOffice\PhpWord\Metadata\Protection * @since 0.12.0 + * @deprecated Get the Document protection from PhpWord->getSettings()->getDocumentProtection(); + * @codeCoverageIgnore */ public function getProtection() { - return $this->metadata['Protection']; + return $this->getSettings()->getDocumentProtection(); } /** @@ -187,6 +189,17 @@ class PhpWord return $this->metadata['Compatibility']; } + /** + * Get compatibility + * + * @return \PhpOffice\PhpWord\Metadata\Settings + * @since 0.14.0 + */ + public function getSettings() + { + return $this->metadata['Settings']; + } + /** * Get all sections * diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php index 3da32cbb..70fad3e8 100644 --- a/src/PhpWord/Settings.php +++ b/src/PhpWord/Settings.php @@ -119,18 +119,6 @@ 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. * @@ -146,13 +134,6 @@ class Settings */ private static $outputEscapingEnabled = false; - /** - * Enables different header for odd and even pages. - * - * @var bool - */ - private static $evenAndOddHeaders = false; - /** * Return the compatibility option used by the XMLWriter * @@ -359,22 +340,6 @@ class Settings self::$outputEscapingEnabled = $outputEscapingEnabled; } - /** - * @return boolean - */ - public static function isEvenAndOddHeaders() - { - return self::$evenAndOddHeaders; - } - - /** - * @param boolean $evenAndOddHeaders - */ - public static function setEvenAndOddHeaders($evenAndOddHeaders) - { - self::$evenAndOddHeaders = $evenAndOddHeaders; - } - /** * Get default font name * @@ -428,46 +393,6 @@ 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/SimpleType/NumberFormat.php b/src/PhpWord/SimpleType/NumberFormat.php index 4c98dbef..9d24a2b3 100644 --- a/src/PhpWord/SimpleType/NumberFormat.php +++ b/src/PhpWord/SimpleType/NumberFormat.php @@ -25,8 +25,6 @@ use PhpOffice\PhpWord\Shared\AbstractEnum; * @since 0.14.0 * * @see http://www.datypic.com/sc/ooxml/t-w_ST_NumberFormat.html. - * - * @codeCoverageIgnore */ final class NumberFormat extends AbstractEnum { diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index 72406462..602205e9 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -105,10 +105,7 @@ class Settings extends AbstractPart 'w:defaultTabStop' => array('@attributes' => array('w:val' => '708')), 'w:hyphenationZone' => array('@attributes' => array('w:val' => '425')), '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(), @@ -143,24 +140,44 @@ class Settings extends AbstractPart ), ); + /** @var \PhpOffice\PhpWord\Metadata\Settings $documentSettings */ + $documentSettings = $this->getParentWriter()->getPhpWord()->getSettings(); + + $this->setOnOffValue('w:hideSpellingErrors', $documentSettings->hasHideSpellingErrors()); + $this->setOnOffValue('w:hideGrammaticalErrors', $documentSettings->hasHideGrammaticalErrors()); + $this->setOnOffValue('w:evenAndOddHeaders', $documentSettings->hasEvenAndOddHeaders()); + // Other settings - $this->getProtection(); + $this->setDocumentProtection($documentSettings->getDocumentProtection()); $this->getCompatibility(); } + /** + * Adds a boolean attribute to the settings array + * + * @param string $settingName + * @param boolean $booleanValue + */ + private function setOnOffValue($settingName, $booleanValue) + { + if ($booleanValue !== null && is_bool($booleanValue)) { + $this->settings[$settingName] = array('@attributes' => array('w:val' => $booleanValue ? 'true': 'false')); + } + } + /** * Get protection settings. * + * @param \PhpOffice\PhpWord\Metadata\Settings $documentProtection * @return void */ - private function getProtection() + private function setDocumentProtection($documentProtection) { - $protection = $this->getParentWriter()->getPhpWord()->getProtection(); - if ($protection->getEditing() !== null) { + if ($documentProtection != null && $documentProtection->getEditing() !== null) { $this->settings['w:documentProtection'] = array( '@attributes' => array( 'w:enforcement' => 1, - 'w:edit' => $protection->getEditing(), + 'w:edit' => $documentProtection->getEditing(), ) ); } diff --git a/tests/PhpWord/Metadata/SettingsTest.php b/tests/PhpWord/Metadata/SettingsTest.php new file mode 100644 index 00000000..35c15edd --- /dev/null +++ b/tests/PhpWord/Metadata/SettingsTest.php @@ -0,0 +1,69 @@ +setEvenAndOddHeaders(true); + $this->assertEquals(true, $oSettings->hasEvenAndOddHeaders()); + } + + /** + * HideGrammaticalErrors + */ + public function testHideGrammaticalErrors() + { + $oSettings = new Settings(); + $oSettings->setHideGrammaticalErrors(true); + $this->assertEquals(true, $oSettings->hasHideGrammaticalErrors()); + } + + /** + * HideSpellingErrors + */ + public function testHideSpellingErrors() + { + $oSettings = new Settings(); + $oSettings->setHideSpellingErrors(true); + $this->assertEquals(true, $oSettings->hasHideSpellingErrors()); + } + + /** + * DocumentProtection + */ + public function testDocumentProtection() + { + $oSettings = new Settings(); + $oSettings->setDocumentProtection(new Protection()); + $this->assertNotNull($oSettings->getDocumentProtection()); + + $oSettings->getDocumentProtection()->setEditing('trackedChanges'); + $this->assertEquals('trackedChanges', $oSettings->getDocumentProtection()->getEditing()); + } +} diff --git a/tests/PhpWord/SettingsTest.php b/tests/PhpWord/SettingsTest.php index 6347f424..f5ac3ed6 100644 --- a/tests/PhpWord/SettingsTest.php +++ b/tests/PhpWord/SettingsTest.php @@ -114,30 +114,6 @@ 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 - */ - public function testSetGetEvenAndOddHeaders() - { - $this->assertFalse(Settings::isEvenAndOddHeaders()); - Settings::setEvenAndOddHeaders(true); - $this->assertTrue(Settings::isEvenAndOddHeaders()); - } - /** * Test load config */ diff --git a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php index 13210fb7..fe6ea61c 100644 --- a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php @@ -41,7 +41,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase public function testDocumentProtection() { $phpWord = new PhpWord(); - $phpWord->getProtection()->setEditing('forms'); + $phpWord->getSettings()->getDocumentProtection()->setEditing('forms'); $doc = TestHelperDOCX::getDocument($phpWord); @@ -92,7 +92,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase public function testSpelling() { $phpWord = new PhpWord(); - Settings::setSpellingErrorsHidden(true); + $phpWord->getSettings()->setHideSpellingErrors(true); $doc = TestHelperDOCX::getDocument($phpWord); @@ -111,7 +111,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase public function testEvenAndOddHeaders() { $phpWord = new PhpWord(); - Settings::setEvenAndOddHeaders(true); + $phpWord->getSettings()->setEvenAndOddHeaders(true); $doc = TestHelperDOCX::getDocument($phpWord);