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