refactor Settings to not mix PHPWord settings and document settings

This commit is contained in:
troosan 2017-07-05 21:42:38 +02:00
parent 21303ed4fb
commit e9cc289243
11 changed files with 252 additions and 117 deletions

View File

@ -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
-------

View File

@ -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
~~~~~~~~~~~~

View File

@ -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';

View File

@ -0,0 +1,131 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2016 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Metadata;
/**
* Setting class
*
* @since 0.14.0
* @link http://www.datypic.com/sc/ooxml/t-w_CT_Settings.html
*/
class Settings
{
/**
* Hide spelling errors
*
* @var boolean
*/
private $hideSpellingErrors = false;
/**
* Hide grammatical errors
*
* @var boolean
*/
private $hideGrammaticalErrors = false;
/**
* Document Editing Restrictions
*
* @var PhpOffice\PhpWord\Metadata\Protection
*/
private $documentProtection;
/**
* Enables different header for odd and even pages.
*
* @var bool
*/
private $evenAndOddHeaders = false;
/**
* @return Protection
*/
public function getDocumentProtection()
{
if ($this->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;
}
}

View File

@ -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
*

View File

@ -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
*

View File

@ -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
{

View File

@ -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(),
)
);
}

View File

@ -0,0 +1,69 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2016 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Metadata;
/**
* Test class for PhpOffice\PhpWord\Metadata\Settings
*
* @runTestsInSeparateProcesses
*/
class SettingsTest extends \PHPUnit_Framework_TestCase
{
/**
* EvenAndOddHeaders
*/
public function testSetEvenAndOddHeaders()
{
$oSettings = new Settings();
$oSettings->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());
}
}

View File

@ -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
*/

View File

@ -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);