Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401]

   1  <?php
   2  
   3  namespace Box\Spout\Writer\XLSX\Manager\Style;
   4  
   5  use Box\Spout\Common\Entity\Style\Style;
   6  
   7  /**
   8   * Class StyleRegistry
   9   * Registry for all used styles
  10   */
  11  class StyleRegistry extends \Box\Spout\Writer\Common\Manager\Style\StyleRegistry
  12  {
  13      /**
  14       * @var array
  15       */
  16      protected $registeredFills = [];
  17  
  18      /**
  19       * @var array [STYLE_ID] => [FILL_ID] maps a style to a fill declaration
  20       */
  21      protected $styleIdToFillMappingTable = [];
  22  
  23      /**
  24       * Excel preserves two default fills with index 0 and 1
  25       * Since Excel is the dominant vendor - we play along here
  26       *
  27       * @var int The fill index counter for custom fills.
  28       */
  29      protected $fillIndex = 2;
  30  
  31      /**
  32       * @var array
  33       */
  34      protected $registeredBorders = [];
  35  
  36      /**
  37       * @var array [STYLE_ID] => [BORDER_ID] maps a style to a border declaration
  38       */
  39      protected $styleIdToBorderMappingTable = [];
  40  
  41      /**
  42       * XLSX specific operations on the registered styles
  43       *
  44       * @param Style $style
  45       * @return Style
  46       */
  47      public function registerStyle(Style $style)
  48      {
  49          $registeredStyle = parent::registerStyle($style);
  50          $this->registerFill($registeredStyle);
  51          $this->registerBorder($registeredStyle);
  52  
  53          return $registeredStyle;
  54      }
  55  
  56      /**
  57       * Register a fill definition
  58       *
  59       * @param Style $style
  60       */
  61      private function registerFill(Style $style)
  62      {
  63          $styleId = $style->getId();
  64  
  65          // Currently - only solid backgrounds are supported
  66          // so $backgroundColor is a scalar value (RGB Color)
  67          $backgroundColor = $style->getBackgroundColor();
  68  
  69          if ($backgroundColor) {
  70              $isBackgroundColorRegistered = isset($this->registeredFills[$backgroundColor]);
  71  
  72              // We need to track the already registered background definitions
  73              if ($isBackgroundColorRegistered) {
  74                  $registeredStyleId = $this->registeredFills[$backgroundColor];
  75                  $registeredFillId = $this->styleIdToFillMappingTable[$registeredStyleId];
  76                  $this->styleIdToFillMappingTable[$styleId] = $registeredFillId;
  77              } else {
  78                  $this->registeredFills[$backgroundColor] = $styleId;
  79                  $this->styleIdToFillMappingTable[$styleId] = $this->fillIndex++;
  80              }
  81          } else {
  82              // The fillId maps a style to a fill declaration
  83              // When there is no background color definition - we default to 0
  84              $this->styleIdToFillMappingTable[$styleId] = 0;
  85          }
  86      }
  87  
  88      /**
  89       * @param int $styleId
  90       * @return int|null Fill ID associated to the given style ID
  91       */
  92      public function getFillIdForStyleId($styleId)
  93      {
  94          return (isset($this->styleIdToFillMappingTable[$styleId])) ?
  95              $this->styleIdToFillMappingTable[$styleId] :
  96              null;
  97      }
  98  
  99      /**
 100       * Register a border definition
 101       *
 102       * @param Style $style
 103       */
 104      private function registerBorder(Style $style)
 105      {
 106          $styleId = $style->getId();
 107  
 108          if ($style->shouldApplyBorder()) {
 109              $border = $style->getBorder();
 110              $serializedBorder = serialize($border);
 111  
 112              $isBorderAlreadyRegistered = isset($this->registeredBorders[$serializedBorder]);
 113  
 114              if ($isBorderAlreadyRegistered) {
 115                  $registeredStyleId = $this->registeredBorders[$serializedBorder];
 116                  $registeredBorderId = $this->styleIdToBorderMappingTable[$registeredStyleId];
 117                  $this->styleIdToBorderMappingTable[$styleId] = $registeredBorderId;
 118              } else {
 119                  $this->registeredBorders[$serializedBorder] = $styleId;
 120                  $this->styleIdToBorderMappingTable[$styleId] = count($this->registeredBorders);
 121              }
 122          } else {
 123              // If no border should be applied - the mapping is the default border: 0
 124              $this->styleIdToBorderMappingTable[$styleId] = 0;
 125          }
 126      }
 127  
 128      /**
 129       * @param int $styleId
 130       * @return int|null Fill ID associated to the given style ID
 131       */
 132      public function getBorderIdForStyleId($styleId)
 133      {
 134          return (isset($this->styleIdToBorderMappingTable[$styleId])) ?
 135              $this->styleIdToBorderMappingTable[$styleId] :
 136              null;
 137      }
 138  
 139      /**
 140       * @return array
 141       */
 142      public function getRegisteredFills()
 143      {
 144          return $this->registeredFills;
 145      }
 146  
 147      /**
 148       * @return array
 149       */
 150      public function getRegisteredBorders()
 151      {
 152          return $this->registeredBorders;
 153      }
 154  }