Completed
Push — master ( e74ef3...7f23cc )
by Adrien
15:26 queued 08:10
created

Settings::setLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 1
cts 1
cp 1
crap 1
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet;
4
5
use GuzzleHttp\Client;
6
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
7
use PhpOffice\PhpSpreadsheet\Chart\Renderer\IRenderer;
8
use PhpOffice\PhpSpreadsheet\Collection\Memory;
9
use Psr\Http\Client\ClientInterface;
10
use Psr\SimpleCache\CacheInterface;
11
12
class Settings
13
{
14
    /**
15
     * Class name of the chart renderer used for rendering charts
16
     * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph.
17
     *
18
     * @var string
19
     */
20
    private static $chartRenderer;
21
22
    /**
23
     * Default options for libxml loader.
24
     *
25
     * @var int
26
     */
27
    private static $libXmlLoaderOptions = null;
28
29
    /**
30
     * Allow/disallow libxml_disable_entity_loader() call when not thread safe.
31
     * Default behaviour is to do the check, but if you're running PHP versions
32
     *      7.2 < 7.2.1
33
     * then you may need to disable this check to prevent unwanted behaviour in other threads
34
     * SECURITY WARNING: Changing this flag is not recommended.
35
     *
36
     * @var bool
37
     */
38
    private static $libXmlDisableEntityLoader = true;
39
40
    /**
41
     * The cache implementation to be used for cell collection.
42
     *
43
     * @var CacheInterface
44
     */
45
    private static $cache;
46
47
    /**
48
     * The HTTP client implementation to be used for network request.
49
     *
50
     * @var ClientInterface
51
     */
52
    private static $client;
53
54
    /**
55
     * Set the locale code to use for formula translations and any special formatting.
56
     *
57
     * @param string $locale The locale code to use (e.g. "fr" or "pt_br" or "en_uk")
58
     *
59
     * @return bool Success or failure
60
     */
61
    public static function setLocale($locale)
62
    {
63 1
        return Calculation::getInstance()->setLocale($locale);
64
    }
65 1
66
    /**
67
     * Identify to PhpSpreadsheet the external library to use for rendering charts.
68
     *
69 1
     * @param string $rendererClass Class name of the chart renderer
70 1
     *    eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
71
     */
72
    public static function setChartRenderer($rendererClass): void
73
    {
74
        if (!is_a($rendererClass, IRenderer::class, true)) {
75
            throw new Exception('Chart renderer must implement ' . IRenderer::class);
76
        }
77
78 1
        self::$chartRenderer = $rendererClass;
79
    }
80 1
81
    /**
82
     * Return the Chart Rendering Library that PhpSpreadsheet is currently configured to use.
83
     *
84
     * @return null|string Class name of the chart renderer
85
     *    eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
86
     */
87
    public static function getChartRenderer()
88 33
    {
89
        return self::$chartRenderer;
90 33
    }
91 15
92
    /**
93 33
     * Set default options for libxml loader.
94 33
     *
95
     * @param int $options Default options for libxml loader
96
     */
97
    public static function setLibXmlLoaderOptions($options): void
98
    {
99
        if ($options === null && defined('LIBXML_DTDLOAD')) {
0 ignored issues
show
introduced by
The condition $options === null is always false.
Loading history...
100
            $options = LIBXML_DTDLOAD | LIBXML_DTDATTR;
101
        }
102 96
        self::$libXmlLoaderOptions = $options;
103
    }
104 96
105 17
    /**
106 94
     * Get default options for libxml loader.
107
     * Defaults to LIBXML_DTDLOAD | LIBXML_DTDATTR when not set explicitly.
108
     *
109
     * @return int Default options for libxml loader
110 96
     */
111
    public static function getLibXmlLoaderOptions()
112
    {
113
        if (self::$libXmlLoaderOptions === null && defined('LIBXML_DTDLOAD')) {
0 ignored issues
show
introduced by
The condition self::libXmlLoaderOptions === null is always false.
Loading history...
114
            self::setLibXmlLoaderOptions(LIBXML_DTDLOAD | LIBXML_DTDATTR);
115
        } elseif (self::$libXmlLoaderOptions === null) {
0 ignored issues
show
introduced by
The condition self::libXmlLoaderOptions === null is always false.
Loading history...
116
            self::$libXmlLoaderOptions = true;
117
        }
118
119
        return self::$libXmlLoaderOptions;
120
    }
121
122
    /**
123
     * Enable/Disable the entity loader for libxml loader.
124
     * Allow/disallow libxml_disable_entity_loader() call when not thread safe.
125
     * Default behaviour is to do the check, but if you're running PHP versions
126
     *      7.2 < 7.2.1
127
     * then you may need to disable this check to prevent unwanted behaviour in other threads
128
     * SECURITY WARNING: Changing this flag to false is not recommended.
129
     *
130
     * @param bool $state
131
     */
132
    public static function setLibXmlDisableEntityLoader($state): void
133 160
    {
134
        self::$libXmlDisableEntityLoader = (bool) $state;
135 160
    }
136
137
    /**
138
     * Return the state of the entity loader (disabled/enabled) for libxml loader.
139
     *
140
     * @return bool $state
141
     */
142
    public static function getLibXmlDisableEntityLoader()
143
    {
144
        return self::$libXmlDisableEntityLoader;
145
    }
146
147
    /**
148
     * Sets the implementation of cache that should be used for cell collection.
149
     */
150
    public static function setCache(CacheInterface $cache): void
151
    {
152
        self::$cache = $cache;
153 359
    }
154
155 359
    /**
156 101
     * Gets the implementation of cache that should be used for cell collection.
157
     *
158
     * @return CacheInterface
159 359
     */
160
    public static function getCache()
161
    {
162
        if (!self::$cache) {
163
            self::$cache = new Memory();
164
        }
165
166
        return self::$cache;
167
    }
168
169
    /**
170
     * Set the HTTP client implementation to be used for network request.
171
     */
172
    public static function setHttpClient(ClientInterface $httpClient): void
173
    {
174
        self::$client = $httpClient;
175
    }
176
177
    /**
178
     * Get the HTTP client implementation to be used for network request.
179
     */
180
    public static function getHttpClient(): ClientInterface
181
    {
182
        if (!self::$client) {
183
            self::$client = new Client();
184
        }
185
186
        return self::$client;
187
    }
188
}
189