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