Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
     * The cache implementation to be used for cell collection.
25
     */
26
    private static ?CacheInterface $cache = null;
27
 
28
    /**
29
     * The HTTP client implementation to be used for network request.
30
     */
31
    private static ?ClientInterface $httpClient = null;
32
 
33
    private static ?RequestFactoryInterface $requestFactory = null;
34
 
35
    /**
36
     * Set the locale code to use for formula translations and any special formatting.
37
     *
38
     * @param string $locale The locale code to use (e.g. "fr" or "pt_br" or "en_uk")
39
     *
40
     * @return bool Success or failure
41
     */
42
    public static function setLocale(string $locale): bool
43
    {
44
        return Calculation::getInstance()->setLocale($locale);
45
    }
46
 
47
    public static function getLocale(): string
48
    {
49
        return Calculation::getInstance()->getLocale();
50
    }
51
 
52
    /**
53
     * Identify to PhpSpreadsheet the external library to use for rendering charts.
54
     *
55
     * @param class-string<IRenderer> $rendererClassName Class name of the chart renderer
56
     *    eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
57
     */
58
    public static function setChartRenderer(string $rendererClassName): void
59
    {
60
        if (!is_a($rendererClassName, IRenderer::class, true)) {
61
            throw new Exception('Chart renderer must implement ' . IRenderer::class);
62
        }
63
 
64
        self::$chartRenderer = $rendererClassName;
65
    }
66
 
67
    public static function unsetChartRenderer(): void
68
    {
69
        self::$chartRenderer = null;
70
    }
71
 
72
    /**
73
     * Return the Chart Rendering Library that PhpSpreadsheet is currently configured to use.
74
     *
75
     * @return null|class-string<IRenderer> Class name of the chart renderer
76
     *    eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
77
     */
78
    public static function getChartRenderer(): ?string
79
    {
80
        return self::$chartRenderer;
81
    }
82
 
83
    public static function htmlEntityFlags(): int
84
    {
85
        return ENT_COMPAT;
86
    }
87
 
88
    /**
89
     * Sets the implementation of cache that should be used for cell collection.
90
     */
91
    public static function setCache(?CacheInterface $cache): void
92
    {
93
        self::$cache = $cache;
94
    }
95
 
96
    /**
97
     * Gets the implementation of cache that is being used for cell collection.
98
     */
99
    public static function getCache(): CacheInterface
100
    {
101
        if (!self::$cache) {
102
            self::$cache = self::useSimpleCacheVersion3() ? new Memory\SimpleCache3() : new Memory\SimpleCache1();
103
        }
104
 
105
        return self::$cache;
106
    }
107
 
108
    public static function useSimpleCacheVersion3(): bool
109
    {
110
        return (new ReflectionClass(CacheInterface::class))->getMethod('get')->getReturnType() !== null;
111
    }
112
 
113
    /**
114
     * Set the HTTP client implementation to be used for network request.
115
     */
116
    public static function setHttpClient(ClientInterface $httpClient, RequestFactoryInterface $requestFactory): void
117
    {
118
        self::$httpClient = $httpClient;
119
        self::$requestFactory = $requestFactory;
120
    }
121
 
122
    /**
123
     * Unset the HTTP client configuration.
124
     */
125
    public static function unsetHttpClient(): void
126
    {
127
        self::$httpClient = null;
128
        self::$requestFactory = null;
129
    }
130
 
131
    /**
132
     * Get the HTTP client implementation to be used for network request.
133
     */
134
    public static function getHttpClient(): ClientInterface
135
    {
136
        if (!self::$httpClient || !self::$requestFactory) {
137
            throw new Exception('HTTP client must be configured via Settings::setHttpClient() to be able to use WEBSERVICE function.');
138
        }
139
 
140
        return self::$httpClient;
141
    }
142
 
143
    /**
144
     * Get the HTTP request factory.
145
     */
146
    public static function getRequestFactory(): RequestFactoryInterface
147
    {
148
        if (!self::$httpClient || !self::$requestFactory) {
149
            throw new Exception('HTTP client must be configured via Settings::setHttpClient() to be able to use WEBSERVICE function.');
150
        }
151
 
152
        return self::$requestFactory;
153
    }
154
}