From 3962dad72f7060cc724bfe92f734e350b83f445c Mon Sep 17 00:00:00 2001 From: troosan Date: Sat, 22 Jul 2017 00:10:12 +0200 Subject: [PATCH 01/10] Add support for changing the document language --- docs/general.rst | 19 ++ samples/Sample_01_SimpleText.php | 3 + src/PhpWord/ComplexType/Language.php | 212 ++++++++++++++++++ src/PhpWord/Metadata/Settings.php | 28 +++ src/PhpWord/Writer/ODText/Part/Styles.php | 17 +- src/PhpWord/Writer/RTF/Part/Document.php | 6 +- src/PhpWord/Writer/Word2007/Part/Settings.php | 28 ++- src/PhpWord/Writer/Word2007/Part/Styles.php | 8 + .../ComplexType/FootnotePropertiesTest.php | 4 +- tests/PhpWord/ComplexType/LanguageTest.php | 66 ++++++ tests/PhpWord/ComplexType/ProofStateTest.php | 61 +++++ .../Writer/Word2007/Part/SettingsTest.php | 23 +- 12 files changed, 464 insertions(+), 11 deletions(-) create mode 100644 src/PhpWord/ComplexType/Language.php create mode 100644 tests/PhpWord/ComplexType/LanguageTest.php create mode 100644 tests/PhpWord/ComplexType/ProofStateTest.php diff --git a/docs/general.rst b/docs/general.rst index 87fecb77a4..5b5c5d9ee2 100644 --- a/docs/general.rst +++ b/docs/general.rst @@ -201,6 +201,25 @@ The default symbol to represent a decimal figure is the ``.`` in english. In fre $phpWord->getSettings()->setDecimalSymbol(','); +Document Language +~~~~~~~~~~~~~~~~~ +The default language of the document can be change with the following. + +.. code-block:: php + + $phpWord->getSettings()->setThemeFontLang(new Language(Language::FR_BE)); + +``Languge`` has 3 parameters, one for Latin languages, one for East Asian languages and one for Complex (Bi-Directional) languages. +A couple of language codes are provided in the ``PhpOffice\PhpWord\ComplexType\Language`` class but any valid code/ID can be used. + +In case you are generating an RTF document the Language need to be set differently. + +.. code-block:: php + + $lang = new Language(); + $lang->setLangId(Language::EN_GB_ID); + $phpWord->getSettings()->setThemeFontLang($lang); + Document information -------------------- diff --git a/samples/Sample_01_SimpleText.php b/samples/Sample_01_SimpleText.php index 1e51b2c072..9df377e2f8 100644 --- a/samples/Sample_01_SimpleText.php +++ b/samples/Sample_01_SimpleText.php @@ -1,9 +1,12 @@ getSettings()->setThemeFontLang(new Language(Language::EN_GB)); $fontStyleName = 'rStyle'; $phpWord->addFontStyle($fontStyleName, array('bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true)); diff --git a/src/PhpWord/ComplexType/Language.php b/src/PhpWord/ComplexType/Language.php new file mode 100644 index 0000000000..5d49d5c7f7 --- /dev/null +++ b/src/PhpWord/ComplexType/Language.php @@ -0,0 +1,212 @@ +setLatin($latin); + } + if ($eastAsia != null) { + $this->setEastAsia($eastAsia); + } + if ($bidirectional!= null) { + $this->setBidirectional($bidirectional); + } + } + + /** + * Set the Latin Language + * + * @param string $latin + * The value for the latin language + * @return self + */ + public function setLatin($latin) + { + $this->validateLocale($latin); + $this->latin = $latin; + return $this; + } + + /** + * Get the Latin Language + * + * @return string + */ + public function getLatin() + { + return $this->latin; + } + + /** + * Set the Language ID + * + * @param int $langId + * The value for the language ID + * @return self + * @see https://technet.microsoft.com/en-us/library/cc287874(v=office.12).aspx + */ + public function setLangId($langId) + { + $this->langId = $langId; + return $this; + } + + /** + * Get the Language ID + * + * @return int + */ + public function getLangId() + { + return $this->langId; + } + + /** + * Set the East Asian Language + * + * @param string $eastAsia + * The value for the east asian language + * @return self + */ + public function setEastAsia($eastAsia) + { + $this->validateLocale($eastAsia); + $this->eastAsia = $eastAsia; + return $this; + } + + /** + * Get the East Asian Language + * + * @return string + */ + public function getEastAsia() + { + return $this->eastAsia; + } + + /** + * Set the Complex Script Language + * + * @param string $bidi + * The value for the complex script language + * @return self + */ + public function setBidirectional($bidirectional) + { + $this->validateLocale($bidirectional); + $this->bidirectional = $bidirectional; + return $this; + } + + /** + * Get the Complex Script Language + * + * @return string + */ + public function getBidirectional() + { + return $this->bidirectional; + } + + /** + * Validates that the language passed is in the format xx-xx + * + * @param string $locale + * @return boolean + */ + private function validateLocale($locale) + { + if (strstr($locale, '-') === false) { + throw new \InvalidArgumentException($locale . ' is not a valid language code'); + } + } +} diff --git a/src/PhpWord/Metadata/Settings.php b/src/PhpWord/Metadata/Settings.php index 9b2c22859e..8b9c033ed6 100644 --- a/src/PhpWord/Metadata/Settings.php +++ b/src/PhpWord/Metadata/Settings.php @@ -19,6 +19,7 @@ use PhpOffice\PhpWord\ComplexType\ProofState; use PhpOffice\PhpWord\SimpleType\Zoom; use PhpOffice\PhpWord\ComplexType\TrackChangesView; +use PhpOffice\PhpWord\ComplexType\Language; /** * Setting class @@ -100,6 +101,13 @@ class Settings */ private $evenAndOddHeaders = false; + /** + * Theme Font Languages + * + * @var Language + */ + private $themeFontLang; + /** * Radix Point for Field Code Evaluation * @@ -291,6 +299,26 @@ public function setZoom($zoom) } } + /** + * Returns the Language + * + * @return Language + */ + public function getThemeFontLang() + { + return $this->themeFontLang; + } + + /** + * sets the Language for this document + * + * @param Language $themeFontLang + */ + public function setThemeFontLang($themeFontLang) + { + $this->themeFontLang = $themeFontLang; + } + /** * Returns the Radix Point for Field Code Evaluation * diff --git a/src/PhpWord/Writer/ODText/Part/Styles.php b/src/PhpWord/Writer/ODText/Part/Styles.php index b50be0e804..ee22aaabb7 100644 --- a/src/PhpWord/Writer/ODText/Part/Styles.php +++ b/src/PhpWord/Writer/ODText/Part/Styles.php @@ -81,22 +81,27 @@ private function writeDefault(XMLWriter $xmlWriter) $xmlWriter->writeAttribute('style:writing-mode', 'page'); $xmlWriter->endElement(); // style:paragraph-properties + $language = $this->getParentWriter()->getPhpWord()->getSettings()->getThemeFontLang(); + $latinLang = $language != null && is_string($language->getLatin()) ? explode('-', $language->getLatin()) : array('fr', 'FR'); + $asianLang = $language != null && is_string($language->getEastAsia()) ? explode('-', $language->getEastAsia()) : array('zh', 'CN'); + $complexLang = $language != null && is_string($language->getBidirectional()) ? explode('-', $language->getBidirectional()) : array('hi', 'IN'); + // Font $xmlWriter->startElement('style:text-properties'); $xmlWriter->writeAttribute('style:use-window-font-color', 'true'); $xmlWriter->writeAttribute('style:font-name', Settings::getDefaultFontName()); $xmlWriter->writeAttribute('fo:font-size', Settings::getDefaultFontSize() . 'pt'); - $xmlWriter->writeAttribute('fo:language', 'fr'); - $xmlWriter->writeAttribute('fo:country', 'FR'); + $xmlWriter->writeAttribute('fo:language', $latinLang[0]); + $xmlWriter->writeAttribute('fo:country', $latinLang[1]); $xmlWriter->writeAttribute('style:letter-kerning', 'true'); $xmlWriter->writeAttribute('style:font-name-asian', Settings::getDefaultFontName() . '2'); $xmlWriter->writeAttribute('style:font-size-asian', Settings::getDefaultFontSize() . 'pt'); - $xmlWriter->writeAttribute('style:language-asian', 'zh'); - $xmlWriter->writeAttribute('style:country-asian', 'CN'); + $xmlWriter->writeAttribute('style:language-asian', $asianLang[0]); + $xmlWriter->writeAttribute('style:country-asian', $asianLang[1]); $xmlWriter->writeAttribute('style:font-name-complex', Settings::getDefaultFontName() . '2'); $xmlWriter->writeAttribute('style:font-size-complex', Settings::getDefaultFontSize() . 'pt'); - $xmlWriter->writeAttribute('style:language-complex', 'hi'); - $xmlWriter->writeAttribute('style:country-complex', 'IN'); + $xmlWriter->writeAttribute('style:language-complex', $complexLang[0]); + $xmlWriter->writeAttribute('style:country-complex', $complexLang[1]); $xmlWriter->writeAttribute('fo:hyphenate', 'false'); $xmlWriter->writeAttribute('fo:hyphenation-remain-char-count', '2'); $xmlWriter->writeAttribute('fo:hyphenation-push-char-count', '2'); diff --git a/src/PhpWord/Writer/RTF/Part/Document.php b/src/PhpWord/Writer/RTF/Part/Document.php index 24ee7b0add..97b2f1b611 100644 --- a/src/PhpWord/Writer/RTF/Part/Document.php +++ b/src/PhpWord/Writer/RTF/Part/Document.php @@ -86,6 +86,10 @@ private function writeInfo() */ private function writeFormatting() { + $docSettings = $this->getParentWriter()->getPhpWord()->getSettings(); + // Applies a language to a text run (defaults to 1036 : French (France)) + $langId = $docSettings->getThemeFontLang() != null && $docSettings->getThemeFontLang()->getLangId() != null ? $docSettings->getThemeFontLang()->getLangId() : 1036; + $content = ''; $content .= '\deftab720'; // Set the default tab size (720 twips) @@ -94,7 +98,7 @@ private function writeFormatting() $content .= '\uc1'; // Set the numberof bytes that follows a unicode character $content .= '\pard'; // Resets to default paragraph properties. $content .= '\nowidctlpar'; // No widow/orphan control - $content .= '\lang1036'; // Applies a language to a text run (1036 : French (France)) + $content .= '\lang' . $langId; $content .= '\kerning1'; // Point size (in half-points) above which to kern character pairs $content .= '\fs' . (Settings::getDefaultFontSize() * 2); // Set the font size in half-points $content .= PHP_EOL; diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index 529d47af06..fb3bdd9628 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -20,6 +20,7 @@ use PhpOffice\PhpWord\Settings as DocumentSettings; use PhpOffice\PhpWord\ComplexType\ProofState; use PhpOffice\PhpWord\ComplexType\TrackChangesView; +use PhpOffice\PhpWord\ComplexType\Language; /** * Word2007 settings part writer: word/settings.xml @@ -110,7 +111,6 @@ private function getSettings() '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:themeFontLang' => array('@attributes' => array('w:val' => 'en-US')), 'w:decimalSymbol' => array('@attributes' => array('w:val' => $documentSettings->getDecimalSymbol())), 'w:listSeparator' => array('@attributes' => array('w:val' => ';')), 'w:compat' => array(), @@ -152,6 +152,7 @@ private function getSettings() $this->setOnOffValue('w:doNotTrackFormatting', $documentSettings->hasDoNotTrackFormatting()); $this->setOnOffValue('w:evenAndOddHeaders', $documentSettings->hasEvenAndOddHeaders()); + $this->setThemeFontLang($documentSettings->getThemeFontLang()); $this->setRevisionView($documentSettings->getRevisionView()); $this->setDocumentProtection($documentSettings->getDocumentProtection()); $this->setProofState($documentSettings->getProofState()); @@ -230,6 +231,31 @@ private function setRevisionView(TrackChangesView $trackChangesView = null) } } + /** + * Sets the language + * + * @param Language $language + */ + private function setThemeFontLang(Language $language = null) + { + if ($language != null && ($language->getLatin() != null || $language->getEastAsia() != null || $language->getBidirectional() != null)) { + + if ($language->getLatin() != null) { + $lang['w:val'] = $language->getLatin(); + } + if ($language->getEastAsia() != null) { + $lang['w:eastAsia'] = $language->getEastAsia(); + } + if ($language->getBidirectional() != null) { + $lang['w:bidi'] = $language->getBidirectional(); + } + + $this->settings['w:themeFontLang'] = array('@attributes' => $lang); + } else { + $this->settings['w:themeFontLang'] = array('@attributes' => array('w:val' => 'en-US')); + } + } + /** * Set the magnification * diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index 7bcb8d92a4..bf47637100 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -85,6 +85,7 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles) { $fontName = PhpWordSettings::getDefaultFontName(); $fontSize = PhpWordSettings::getDefaultFontSize(); + $language = $this->getParentWriter()->getPhpWord()->getSettings()->getThemeFontLang(); // Default font $xmlWriter->startElement('w:docDefaults'); @@ -102,6 +103,13 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles) $xmlWriter->startElement('w:szCs'); $xmlWriter->writeAttribute('w:val', $fontSize * 2); $xmlWriter->endElement(); // w:szCs + if ($language != null) { + $xmlWriter->startElement('w:lang'); + $xmlWriter->writeAttributeIf($language->getLatin() != null, 'w:val', $language->getLatin()); + $xmlWriter->writeAttributeIf($language->getEastAsia() != null, 'w:eastAsia', $language->getEastAsia()); + $xmlWriter->writeAttributeIf($language->getBidirectional() != null, 'w:bidi', $language->getBidirectional()); + $xmlWriter->endElement(); // w:lang + } $xmlWriter->endElement(); // w:rPr $xmlWriter->endElement(); // w:rPrDefault $xmlWriter->endElement(); // w:docDefaults diff --git a/tests/PhpWord/ComplexType/FootnotePropertiesTest.php b/tests/PhpWord/ComplexType/FootnotePropertiesTest.php index 025e8c91b4..bdfcd8ba75 100644 --- a/tests/PhpWord/ComplexType/FootnotePropertiesTest.php +++ b/tests/PhpWord/ComplexType/FootnotePropertiesTest.php @@ -21,9 +21,9 @@ use PhpOffice\PhpWord\SimpleType\NumberFormat; /** - * Test class for PhpOffice\PhpWord\SimpleType\FootnoteProperties + * Test class for PhpOffice\PhpWord\ComplexType\FootnoteProperties * - * @coversDefaultClass \PhpOffice\PhpWord\SimpleType\FootnoteProperties + * @coversDefaultClass \PhpOffice\PhpWord\ComplexType\FootnoteProperties * @runTestsInSeparateProcesses */ class FootnotePropertiesTest extends \PHPUnit_Framework_TestCase diff --git a/tests/PhpWord/ComplexType/LanguageTest.php b/tests/PhpWord/ComplexType/LanguageTest.php new file mode 100644 index 0000000000..c3ac75a9dc --- /dev/null +++ b/tests/PhpWord/ComplexType/LanguageTest.php @@ -0,0 +1,66 @@ +assertEquals(Language::DE_DE, $language->getLatin()); + $this->assertEquals(Language::KO_KR, $language->getEastAsia()); + $this->assertEquals(Language::HE_IL, $language->getBidirectional()); + } + + /** + * Tests the getters and setters + */ + public function testGetSet() + { + $language = new Language(); + $language->setLatin(Language::DE_DE); + $language->setEastAsia(Language::KO_KR); + $language->setBidirectional(Language::HE_IL); + $language->setLangId(Language::DE_DE_ID); + + $this->assertEquals(Language::DE_DE, $language->getLatin()); + $this->assertEquals(Language::KO_KR, $language->getEastAsia()); + $this->assertEquals(Language::HE_IL, $language->getBidirectional()); + $this->assertEquals(Language::DE_DE_ID, $language->getLangId()); + } + + /** + * Test throws exception if wrong locale is given + * + * @expectedException \InvalidArgumentException + */ + public function testWrongLanguage() + { + $language = new Language(); + $language->setLatin('fr'); + } +} diff --git a/tests/PhpWord/ComplexType/ProofStateTest.php b/tests/PhpWord/ComplexType/ProofStateTest.php new file mode 100644 index 0000000000..0b7e74ca89 --- /dev/null +++ b/tests/PhpWord/ComplexType/ProofStateTest.php @@ -0,0 +1,61 @@ +setGrammar(ProofState::CLEAN); + $pState->setSpelling(ProofState::DIRTY); + + $this->assertEquals(ProofState::CLEAN, $pState->getGrammar()); + $this->assertEquals(ProofState::DIRTY, $pState->getSpelling()); + } + + /** + * Test throws exception if wrong grammar proof state value given + * + * @expectedException \InvalidArgumentException + */ + public function testWrongGrammar() + { + $pState = new ProofState(); + $pState->setGrammar('Wrong'); + } + + /** + * Test throws exception if wrong spelling proof state value given + * + * @expectedException \InvalidArgumentException + */ + public function testWrongSpelling() + { + $pState= new ProofState(); + $pState->setSpelling('Wrong'); + } +} diff --git a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php index 828e12835c..0e6ba6c8ce 100644 --- a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php @@ -21,6 +21,7 @@ use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\SimpleType\Zoom; use PhpOffice\PhpWord\ComplexType\TrackChangesView; +use PhpOffice\PhpWord\ComplexType\Language; /** * Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Settings @@ -73,7 +74,7 @@ public function testCompatibility() /** * Test language */ - public function testLanguage() + public function testDefaultLanguage() { $phpWord = new PhpWord(); @@ -88,6 +89,26 @@ public function testLanguage() $this->assertEquals('en-US', $element->getAttribute('w:val')); } + /** + * Test language + */ + public function testLanguage() + { + $phpWord = new PhpWord(); + $phpWord->getSettings()->setThemeFontLang(new Language(Language::DE_DE, Language::KO_KR, Language::HE_IL)); + $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(Language::DE_DE, $element->getAttribute('w:val')); + $this->assertEquals(Language::KO_KR, $element->getAttribute('w:eastAsia')); + $this->assertEquals(Language::HE_IL, $element->getAttribute('w:bidi')); + } + /** * Test spelling */ From f3af206e5b82a0a2aadde917edc1cbe93c55086f Mon Sep 17 00:00:00 2001 From: troosan Date: Sat, 22 Jul 2017 00:44:14 +0200 Subject: [PATCH 02/10] fix settings reader --- src/PhpWord/ComplexType/Language.php | 4 ++-- src/PhpWord/Reader/Word2007/Settings.php | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/ComplexType/Language.php b/src/PhpWord/ComplexType/Language.php index 5d49d5c7f7..5e18350af8 100644 --- a/src/PhpWord/ComplexType/Language.php +++ b/src/PhpWord/ComplexType/Language.php @@ -203,9 +203,9 @@ public function getBidirectional() * @param string $locale * @return boolean */ - private function validateLocale($locale) + private function validateLocale($locale = null) { - if (strstr($locale, '-') === false) { + if ($locale != null && strstr($locale, '-') === false) { throw new \InvalidArgumentException($locale . ' is not a valid language code'); } } diff --git a/src/PhpWord/Reader/Word2007/Settings.php b/src/PhpWord/Reader/Word2007/Settings.php index d2ffc1f34b..68a72bed8c 100644 --- a/src/PhpWord/Reader/Word2007/Settings.php +++ b/src/PhpWord/Reader/Word2007/Settings.php @@ -20,6 +20,7 @@ use PhpOffice\Common\XMLReader; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\ComplexType\TrackChangesView; +use PhpOffice\PhpWord\ComplexType\Language; /** * Settings reader @@ -66,6 +67,28 @@ public function read(PhpWord $phpWord) } } + /** + * Sets the document Language + * + * @param XMLReader $xmlReader + * @param PhpWord $phpWord + * @param \DOMNode $node + */ + protected function setThemeFontLang(XMLReader $xmlReader, PhpWord $phpWord, \DOMNode $node) + { + + $val = $xmlReader->getAttribute('w:val', $node); + $eastAsia = $xmlReader->getAttribute('w:eastAsia', $node); + $bidi = $xmlReader->getAttribute('w:bidi', $node); + + $themeFontLang = new Language(); + $themeFontLang->setLatin($val); + $themeFontLang->setLatin($eastAsia); + $themeFontLang->setLatin($bidi); + + $phpWord->getSettings()->setThemeFontLang($themeFontLang); + } + /** * Sets the document protection * From c18fd6cf4c8c352cabff8ea10c7d5eb381b3375a Mon Sep 17 00:00:00 2001 From: troosan Date: Wed, 9 Aug 2017 23:21:21 +0200 Subject: [PATCH 03/10] Make Language extend AbstractStyle so it can be set on Font style --- docs/styles.rst | 11 ++-- samples/Sample_01_SimpleText.php | 12 +++- samples/Sample_11_ReadWord2007.php | 2 +- src/PhpWord/Metadata/Settings.php | 2 +- src/PhpWord/Reader/Word2007/AbstractPart.php | 29 ++++---- src/PhpWord/Reader/Word2007/Settings.php | 4 +- src/PhpWord/Style/Font.php | 33 ++++++++++ .../{ComplexType => Style}/Language.php | 4 +- src/PhpWord/Style/Paragraph.php | 34 +++++++++- src/PhpWord/Writer/Word2007/Part/Settings.php | 4 +- src/PhpWord/Writer/Word2007/Style/Font.php | 10 +++ .../Writer/Word2007/Style/Paragraph.php | 5 +- tests/PhpWord/ComplexType/LanguageTest.php | 66 ------------------- tests/PhpWord/Style/FontTest.php | 1 + tests/PhpWord/Style/LanguageTest.php | 62 +++++++++++++++++ tests/PhpWord/Style/ParagraphTest.php | 1 + .../Writer/Word2007/Part/SettingsTest.php | 6 +- 17 files changed, 189 insertions(+), 97 deletions(-) rename src/PhpWord/{ComplexType => Style}/Language.php (98%) delete mode 100644 tests/PhpWord/ComplexType/LanguageTest.php create mode 100644 tests/PhpWord/Style/LanguageTest.php diff --git a/docs/styles.rst b/docs/styles.rst index 4f8a53fede..e8d2aec8a7 100644 --- a/docs/styles.rst +++ b/docs/styles.rst @@ -55,6 +55,8 @@ Available Font style options: - ``subScript``. Subscript, *true* or *false*. - ``superScript``. Superscript, *true* or *false*. - ``underline``. Underline, *dash*, *dotted*, etc. +- ``lang``. Language, either a language code like *en-US*, *fr-BE*, etc. or an object (or as an array) if you need to set eastAsian or bidirectional languages + See ``\PhpOffice\PhpWord\Style\Language`` class for some language codes. .. _paragraph-style: @@ -64,7 +66,7 @@ Paragraph Available Paragraph style options: - ``alignment``. Supports all alignment modes since 1st Edition of ECMA-376 standard up till ISO/IEC 29500:2012. -See ``\PhpOffice\PhpWord\SimpleType\Jc`` class for the details. + See ``\PhpOffice\PhpWord\SimpleType\Jc`` class for the details. - ``basedOn``. Parent style. - ``hanging``. Hanging by how much. - ``indent``. Indent by how much. @@ -87,7 +89,7 @@ Table Available Table style options: - ``alignment``. Supports all alignment modes since 1st Edition of ECMA-376 standard up till ISO/IEC 29500:2012. -See ``\PhpOffice\PhpWord\SimpleType\JcTable`` and ``\PhpOffice\PhpWord\SimpleType\Jc`` classes for the details. + See ``\PhpOffice\PhpWord\SimpleType\JcTable`` and ``\PhpOffice\PhpWord\SimpleType\Jc`` classes for the details. - ``bgColor``. Background color, e.g. '9966CC'. - ``border(Top|Right|Bottom|Left)Color``. Border color, e.g. '9966CC'. - ``border(Top|Right|Bottom|Left)Size``. Border size in twips. @@ -106,7 +108,8 @@ Available Cell style options: - ``border(Top|Right|Bottom|Left)Color``. Border color, e.g. '9966CC'. - ``border(Top|Right|Bottom|Left)Size``. Border size in twips. - ``gridSpan``. Number of columns spanned. -- ``textDirection(btLr|tbRl)``. Direction of text. You can use constants ``\PhpOffice\PhpWord\Style\Cell::TEXT_DIR_BTLR`` and ``\PhpOffice\PhpWord\Style\Cell::TEXT_DIR_TBRL`` +- ``textDirection(btLr|tbRl)``. Direction of text. + You can use constants ``\PhpOffice\PhpWord\Style\Cell::TEXT_DIR_BTLR`` and ``\PhpOffice\PhpWord\Style\Cell::TEXT_DIR_TBRL`` - ``valign``. Vertical alignment, *top*, *center*, *both*, *bottom*. - ``vMerge``. *restart* or *continue*. - ``width``. Cell width in twips. @@ -133,7 +136,7 @@ Numbering level Available NumberingLevel style options: - ``alignment``. Supports all alignment modes since 1st Edition of ECMA-376 standard up till ISO/IEC 29500:2012. -See ``\PhpOffice\PhpWord\SimpleType\Jc`` class for the details. + See ``\PhpOffice\PhpWord\SimpleType\Jc`` class for the details. - ``font``. Font name. - ``format``. Numbering format bullet\|decimal\|upperRoman\|lowerRoman\|upperLetter\|lowerLetter. - ``hanging``. See paragraph style. diff --git a/samples/Sample_01_SimpleText.php b/samples/Sample_01_SimpleText.php index 9df377e2f8..fae6c21031 100644 --- a/samples/Sample_01_SimpleText.php +++ b/samples/Sample_01_SimpleText.php @@ -1,12 +1,16 @@ getSettings()->setThemeFontLang(new Language(Language::EN_GB)); +$phpWord->getSettings()->setThemeFontLang($languageEnGb); $fontStyleName = 'rStyle'; $phpWord->addFontStyle($fontStyleName, array('bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true)); @@ -23,6 +27,10 @@ $section->addTitle('Welcome to PhpWord', 1); $section->addText('Hello World!'); +// $pStyle = new Font(); +// $pStyle->setLang() +$section->addText('Ce texte-ci est en français.', array('lang' => \PhpOffice\PhpWord\Style\Language::FR_BE)); + // Two text break $section->addTextBreak(2); diff --git a/samples/Sample_11_ReadWord2007.php b/samples/Sample_11_ReadWord2007.php index c0b54c7a49..232fe05767 100644 --- a/samples/Sample_11_ReadWord2007.php +++ b/samples/Sample_11_ReadWord2007.php @@ -3,7 +3,7 @@ // Read contents $name = basename(__FILE__, '.php'); -$source = __DIR__ . "/resources/{$name}.docx"; +$source = __DIR__ . "/resources/sample.docx"; echo date('H:i:s'), " Reading contents from `{$source}`", EOL; $phpWord = \PhpOffice\PhpWord\IOFactory::load($source); diff --git a/src/PhpWord/Metadata/Settings.php b/src/PhpWord/Metadata/Settings.php index 8b9c033ed6..d7f2467690 100644 --- a/src/PhpWord/Metadata/Settings.php +++ b/src/PhpWord/Metadata/Settings.php @@ -19,7 +19,7 @@ use PhpOffice\PhpWord\ComplexType\ProofState; use PhpOffice\PhpWord\SimpleType\Zoom; use PhpOffice\PhpWord\ComplexType\TrackChangesView; -use PhpOffice\PhpWord\ComplexType\Language; +use PhpOffice\PhpWord\Style\Language; /** * Setting class diff --git a/src/PhpWord/Reader/Word2007/AbstractPart.php b/src/PhpWord/Reader/Word2007/AbstractPart.php index 51970a06c3..41bf0070d6 100644 --- a/src/PhpWord/Reader/Word2007/AbstractPart.php +++ b/src/PhpWord/Reader/Word2007/AbstractPart.php @@ -316,18 +316,20 @@ protected function readParagraphStyle(XMLReader $xmlReader, \DOMElement $domNode $styleNode = $xmlReader->getElement('w:pPr', $domNode); $styleDefs = array( - 'styleName' => array(self::READ_VALUE, 'w:pStyle'), - 'alignment' => array(self::READ_VALUE, 'w:jc'), - 'basedOn' => array(self::READ_VALUE, 'w:basedOn'), - 'next' => array(self::READ_VALUE, 'w:next'), - 'indent' => array(self::READ_VALUE, 'w:ind', 'w:left'), - 'hanging' => array(self::READ_VALUE, 'w:ind', 'w:hanging'), - 'spaceAfter' => array(self::READ_VALUE, 'w:spacing', 'w:after'), - 'spaceBefore' => array(self::READ_VALUE, 'w:spacing', 'w:before'), - 'widowControl' => array(self::READ_FALSE, 'w:widowControl'), - 'keepNext' => array(self::READ_TRUE, 'w:keepNext'), - 'keepLines' => array(self::READ_TRUE, 'w:keepLines'), - 'pageBreakBefore' => array(self::READ_TRUE, 'w:pageBreakBefore'), + 'styleName' => array(self::READ_VALUE, 'w:pStyle'), + 'alignment' => array(self::READ_VALUE, 'w:jc'), + 'basedOn' => array(self::READ_VALUE, 'w:basedOn'), + 'next' => array(self::READ_VALUE, 'w:next'), + 'indent' => array(self::READ_VALUE, 'w:ind', 'w:left'), + 'hanging' => array(self::READ_VALUE, 'w:ind', 'w:hanging'), + 'spaceAfter' => array(self::READ_VALUE, 'w:spacing', 'w:after'), + 'spaceBefore' => array(self::READ_VALUE, 'w:spacing', 'w:before'), + 'widowControl' => array(self::READ_FALSE, 'w:widowControl'), + 'keepNext' => array(self::READ_TRUE, 'w:keepNext'), + 'keepLines' => array(self::READ_TRUE, 'w:keepLines'), + 'pageBreakBefore' => array(self::READ_TRUE, 'w:pageBreakBefore'), + 'contextualSpacing' => array(self::READ_TRUE, 'w:contextualSpacing'), + 'bidi' => array(self::READ_TRUE, 'w:bidi'), ); return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs); @@ -371,6 +373,9 @@ protected function readFontStyle(XMLReader $xmlReader, \DOMElement $domNode) 'subScript' => array(self::READ_EQUAL, 'w:vertAlign', 'w:val', 'subscript'), 'fgColor' => array(self::READ_VALUE, 'w:highlight'), 'rtl' => array(self::READ_TRUE, 'w:rtl'), + 'font-latin' => array(self::READ_VALUE, 'w:font', 'w:val'), + 'font-eastAsia' => array(self::READ_VALUE, 'w:font', 'w:eastAsia'), + 'font-bidi' => array(self::READ_VALUE, 'w:font', 'w:bidi'), ); return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs); diff --git a/src/PhpWord/Reader/Word2007/Settings.php b/src/PhpWord/Reader/Word2007/Settings.php index 68a72bed8c..c4baa0cb78 100644 --- a/src/PhpWord/Reader/Word2007/Settings.php +++ b/src/PhpWord/Reader/Word2007/Settings.php @@ -18,9 +18,9 @@ namespace PhpOffice\PhpWord\Reader\Word2007; use PhpOffice\Common\XMLReader; -use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\ComplexType\TrackChangesView; -use PhpOffice\PhpWord\ComplexType\Language; +use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\Style\Language; /** * Settings reader diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index b625e3b8a9..7be6ddddef 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -228,6 +228,12 @@ class Font extends AbstractStyle */ private $rtl = false; + /** + * Languages + * @var \PhpOffice\PhpWord\Style\Language + */ + private $lang; + /** * Create new font style * @@ -276,6 +282,7 @@ public function getStyleValues() 'paragraph' => $this->getParagraph(), 'rtl' => $this->isRTL(), 'shading' => $this->getShading(), + 'lang' => $this->getLang(), ); return $styles; @@ -783,6 +790,32 @@ public function setShading($value = null) return $this; } + /** + * Get language + * + * @return \PhpOffice\PhpWord\Style\Language + */ + public function getLang() + { + return $this->lang; + } + + /** + * Set language + * + * @param mixed $value + * @return self + */ + public function setLang($value = null) + { + if (is_string($value)) { + $value = new Language($value); + } + $this->setObjectVal($value, 'Language', $this->lang); + + return $this; + } + /** * Get bold * diff --git a/src/PhpWord/ComplexType/Language.php b/src/PhpWord/Style/Language.php similarity index 98% rename from src/PhpWord/ComplexType/Language.php rename to src/PhpWord/Style/Language.php index 5e18350af8..8f561461c5 100644 --- a/src/PhpWord/ComplexType/Language.php +++ b/src/PhpWord/Style/Language.php @@ -14,7 +14,7 @@ * @copyright 2010-2016 PHPWord contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ -namespace PhpOffice\PhpWord\ComplexType; +namespace PhpOffice\PhpWord\Style; /** * Language @@ -23,7 +23,7 @@ * @see http://www.datypic.com/sc/ooxml/t-w_CT_Language.html * @see https://technet.microsoft.com/en-us/library/cc287874(v=office.12).aspx */ -final class Language +final class Language extends AbstractStyle { const EN_US = 'en-US'; diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index bad9ace97b..3b34c2057e 100644 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -165,6 +165,13 @@ class Paragraph extends Border */ private $contextualSpacing = false; + /** + * Right to Left Paragraph Layout + * + * @var bool + */ + private $bidi = false; + /** * Set Style value * @@ -216,6 +223,7 @@ public function getStyleValues() 'tabs' => $this->getTabs(), 'shading' => $this->getShading(), 'contextualSpacing' => $this->hasContextualSpacing(), + 'bidi' => $this->isBidi(), ); return $styles; @@ -739,7 +747,7 @@ public function setShading($value = null) return $this; } - + /** * Get contextualSpacing * @@ -762,4 +770,28 @@ public function setContextualSpacing($contextualSpacing) return $this; } + + /** + * Get bidirectional + * + * @return bool + */ + public function isBidi() + { + return $this->bidi; + } + + /** + * Set bidi + * + * @param bool $bidi + * Set to true to write from right to left + * @return self + */ + public function setBidi($bidi) + { + $this->bidi = $bidi; + + return $this; + } } diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index fb3bdd9628..f3e21e6a2e 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -17,10 +17,10 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; -use PhpOffice\PhpWord\Settings as DocumentSettings; use PhpOffice\PhpWord\ComplexType\ProofState; use PhpOffice\PhpWord\ComplexType\TrackChangesView; -use PhpOffice\PhpWord\ComplexType\Language; +use PhpOffice\PhpWord\Settings as DocumentSettings; +use PhpOffice\PhpWord\Style\Language; /** * Word2007 settings part writer: word/settings.xml diff --git a/src/PhpWord/Writer/Word2007/Style/Font.php b/src/PhpWord/Writer/Word2007/Style/Font.php index 97cf3088b9..a73fc98e4b 100644 --- a/src/PhpWord/Writer/Word2007/Style/Font.php +++ b/src/PhpWord/Writer/Word2007/Style/Font.php @@ -86,6 +86,16 @@ private function writeStyle() $xmlWriter->endElement(); } + //Language + $language = $style->getLang(); + if ($language != null && ($language->getLatin() != null || $language->getEastAsia() != null || $language->getBidirectional() != null)) { + $xmlWriter->startElement('w:lang'); + $xmlWriter->writeAttributeIf($language->getLatin() != null, 'w:val', $language->getLatin()); + $xmlWriter->writeAttributeIf($language->getEastAsia() != null, 'w:eastAsia', $language->getEastAsia()); + $xmlWriter->writeAttributeIf($language->getBidirectional() != null, 'w:bidi', $language->getBidirectional()); + $xmlWriter->endElement(); + } + // Color $color = $style->getColor(); $xmlWriter->writeElementIf($color !== null, 'w:color', 'w:val', $color); diff --git a/src/PhpWord/Writer/Word2007/Style/Paragraph.php b/src/PhpWord/Writer/Word2007/Style/Paragraph.php index 787f2a5f7f..32f8abbaaa 100644 --- a/src/PhpWord/Writer/Word2007/Style/Paragraph.php +++ b/src/PhpWord/Writer/Word2007/Style/Paragraph.php @@ -106,7 +106,10 @@ private function writeStyle() } $xmlWriter->endElement(); } - + + //Right to left + $xmlWriter->writeElementIf($styles['bidi'] === true, 'w:bidi'); + //Paragraph contextualSpacing $xmlWriter->writeElementIf($styles['contextualSpacing'] === true, 'w:contextualSpacing'); diff --git a/tests/PhpWord/ComplexType/LanguageTest.php b/tests/PhpWord/ComplexType/LanguageTest.php deleted file mode 100644 index c3ac75a9dc..0000000000 --- a/tests/PhpWord/ComplexType/LanguageTest.php +++ /dev/null @@ -1,66 +0,0 @@ -assertEquals(Language::DE_DE, $language->getLatin()); - $this->assertEquals(Language::KO_KR, $language->getEastAsia()); - $this->assertEquals(Language::HE_IL, $language->getBidirectional()); - } - - /** - * Tests the getters and setters - */ - public function testGetSet() - { - $language = new Language(); - $language->setLatin(Language::DE_DE); - $language->setEastAsia(Language::KO_KR); - $language->setBidirectional(Language::HE_IL); - $language->setLangId(Language::DE_DE_ID); - - $this->assertEquals(Language::DE_DE, $language->getLatin()); - $this->assertEquals(Language::KO_KR, $language->getEastAsia()); - $this->assertEquals(Language::HE_IL, $language->getBidirectional()); - $this->assertEquals(Language::DE_DE_ID, $language->getLangId()); - } - - /** - * Test throws exception if wrong locale is given - * - * @expectedException \InvalidArgumentException - */ - public function testWrongLanguage() - { - $language = new Language(); - $language->setLatin('fr'); - } -} diff --git a/tests/PhpWord/Style/FontTest.php b/tests/PhpWord/Style/FontTest.php index 61648d4ebf..c8fd4dab6a 100644 --- a/tests/PhpWord/Style/FontTest.php +++ b/tests/PhpWord/Style/FontTest.php @@ -74,6 +74,7 @@ public function testSetStyleValueWithNullOrEmpty() 'scale' => null, 'spacing' => null, 'kerning' => null, + 'lang' => null, ); foreach ($attributes as $key => $default) { $get = is_bool($default) ? "is{$key}" : "get{$key}"; diff --git a/tests/PhpWord/Style/LanguageTest.php b/tests/PhpWord/Style/LanguageTest.php new file mode 100644 index 0000000000..bd4669568e --- /dev/null +++ b/tests/PhpWord/Style/LanguageTest.php @@ -0,0 +1,62 @@ + array(null, 'fr-BE'), + 'eastAsia' => array(null, 'ja-JP'), + 'bidirectional' => array(null, 'ar-SA'), + 'langId' => array(null, 1036), + ); + foreach ($properties as $property => $value) { + list($default, $expected) = $value; + $get = "get{$property}"; + $set = "set{$property}"; + + $this->assertEquals($default, $object->$get()); // Default value + + $object->$set($expected); + + $this->assertEquals($expected, $object->$get()); // New value + } + } + + /** + * Test throws exception if wrong locale is given + * + * @expectedException \InvalidArgumentException + */ + public function testWrongLanguage() + { + $language = new Language(); + $language->setLatin('fr'); + } +} diff --git a/tests/PhpWord/Style/ParagraphTest.php b/tests/PhpWord/Style/ParagraphTest.php index 86d6e89607..b78c557a04 100644 --- a/tests/PhpWord/Style/ParagraphTest.php +++ b/tests/PhpWord/Style/ParagraphTest.php @@ -80,6 +80,7 @@ public function testSetStyleValueNormal() 'keepLines' => true, 'pageBreakBefore' => true, 'contextualSpacing' => true, + 'bidi' => true, ); foreach ($attributes as $key => $value) { $get = $this->findGetter($key, $value, $object); diff --git a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php index 0e6ba6c8ce..99c66680ff 100644 --- a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php @@ -16,12 +16,12 @@ */ namespace PhpOffice\PhpWord\Writer\Word2007\Part; +use PhpOffice\PhpWord\ComplexType\TrackChangesView; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\TestHelperDOCX; use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\SimpleType\Zoom; -use PhpOffice\PhpWord\ComplexType\TrackChangesView; -use PhpOffice\PhpWord\ComplexType\Language; +use PhpOffice\PhpWord\Style\Language; +use PhpOffice\PhpWord\TestHelperDOCX; /** * Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Settings From 1c4164f98cdb1318efb22faf9021b608ce852a24 Mon Sep 17 00:00:00 2001 From: troosan Date: Wed, 9 Aug 2017 23:40:40 +0200 Subject: [PATCH 04/10] fix test & add doc --- CHANGELOG.md | 1 + src/PhpWord/Style/Font.php | 2 +- src/PhpWord/Style/Language.php | 10 +++++----- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1e0245ff6..4392ed53e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ This release fixes several bugs and adds some new features - Introduced the `\PhpOffice\PhpWord\SimpleType\NumberFormat` simple type. - @troosan - Support for ContextualSpacing - @postHawk #1088 - Possiblity to hide spelling and/or grammatical errors - @troosan #542 +- Possiblity to set default document language as well as changing the language for each text element - @troosan #1108 ### Fixed - Images are not being printed when generating PDF - @hubertinio #1074 #431 diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index 7be6ddddef..6dd4adb752 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -808,7 +808,7 @@ public function getLang() */ public function setLang($value = null) { - if (is_string($value)) { + if (is_string($value) && $value != '') { $value = new Language($value); } $this->setObjectVal($value, 'Language', $this->lang); diff --git a/src/PhpWord/Style/Language.php b/src/PhpWord/Style/Language.php index 8f561461c5..89b0049413 100644 --- a/src/PhpWord/Style/Language.php +++ b/src/PhpWord/Style/Language.php @@ -90,13 +90,13 @@ final class Language extends AbstractStyle public function __construct($latin = null, $eastAsia = null, $bidirectional = null) { - if ($latin!= null) { + if ($latin != null) { $this->setLatin($latin); } if ($eastAsia != null) { $this->setEastAsia($eastAsia); } - if ($bidirectional!= null) { + if ($bidirectional != null) { $this->setBidirectional($bidirectional); } } @@ -108,7 +108,7 @@ public function __construct($latin = null, $eastAsia = null, $bidirectional = nu * The value for the latin language * @return self */ - public function setLatin($latin) + public function setLatin($latin = null) { $this->validateLocale($latin); $this->latin = $latin; @@ -156,7 +156,7 @@ public function getLangId() * The value for the east asian language * @return self */ - public function setEastAsia($eastAsia) + public function setEastAsia($eastAsia = null) { $this->validateLocale($eastAsia); $this->eastAsia = $eastAsia; @@ -180,7 +180,7 @@ public function getEastAsia() * The value for the complex script language * @return self */ - public function setBidirectional($bidirectional) + public function setBidirectional($bidirectional = null) { $this->validateLocale($bidirectional); $this->bidirectional = $bidirectional; From 45ba8b5f9fff743fe6d1eecc69403fb1a85034bc Mon Sep 17 00:00:00 2001 From: troosan Date: Thu, 10 Aug 2017 00:41:33 +0200 Subject: [PATCH 05/10] fix scrutinizer warnings --- src/PhpWord/Element/FormField.php | 2 +- src/PhpWord/Element/TOC.php | 4 ++-- src/PhpWord/Style/Language.php | 4 ++-- src/PhpWord/Writer/Word2007/Part/Settings.php | 13 ++++++------- src/PhpWord/Writer/Word2007/Style/AbstractStyle.php | 2 +- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/PhpWord/Element/FormField.php b/src/PhpWord/Element/FormField.php index c7cb44d240..77918754f2 100644 --- a/src/PhpWord/Element/FormField.php +++ b/src/PhpWord/Element/FormField.php @@ -35,7 +35,7 @@ class FormField extends Text /** * Form field name * - * @var string + * @var string|bool|int */ private $name; diff --git a/src/PhpWord/Element/TOC.php b/src/PhpWord/Element/TOC.php index 54ae384433..d3ab2be146 100644 --- a/src/PhpWord/Element/TOC.php +++ b/src/PhpWord/Element/TOC.php @@ -36,7 +36,7 @@ class TOC extends AbstractElement /** * Font style * - * @var \PhpOffice\PhpWord\Style\Font|array|string + * @var \PhpOffice\PhpWord\Style\Font|string */ private $fontStyle; @@ -121,7 +121,7 @@ public function getStyleTOC() /** * Get Font Style * - * @return \PhpOffice\PhpWord\Style\Font + * @return \PhpOffice\PhpWord\Style\Font|string */ public function getStyleFont() { diff --git a/src/PhpWord/Style/Language.php b/src/PhpWord/Style/Language.php index 89b0049413..73a6757a1c 100644 --- a/src/PhpWord/Style/Language.php +++ b/src/PhpWord/Style/Language.php @@ -176,7 +176,7 @@ public function getEastAsia() /** * Set the Complex Script Language * - * @param string $bidi + * @param string $bidirectional * The value for the complex script language * @return self */ @@ -205,7 +205,7 @@ public function getBidirectional() */ private function validateLocale($locale = null) { - if ($locale != null && strstr($locale, '-') === false) { + if ($locale !== null && strstr($locale, '-') === false) { throw new \InvalidArgumentException($locale . ' is not a valid language code'); } } diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index f3e21e6a2e..8a58cd204d 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -220,7 +220,6 @@ private function setProofState(ProofState $proofState = null) private function setRevisionView(TrackChangesView $trackChangesView = null) { if ($trackChangesView != null) { - $revisionView['w:markup'] = $trackChangesView->hasMarkup() ? 'true': 'false'; $revisionView['w:comments'] = $trackChangesView->hasComments() ? 'true': 'false'; $revisionView['w:insDel'] = $trackChangesView->hasInsDel() ? 'true': 'false'; @@ -238,8 +237,8 @@ private function setRevisionView(TrackChangesView $trackChangesView = null) */ private function setThemeFontLang(Language $language = null) { - if ($language != null && ($language->getLatin() != null || $language->getEastAsia() != null || $language->getBidirectional() != null)) { - + $lang = array(); + if ($language != null) { if ($language->getLatin() != null) { $lang['w:val'] = $language->getLatin(); } @@ -249,11 +248,11 @@ private function setThemeFontLang(Language $language = null) if ($language->getBidirectional() != null) { $lang['w:bidi'] = $language->getBidirectional(); } - - $this->settings['w:themeFontLang'] = array('@attributes' => $lang); - } else { - $this->settings['w:themeFontLang'] = array('@attributes' => array('w:val' => 'en-US')); } + if (count($lang) == 0) { + $lang['w:val'] = 'en-US'; + } + $this->settings['w:themeFontLang'] = array('@attributes' => $lang); } /** diff --git a/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php b/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php index d0ee5a0def..0b8b0a52aa 100644 --- a/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php +++ b/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php @@ -71,7 +71,7 @@ protected function getXmlWriter() /** * Get Style * - * @return \PhpOffice\PhpWord\Style\AbstractStyle + * @return string|\PhpOffice\PhpWord\Style\AbstractStyle */ protected function getStyle() { From 93152df69683e38b7f2bde4bbe596f74e752d1f2 Mon Sep 17 00:00:00 2001 From: troosan Date: Thu, 10 Aug 2017 00:47:11 +0200 Subject: [PATCH 06/10] more fixes --- src/PhpWord/Style/Language.php | 20 ++++++++++++------- src/PhpWord/Writer/Word2007/Part/Settings.php | 1 - 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/PhpWord/Style/Language.php b/src/PhpWord/Style/Language.php index 73a6757a1c..6783b50ea3 100644 --- a/src/PhpWord/Style/Language.php +++ b/src/PhpWord/Style/Language.php @@ -88,6 +88,12 @@ final class Language extends AbstractStyle */ private $bidirectional; + /** + * + * @param string|null $latin + * @param string|null $eastAsia + * @param string|null $bidirectional + */ public function __construct($latin = null, $eastAsia = null, $bidirectional = null) { if ($latin != null) { @@ -104,7 +110,7 @@ public function __construct($latin = null, $eastAsia = null, $bidirectional = nu /** * Set the Latin Language * - * @param string $latin + * @param string|null $latin * The value for the latin language * @return self */ @@ -118,7 +124,7 @@ public function setLatin($latin = null) /** * Get the Latin Language * - * @return string + * @return string|null */ public function getLatin() { @@ -152,7 +158,7 @@ public function getLangId() /** * Set the East Asian Language * - * @param string $eastAsia + * @param string|null $eastAsia * The value for the east asian language * @return self */ @@ -166,7 +172,7 @@ public function setEastAsia($eastAsia = null) /** * Get the East Asian Language * - * @return string + * @return string|null */ public function getEastAsia() { @@ -176,7 +182,7 @@ public function getEastAsia() /** * Set the Complex Script Language * - * @param string $bidirectional + * @param string|null $bidirectional * The value for the complex script language * @return self */ @@ -190,7 +196,7 @@ public function setBidirectional($bidirectional = null) /** * Get the Complex Script Language * - * @return string + * @return string|null */ public function getBidirectional() { @@ -200,7 +206,7 @@ public function getBidirectional() /** * Validates that the language passed is in the format xx-xx * - * @param string $locale + * @param string|null $locale * @return boolean */ private function validateLocale($locale = null) diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index 8a58cd204d..a7196d448d 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -19,7 +19,6 @@ use PhpOffice\PhpWord\ComplexType\ProofState; use PhpOffice\PhpWord\ComplexType\TrackChangesView; -use PhpOffice\PhpWord\Settings as DocumentSettings; use PhpOffice\PhpWord\Style\Language; /** From 48641c6528e340b15c52cd17ace853aa195d937f Mon Sep 17 00:00:00 2001 From: troosan Date: Fri, 11 Aug 2017 00:04:40 +0200 Subject: [PATCH 07/10] improve scrutinizer checks --- src/PhpWord/Reader/Word2007/Settings.php | 10 ++++----- src/PhpWord/Style/Language.php | 22 +++++++++---------- src/PhpWord/Writer/Word2007/Part/Settings.php | 16 ++++---------- src/PhpWord/Writer/Word2007/Part/Styles.php | 11 +++++----- src/PhpWord/Writer/Word2007/Style/Font.php | 8 +++---- 5 files changed, 30 insertions(+), 37 deletions(-) diff --git a/src/PhpWord/Reader/Word2007/Settings.php b/src/PhpWord/Reader/Word2007/Settings.php index c4baa0cb78..7c27d741d6 100644 --- a/src/PhpWord/Reader/Word2007/Settings.php +++ b/src/PhpWord/Reader/Word2007/Settings.php @@ -74,7 +74,7 @@ public function read(PhpWord $phpWord) * @param PhpWord $phpWord * @param \DOMNode $node */ - protected function setThemeFontLang(XMLReader $xmlReader, PhpWord $phpWord, \DOMNode $node) + protected function setThemeFontLang(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node) { $val = $xmlReader->getAttribute('w:val', $node); @@ -96,7 +96,7 @@ protected function setThemeFontLang(XMLReader $xmlReader, PhpWord $phpWord, \DOM * @param PhpWord $phpWord * @param \DOMNode $node */ - protected function setDocumentProtection(XMLReader $xmlReader, PhpWord $phpWord, \DOMNode $node) + protected function setDocumentProtection(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node) { $documentProtection = $phpWord->getSettings()->getDocumentProtection(); @@ -111,7 +111,7 @@ protected function setDocumentProtection(XMLReader $xmlReader, PhpWord $phpWord, * @param PhpWord $phpWord * @param \DOMNode $node */ - protected function setProofState(XMLReader $xmlReader, PhpWord $phpWord, \DOMNode $node) + protected function setProofState(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node) { $proofState = $phpWord->getSettings()->getProofState(); @@ -133,7 +133,7 @@ protected function setProofState(XMLReader $xmlReader, PhpWord $phpWord, \DOMNod * @param PhpWord $phpWord * @param \DOMNode $node */ - protected function setZoom(XMLReader $xmlReader, PhpWord $phpWord, \DOMNode $node) + protected function setZoom(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node) { $percent = $xmlReader->getAttribute('w:percent', $node); $val = $xmlReader->getAttribute('w:val', $node); @@ -150,7 +150,7 @@ protected function setZoom(XMLReader $xmlReader, PhpWord $phpWord, \DOMNode $nod * @param PhpWord $phpWord * @param \DOMNode $node */ - protected function setRevisionView(XMLReader $xmlReader, PhpWord $phpWord, \DOMNode $node) + protected function setRevisionView(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node) { $revisionView = new TrackChangesView(); $revisionView->setMarkup($xmlReader->getAttribute('w:markup', $node)); diff --git a/src/PhpWord/Style/Language.php b/src/PhpWord/Style/Language.php index 6783b50ea3..4e9220fd44 100644 --- a/src/PhpWord/Style/Language.php +++ b/src/PhpWord/Style/Language.php @@ -96,13 +96,13 @@ final class Language extends AbstractStyle */ public function __construct($latin = null, $eastAsia = null, $bidirectional = null) { - if ($latin != null) { + if (!empty($latin)) { $this->setLatin($latin); } - if ($eastAsia != null) { + if (!empty($eastAsia)) { $this->setEastAsia($eastAsia); } - if ($bidirectional != null) { + if (!empty($bidirectional)) { $this->setBidirectional($bidirectional); } } @@ -110,11 +110,11 @@ public function __construct($latin = null, $eastAsia = null, $bidirectional = nu /** * Set the Latin Language * - * @param string|null $latin + * @param string $latin * The value for the latin language * @return self */ - public function setLatin($latin = null) + public function setLatin($latin) { $this->validateLocale($latin); $this->latin = $latin; @@ -158,11 +158,11 @@ public function getLangId() /** * Set the East Asian Language * - * @param string|null $eastAsia + * @param string $eastAsia * The value for the east asian language * @return self */ - public function setEastAsia($eastAsia = null) + public function setEastAsia($eastAsia) { $this->validateLocale($eastAsia); $this->eastAsia = $eastAsia; @@ -182,11 +182,11 @@ public function getEastAsia() /** * Set the Complex Script Language * - * @param string|null $bidirectional + * @param string $bidirectional * The value for the complex script language * @return self */ - public function setBidirectional($bidirectional = null) + public function setBidirectional($bidirectional) { $this->validateLocale($bidirectional); $this->bidirectional = $bidirectional; @@ -206,10 +206,10 @@ public function getBidirectional() /** * Validates that the language passed is in the format xx-xx * - * @param string|null $locale + * @param string $locale * @return boolean */ - private function validateLocale($locale = null) + private function validateLocale($locale) { if ($locale !== null && strstr($locale, '-') === false) { throw new \InvalidArgumentException($locale . ' is not a valid language code'); diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index a7196d448d..11c5d83811 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -236,20 +236,12 @@ private function setRevisionView(TrackChangesView $trackChangesView = null) */ private function setThemeFontLang(Language $language = null) { + $latinLanguage = ($language == null || $language->getLatin() == null) ? 'en-US' : $language->getLatin(); $lang = array(); + $lang['w:val'] = $latinLanguage; if ($language != null) { - if ($language->getLatin() != null) { - $lang['w:val'] = $language->getLatin(); - } - if ($language->getEastAsia() != null) { - $lang['w:eastAsia'] = $language->getEastAsia(); - } - if ($language->getBidirectional() != null) { - $lang['w:bidi'] = $language->getBidirectional(); - } - } - if (count($lang) == 0) { - $lang['w:val'] = 'en-US'; + $lang['w:eastAsia'] = $language->getEastAsia() === null ? 'x-none' : $language->getEastAsia(); + $lang['w:bidi'] = $language->getBidirectional() === null ? 'x-none' : $language->getBidirectional(); } $this->settings['w:themeFontLang'] = array('@attributes' => $lang); } diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index bf47637100..9f114be333 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -86,6 +86,7 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles) $fontName = PhpWordSettings::getDefaultFontName(); $fontSize = PhpWordSettings::getDefaultFontSize(); $language = $this->getParentWriter()->getPhpWord()->getSettings()->getThemeFontLang(); + $latinLanguage = ($language == null || $language->getLatin() == null) ? 'en-US' : $language->getLatin(); // Default font $xmlWriter->startElement('w:docDefaults'); @@ -103,13 +104,13 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles) $xmlWriter->startElement('w:szCs'); $xmlWriter->writeAttribute('w:val', $fontSize * 2); $xmlWriter->endElement(); // w:szCs + $xmlWriter->startElement('w:lang'); + $xmlWriter->writeAttribute('w:val', $latinLanguage); if ($language != null) { - $xmlWriter->startElement('w:lang'); - $xmlWriter->writeAttributeIf($language->getLatin() != null, 'w:val', $language->getLatin()); - $xmlWriter->writeAttributeIf($language->getEastAsia() != null, 'w:eastAsia', $language->getEastAsia()); - $xmlWriter->writeAttributeIf($language->getBidirectional() != null, 'w:bidi', $language->getBidirectional()); - $xmlWriter->endElement(); // w:lang + $xmlWriter->writeAttributeIf($language->getEastAsia() !== null, 'w:eastAsia', $language->getEastAsia()); + $xmlWriter->writeAttributeIf($language->getBidirectional() !== null, 'w:bidi', $language->getBidirectional()); } + $xmlWriter->endElement(); // w:lang $xmlWriter->endElement(); // w:rPr $xmlWriter->endElement(); // w:rPrDefault $xmlWriter->endElement(); // w:docDefaults diff --git a/src/PhpWord/Writer/Word2007/Style/Font.php b/src/PhpWord/Writer/Word2007/Style/Font.php index a73fc98e4b..2248a4485f 100644 --- a/src/PhpWord/Writer/Word2007/Style/Font.php +++ b/src/PhpWord/Writer/Word2007/Style/Font.php @@ -88,11 +88,11 @@ private function writeStyle() //Language $language = $style->getLang(); - if ($language != null && ($language->getLatin() != null || $language->getEastAsia() != null || $language->getBidirectional() != null)) { + if ($language != null && ($language->getLatin() !== null || $language->getEastAsia() !== null || $language->getBidirectional() !== null)) { $xmlWriter->startElement('w:lang'); - $xmlWriter->writeAttributeIf($language->getLatin() != null, 'w:val', $language->getLatin()); - $xmlWriter->writeAttributeIf($language->getEastAsia() != null, 'w:eastAsia', $language->getEastAsia()); - $xmlWriter->writeAttributeIf($language->getBidirectional() != null, 'w:bidi', $language->getBidirectional()); + $xmlWriter->writeAttributeIf($language->getLatin() !== null, 'w:val', $language->getLatin()); + $xmlWriter->writeAttributeIf($language->getEastAsia() !== null, 'w:eastAsia', $language->getEastAsia()); + $xmlWriter->writeAttributeIf($language->getBidirectional() !== null, 'w:bidi', $language->getBidirectional()); $xmlWriter->endElement(); } From 1aac0259855bdc399e751fd33fc14739da5ac809 Mon Sep 17 00:00:00 2001 From: troosan Date: Fri, 11 Aug 2017 00:17:57 +0200 Subject: [PATCH 08/10] fix some warnings --- samples/Sample_10_EastAsianFontStyle.php | 2 +- samples/Sample_11_ReadWord2007.php | 2 +- src/PhpWord/Element/Bookmark.php | 2 -- src/PhpWord/Element/CheckBox.php | 1 - src/PhpWord/Element/FormField.php | 1 - src/PhpWord/Element/Link.php | 1 - src/PhpWord/Element/PreserveText.php | 1 - src/PhpWord/Element/SDT.php | 1 - src/PhpWord/Style/AbstractStyle.php | 2 +- src/PhpWord/Style/ListItem.php | 3 ++- src/PhpWord/Style/TextBox.php | 3 ++- 11 files changed, 7 insertions(+), 12 deletions(-) diff --git a/samples/Sample_10_EastAsianFontStyle.php b/samples/Sample_10_EastAsianFontStyle.php index 2541af86dc..87345ae0e8 100644 --- a/samples/Sample_10_EastAsianFontStyle.php +++ b/samples/Sample_10_EastAsianFontStyle.php @@ -7,7 +7,7 @@ $section = $phpWord->addSection(); $header = array('size' => 16, 'bold' => true); //1.Use EastAisa FontStyle -$section->addText('中文楷体样式测试', array('name' => '楷体', 'size' => 16, 'color' => '1B2232')); +$section->addText('中文楷体样式测试', array('name' => '楷体', 'size' => 16, 'color' => '1B2232', 'lang' => array('latin' => 'en-US', 'eastAsia' => 'zh-CN'))); // Save file echo write($phpWord, basename(__FILE__, '.php'), $writers); diff --git a/samples/Sample_11_ReadWord2007.php b/samples/Sample_11_ReadWord2007.php index 232fe05767..c0b54c7a49 100644 --- a/samples/Sample_11_ReadWord2007.php +++ b/samples/Sample_11_ReadWord2007.php @@ -3,7 +3,7 @@ // Read contents $name = basename(__FILE__, '.php'); -$source = __DIR__ . "/resources/sample.docx"; +$source = __DIR__ . "/resources/{$name}.docx"; echo date('H:i:s'), " Reading contents from `{$source}`", EOL; $phpWord = \PhpOffice\PhpWord\IOFactory::load($source); diff --git a/src/PhpWord/Element/Bookmark.php b/src/PhpWord/Element/Bookmark.php index c865893fad..4df06afb4e 100644 --- a/src/PhpWord/Element/Bookmark.php +++ b/src/PhpWord/Element/Bookmark.php @@ -45,9 +45,7 @@ class Bookmark extends AbstractElement */ public function __construct($name) { - $this->name = CommonText::toUTF8($name); - return $this; } /** diff --git a/src/PhpWord/Element/CheckBox.php b/src/PhpWord/Element/CheckBox.php index 3fc578ef37..b049c7f1bf 100644 --- a/src/PhpWord/Element/CheckBox.php +++ b/src/PhpWord/Element/CheckBox.php @@ -40,7 +40,6 @@ class CheckBox extends Text * @param string $text * @param mixed $fontStyle * @param mixed $paragraphStyle - * @return self */ public function __construct($name = null, $text = null, $fontStyle = null, $paragraphStyle = null) { diff --git a/src/PhpWord/Element/FormField.php b/src/PhpWord/Element/FormField.php index 77918754f2..414714a8d4 100644 --- a/src/PhpWord/Element/FormField.php +++ b/src/PhpWord/Element/FormField.php @@ -70,7 +70,6 @@ class FormField extends Text * @param string $type * @param mixed $fontStyle * @param mixed $paragraphStyle - * @return self */ public function __construct($type, $fontStyle = null, $paragraphStyle = null) { diff --git a/src/PhpWord/Element/Link.php b/src/PhpWord/Element/Link.php index 4a72e167f0..6641b46d5b 100644 --- a/src/PhpWord/Element/Link.php +++ b/src/PhpWord/Element/Link.php @@ -83,7 +83,6 @@ public function __construct($source, $text = null, $fontStyle = null, $paragraph $this->fontStyle = $this->setNewStyle(new Font('text'), $fontStyle); $this->paragraphStyle = $this->setNewStyle(new Paragraph(), $paragraphStyle); $this->internal = $internal; - return $this; } /** diff --git a/src/PhpWord/Element/PreserveText.php b/src/PhpWord/Element/PreserveText.php index 65e17e35b5..813c13960d 100644 --- a/src/PhpWord/Element/PreserveText.php +++ b/src/PhpWord/Element/PreserveText.php @@ -54,7 +54,6 @@ class PreserveText extends AbstractElement * @param string $text * @param mixed $fontStyle * @param mixed $paragraphStyle - * @return self */ public function __construct($text = null, $fontStyle = null, $paragraphStyle = null) { diff --git a/src/PhpWord/Element/SDT.php b/src/PhpWord/Element/SDT.php index 58a477d92b..b0c68b0f85 100644 --- a/src/PhpWord/Element/SDT.php +++ b/src/PhpWord/Element/SDT.php @@ -51,7 +51,6 @@ class SDT extends Text * @param string $type * @param mixed $fontStyle * @param mixed $paragraphStyle - * @return self */ public function __construct($type, $fontStyle = null, $paragraphStyle = null) { diff --git a/src/PhpWord/Style/AbstractStyle.php b/src/PhpWord/Style/AbstractStyle.php index 05c79ea2da..e2b6dce9e8 100644 --- a/src/PhpWord/Style/AbstractStyle.php +++ b/src/PhpWord/Style/AbstractStyle.php @@ -329,7 +329,7 @@ protected function setObjectVal($value, $styleName, &$style) protected function setPairedVal(&$property, &$pairProperty, $value) { $property = $this->setBoolVal($value, $property); - if ($value == true) { + if ($value === true) { $pairProperty = false; } diff --git a/src/PhpWord/Style/ListItem.php b/src/PhpWord/Style/ListItem.php index 18ea0bf264..61a8349bc9 100644 --- a/src/PhpWord/Style/ListItem.php +++ b/src/PhpWord/Style/ListItem.php @@ -247,11 +247,12 @@ private function getListTypeStyle() // Populate style and register to global Style register $style = $listTypeStyles[$this->listType]; + $numProperties = count($properties); foreach ($style['levels'] as $key => $value) { $level = array(); $levelProperties = explode(', ', $value); $level['level'] = $key; - for ($i = 0; $i < count($properties); $i++) { + for ($i = 0; $i < $numProperties; $i++) { $property = $properties[$i]; $level[$property] = $levelProperties[$i]; } diff --git a/src/PhpWord/Style/TextBox.php b/src/PhpWord/Style/TextBox.php index 600fb8eabf..6783cd1898 100644 --- a/src/PhpWord/Style/TextBox.php +++ b/src/PhpWord/Style/TextBox.php @@ -183,7 +183,8 @@ public function hasInnerMargins() { $hasInnerMargins = false; $margins = $this->getInnerMargin(); - for ($i = 0; $i < count($margins); $i++) { + $numMargins = count($margins); + for ($i = 0; $i < $numMargins; $i++) { if ($margins[$i] !== null) { $hasInnerMargins = true; } From d7a433a2fd123e8681dea1d87d26e05b4f6385e3 Mon Sep 17 00:00:00 2001 From: troosan Date: Sat, 12 Aug 2017 21:46:11 +0200 Subject: [PATCH 09/10] fix more warnings --- src/PhpWord/ComplexType/TrackChangesView.php | 2 +- src/PhpWord/Element/Comment.php | 1 - src/PhpWord/Element/TrackChange.php | 2 +- src/PhpWord/Reader/Word2007/Settings.php | 8 ++++---- src/PhpWord/Writer/Word2007/Part/Settings.php | 2 +- src/PhpWord/Writer/Word2007/Part/Styles.php | 2 +- 6 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/PhpWord/ComplexType/TrackChangesView.php b/src/PhpWord/ComplexType/TrackChangesView.php index ea26bc3c3b..fbb46c3a35 100644 --- a/src/PhpWord/ComplexType/TrackChangesView.php +++ b/src/PhpWord/ComplexType/TrackChangesView.php @@ -135,7 +135,7 @@ public function hasFormatting() /** * Set Display Formatting Revisions * - * @param boolean $insDel + * @param boolean $formatting * Set to true to show formatting revisions */ public function setFormatting($formatting) diff --git a/src/PhpWord/Element/Comment.php b/src/PhpWord/Element/Comment.php index def9d5a89d..05f3dd78c8 100644 --- a/src/PhpWord/Element/Comment.php +++ b/src/PhpWord/Element/Comment.php @@ -61,7 +61,6 @@ public function __construct($author, $date, $initials) { parent::__construct($author, $date); $this->initials = $initials; - return $this; } /** diff --git a/src/PhpWord/Element/TrackChange.php b/src/PhpWord/Element/TrackChange.php index 782e6f3550..1df7148db6 100644 --- a/src/PhpWord/Element/TrackChange.php +++ b/src/PhpWord/Element/TrackChange.php @@ -45,7 +45,7 @@ class TrackChange extends AbstractContainer * Create a new TrackChange Element * * @param string $author - * @param DateTime $date + * @param \DateTime $date */ public function __construct($author, \DateTime $date) { diff --git a/src/PhpWord/Reader/Word2007/Settings.php b/src/PhpWord/Reader/Word2007/Settings.php index 7c27d741d6..4d8de147e4 100644 --- a/src/PhpWord/Reader/Word2007/Settings.php +++ b/src/PhpWord/Reader/Word2007/Settings.php @@ -118,10 +118,10 @@ protected function setProofState(XMLReader $xmlReader, PhpWord $phpWord, \DOMEle $spelling = $xmlReader->getAttribute('w:spelling', $node); $grammar = $xmlReader->getAttribute('w:grammar', $node); - if ($spelling != null) { + if ($spelling !== null) { $proofState->setSpelling($spelling); } - if ($grammar != null) { + if ($grammar !== null) { $proofState->setGrammar($grammar); } } @@ -138,8 +138,8 @@ protected function setZoom(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $ $percent = $xmlReader->getAttribute('w:percent', $node); $val = $xmlReader->getAttribute('w:val', $node); - if ($percent != null || $val != null) { - $phpWord->getSettings()->setZoom($percent == null ? $val : $percent); + if ($percent !== null || $val !== null) { + $phpWord->getSettings()->setZoom($percent === null ? $val : $percent); } } diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index 11c5d83811..316d4328f6 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -236,7 +236,7 @@ private function setRevisionView(TrackChangesView $trackChangesView = null) */ private function setThemeFontLang(Language $language = null) { - $latinLanguage = ($language == null || $language->getLatin() == null) ? 'en-US' : $language->getLatin(); + $latinLanguage = ($language == null || empty($language->getLatin())) ? 'en-US' : $language->getLatin(); $lang = array(); $lang['w:val'] = $latinLanguage; if ($language != null) { diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index 9f114be333..9565cb782c 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -86,7 +86,7 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles) $fontName = PhpWordSettings::getDefaultFontName(); $fontSize = PhpWordSettings::getDefaultFontSize(); $language = $this->getParentWriter()->getPhpWord()->getSettings()->getThemeFontLang(); - $latinLanguage = ($language == null || $language->getLatin() == null) ? 'en-US' : $language->getLatin(); + $latinLanguage = ($language == null || empty($language->getLatin())) ? 'en-US' : $language->getLatin(); // Default font $xmlWriter->startElement('w:docDefaults'); From 61a1e0cc4cadf62cfacfa14261a03c97832fd937 Mon Sep 17 00:00:00 2001 From: troosan Date: Sun, 13 Aug 2017 20:53:31 +0200 Subject: [PATCH 10/10] improve comments --- src/PhpWord/ComplexType/TrackChangesView.php | 4 ++-- src/PhpWord/Element/AbstractElement.php | 2 +- src/PhpWord/Writer/Word2007/Part/Settings.php | 8 ++++---- src/PhpWord/Writer/Word2007/Part/Styles.php | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/PhpWord/ComplexType/TrackChangesView.php b/src/PhpWord/ComplexType/TrackChangesView.php index fbb46c3a35..9c8948ae1d 100644 --- a/src/PhpWord/ComplexType/TrackChangesView.php +++ b/src/PhpWord/ComplexType/TrackChangesView.php @@ -135,10 +135,10 @@ public function hasFormatting() /** * Set Display Formatting Revisions * - * @param boolean $formatting + * @param boolean|null $formatting * Set to true to show formatting revisions */ - public function setFormatting($formatting) + public function setFormatting($formatting = null) { $this->formatting = $formatting === null ? true : $formatting; } diff --git a/src/PhpWord/Element/AbstractElement.php b/src/PhpWord/Element/AbstractElement.php index 8ff64194ab..e60f504bbd 100644 --- a/src/PhpWord/Element/AbstractElement.php +++ b/src/PhpWord/Element/AbstractElement.php @@ -231,7 +231,7 @@ public function setElementIndex($value) /** * Get element unique ID * - * @return string + * @return integer */ public function getElementId() { diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index 316d4328f6..143eb72f3f 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -179,7 +179,7 @@ private function setOnOffValue($settingName, $booleanValue) /** * Get protection settings. * - * @param \PhpOffice\PhpWord\Metadata\Settings $documentProtection + * @param \PhpOffice\PhpWord\Metadata\Protection $documentProtection * @return void */ private function setDocumentProtection($documentProtection) @@ -212,9 +212,9 @@ private function setProofState(ProofState $proofState = null) } /** - * Set the Proof state + * Set the Revision View * - * @param ProofState $proofState + * @param TrackChangesView $trackChangesView */ private function setRevisionView(TrackChangesView $trackChangesView = null) { @@ -236,7 +236,7 @@ private function setRevisionView(TrackChangesView $trackChangesView = null) */ private function setThemeFontLang(Language $language = null) { - $latinLanguage = ($language == null || empty($language->getLatin())) ? 'en-US' : $language->getLatin(); + $latinLanguage = ($language == null || $language->getLatin() === null) ? 'en-US' : $language->getLatin(); $lang = array(); $lang['w:val'] = $latinLanguage; if ($language != null) { diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index 9565cb782c..67a054074d 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -86,7 +86,7 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles) $fontName = PhpWordSettings::getDefaultFontName(); $fontSize = PhpWordSettings::getDefaultFontSize(); $language = $this->getParentWriter()->getPhpWord()->getSettings()->getThemeFontLang(); - $latinLanguage = ($language == null || empty($language->getLatin())) ? 'en-US' : $language->getLatin(); + $latinLanguage = ($language == null || $language->getLatin() === null) ? 'en-US' : $language->getLatin(); // Default font $xmlWriter->startElement('w:docDefaults');