Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   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  
  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;
  28  
  29      /**
  30       * The cache implementation to be used for cell collection.
  31       *
  32       * @var CacheInterface
  33       */
  34      private static $cache;
  35  
  36      /**
  37       * The HTTP client implementation to be used for network request.
  38       *
  39       * @var null|ClientInterface
  40       */
  41      private static $httpClient;
  42  
  43      /**
  44       * @var null|RequestFactoryInterface
  45       */
  46      private static $requestFactory;
  47  
  48      /**
  49       * Set the locale code to use for formula translations and any special formatting.
  50       *
  51       * @param string $locale The locale code to use (e.g. "fr" or "pt_br" or "en_uk")
  52       *
  53       * @return bool Success or failure
  54       */
  55      public static function setLocale(string $locale)
  56      {
  57          return Calculation::getInstance()->setLocale($locale);
  58      }
  59  
  60      public static function getLocale(): string
  61      {
  62          return Calculation::getInstance()->getLocale();
  63      }
  64  
  65      /**
  66       * Identify to PhpSpreadsheet the external library to use for rendering charts.
  67       *
  68       * @param string $rendererClassName Class name of the chart renderer
  69       *    eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
  70       */
  71      public static function setChartRenderer(string $rendererClassName): void
  72      {
  73          if (!is_a($rendererClassName, IRenderer::class, true)) {
  74              throw new Exception('Chart renderer must implement ' . IRenderer::class);
  75          }
  76  
  77          self::$chartRenderer = $rendererClassName;
  78      }
  79  
  80      /**
  81       * Return the Chart Rendering Library that PhpSpreadsheet is currently configured to use.
  82       *
  83       * @return null|string Class name of the chart renderer
  84       *    eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
  85       */
  86      public static function getChartRenderer(): ?string
  87      {
  88          return self::$chartRenderer;
  89      }
  90  
  91      public static function htmlEntityFlags(): int
  92      {
  93          return \ENT_COMPAT;
  94      }
  95  
  96      /**
  97       * Set default options for libxml loader.
  98       *
  99       * @param int $options Default options for libxml loader
 100       */
 101      public static function setLibXmlLoaderOptions($options): void
 102      {
 103          if ($options === null && defined('LIBXML_DTDLOAD')) {
 104              $options = LIBXML_DTDLOAD | LIBXML_DTDATTR;
 105          }
 106          self::$libXmlLoaderOptions = $options;
 107      }
 108  
 109      /**
 110       * Get default options for libxml loader.
 111       * Defaults to LIBXML_DTDLOAD | LIBXML_DTDATTR when not set explicitly.
 112       *
 113       * @return int Default options for libxml loader
 114       */
 115      public static function getLibXmlLoaderOptions(): int
 116      {
 117          if (self::$libXmlLoaderOptions === null && defined('LIBXML_DTDLOAD')) {
 118              self::setLibXmlLoaderOptions(LIBXML_DTDLOAD | LIBXML_DTDATTR);
 119          } elseif (self::$libXmlLoaderOptions === null) {
 120              self::$libXmlLoaderOptions = 0;
 121          }
 122  
 123          return self::$libXmlLoaderOptions;
 124      }
 125  
 126      /**
 127       * Deprecated, has no effect.
 128       *
 129       * @param bool $state
 130       *
 131       * @deprecated will be removed without replacement as it is no longer necessary on PHP 7.3.0+
 132       */
 133      public static function setLibXmlDisableEntityLoader($state): void
 134      {
 135          // noop
 136      }
 137  
 138      /**
 139       * Deprecated, has no effect.
 140       *
 141       * @return bool $state
 142       *
 143       * @deprecated will be removed without replacement as it is no longer necessary on PHP 7.3.0+
 144       */
 145      public static function getLibXmlDisableEntityLoader(): bool
 146      {
 147          return true;
 148      }
 149  
 150      /**
 151       * Sets the implementation of cache that should be used for cell collection.
 152       */
 153      public static function setCache(CacheInterface $cache): void
 154      {
 155          self::$cache = $cache;
 156      }
 157  
 158      /**
 159       * Gets the implementation of cache that is being used for cell collection.
 160       */
 161      public static function getCache(): CacheInterface
 162      {
 163          if (!self::$cache) {
 164              self::$cache = new Memory();
 165          }
 166  
 167          return self::$cache;
 168      }
 169  
 170      /**
 171       * Set the HTTP client implementation to be used for network request.
 172       */
 173      public static function setHttpClient(ClientInterface $httpClient, RequestFactoryInterface $requestFactory): void
 174      {
 175          self::$httpClient = $httpClient;
 176          self::$requestFactory = $requestFactory;
 177      }
 178  
 179      /**
 180       * Unset the HTTP client configuration.
 181       */
 182      public static function unsetHttpClient(): void
 183      {
 184          self::$httpClient = null;
 185          self::$requestFactory = null;
 186      }
 187  
 188      /**
 189       * Get the HTTP client implementation to be used for network request.
 190       */
 191      public static function getHttpClient(): ClientInterface
 192      {
 193          self::assertHttpClient();
 194  
 195          return self::$httpClient;
 196      }
 197  
 198      /**
 199       * Get the HTTP request factory.
 200       */
 201      public static function getRequestFactory(): RequestFactoryInterface
 202      {
 203          self::assertHttpClient();
 204  
 205          return self::$requestFactory;
 206      }
 207  
 208      private static function assertHttpClient(): void
 209      {
 210          if (!self::$httpClient || !self::$requestFactory) {
 211              throw new Exception('HTTP client must be configured via Settings::setHttpClient() to be able to use WEBSERVICE function.');
 212          }
 213      }
 214  }