Completed
Push — develop ( 069c66...25ff91 )
by Adrien
23:18
created

Settings::setChartRendererPath()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.576

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 3
cts 5
cp 0.6
crap 3.576
rs 9.6666
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet;
4
5
use PhpOffice\PhpSpreadsheet\Collection\Memory;
6
use Psr\SimpleCache\CacheInterface;
7
8
class Settings
9
{
10
    /**    Optional Chart Rendering libraries */
11
    const CHART_RENDERER_JPGRAPH = 'JpGraph';
12
13
    private static $chartRenderers = [
14
        self::CHART_RENDERER_JPGRAPH,
15
    ];
16
17
    /**
18
     * Name of the external Library used for rendering charts
19
     * e.g.
20
     *        jpgraph.
21
     *
22
     * @var string
23
     */
24
    private static $chartRendererName;
25
26
    /**
27
     * Directory Path to the external Library used for rendering charts.
28
     *
29
     * @var string
30
     */
31
    private static $chartRendererPath;
32
33
    /**
34
     * Default options for libxml loader.
35
     *
36
     * @var int
37
     */
38
    private static $libXmlLoaderOptions = null;
39
40
    /**
41
     * The cache implementation to be used for cell collection.
42
     *
43
     * @var CacheInterface
44
     */
45
    private static $cache;
46
47
    /**
48
     * Set the locale code to use for formula translations and any special formatting.
49
     *
50
     * @param string $locale The locale code to use (e.g. "fr" or "pt_br" or "en_uk")
51
     *
52
     * @return bool Success or failure
53
     */
54
    public static function setLocale($locale)
55
    {
56
        return Calculation::getInstance()->setLocale($locale);
57
    }
58
59
    /**
60
     * Set details of the external library that PhpSpreadsheet should use for rendering charts.
61
     *
62
     * @param string $libraryName Internal reference name of the library
63
     *    e.g. \PhpOffice\PhpSpreadsheet\Settings::CHART_RENDERER_JPGRAPH
64
     * @param string $libraryBaseDir Directory path to the library's base folder
65
     *
66
     * @return bool Success or failure
67
     */
68 3
    public static function setChartRenderer($libraryName, $libraryBaseDir)
69
    {
70 3
        if (!self::setChartRendererName($libraryName)) {
71
            return false;
72
        }
73
74 3
        return self::setChartRendererPath($libraryBaseDir);
75
    }
76
77
    /**
78
     * Identify to PhpSpreadsheet the external library to use for rendering charts.
79
     *
80
     * @param string $libraryName Internal reference name of the library
81
     *    e.g. \PhpOffice\PhpSpreadsheet\Settings::CHART_RENDERER_JPGRAPH
82
     *
83
     * @return bool Success or failure
84
     */
85 3
    public static function setChartRendererName($libraryName)
86
    {
87 3
        if (!in_array($libraryName, self::$chartRenderers)) {
88
            return false;
89
        }
90 3
        self::$chartRendererName = $libraryName;
91
92 3
        return true;
93
    }
94
95
    /**
96
     * Tell PhpSpreadsheet where to find the external library to use for rendering charts.
97
     *
98
     * @param string $libraryBaseDir Directory path to the library's base folder
99
     *
100
     * @return bool Success or failure
101
     */
102 3
    public static function setChartRendererPath($libraryBaseDir)
103
    {
104 3
        if ((file_exists($libraryBaseDir) === false) || (is_readable($libraryBaseDir) === false)) {
105 3
            return false;
106
        }
107
        self::$chartRendererPath = $libraryBaseDir;
108
109
        return true;
110
    }
111
112
    /**
113
     * Return the Chart Rendering Library that PhpSpreadsheet is currently configured to use (e.g. jpgraph).
114
     *
115
     * @return null|string Internal reference name of the Chart Rendering Library that PhpSpreadsheet is
116
     *    currently configured to use
117
     *    e.g. \PhpOffice\PhpSpreadsheet\Settings::CHART_RENDERER_JPGRAPH
118
     */
119
    public static function getChartRendererName()
120
    {
121
        return self::$chartRendererName;
122
    }
123
124
    /**
125
     * Return the directory path to the Chart Rendering Library that PhpSpreadsheet is currently configured to use.
126
     *
127
     * @return null|string Directory Path to the Chart Rendering Library that PhpSpreadsheet is
128
     *     currently configured to use
129
     */
130
    public static function getChartRendererPath()
131
    {
132
        return self::$chartRendererPath;
133
    }
134
135
    /**
136
     * Set default options for libxml loader.
137
     *
138
     * @param int $options Default options for libxml loader
139
     */
140 16
    public static function setLibXmlLoaderOptions($options)
141
    {
142 16
        if ($options === null && defined('LIBXML_DTDLOAD')) {
143
            $options = LIBXML_DTDLOAD | LIBXML_DTDATTR;
144
        }
145 16
        self::$libXmlLoaderOptions = $options;
146 16
    }
147
148
    /**
149
     * Get default options for libxml loader.
150
     * Defaults to LIBXML_DTDLOAD | LIBXML_DTDATTR when not set explicitly.
151
     *
152
     * @return int Default options for libxml loader
153
     */
154 28
    public static function getLibXmlLoaderOptions()
155
    {
156 28
        if (self::$libXmlLoaderOptions === null && defined('LIBXML_DTDLOAD')) {
157 15
            self::setLibXmlLoaderOptions(LIBXML_DTDLOAD | LIBXML_DTDATTR);
158 25
        } elseif (self::$libXmlLoaderOptions === null) {
159
            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...
160
        }
161
162 28
        return self::$libXmlLoaderOptions;
163
    }
164
165
    /**
166
     * Sets the implementation of cache that should be used for cell collection.
167
     *
168
     * @param CacheInterface $cache
169
     */
170
    public static function setCache(CacheInterface $cache)
171
    {
172
        self::$cache = $cache;
173
    }
174
175
    /**
176
     * Gets the implementation of cache that should be used for cell collection.
177
     *
178
     * @return CacheInterface
179
     */
180 149
    public static function getCache()
181
    {
182 149
        if (!self::$cache) {
183 97
            self::$cache = new Memory();
184
        }
185
186 149
        return self::$cache;
187
    }
188
}
189