Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
92 / 92
100.00% covered (success)
100.00%
30 / 30
CRAP
100.00% covered (success)
100.00%
1 / 1
Settings
100.00% covered (success)
100.00%
92 / 92
100.00% covered (success)
100.00%
30 / 30
52
100.00% covered (success)
100.00%
1 / 1
 hasCompatibility
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCompatibility
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getZipClass
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setZipClass
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 setPdfRenderer
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getPdfRendererName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setPdfRendererName
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getPdfRendererPath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setPdfRendererOptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPdfRendererOptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setPdfRendererPath
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 getMeasurementUnit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMeasurementUnit
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
 setTempDir
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTempDir
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 isOutputEscapingEnabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setOutputEscapingEnabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDefaultFontName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDefaultAsianFontName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setDefaultFontName
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 setDefaultAsianFontName
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getDefaultFontColor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setDefaultFontColor
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getDefaultFontSize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setDefaultFontSize
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 setDefaultRtl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isDefaultRtl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 loadConfig
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
8
 getDefaultPaper
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setDefaultPaper
100.00% covered (success)
100.00%
4 / 4
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 * 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 * For the full copyright and license information, please read the LICENSE
9 * file that was distributed with this source code. For the full list of
10 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
11 *
12 * @see         https://github.com/PHPOffice/PHPWord
13 *
14 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
15 */
16
17namespace PhpOffice\PhpWord;
18
19/**
20 * PHPWord settings class.
21 *
22 * @since 0.8.0
23 */
24class Settings
25{
26    /**
27     * Zip libraries.
28     *
29     * @const string
30     */
31    public const ZIPARCHIVE = 'ZipArchive';
32    public const PCLZIP = 'PclZip';
33    public const OLD_LIB = Shared\ZipArchive::class; // @deprecated 0.11
34
35    /**
36     * PDF rendering libraries.
37     *
38     * @const string
39     */
40    public const PDF_RENDERER_DOMPDF = 'DomPDF';
41    public const PDF_RENDERER_TCPDF = 'TCPDF';
42    public const PDF_RENDERER_MPDF = 'MPDF';
43
44    /**
45     * Measurement units multiplication factor.
46     * Applied to:
47     * - Section: margins, header/footer height, gutter, column spacing
48     * - Tab: position
49     * - Indentation: left, right, firstLine, hanging
50     * - Spacing: before, after.
51     *
52     * @const string
53     */
54    public const UNIT_TWIP = 'twip'; // = 1/20 point
55    public const UNIT_CM = 'cm';
56    public const UNIT_MM = 'mm';
57    public const UNIT_INCH = 'inch';
58    public const UNIT_POINT = 'point'; // = 1/72 inch
59    public const UNIT_PICA = 'pica'; // = 1/6 inch = 12 points
60
61    /**
62     * Default font settings.
63     * OOXML defined font size values in halfpoints, i.e. twice of what PhpWord
64     * use, and the conversion will be conducted during XML writing.
65     */
66    public const DEFAULT_FONT_NAME = 'Arial';
67    public const DEFAULT_FONT_SIZE = 10;
68    public const DEFAULT_FONT_COLOR = '000000';
69    public const DEFAULT_FONT_CONTENT_TYPE = 'default'; // default|eastAsia|cs
70    public const DEFAULT_PAPER = 'A4';
71
72    /**
73     * Compatibility option for XMLWriter.
74     *
75     * @var bool
76     */
77    private static $xmlWriterCompatibility = true;
78
79    /**
80     * Name of the class used for Zip file management.
81     *
82     * @var string
83     */
84    private static $zipClass = self::ZIPARCHIVE;
85
86    /**
87     * Name of the external Library used for rendering PDF files.
88     *
89     * @var null|string
90     */
91    private static $pdfRendererName;
92
93    /**
94     * Options used for rendering PDF files.
95     *
96     * @var array
97     */
98    private static $pdfRendererOptions = [];
99
100    /**
101     * Directory Path to the external Library used for rendering PDF files.
102     *
103     * @var null|string
104     */
105    private static $pdfRendererPath;
106
107    /**
108     * Measurement unit.
109     *
110     * @var string
111     */
112    private static $measurementUnit = self::UNIT_TWIP;
113
114    /**
115     * Default font name.
116     *
117     * @var string
118     */
119    private static $defaultFontName = self::DEFAULT_FONT_NAME;
120
121    /**
122     * Default asian font name.
123     *
124     * @var string
125     */
126    private static $defaultAsianFontName = self::DEFAULT_FONT_NAME;
127
128    /**
129     * Default font color.
130     *
131     * @var string
132     */
133    private static $defaultFontColor = self::DEFAULT_FONT_COLOR;
134
135    /**
136     * Default font size.
137     *
138     * @var float|int
139     */
140    private static $defaultFontSize = self::DEFAULT_FONT_SIZE;
141
142    /**
143     * Default paper.
144     *
145     * @var string
146     */
147    private static $defaultPaper = self::DEFAULT_PAPER;
148
149    /**
150     * Is RTL by default ?
151     *
152     * @var ?bool
153     */
154    private static $defaultRtl;
155
156    /**
157     * The user defined temporary directory.
158     *
159     * @var string
160     */
161    private static $tempDir = '';
162
163    /**
164     * Enables built-in output escaping mechanism.
165     * Default value is `false` for backward compatibility with versions below 0.13.0.
166     *
167     * @var bool
168     */
169    private static $outputEscapingEnabled = false;
170
171    /**
172     * Return the compatibility option used by the XMLWriter.
173     *
174     * @return bool Compatibility
175     */
176    public static function hasCompatibility(): bool
177    {
178        return self::$xmlWriterCompatibility;
179    }
180
181    /**
182     * Set the compatibility option used by the XMLWriter.
183     * This sets the setIndent and setIndentString for better compatibility.
184     */
185    public static function setCompatibility(bool $compatibility): bool
186    {
187        self::$xmlWriterCompatibility = $compatibility;
188
189        return true;
190    }
191
192    /**
193     * Get zip handler class.
194     */
195    public static function getZipClass(): string
196    {
197        return self::$zipClass;
198    }
199
200    /**
201     * Set zip handler class.
202     */
203    public static function setZipClass(string $zipClass): bool
204    {
205        if (in_array($zipClass, [self::PCLZIP, self::ZIPARCHIVE, self::OLD_LIB])) {
206            self::$zipClass = $zipClass;
207
208            return true;
209        }
210
211        return false;
212    }
213
214    /**
215     * Set details of the external library for rendering PDF files.
216     *
217     * @return bool Success or failure
218     */
219    public static function setPdfRenderer(string $libraryName, string $libraryBaseDir): bool
220    {
221        if (!self::setPdfRendererName($libraryName)) {
222            return false;
223        }
224
225        return self::setPdfRendererPath($libraryBaseDir);
226    }
227
228    /**
229     * Return the PDF Rendering Library.
230     */
231    public static function getPdfRendererName(): ?string
232    {
233        return self::$pdfRendererName;
234    }
235
236    /**
237     * Identify the external library to use for rendering PDF files.
238     */
239    public static function setPdfRendererName(?string $libraryName): bool
240    {
241        $pdfRenderers = [self::PDF_RENDERER_DOMPDF, self::PDF_RENDERER_TCPDF, self::PDF_RENDERER_MPDF];
242        if (!in_array($libraryName, $pdfRenderers)) {
243            return false;
244        }
245        self::$pdfRendererName = $libraryName;
246
247        return true;
248    }
249
250    /**
251     * Return the directory path to the PDF Rendering Library.
252     */
253    public static function getPdfRendererPath(): ?string
254    {
255        return self::$pdfRendererPath;
256    }
257
258    /**
259     * Set options of the external library for rendering PDF files.
260     */
261    public static function setPdfRendererOptions(array $options): void
262    {
263        self::$pdfRendererOptions = $options;
264    }
265
266    /**
267     * Return the PDF Rendering Options.
268     */
269    public static function getPdfRendererOptions(): array
270    {
271        return self::$pdfRendererOptions;
272    }
273
274    /**
275     * Location of external library to use for rendering PDF files.
276     *
277     * @param null|string $libraryBaseDir Directory path to the library's base folder
278     *
279     * @return bool Success or failure
280     */
281    public static function setPdfRendererPath(?string $libraryBaseDir): bool
282    {
283        if (!$libraryBaseDir || false === file_exists($libraryBaseDir) || false === is_readable($libraryBaseDir)) {
284            return false;
285        }
286        self::$pdfRendererPath = $libraryBaseDir;
287
288        return true;
289    }
290
291    /**
292     * Get measurement unit.
293     */
294    public static function getMeasurementUnit(): string
295    {
296        return self::$measurementUnit;
297    }
298
299    /**
300     * Set measurement unit.
301     */
302    public static function setMeasurementUnit(string $value): bool
303    {
304        $units = [
305            self::UNIT_TWIP,
306            self::UNIT_CM,
307            self::UNIT_MM,
308            self::UNIT_INCH,
309            self::UNIT_POINT,
310            self::UNIT_PICA,
311        ];
312        if (!in_array($value, $units)) {
313            return false;
314        }
315        self::$measurementUnit = $value;
316
317        return true;
318    }
319
320    /**
321     * Sets the user defined path to temporary directory.
322     *
323     * @param string $tempDir The user defined path to temporary directory
324     *
325     * @since 0.12.0
326     */
327    public static function setTempDir(string $tempDir): void
328    {
329        self::$tempDir = $tempDir;
330    }
331
332    /**
333     * Returns path to temporary directory.
334     *
335     * @since 0.12.0
336     */
337    public static function getTempDir(): string
338    {
339        if (!empty(self::$tempDir)) {
340            $tempDir = self::$tempDir;
341        } else {
342            $tempDir = sys_get_temp_dir();
343        }
344
345        return $tempDir;
346    }
347
348    /**
349     * @since 0.13.0
350     */
351    public static function isOutputEscapingEnabled(): bool
352    {
353        return self::$outputEscapingEnabled;
354    }
355
356    /**
357     * @since 0.13.0
358     */
359    public static function setOutputEscapingEnabled(bool $outputEscapingEnabled): void
360    {
361        self::$outputEscapingEnabled = $outputEscapingEnabled;
362    }
363
364    /**
365     * Get default font name.
366     */
367    public static function getDefaultFontName(): string
368    {
369        return self::$defaultFontName;
370    }
371
372    /**
373     * Get default font name.
374     */
375    public static function getDefaultAsianFontName(): string
376    {
377        return self::$defaultAsianFontName;
378    }
379
380    /**
381     * Set default font name.
382     */
383    public static function setDefaultFontName(string $value): bool
384    {
385        if (trim($value) !== '') {
386            self::$defaultFontName = $value;
387
388            return true;
389        }
390
391        return false;
392    }
393
394    public static function setDefaultAsianFontName(string $value): bool
395    {
396        if (trim($value) !== '') {
397            self::$defaultAsianFontName = $value;
398
399            return true;
400        }
401
402        return false;
403    }
404
405    /**
406     * Get default font color.
407     */
408    public static function getDefaultFontColor(): string
409    {
410        return self::$defaultFontColor;
411    }
412
413    /**
414     * Set default font color.
415     */
416    public static function setDefaultFontColor(string $value): bool
417    {
418        if (trim($value) !== '') {
419            self::$defaultFontColor = $value;
420
421            return true;
422        }
423
424        return false;
425    }
426
427    /**
428     * Get default font size.
429     *
430     * @return float|int
431     */
432    public static function getDefaultFontSize()
433    {
434        return self::$defaultFontSize;
435    }
436
437    /**
438     * Set default font size.
439     *
440     * @param null|float|int $value
441     */
442    public static function setDefaultFontSize($value): bool
443    {
444        if ((is_int($value) || is_float($value)) && (int) $value > 0) {
445            self::$defaultFontSize = $value;
446
447            return true;
448        }
449
450        return false;
451    }
452
453    public static function setDefaultRtl(?bool $defaultRtl): void
454    {
455        self::$defaultRtl = $defaultRtl;
456    }
457
458    public static function isDefaultRtl(): ?bool
459    {
460        return self::$defaultRtl;
461    }
462
463    /**
464     * Load setting from phpword.yml or phpword.yml.dist.
465     */
466    public static function loadConfig(?string $filename = null): array
467    {
468        // Get config file
469        $configFile = null;
470        $configPath = __DIR__ . '/../../';
471        if ($filename !== null) {
472            $files = [$filename];
473        } else {
474            $files = ["{$configPath}phpword.ini", "{$configPath}phpword.ini.dist"];
475        }
476        foreach ($files as $file) {
477            if (file_exists($file)) {
478                $configFile = realpath($file);
479
480                break;
481            }
482        }
483
484        // Parse config file
485        $config = [];
486        if ($configFile !== null) {
487            $config = @parse_ini_file($configFile);
488            if ($config === false) {
489                return [];
490            }
491        }
492
493        // Set config value
494        $appliedConfig = [];
495        foreach ($config as $key => $value) {
496            $method = "set{$key}";
497            if (method_exists(__CLASS__, $method)) {
498                self::$method($value);
499                $appliedConfig[$key] = $value;
500            }
501        }
502
503        return $appliedConfig;
504    }
505
506    /**
507     * Get default paper.
508     */
509    public static function getDefaultPaper(): string
510    {
511        return self::$defaultPaper;
512    }
513
514    /**
515     * Set default paper.
516     */
517    public static function setDefaultPaper(string $value): bool
518    {
519        if (trim($value) !== '') {
520            self::$defaultPaper = $value;
521
522            return true;
523        }
524
525        return false;
526    }
527}