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]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Style;
   4  
   5  class Fill extends Supervisor
   6  {
   7      // Fill types
   8      const FILL_NONE = 'none';
   9      const FILL_SOLID = 'solid';
  10      const FILL_GRADIENT_LINEAR = 'linear';
  11      const FILL_GRADIENT_PATH = 'path';
  12      const FILL_PATTERN_DARKDOWN = 'darkDown';
  13      const FILL_PATTERN_DARKGRAY = 'darkGray';
  14      const FILL_PATTERN_DARKGRID = 'darkGrid';
  15      const FILL_PATTERN_DARKHORIZONTAL = 'darkHorizontal';
  16      const FILL_PATTERN_DARKTRELLIS = 'darkTrellis';
  17      const FILL_PATTERN_DARKUP = 'darkUp';
  18      const FILL_PATTERN_DARKVERTICAL = 'darkVertical';
  19      const FILL_PATTERN_GRAY0625 = 'gray0625';
  20      const FILL_PATTERN_GRAY125 = 'gray125';
  21      const FILL_PATTERN_LIGHTDOWN = 'lightDown';
  22      const FILL_PATTERN_LIGHTGRAY = 'lightGray';
  23      const FILL_PATTERN_LIGHTGRID = 'lightGrid';
  24      const FILL_PATTERN_LIGHTHORIZONTAL = 'lightHorizontal';
  25      const FILL_PATTERN_LIGHTTRELLIS = 'lightTrellis';
  26      const FILL_PATTERN_LIGHTUP = 'lightUp';
  27      const FILL_PATTERN_LIGHTVERTICAL = 'lightVertical';
  28      const FILL_PATTERN_MEDIUMGRAY = 'mediumGray';
  29  
  30      /**
  31       * @var null|int
  32       */
  33      public $startcolorIndex;
  34  
  35      /**
  36       * @var null|int
  37       */
  38      public $endcolorIndex;
  39  
  40      /**
  41       * Fill type.
  42       *
  43       * @var null|string
  44       */
  45      protected $fillType = self::FILL_NONE;
  46  
  47      /**
  48       * Rotation.
  49       *
  50       * @var float
  51       */
  52      protected $rotation = 0.0;
  53  
  54      /**
  55       * Start color.
  56       *
  57       * @var Color
  58       */
  59      protected $startColor;
  60  
  61      /**
  62       * End color.
  63       *
  64       * @var Color
  65       */
  66      protected $endColor;
  67  
  68      /** @var bool */
  69      private $colorChanged = false;
  70  
  71      /**
  72       * Create a new Fill.
  73       *
  74       * @param bool $isSupervisor Flag indicating if this is a supervisor or not
  75       *                                    Leave this value at default unless you understand exactly what
  76       *                                        its ramifications are
  77       * @param bool $isConditional Flag indicating if this is a conditional style or not
  78       *                                    Leave this value at default unless you understand exactly what
  79       *                                        its ramifications are
  80       */
  81      public function __construct($isSupervisor = false, $isConditional = false)
  82      {
  83          // Supervisor?
  84          parent::__construct($isSupervisor);
  85  
  86          // Initialise values
  87          if ($isConditional) {
  88              $this->fillType = null;
  89          }
  90          $this->startColor = new Color(Color::COLOR_WHITE, $isSupervisor, $isConditional);
  91          $this->endColor = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional);
  92  
  93          // bind parent if we are a supervisor
  94          if ($isSupervisor) {
  95              $this->startColor->bindParent($this, 'startColor');
  96              $this->endColor->bindParent($this, 'endColor');
  97          }
  98      }
  99  
 100      /**
 101       * Get the shared style component for the currently active cell in currently active sheet.
 102       * Only used for style supervisor.
 103       *
 104       * @return Fill
 105       */
 106      public function getSharedComponent()
 107      {
 108          /** @var Style */
 109          $parent = $this->parent;
 110  
 111          return $parent->getSharedComponent()->getFill();
 112      }
 113  
 114      /**
 115       * Build style array from subcomponents.
 116       *
 117       * @param array $array
 118       *
 119       * @return array
 120       */
 121      public function getStyleArray($array)
 122      {
 123          return ['fill' => $array];
 124      }
 125  
 126      /**
 127       * Apply styles from array.
 128       *
 129       * <code>
 130       * $spreadsheet->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
 131       *     [
 132       *         'fillType' => Fill::FILL_GRADIENT_LINEAR,
 133       *         'rotation' => 0.0,
 134       *         'startColor' => [
 135       *             'rgb' => '000000'
 136       *         ],
 137       *         'endColor' => [
 138       *             'argb' => 'FFFFFFFF'
 139       *         ]
 140       *     ]
 141       * );
 142       * </code>
 143       *
 144       * @param array $styleArray Array containing style information
 145       *
 146       * @return $this
 147       */
 148      public function applyFromArray(array $styleArray)
 149      {
 150          if ($this->isSupervisor) {
 151              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
 152          } else {
 153              if (isset($styleArray['fillType'])) {
 154                  $this->setFillType($styleArray['fillType']);
 155              }
 156              if (isset($styleArray['rotation'])) {
 157                  $this->setRotation($styleArray['rotation']);
 158              }
 159              if (isset($styleArray['startColor'])) {
 160                  $this->getStartColor()->applyFromArray($styleArray['startColor']);
 161              }
 162              if (isset($styleArray['endColor'])) {
 163                  $this->getEndColor()->applyFromArray($styleArray['endColor']);
 164              }
 165              if (isset($styleArray['color'])) {
 166                  $this->getStartColor()->applyFromArray($styleArray['color']);
 167                  $this->getEndColor()->applyFromArray($styleArray['color']);
 168              }
 169          }
 170  
 171          return $this;
 172      }
 173  
 174      /**
 175       * Get Fill Type.
 176       *
 177       * @return null|string
 178       */
 179      public function getFillType()
 180      {
 181          if ($this->isSupervisor) {
 182              return $this->getSharedComponent()->getFillType();
 183          }
 184  
 185          return $this->fillType;
 186      }
 187  
 188      /**
 189       * Set Fill Type.
 190       *
 191       * @param string $fillType Fill type, see self::FILL_*
 192       *
 193       * @return $this
 194       */
 195      public function setFillType($fillType)
 196      {
 197          if ($this->isSupervisor) {
 198              $styleArray = $this->getStyleArray(['fillType' => $fillType]);
 199              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 200          } else {
 201              $this->fillType = $fillType;
 202          }
 203  
 204          return $this;
 205      }
 206  
 207      /**
 208       * Get Rotation.
 209       *
 210       * @return float
 211       */
 212      public function getRotation()
 213      {
 214          if ($this->isSupervisor) {
 215              return $this->getSharedComponent()->getRotation();
 216          }
 217  
 218          return $this->rotation;
 219      }
 220  
 221      /**
 222       * Set Rotation.
 223       *
 224       * @param float $angleInDegrees
 225       *
 226       * @return $this
 227       */
 228      public function setRotation($angleInDegrees)
 229      {
 230          if ($this->isSupervisor) {
 231              $styleArray = $this->getStyleArray(['rotation' => $angleInDegrees]);
 232              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 233          } else {
 234              $this->rotation = $angleInDegrees;
 235          }
 236  
 237          return $this;
 238      }
 239  
 240      /**
 241       * Get Start Color.
 242       *
 243       * @return Color
 244       */
 245      public function getStartColor()
 246      {
 247          return $this->startColor;
 248      }
 249  
 250      /**
 251       * Set Start Color.
 252       *
 253       * @return $this
 254       */
 255      public function setStartColor(Color $color)
 256      {
 257          $this->colorChanged = true;
 258          // make sure parameter is a real color and not a supervisor
 259          $color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
 260  
 261          if ($this->isSupervisor) {
 262              $styleArray = $this->getStartColor()->getStyleArray(['argb' => $color->getARGB()]);
 263              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 264          } else {
 265              $this->startColor = $color;
 266          }
 267  
 268          return $this;
 269      }
 270  
 271      /**
 272       * Get End Color.
 273       *
 274       * @return Color
 275       */
 276      public function getEndColor()
 277      {
 278          return $this->endColor;
 279      }
 280  
 281      /**
 282       * Set End Color.
 283       *
 284       * @return $this
 285       */
 286      public function setEndColor(Color $color)
 287      {
 288          $this->colorChanged = true;
 289          // make sure parameter is a real color and not a supervisor
 290          $color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
 291  
 292          if ($this->isSupervisor) {
 293              $styleArray = $this->getEndColor()->getStyleArray(['argb' => $color->getARGB()]);
 294              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 295          } else {
 296              $this->endColor = $color;
 297          }
 298  
 299          return $this;
 300      }
 301  
 302      public function getColorsChanged(): bool
 303      {
 304          if ($this->isSupervisor) {
 305              $changed = $this->getSharedComponent()->colorChanged;
 306          } else {
 307              $changed = $this->colorChanged;
 308          }
 309  
 310          return $changed || $this->startColor->getHasChanged() || $this->endColor->getHasChanged();
 311      }
 312  
 313      /**
 314       * Get hash code.
 315       *
 316       * @return string Hash code
 317       */
 318      public function getHashCode()
 319      {
 320          if ($this->isSupervisor) {
 321              return $this->getSharedComponent()->getHashCode();
 322          }
 323          // Note that we don't care about colours for fill type NONE, but could have duplicate NONEs with
 324          //  different hashes if we don't explicitly prevent this
 325          return md5(
 326              $this->getFillType() .
 327              $this->getRotation() .
 328              ($this->getFillType() !== self::FILL_NONE ? $this->getStartColor()->getHashCode() : '') .
 329              ($this->getFillType() !== self::FILL_NONE ? $this->getEndColor()->getHashCode() : '') .
 330              ((string) $this->getColorsChanged()) .
 331              __CLASS__
 332          );
 333      }
 334  
 335      protected function exportArray1(): array
 336      {
 337          $exportedArray = [];
 338          $this->exportArray2($exportedArray, 'fillType', $this->getFillType());
 339          $this->exportArray2($exportedArray, 'rotation', $this->getRotation());
 340          if ($this->getColorsChanged()) {
 341              $this->exportArray2($exportedArray, 'endColor', $this->getEndColor());
 342              $this->exportArray2($exportedArray, 'startColor', $this->getStartColor());
 343          }
 344  
 345          return $exportedArray;
 346      }
 347  }