Passed
Push — master ( fb74dc...f37b11 )
by
unknown
18:17 queued 06:47
created

Settings::getLibXmlLoaderOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6
use PhpOffice\PhpSpreadsheet\Chart\Renderer\IRenderer;
7
use PhpOffice\PhpSpreadsheet\Collection\Memory;
8
use Psr\Http\Client\ClientInterface;
9
use Psr\Http\Message\RequestFactoryInterface;
10
use Psr\SimpleCache\CacheInterface;
11
use ReflectionClass;
12
13
class Settings
14
{
15
    /**
16
     * Class name of the chart renderer used for rendering charts
17
     * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph.
18
     *
19
     * @var null|class-string<IRenderer>
20
     */
21
    private static ?string $chartRenderer = null;
22
23
    /**
24
     * Default options for libxml loader.
25
     */
26
    private static ?int $libXmlLoaderOptions = null;
27
28
    /**
29
     * The cache implementation to be used for cell collection.
30
     */
31
    private static ?CacheInterface $cache = null;
32
33
    /**
34
     * The HTTP client implementation to be used for network request.
35
     */
36
    private static ?ClientInterface $httpClient = null;
37
38
    private static ?RequestFactoryInterface $requestFactory = null;
39
40
    /**
41
     * Set the locale code to use for formula translations and any special formatting.
42
     *
43
     * @param string $locale The locale code to use (e.g. "fr" or "pt_br" or "en_uk")
44
     *
45
     * @return bool Success or failure
46
     */
47 745
    public static function setLocale(string $locale): bool
48
    {
49 745
        return Calculation::getInstance()->setLocale($locale);
50
    }
51
52 745
    public static function getLocale(): string
53
    {
54 745
        return Calculation::getInstance()->getLocale();
55
    }
56
57
    /**
58
     * Identify to PhpSpreadsheet the external library to use for rendering charts.
59
     *
60
     * @param class-string<IRenderer> $rendererClassName Class name of the chart renderer
61
     *    eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
62
     */
63 7
    public static function setChartRenderer(string $rendererClassName): void
64
    {
65 7
        if (!is_a($rendererClassName, IRenderer::class, true)) {
66 1
            throw new Exception('Chart renderer must implement ' . IRenderer::class);
67
        }
68
69 6
        self::$chartRenderer = $rendererClassName;
70
    }
71
72 1
    public static function unsetChartRenderer(): void
73
    {
74 1
        self::$chartRenderer = null;
75
    }
76
77
    /**
78
     * Return the Chart Rendering Library that PhpSpreadsheet is currently configured to use.
79
     *
80
     * @return null|class-string<IRenderer> Class name of the chart renderer
81
     *    eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
82
     */
83 6
    public static function getChartRenderer(): ?string
84
    {
85 6
        return self::$chartRenderer;
86
    }
87
88 508
    public static function htmlEntityFlags(): int
89
    {
90 508
        return ENT_COMPAT;
91
    }
92
93
    /**
94
     * Set default options for libxml loader.
95
     *
96
     * @param ?int $options Default options for libxml loader
97
     *
98
     * @deprecated 3.5.0 no longer needed
99
     */
100 1
    public static function setLibXmlLoaderOptions(?int $options): int
101
    {
102 1
        if ($options === null) {
103
            $options = defined('LIBXML_DTDLOAD') ? (LIBXML_DTDLOAD | LIBXML_DTDATTR) : 0;
104
        }
105 1
        self::$libXmlLoaderOptions = $options;
106
107 1
        return $options;
108
    }
109
110
    /**
111
     * Get default options for libxml loader.
112
     * Defaults to LIBXML_DTDLOAD | LIBXML_DTDATTR when not set explicitly.
113
     *
114
     * @return int Default options for libxml loader
115
     *
116
     * @deprecated 3.5.0 no longer needed
117
     */
118 3
    public static function getLibXmlLoaderOptions(): int
119
    {
120 3
        return self::$libXmlLoaderOptions ?? (defined('LIBXML_DTDLOAD') ? (LIBXML_DTDLOAD | LIBXML_DTDATTR) : 0);
121
    }
122
123
    /**
124
     * Sets the implementation of cache that should be used for cell collection.
125
     */
126 5
    public static function setCache(?CacheInterface $cache): void
127
    {
128 5
        self::$cache = $cache;
129
    }
130
131
    /**
132
     * Gets the implementation of cache that is being used for cell collection.
133
     */
134 10280
    public static function getCache(): CacheInterface
135
    {
136 10280
        if (!self::$cache) {
137 290
            self::$cache = self::useSimpleCacheVersion3() ? new Memory\SimpleCache3() : new Memory\SimpleCache1();
138
        }
139
140 10280
        return self::$cache;
141
    }
142
143 292
    public static function useSimpleCacheVersion3(): bool
144
    {
145 292
        return (new ReflectionClass(CacheInterface::class))->getMethod('get')->getReturnType() !== null;
146
    }
147
148
    /**
149
     * Set the HTTP client implementation to be used for network request.
150
     */
151 4
    public static function setHttpClient(ClientInterface $httpClient, RequestFactoryInterface $requestFactory): void
152
    {
153 4
        self::$httpClient = $httpClient;
154 4
        self::$requestFactory = $requestFactory;
155
    }
156
157
    /**
158
     * Unset the HTTP client configuration.
159
     */
160 7
    public static function unsetHttpClient(): void
161
    {
162 7
        self::$httpClient = null;
163 7
        self::$requestFactory = null;
164
    }
165
166
    /**
167
     * Get the HTTP client implementation to be used for network request.
168
     */
169 5
    public static function getHttpClient(): ClientInterface
170
    {
171 5
        if (!self::$httpClient || !self::$requestFactory) {
172 1
            throw new Exception('HTTP client must be configured via Settings::setHttpClient() to be able to use WEBSERVICE function.');
173
        }
174
175 4
        return self::$httpClient;
176
    }
177
178
    /**
179
     * Get the HTTP request factory.
180
     */
181 5
    public static function getRequestFactory(): RequestFactoryInterface
182
    {
183 5
        if (!self::$httpClient || !self::$requestFactory) {
184 1
            throw new Exception('HTTP client must be configured via Settings::setHttpClient() to be able to use WEBSERVICE function.');
185
        }
186
187 4
        return self::$requestFactory;
188
    }
189
}
190