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  use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
   6  
   7  class Border extends Supervisor
   8  {
   9      // Border style
  10      const BORDER_NONE = 'none';
  11      const BORDER_DASHDOT = 'dashDot';
  12      const BORDER_DASHDOTDOT = 'dashDotDot';
  13      const BORDER_DASHED = 'dashed';
  14      const BORDER_DOTTED = 'dotted';
  15      const BORDER_DOUBLE = 'double';
  16      const BORDER_HAIR = 'hair';
  17      const BORDER_MEDIUM = 'medium';
  18      const BORDER_MEDIUMDASHDOT = 'mediumDashDot';
  19      const BORDER_MEDIUMDASHDOTDOT = 'mediumDashDotDot';
  20      const BORDER_MEDIUMDASHED = 'mediumDashed';
  21      const BORDER_SLANTDASHDOT = 'slantDashDot';
  22      const BORDER_THICK = 'thick';
  23      const BORDER_THIN = 'thin';
  24  
  25      /**
  26       * Border style.
  27       *
  28       * @var string
  29       */
  30      protected $borderStyle = self::BORDER_NONE;
  31  
  32      /**
  33       * Border color.
  34       *
  35       * @var Color
  36       */
  37      protected $color;
  38  
  39      /**
  40       * @var int
  41       */
  42      public $colorIndex;
  43  
  44      /**
  45       * Create a new Border.
  46       *
  47       * @param bool $isSupervisor Flag indicating if this is a supervisor or not
  48       *                                    Leave this value at default unless you understand exactly what
  49       *                                        its ramifications are
  50       * @param bool $isConditional Flag indicating if this is a conditional style or not
  51       *                                    Leave this value at default unless you understand exactly what
  52       *                                        its ramifications are
  53       */
  54      public function __construct($isSupervisor = false, $isConditional = false)
  55      {
  56          // Supervisor?
  57          parent::__construct($isSupervisor);
  58  
  59          // Initialise values
  60          $this->color = new Color(Color::COLOR_BLACK, $isSupervisor);
  61  
  62          // bind parent if we are a supervisor
  63          if ($isSupervisor) {
  64              $this->color->bindParent($this, 'color');
  65          }
  66      }
  67  
  68      /**
  69       * Get the shared style component for the currently active cell in currently active sheet.
  70       * Only used for style supervisor.
  71       *
  72       * @return Border
  73       */
  74      public function getSharedComponent()
  75      {
  76          switch ($this->parentPropertyName) {
  77              case 'bottom':
  78                  return $this->parent->getSharedComponent()->getBottom();
  79              case 'diagonal':
  80                  return $this->parent->getSharedComponent()->getDiagonal();
  81              case 'left':
  82                  return $this->parent->getSharedComponent()->getLeft();
  83              case 'right':
  84                  return $this->parent->getSharedComponent()->getRight();
  85              case 'top':
  86                  return $this->parent->getSharedComponent()->getTop();
  87          }
  88  
  89          throw new PhpSpreadsheetException('Cannot get shared component for a pseudo-border.');
  90      }
  91  
  92      /**
  93       * Build style array from subcomponents.
  94       *
  95       * @param array $array
  96       *
  97       * @return array
  98       */
  99      public function getStyleArray($array)
 100      {
 101          return $this->parent->getStyleArray([$this->parentPropertyName => $array]);
 102      }
 103  
 104      /**
 105       * Apply styles from array.
 106       *
 107       * <code>
 108       * $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
 109       *        [
 110       *            'borderStyle' => Border::BORDER_DASHDOT,
 111       *            'color' => [
 112       *                'rgb' => '808080'
 113       *            ]
 114       *        ]
 115       * );
 116       * </code>
 117       *
 118       * @param array $pStyles Array containing style information
 119       *
 120       * @return $this
 121       */
 122      public function applyFromArray(array $pStyles)
 123      {
 124          if ($this->isSupervisor) {
 125              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
 126          } else {
 127              if (isset($pStyles['borderStyle'])) {
 128                  $this->setBorderStyle($pStyles['borderStyle']);
 129              }
 130              if (isset($pStyles['color'])) {
 131                  $this->getColor()->applyFromArray($pStyles['color']);
 132              }
 133          }
 134  
 135          return $this;
 136      }
 137  
 138      /**
 139       * Get Border style.
 140       *
 141       * @return string
 142       */
 143      public function getBorderStyle()
 144      {
 145          if ($this->isSupervisor) {
 146              return $this->getSharedComponent()->getBorderStyle();
 147          }
 148  
 149          return $this->borderStyle;
 150      }
 151  
 152      /**
 153       * Set Border style.
 154       *
 155       * @param bool|string $pValue
 156       *                            When passing a boolean, FALSE equates Border::BORDER_NONE
 157       *                                and TRUE to Border::BORDER_MEDIUM
 158       *
 159       * @return $this
 160       */
 161      public function setBorderStyle($pValue)
 162      {
 163          if (empty($pValue)) {
 164              $pValue = self::BORDER_NONE;
 165          } elseif (is_bool($pValue) && $pValue) {
 166              $pValue = self::BORDER_MEDIUM;
 167          }
 168          if ($this->isSupervisor) {
 169              $styleArray = $this->getStyleArray(['borderStyle' => $pValue]);
 170              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 171          } else {
 172              $this->borderStyle = $pValue;
 173          }
 174  
 175          return $this;
 176      }
 177  
 178      /**
 179       * Get Border Color.
 180       *
 181       * @return Color
 182       */
 183      public function getColor()
 184      {
 185          return $this->color;
 186      }
 187  
 188      /**
 189       * Set Border Color.
 190       *
 191       * @return $this
 192       */
 193      public function setColor(Color $pValue)
 194      {
 195          // make sure parameter is a real color and not a supervisor
 196          $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
 197  
 198          if ($this->isSupervisor) {
 199              $styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]);
 200              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 201          } else {
 202              $this->color = $color;
 203          }
 204  
 205          return $this;
 206      }
 207  
 208      /**
 209       * Get hash code.
 210       *
 211       * @return string Hash code
 212       */
 213      public function getHashCode()
 214      {
 215          if ($this->isSupervisor) {
 216              return $this->getSharedComponent()->getHashCode();
 217          }
 218  
 219          return md5(
 220              $this->borderStyle .
 221              $this->color->getHashCode() .
 222              __CLASS__
 223          );
 224      }
 225  
 226      protected function exportArray1(): array
 227      {
 228          $exportedArray = [];
 229          $this->exportArray2($exportedArray, 'borderStyle', $this->getBorderStyle());
 230          $this->exportArray2($exportedArray, 'color', $this->getColor());
 231  
 232          return $exportedArray;
 233      }
 234  }