Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
   4  
   5  use NumberFormatter;
   6  use PhpOffice\PhpSpreadsheet\Exception;
   7  
   8  final class Locale
   9  {
  10      /**
  11       * Language code: ISO-639 2 character, alpha.
  12       * Optional script code: ISO-15924 4 alpha.
  13       * Optional country code: ISO-3166-1, 2 character alpha.
  14       * Separated by underscores or dashes.
  15       */
  16      public const STRUCTURE = '/^(?P<language>[a-z]{2})([-_](?P<script>[a-z]{4}))?([-_](?P<country>[a-z]{2}))?$/i';
  17  
  18      private NumberFormatter $formatter;
  19  
  20      public function __construct(?string $locale, int $style)
  21      {
  22          if (class_exists(NumberFormatter::class) === false) {
  23              throw new Exception();
  24          }
  25  
  26          $formatterLocale = str_replace('-', '_', $locale ?? '');
  27          $this->formatter = new NumberFormatter($formatterLocale, $style);
  28          if ($this->formatter->getLocale() !== $formatterLocale) {
  29              throw new Exception("Unable to read locale data for '{$locale}'");
  30          }
  31      }
  32  
  33      public function format(): string
  34      {
  35          return $this->formatter->getPattern();
  36      }
  37  }