Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.12% covered (success)
94.12%
48 / 51
87.50% covered (warning)
87.50%
7 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Settings
94.12% covered (success)
94.12%
48 / 51
87.50% covered (warning)
87.50%
7 / 8
21.09
0.00% covered (danger)
0.00%
0 / 1
 read
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
6
 setThemeFontLang
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 setDocumentProtection
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 setProofState
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 setZoom
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 setRevisionView
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 setConsecutiveHyphenLimit
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 setHyphenationZone
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * This file is part of PHPWord - A pure PHP library for reading and writing
4 * word processing documents.
5 *
6 * PHPWord is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12 *
13 * @see         https://github.com/PHPOffice/PHPWord
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18namespace PhpOffice\PhpWord\Reader\Word2007;
19
20use DOMElement;
21use PhpOffice\PhpWord\ComplexType\TrackChangesView;
22use PhpOffice\PhpWord\PhpWord;
23use PhpOffice\PhpWord\Shared\XMLReader;
24use PhpOffice\PhpWord\Style\Language;
25
26/**
27 * Settings reader.
28 *
29 * @since 0.14.0
30 */
31class Settings extends AbstractPart
32{
33    /**
34     * @var array<string>
35     */
36    private $booleanProperties = [
37        'mirrorMargins',
38        'hideSpellingErrors',
39        'hideGrammaticalErrors',
40        'trackRevisions',
41        'doNotTrackMoves',
42        'doNotTrackFormatting',
43        'evenAndOddHeaders',
44        'updateFields',
45        'autoHyphenation',
46        'doNotHyphenateCaps',
47        'bookFoldPrinting',
48    ];
49
50    /**
51     * Read settings.xml.
52     */
53    public function read(PhpWord $phpWord): void
54    {
55        $xmlReader = new XMLReader();
56        $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
57
58        $docSettings = $phpWord->getSettings();
59
60        $nodes = $xmlReader->getElements('*');
61        if ($nodes->length > 0) {
62            foreach ($nodes as $node) {
63                $name = str_replace('w:', '', $node->nodeName);
64                $value = $xmlReader->getAttribute('w:val', $node);
65                $method = 'set' . $name;
66
67                if (in_array($name, $this->booleanProperties)) {
68                    $docSettings->$method($value !== 'false');
69                } elseif (method_exists($this, $method)) {
70                    $this->$method($xmlReader, $phpWord, $node);
71                } elseif (method_exists($docSettings, $method)) {
72                    $docSettings->$method($value);
73                }
74            }
75        }
76    }
77
78    /**
79     * Sets the document Language.
80     */
81    protected function setThemeFontLang(XMLReader $xmlReader, PhpWord $phpWord, DOMElement $node): void
82    {
83        $val = $xmlReader->getAttribute('w:val', $node);
84        $eastAsia = $xmlReader->getAttribute('w:eastAsia', $node);
85        $bidi = $xmlReader->getAttribute('w:bidi', $node);
86
87        $themeFontLang = new Language();
88        $themeFontLang->setLatin($val);
89        $themeFontLang->setEastAsia($eastAsia);
90        $themeFontLang->setBidirectional($bidi);
91
92        $phpWord->getSettings()->setThemeFontLang($themeFontLang);
93    }
94
95    /**
96     * Sets the document protection.
97     */
98    protected function setDocumentProtection(XMLReader $xmlReader, PhpWord $phpWord, DOMElement $node): void
99    {
100        $documentProtection = $phpWord->getSettings()->getDocumentProtection();
101
102        $edit = $xmlReader->getAttribute('w:edit', $node);
103        if ($edit !== null) {
104            $documentProtection->setEditing($edit);
105        }
106    }
107
108    /**
109     * Sets the proof state.
110     */
111    protected function setProofState(XMLReader $xmlReader, PhpWord $phpWord, DOMElement $node): void
112    {
113        $proofState = $phpWord->getSettings()->getProofState();
114
115        $spelling = $xmlReader->getAttribute('w:spelling', $node);
116        $grammar = $xmlReader->getAttribute('w:grammar', $node);
117
118        if ($spelling !== null) {
119            $proofState->setSpelling($spelling);
120        }
121        if ($grammar !== null) {
122            $proofState->setGrammar($grammar);
123        }
124    }
125
126    /**
127     * Sets the proof state.
128     */
129    protected function setZoom(XMLReader $xmlReader, PhpWord $phpWord, DOMElement $node): void
130    {
131        $percent = $xmlReader->getAttribute('w:percent', $node);
132        $val = $xmlReader->getAttribute('w:val', $node);
133
134        if ($percent !== null || $val !== null) {
135            $phpWord->getSettings()->setZoom($percent === null ? $val : $percent);
136        }
137    }
138
139    /**
140     * Set the Revision view.
141     */
142    protected function setRevisionView(XMLReader $xmlReader, PhpWord $phpWord, DOMElement $node): void
143    {
144        $revisionView = new TrackChangesView();
145        $revisionView->setMarkup(filter_var($xmlReader->getAttribute('w:markup', $node), FILTER_VALIDATE_BOOLEAN));
146        $revisionView->setComments($xmlReader->getAttribute('w:comments', $node));
147        $revisionView->setInsDel(filter_var($xmlReader->getAttribute('w:insDel', $node), FILTER_VALIDATE_BOOLEAN));
148        $revisionView->setFormatting(filter_var($xmlReader->getAttribute('w:formatting', $node), FILTER_VALIDATE_BOOLEAN));
149        $revisionView->setInkAnnotations(filter_var($xmlReader->getAttribute('w:inkAnnotations', $node), FILTER_VALIDATE_BOOLEAN));
150        $phpWord->getSettings()->setRevisionView($revisionView);
151    }
152
153    protected function setConsecutiveHyphenLimit(XMLReader $xmlReader, PhpWord $phpWord, DOMElement $node): void
154    {
155        $value = $xmlReader->getAttribute('w:val', $node);
156
157        if ($value !== null) {
158            $phpWord->getSettings()->setConsecutiveHyphenLimit($value);
159        }
160    }
161
162    protected function setHyphenationZone(XMLReader $xmlReader, PhpWord $phpWord, DOMElement $node): void
163    {
164        $value = $xmlReader->getAttribute('w:val', $node);
165
166        if ($value !== null) {
167            $phpWord->getSettings()->setHyphenationZone($value);
168        }
169    }
170}