Completed
Push — develop ( 66f372...1cdc85 )
by Adrien
20:59
created

Settings::getZipClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet;
4
5
/**
6
 * Copyright (c) 2006 - 2016 PhpSpreadsheet.
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21
 *
22
 * @category   PhpSpreadsheet
23
 *
24
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
25
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
26
 */
27
class Settings
28
{
29
    /**    constants */
30
    /**    Available Zip library classes */
31
    const PCLZIP = \PhpOffice\PhpSpreadsheet\Shared\ZipArchive::class;
32
    const ZIPARCHIVE = \ZipArchive::class;
33
34
    /**    Optional Chart Rendering libraries */
35
    const CHART_RENDERER_JPGRAPH = 'JpGraph';
36
37
    /**    Optional PDF Rendering libraries */
38
    const PDF_RENDERER_TCPDF = 'TcPDF';
39
    const PDF_RENDERER_DOMPDF = 'DomPDF';
40
    const PDF_RENDERER_MPDF = 'MPDF';
41
42
    private static $chartRenderers = [
43
        self::CHART_RENDERER_JPGRAPH,
44
    ];
45
46
    private static $pdfRenderers = [
47
        self::PDF_RENDERER_TCPDF,
48
        self::PDF_RENDERER_DOMPDF,
49
        self::PDF_RENDERER_MPDF,
50
    ];
51
52
    /**
53
     * Name of the class used for Zip file management
54
     *    e.g.
55
     *        ZipArchive.
56
     *
57
     * @var string
58
     */
59
    private static $zipClass = self::ZIPARCHIVE;
60
61
    /**
62
     * Name of the external Library used for rendering charts
63
     *    e.g.
64
     *        jpgraph.
65
     *
66
     * @var string
67
     */
68
    private static $chartRendererName;
69
70
    /**
71
     * Directory Path to the external Library used for rendering charts.
72
     *
73
     * @var string
74
     */
75
    private static $chartRendererPath;
76
77
    /**
78
     * Name of the external Library used for rendering PDF files
79
     *    e.g.
80
     *         mPDF.
81
     *
82
     * @var string
83
     */
84
    private static $pdfRendererName;
85
86
    /**
87
     * Default options for libxml loader.
88
     *
89
     * @var int
90
     */
91
    private static $libXmlLoaderOptions = null;
92
93
    /**
94
     * Set the Zip handler Class that PhpSpreadsheet should use for Zip file management (PCLZip or ZipArchive).
95
     *
96
     * @param string $zipClass    The Zip handler class that PhpSpreadsheet should use for Zip file management
97
     *      e.g. \PhpOffice\PhpSpreadsheet\Settings::PCLZIP or \PhpOffice\PhpSpreadsheet\Settings::ZIPARCHIVE
98
     *
99
     * @return    bool    Success or failure
100
     */
101 1
    public static function setZipClass($zipClass)
102
    {
103 1
        if (($zipClass === self::PCLZIP) ||
104 1
            ($zipClass === self::ZIPARCHIVE)) {
105 1
            self::$zipClass = $zipClass;
106
107 1
            return true;
108
        }
109
110
        return false;
111
    }
112
113
    /**
114
     * Return the name of the Zip handler Class that PhpSpreadsheet is configured to use (PCLZip or ZipArchive)
115
     *    or Zip file management.
116
     *
117
     * @return string Name of the Zip handler Class that PhpSpreadsheet is configured to use
118
     *    for Zip file management
119
     *    e.g. \PhpOffice\PhpSpreadsheet\Settings::PCLZIP or \PhpOffice\PhpSpreadsheet\Settings::ZIPARCHIVE
120
     */
121 58
    public static function getZipClass()
122
    {
123 58
        return self::$zipClass;
124
    }
125
126
    /**
127
     * Return the name of the method that is currently configured for cell cacheing.
128
     *
129
     * @return string Name of the cacheing method
130
     */
131
    public static function getCacheStorageMethod()
132
    {
133
        return CachedObjectStorageFactory::getCacheStorageMethod();
134
    }
135
136
    /**
137
     * Return the name of the class that is currently being used for cell cacheing.
138
     *
139
     * @return string Name of the class currently being used for cacheing
140
     */
141
    public static function getCacheStorageClass()
142
    {
143
        return CachedObjectStorageFactory::getCacheStorageClass();
144
    }
145
146
    /**
147
     * Set the method that should be used for cell caching.
148
     *
149
     * @param string $method Name of the caching method
150
     * @param array $arguments Optional configuration arguments for the caching method
151
     *
152
     * @return bool Success or failure
153
     */
154
    public static function setCacheStorageMethod($method = CachedObjectStorageFactory::CACHE_IN_MEMORY, $arguments = [])
155
    {
156
        return CachedObjectStorageFactory::initialize($method, $arguments);
157
    }
158
159
    /**
160
     * Set the locale code to use for formula translations and any special formatting.
161
     *
162
     * @param string $locale The locale code to use (e.g. "fr" or "pt_br" or "en_uk")
163
     *
164
     * @return bool Success or failure
165
     */
166
    public static function setLocale($locale = 'en_us')
167
    {
168
        return Calculation::getInstance()->setLocale($locale);
169
    }
170
171
    /**
172
     * Set details of the external library that PhpSpreadsheet should use for rendering charts.
173
     *
174
     * @param string $libraryName    Internal reference name of the library
175
     *    e.g. \PhpOffice\PhpSpreadsheet\Settings::CHART_RENDERER_JPGRAPH
176
     * @param string $libraryBaseDir Directory path to the library's base folder
177
     *
178
     * @return    bool    Success or failure
179
     */
180 3
    public static function setChartRenderer($libraryName, $libraryBaseDir)
181
    {
182 3
        if (!self::setChartRendererName($libraryName)) {
183
            return false;
184
        }
185
186 3
        return self::setChartRendererPath($libraryBaseDir);
187
    }
188
189
    /**
190
     * Identify to PhpSpreadsheet the external library to use for rendering charts.
191
     *
192
     * @param string $libraryName    Internal reference name of the library
193
     *    e.g. \PhpOffice\PhpSpreadsheet\Settings::CHART_RENDERER_JPGRAPH
194
     *
195
     * @return    bool    Success or failure
196
     */
197 3
    public static function setChartRendererName($libraryName)
198
    {
199 3
        if (!in_array($libraryName, self::$chartRenderers)) {
200
            return false;
201
        }
202 3
        self::$chartRendererName = $libraryName;
203
204 3
        return true;
205
    }
206
207
    /**
208
     * Tell PhpSpreadsheet where to find the external library to use for rendering charts.
209
     *
210
     * @param string $libraryBaseDir    Directory path to the library's base folder
211
     *
212
     * @return    bool    Success or failure
213
     */
214 3
    public static function setChartRendererPath($libraryBaseDir)
215
    {
216 3
        if ((file_exists($libraryBaseDir) === false) || (is_readable($libraryBaseDir) === false)) {
217 3
            return false;
218
        }
219
        self::$chartRendererPath = $libraryBaseDir;
220
221
        return true;
222
    }
223
224
    /**
225
     * Return the Chart Rendering Library that PhpSpreadsheet is currently configured to use (e.g. jpgraph).
226
     *
227
     * @return string|null Internal reference name of the Chart Rendering Library that PhpSpreadsheet is
228
     *    currently configured to use
229
     *    e.g. \PhpOffice\PhpSpreadsheet\Settings::CHART_RENDERER_JPGRAPH
230
     */
231
    public static function getChartRendererName()
232
    {
233
        return self::$chartRendererName;
234
    }
235
236
    /**
237
     * Return the directory path to the Chart Rendering Library that PhpSpreadsheet is currently configured to use.
238
     *
239
     * @return string|null Directory Path to the Chart Rendering Library that PhpSpreadsheet is
240
     *     currently configured to use
241
     */
242
    public static function getChartRendererPath()
243
    {
244
        return self::$chartRendererPath;
245
    }
246
247
    /**
248
     * Identify to PhpSpreadsheet the external library to use for rendering PDF files.
249
     *
250
     * @param string $libraryName Internal reference name of the library
251
     *     e.g. \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_TCPDF,
252
     *          \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_DOMPDF
253
     *       or \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_MPDF
254
     */
255 5
    public static function setPdfRendererName($libraryName)
256
    {
257 5
        if (!in_array($libraryName, self::$pdfRenderers)) {
258
            throw new Exception('"' . $libraryName . '" is not a valid PDF library name');
259
        }
260 5
        self::$pdfRendererName = $libraryName;
261 5
    }
262
263
    /**
264
     * Return the PDF Rendering Library that PhpSpreadsheet is currently configured to use (e.g. dompdf).
265
     *
266
     * @return string|null Internal reference name of the PDF Rendering Library that PhpSpreadsheet is
267
     *     currently configured to use
268
     *  e.g. \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_TCPDF,
269
     *       \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_DOMPDF
270
     *    or \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_MPDF
271
     */
272 4
    public static function getPdfRendererName()
273
    {
274 4
        return self::$pdfRendererName;
275
    }
276
277
    /**
278
     * Set default options for libxml loader.
279
     *
280
     * @param int $options Default options for libxml loader
281
     */
282 14
    public static function setLibXmlLoaderOptions($options = null)
283
    {
284 14
        if (is_null($options) && defined('LIBXML_DTDLOAD')) {
285
            $options = LIBXML_DTDLOAD | LIBXML_DTDATTR;
286
        }
287 14
        @libxml_disable_entity_loader((bool) $options);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
288 14
        self::$libXmlLoaderOptions = $options;
289 14
    }
290
291
    /**
292
     * Get default options for libxml loader.
293
     * Defaults to LIBXML_DTDLOAD | LIBXML_DTDATTR when not set explicitly.
294
     *
295
     * @return int Default options for libxml loader
296
     */
297 15
    public static function getLibXmlLoaderOptions()
298
    {
299 15
        if (is_null(self::$libXmlLoaderOptions) && defined('LIBXML_DTDLOAD')) {
300 13
            self::setLibXmlLoaderOptions(LIBXML_DTDLOAD | LIBXML_DTDATTR);
301 12
        } elseif (is_null(self::$libXmlLoaderOptions)) {
302
            self::$libXmlLoaderOptions = true;
0 ignored issues
show
Documentation Bug introduced by
The property $libXmlLoaderOptions was declared of type integer, but true is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
303
        }
304 15
        @libxml_disable_entity_loader((bool) self::$libXmlLoaderOptions);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
305
306 15
        return self::$libXmlLoaderOptions;
307
    }
308
}
309