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] [Versions 400 and 402] [Versions 400 and 403]

   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 null|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       */
  51      public function __construct($isSupervisor = false)
  52      {
  53          // Supervisor?
  54          parent::__construct($isSupervisor);
  55  
  56          // Initialise values
  57          $this->color = new Color(Color::COLOR_BLACK, $isSupervisor);
  58  
  59          // bind parent if we are a supervisor
  60          if ($isSupervisor) {
  61              $this->color->bindParent($this, 'color');
  62          }
  63      }
  64  
  65      /**
  66       * Get the shared style component for the currently active cell in currently active sheet.
  67       * Only used for style supervisor.
  68       *
  69       * @return Border
  70       */
  71      public function getSharedComponent()
  72      {
  73          /** @var Style */
  74          $parent = $this->parent;
  75  
  76          /** @var Borders $sharedComponent */
  77          $sharedComponent = $parent->getSharedComponent();
  78          switch ($this->parentPropertyName) {
  79              case 'bottom':
  80                  return $sharedComponent->getBottom();
  81              case 'diagonal':
  82                  return $sharedComponent->getDiagonal();
  83              case 'left':
  84                  return $sharedComponent->getLeft();
  85              case 'right':
  86                  return $sharedComponent->getRight();
  87              case 'top':
  88                  return $sharedComponent->getTop();
  89          }
  90  
  91          throw new PhpSpreadsheetException('Cannot get shared component for a pseudo-border.');
  92      }
  93  
  94      /**
  95       * Build style array from subcomponents.
  96       *
  97       * @param array $array
  98       *
  99       * @return array
 100       */
 101      public function getStyleArray($array)
 102      {
 103          /** @var Style */
 104          $parent = $this->parent;
 105  
 106          return $parent->getStyleArray([$this->parentPropertyName => $array]);
 107      }
 108  
 109      /**
 110       * Apply styles from array.
 111       *
 112       * <code>
 113       * $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
 114       *        [
 115       *            'borderStyle' => Border::BORDER_DASHDOT,
 116       *            'color' => [
 117       *                'rgb' => '808080'
 118       *            ]
 119       *        ]
 120       * );
 121       * </code>
 122       *
 123       * @param array $styleArray Array containing style information
 124       *
 125       * @return $this
 126       */
 127      public function applyFromArray(array $styleArray)
 128      {
 129          if ($this->isSupervisor) {
 130              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
 131          } else {
 132              if (isset($styleArray['borderStyle'])) {
 133                  $this->setBorderStyle($styleArray['borderStyle']);
 134              }
 135              if (isset($styleArray['color'])) {
 136                  $this->getColor()->applyFromArray($styleArray['color']);
 137              }
 138          }
 139  
 140          return $this;
 141      }
 142  
 143      /**
 144       * Get Border style.
 145       *
 146       * @return string
 147       */
 148      public function getBorderStyle()
 149      {
 150          if ($this->isSupervisor) {
 151              return $this->getSharedComponent()->getBorderStyle();
 152          }
 153  
 154          return $this->borderStyle;
 155      }
 156  
 157      /**
 158       * Set Border style.
 159       *
 160       * @param bool|string $style
 161       *                            When passing a boolean, FALSE equates Border::BORDER_NONE
 162       *                                and TRUE to Border::BORDER_MEDIUM
 163       *
 164       * @return $this
 165       */
 166      public function setBorderStyle($style)
 167      {
 168          if (empty($style)) {
 169              $style = self::BORDER_NONE;
 170          } elseif (is_bool($style)) {
 171              $style = self::BORDER_MEDIUM;
 172          }
 173  
 174          if ($this->isSupervisor) {
 175              $styleArray = $this->getStyleArray(['borderStyle' => $style]);
 176              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 177          } else {
 178              $this->borderStyle = $style;
 179          }
 180  
 181          return $this;
 182      }
 183  
 184      /**
 185       * Get Border Color.
 186       *
 187       * @return Color
 188       */
 189      public function getColor()
 190      {
 191          return $this->color;
 192      }
 193  
 194      /**
 195       * Set Border Color.
 196       *
 197       * @return $this
 198       */
 199      public function setColor(Color $color)
 200      {
 201          // make sure parameter is a real color and not a supervisor
 202          $color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
 203  
 204          if ($this->isSupervisor) {
 205              $styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]);
 206              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 207          } else {
 208              $this->color = $color;
 209          }
 210  
 211          return $this;
 212      }
 213  
 214      /**
 215       * Get hash code.
 216       *
 217       * @return string Hash code
 218       */
 219      public function getHashCode()
 220      {
 221          if ($this->isSupervisor) {
 222              return $this->getSharedComponent()->getHashCode();
 223          }
 224  
 225          return md5(
 226              $this->borderStyle .
 227              $this->color->getHashCode() .
 228              __CLASS__
 229          );
 230      }
 231  
 232      protected function exportArray1(): array
 233      {
 234          $exportedArray = [];
 235          $this->exportArray2($exportedArray, 'borderStyle', $this->getBorderStyle());
 236          $this->exportArray2($exportedArray, 'color', $this->getColor());
 237  
 238          return $exportedArray;
 239      }
 240  }