From d23409f687ec864ba6c489923be124ce29efb8e0 Mon Sep 17 00:00:00 2001 From: antoine Date: Sat, 28 Jan 2017 11:39:43 +0100 Subject: [PATCH] Add possibility to show/hide spelling and grammatical errors --- docs/general.rst | 13 +++++ src/PhpWord/Settings.php | 52 +++++++++++++++++++ src/PhpWord/Writer/Word2007/Part/Settings.php | 4 ++ tests/PhpWord/SettingsTest.php | 14 +++++ .../Writer/Word2007/Part/SettingsTest.php | 38 ++++++++++++++ 5 files changed, 121 insertions(+) diff --git a/docs/general.rst b/docs/general.rst index 27d0448a..9b7eb54b 100644 --- a/docs/general.rst +++ b/docs/general.rst @@ -183,3 +183,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 e049c46e..b1ef2759 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. * @@ -393,6 +405,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 d881e13a..f13381d2 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; +use PhpOffice\PhpWord\Settings as DocumentSettings; + /** * Word2007 settings part writer: word/settings.xml * @@ -104,6 +106,8 @@ class Settings extends AbstractPart 'w:hyphenationZone' => array('@attributes' => array('w:val' => '425')), 'w:characterSpacingControl' => array('@attributes' => array('w:val' => 'doNotCompress')), '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 f5ac3ed6..0d007b74 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 load config */ diff --git a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php index 6ed23e44..7ae06e14 100644 --- a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php @@ -18,6 +18,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\TestHelperDOCX; +use PhpOffice\PhpWord\Settings; /** * Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Settings @@ -66,4 +67,41 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertTrue($doc->elementExists($path, $file)); $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')); + } }