Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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 int
  32       */
  33      public $startcolorIndex;
  34  
  35      /**
  36       * @var int
  37       */
  38      public $endcolorIndex;
  39  
  40      /**
  41       * Fill type.
  42       *
  43       * @var string
  44       */
  45      protected $fillType = self::FILL_NONE;
  46  
  47      /**
  48       * Rotation.
  49       *
  50       * @var float
  51       */
  52      protected $rotation = 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      /**
  69       * Create a new Fill.
  70       *
  71       * @param bool $isSupervisor Flag indicating if this is a supervisor or not
  72       *                                    Leave this value at default unless you understand exactly what
  73       *                                        its ramifications are
  74       * @param bool $isConditional Flag indicating if this is a conditional style or not
  75       *                                    Leave this value at default unless you understand exactly what
  76       *                                        its ramifications are
  77       */
  78      public function __construct($isSupervisor = false, $isConditional = false)
  79      {
  80          // Supervisor?
  81          parent::__construct($isSupervisor);
  82  
  83          // Initialise values
  84          if ($isConditional) {
  85              $this->fillType = null;
  86          }
  87          $this->startColor = new Color(Color::COLOR_WHITE, $isSupervisor, $isConditional);
  88          $this->endColor = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional);
  89  
  90          // bind parent if we are a supervisor
  91          if ($isSupervisor) {
  92              $this->startColor->bindParent($this, 'startColor');
  93              $this->endColor->bindParent($this, 'endColor');
  94          }
  95      }
  96  
  97      /**
  98       * Get the shared style component for the currently active cell in currently active sheet.
  99       * Only used for style supervisor.
 100       *
 101       * @return Fill
 102       */
 103      public function getSharedComponent()
 104      {
 105          return $this->parent->getSharedComponent()->getFill();
 106      }
 107  
 108      /**
 109       * Build style array from subcomponents.
 110       *
 111       * @param array $array
 112       *
 113       * @return array
 114       */
 115      public function getStyleArray($array)
 116      {
 117          return ['fill' => $array];
 118      }
 119  
 120      /**
 121       * Apply styles from array.
 122       *
 123       * <code>
 124       * $spreadsheet->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
 125       *     [
 126       *         'fillType' => Fill::FILL_GRADIENT_LINEAR,
 127       *         'rotation' => 0,
 128       *         'startColor' => [
 129       *             'rgb' => '000000'
 130       *         ],
 131       *         'endColor' => [
 132       *             'argb' => 'FFFFFFFF'
 133       *         ]
 134       *     ]
 135       * );
 136       * </code>
 137       *
 138       * @param array $pStyles Array containing style information
 139       *
 140       * @return $this
 141       */
 142      public function applyFromArray(array $pStyles)
 143      {
 144          if ($this->isSupervisor) {
 145              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
 146          } else {
 147              if (isset($pStyles['fillType'])) {
 148                  $this->setFillType($pStyles['fillType']);
 149              }
 150              if (isset($pStyles['rotation'])) {
 151                  $this->setRotation($pStyles['rotation']);
 152              }
 153              if (isset($pStyles['startColor'])) {
 154                  $this->getStartColor()->applyFromArray($pStyles['startColor']);
 155              }
 156              if (isset($pStyles['endColor'])) {
 157                  $this->getEndColor()->applyFromArray($pStyles['endColor']);
 158              }
 159              if (isset($pStyles['color'])) {
 160                  $this->getStartColor()->applyFromArray($pStyles['color']);
 161                  $this->getEndColor()->applyFromArray($pStyles['color']);
 162              }
 163          }
 164  
 165          return $this;
 166      }
 167  
 168      /**
 169       * Get Fill Type.
 170       *
 171       * @return string
 172       */
 173      public function getFillType()
 174      {
 175          if ($this->isSupervisor) {
 176              return $this->getSharedComponent()->getFillType();
 177          }
 178  
 179          return $this->fillType;
 180      }
 181  
 182      /**
 183       * Set Fill Type.
 184       *
 185       * @param string $pValue Fill type, see self::FILL_*
 186       *
 187       * @return $this
 188       */
 189      public function setFillType($pValue)
 190      {
 191          if ($this->isSupervisor) {
 192              $styleArray = $this->getStyleArray(['fillType' => $pValue]);
 193              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 194          } else {
 195              $this->fillType = $pValue;
 196          }
 197  
 198          return $this;
 199      }
 200  
 201      /**
 202       * Get Rotation.
 203       *
 204       * @return float
 205       */
 206      public function getRotation()
 207      {
 208          if ($this->isSupervisor) {
 209              return $this->getSharedComponent()->getRotation();
 210          }
 211  
 212          return $this->rotation;
 213      }
 214  
 215      /**
 216       * Set Rotation.
 217       *
 218       * @param float $pValue
 219       *
 220       * @return $this
 221       */
 222      public function setRotation($pValue)
 223      {
 224          if ($this->isSupervisor) {
 225              $styleArray = $this->getStyleArray(['rotation' => $pValue]);
 226              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 227          } else {
 228              $this->rotation = $pValue;
 229          }
 230  
 231          return $this;
 232      }
 233  
 234      /**
 235       * Get Start Color.
 236       *
 237       * @return Color
 238       */
 239      public function getStartColor()
 240      {
 241          return $this->startColor;
 242      }
 243  
 244      /**
 245       * Set Start Color.
 246       *
 247       * @return $this
 248       */
 249      public function setStartColor(Color $pValue)
 250      {
 251          // make sure parameter is a real color and not a supervisor
 252          $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
 253  
 254          if ($this->isSupervisor) {
 255              $styleArray = $this->getStartColor()->getStyleArray(['argb' => $color->getARGB()]);
 256              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 257          } else {
 258              $this->startColor = $color;
 259          }
 260  
 261          return $this;
 262      }
 263  
 264      /**
 265       * Get End Color.
 266       *
 267       * @return Color
 268       */
 269      public function getEndColor()
 270      {
 271          return $this->endColor;
 272      }
 273  
 274      /**
 275       * Set End Color.
 276       *
 277       * @return $this
 278       */
 279      public function setEndColor(Color $pValue)
 280      {
 281          // make sure parameter is a real color and not a supervisor
 282          $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
 283  
 284          if ($this->isSupervisor) {
 285              $styleArray = $this->getEndColor()->getStyleArray(['argb' => $color->getARGB()]);
 286              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 287          } else {
 288              $this->endColor = $color;
 289          }
 290  
 291          return $this;
 292      }
 293  
 294      /**
 295       * Get hash code.
 296       *
 297       * @return string Hash code
 298       */
 299      public function getHashCode()
 300      {
 301          if ($this->isSupervisor) {
 302              return $this->getSharedComponent()->getHashCode();
 303          }
 304          // Note that we don't care about colours for fill type NONE, but could have duplicate NONEs with
 305          //  different hashes if we don't explicitly prevent this
 306          return md5(
 307              $this->getFillType() .
 308              $this->getRotation() .
 309              ($this->getFillType() !== self::FILL_NONE ? $this->getStartColor()->getHashCode() : '') .
 310              ($this->getFillType() !== self::FILL_NONE ? $this->getEndColor()->getHashCode() : '') .
 311              __CLASS__
 312          );
 313      }
 314  
 315      protected function exportArray1(): array
 316      {
 317          $exportedArray = [];
 318          $this->exportArray2($exportedArray, 'endColor', $this->getEndColor());
 319          $this->exportArray2($exportedArray, 'fillType', $this->getFillType());
 320          $this->exportArray2($exportedArray, 'rotation', $this->getRotation());
 321          $this->exportArray2($exportedArray, 'startColor', $this->getStartColor());
 322  
 323          return $exportedArray;
 324      }
 325  }