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 Borders extends Supervisor
   8  {
   9      // Diagonal directions
  10      const DIAGONAL_NONE = 0;
  11      const DIAGONAL_UP = 1;
  12      const DIAGONAL_DOWN = 2;
  13      const DIAGONAL_BOTH = 3;
  14  
  15      /**
  16       * Left.
  17       *
  18       * @var Border
  19       */
  20      protected $left;
  21  
  22      /**
  23       * Right.
  24       *
  25       * @var Border
  26       */
  27      protected $right;
  28  
  29      /**
  30       * Top.
  31       *
  32       * @var Border
  33       */
  34      protected $top;
  35  
  36      /**
  37       * Bottom.
  38       *
  39       * @var Border
  40       */
  41      protected $bottom;
  42  
  43      /**
  44       * Diagonal.
  45       *
  46       * @var Border
  47       */
  48      protected $diagonal;
  49  
  50      /**
  51       * DiagonalDirection.
  52       *
  53       * @var int
  54       */
  55      protected $diagonalDirection;
  56  
  57      /**
  58       * All borders pseudo-border. Only applies to supervisor.
  59       *
  60       * @var Border
  61       */
  62      protected $allBorders;
  63  
  64      /**
  65       * Outline pseudo-border. Only applies to supervisor.
  66       *
  67       * @var Border
  68       */
  69      protected $outline;
  70  
  71      /**
  72       * Inside pseudo-border. Only applies to supervisor.
  73       *
  74       * @var Border
  75       */
  76      protected $inside;
  77  
  78      /**
  79       * Vertical pseudo-border. Only applies to supervisor.
  80       *
  81       * @var Border
  82       */
  83      protected $vertical;
  84  
  85      /**
  86       * Horizontal pseudo-border. Only applies to supervisor.
  87       *
  88       * @var Border
  89       */
  90      protected $horizontal;
  91  
  92      /**
  93       * Create a new Borders.
  94       *
  95       * @param bool $isSupervisor Flag indicating if this is a supervisor or not
  96       *                                    Leave this value at default unless you understand exactly what
  97       *                                        its ramifications are
  98       * @param bool $isConditional Flag indicating if this is a conditional style or not
  99       *                                    Leave this value at default unless you understand exactly what
 100       *                                        its ramifications are
 101       */
 102      public function __construct($isSupervisor = false, $isConditional = false)
 103      {
 104          // Supervisor?
 105          parent::__construct($isSupervisor);
 106  
 107          // Initialise values
 108          $this->left = new Border($isSupervisor, $isConditional);
 109          $this->right = new Border($isSupervisor, $isConditional);
 110          $this->top = new Border($isSupervisor, $isConditional);
 111          $this->bottom = new Border($isSupervisor, $isConditional);
 112          $this->diagonal = new Border($isSupervisor, $isConditional);
 113          $this->diagonalDirection = self::DIAGONAL_NONE;
 114  
 115          // Specially for supervisor
 116          if ($isSupervisor) {
 117              // Initialize pseudo-borders
 118              $this->allBorders = new Border(true);
 119              $this->outline = new Border(true);
 120              $this->inside = new Border(true);
 121              $this->vertical = new Border(true);
 122              $this->horizontal = new Border(true);
 123  
 124              // bind parent if we are a supervisor
 125              $this->left->bindParent($this, 'left');
 126              $this->right->bindParent($this, 'right');
 127              $this->top->bindParent($this, 'top');
 128              $this->bottom->bindParent($this, 'bottom');
 129              $this->diagonal->bindParent($this, 'diagonal');
 130              $this->allBorders->bindParent($this, 'allBorders');
 131              $this->outline->bindParent($this, 'outline');
 132              $this->inside->bindParent($this, 'inside');
 133              $this->vertical->bindParent($this, 'vertical');
 134              $this->horizontal->bindParent($this, 'horizontal');
 135          }
 136      }
 137  
 138      /**
 139       * Get the shared style component for the currently active cell in currently active sheet.
 140       * Only used for style supervisor.
 141       *
 142       * @return Borders
 143       */
 144      public function getSharedComponent()
 145      {
 146          return $this->parent->getSharedComponent()->getBorders();
 147      }
 148  
 149      /**
 150       * Build style array from subcomponents.
 151       *
 152       * @param array $array
 153       *
 154       * @return array
 155       */
 156      public function getStyleArray($array)
 157      {
 158          return ['borders' => $array];
 159      }
 160  
 161      /**
 162       * Apply styles from array.
 163       *
 164       * <code>
 165       * $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
 166       *         [
 167       *             'bottom' => [
 168       *                 'borderStyle' => Border::BORDER_DASHDOT,
 169       *                 'color' => [
 170       *                     'rgb' => '808080'
 171       *                 ]
 172       *             ],
 173       *             'top' => [
 174       *                 'borderStyle' => Border::BORDER_DASHDOT,
 175       *                 'color' => [
 176       *                     'rgb' => '808080'
 177       *                 ]
 178       *             ]
 179       *         ]
 180       * );
 181       * </code>
 182       *
 183       * <code>
 184       * $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
 185       *         [
 186       *             'allBorders' => [
 187       *                 'borderStyle' => Border::BORDER_DASHDOT,
 188       *                 'color' => [
 189       *                     'rgb' => '808080'
 190       *                 ]
 191       *             ]
 192       *         ]
 193       * );
 194       * </code>
 195       *
 196       * @param array $pStyles Array containing style information
 197       *
 198       * @return $this
 199       */
 200      public function applyFromArray(array $pStyles)
 201      {
 202          if ($this->isSupervisor) {
 203              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
 204          } else {
 205              if (isset($pStyles['left'])) {
 206                  $this->getLeft()->applyFromArray($pStyles['left']);
 207              }
 208              if (isset($pStyles['right'])) {
 209                  $this->getRight()->applyFromArray($pStyles['right']);
 210              }
 211              if (isset($pStyles['top'])) {
 212                  $this->getTop()->applyFromArray($pStyles['top']);
 213              }
 214              if (isset($pStyles['bottom'])) {
 215                  $this->getBottom()->applyFromArray($pStyles['bottom']);
 216              }
 217              if (isset($pStyles['diagonal'])) {
 218                  $this->getDiagonal()->applyFromArray($pStyles['diagonal']);
 219              }
 220              if (isset($pStyles['diagonalDirection'])) {
 221                  $this->setDiagonalDirection($pStyles['diagonalDirection']);
 222              }
 223              if (isset($pStyles['allBorders'])) {
 224                  $this->getLeft()->applyFromArray($pStyles['allBorders']);
 225                  $this->getRight()->applyFromArray($pStyles['allBorders']);
 226                  $this->getTop()->applyFromArray($pStyles['allBorders']);
 227                  $this->getBottom()->applyFromArray($pStyles['allBorders']);
 228              }
 229          }
 230  
 231          return $this;
 232      }
 233  
 234      /**
 235       * Get Left.
 236       *
 237       * @return Border
 238       */
 239      public function getLeft()
 240      {
 241          return $this->left;
 242      }
 243  
 244      /**
 245       * Get Right.
 246       *
 247       * @return Border
 248       */
 249      public function getRight()
 250      {
 251          return $this->right;
 252      }
 253  
 254      /**
 255       * Get Top.
 256       *
 257       * @return Border
 258       */
 259      public function getTop()
 260      {
 261          return $this->top;
 262      }
 263  
 264      /**
 265       * Get Bottom.
 266       *
 267       * @return Border
 268       */
 269      public function getBottom()
 270      {
 271          return $this->bottom;
 272      }
 273  
 274      /**
 275       * Get Diagonal.
 276       *
 277       * @return Border
 278       */
 279      public function getDiagonal()
 280      {
 281          return $this->diagonal;
 282      }
 283  
 284      /**
 285       * Get AllBorders (pseudo-border). Only applies to supervisor.
 286       *
 287       * @return Border
 288       */
 289      public function getAllBorders()
 290      {
 291          if (!$this->isSupervisor) {
 292              throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
 293          }
 294  
 295          return $this->allBorders;
 296      }
 297  
 298      /**
 299       * Get Outline (pseudo-border). Only applies to supervisor.
 300       *
 301       * @return Border
 302       */
 303      public function getOutline()
 304      {
 305          if (!$this->isSupervisor) {
 306              throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
 307          }
 308  
 309          return $this->outline;
 310      }
 311  
 312      /**
 313       * Get Inside (pseudo-border). Only applies to supervisor.
 314       *
 315       * @return Border
 316       */
 317      public function getInside()
 318      {
 319          if (!$this->isSupervisor) {
 320              throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
 321          }
 322  
 323          return $this->inside;
 324      }
 325  
 326      /**
 327       * Get Vertical (pseudo-border). Only applies to supervisor.
 328       *
 329       * @return Border
 330       */
 331      public function getVertical()
 332      {
 333          if (!$this->isSupervisor) {
 334              throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
 335          }
 336  
 337          return $this->vertical;
 338      }
 339  
 340      /**
 341       * Get Horizontal (pseudo-border). Only applies to supervisor.
 342       *
 343       * @return Border
 344       */
 345      public function getHorizontal()
 346      {
 347          if (!$this->isSupervisor) {
 348              throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
 349          }
 350  
 351          return $this->horizontal;
 352      }
 353  
 354      /**
 355       * Get DiagonalDirection.
 356       *
 357       * @return int
 358       */
 359      public function getDiagonalDirection()
 360      {
 361          if ($this->isSupervisor) {
 362              return $this->getSharedComponent()->getDiagonalDirection();
 363          }
 364  
 365          return $this->diagonalDirection;
 366      }
 367  
 368      /**
 369       * Set DiagonalDirection.
 370       *
 371       * @param int $pValue see self::DIAGONAL_*
 372       *
 373       * @return $this
 374       */
 375      public function setDiagonalDirection($pValue)
 376      {
 377          if ($pValue == '') {
 378              $pValue = self::DIAGONAL_NONE;
 379          }
 380          if ($this->isSupervisor) {
 381              $styleArray = $this->getStyleArray(['diagonalDirection' => $pValue]);
 382              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 383          } else {
 384              $this->diagonalDirection = $pValue;
 385          }
 386  
 387          return $this;
 388      }
 389  
 390      /**
 391       * Get hash code.
 392       *
 393       * @return string Hash code
 394       */
 395      public function getHashCode()
 396      {
 397          if ($this->isSupervisor) {
 398              return $this->getSharedComponent()->getHashcode();
 399          }
 400  
 401          return md5(
 402              $this->getLeft()->getHashCode() .
 403              $this->getRight()->getHashCode() .
 404              $this->getTop()->getHashCode() .
 405              $this->getBottom()->getHashCode() .
 406              $this->getDiagonal()->getHashCode() .
 407              $this->getDiagonalDirection() .
 408              __CLASS__
 409          );
 410      }
 411  
 412      protected function exportArray1(): array
 413      {
 414          $exportedArray = [];
 415          $this->exportArray2($exportedArray, 'bottom', $this->getBottom());
 416          $this->exportArray2($exportedArray, 'diagonal', $this->getDiagonal());
 417          $this->exportArray2($exportedArray, 'diagonalDirection', $this->getDiagonalDirection());
 418          $this->exportArray2($exportedArray, 'left', $this->getLeft());
 419          $this->exportArray2($exportedArray, 'right', $this->getRight());
 420          $this->exportArray2($exportedArray, 'top', $this->getTop());
 421  
 422          return $exportedArray;
 423      }
 424  }