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