Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Reader\Xml\Style;
   4  
   5  use PhpOffice\PhpSpreadsheet\Style\Border as BorderStyle;
   6  use PhpOffice\PhpSpreadsheet\Style\Borders;
   7  use SimpleXMLElement;
   8  
   9  class Border extends StyleBase
  10  {
  11      protected const BORDER_POSITIONS = [
  12          'top',
  13          'left',
  14          'bottom',
  15          'right',
  16      ];
  17  
  18      /**
  19       * @var array
  20       */
  21      public const BORDER_MAPPINGS = [
  22          'borderStyle' => [
  23              '1continuous' => BorderStyle::BORDER_THIN,
  24              '1dash' => BorderStyle::BORDER_DASHED,
  25              '1dashdot' => BorderStyle::BORDER_DASHDOT,
  26              '1dashdotdot' => BorderStyle::BORDER_DASHDOTDOT,
  27              '1dot' => BorderStyle::BORDER_DOTTED,
  28              '1double' => BorderStyle::BORDER_DOUBLE,
  29              '2continuous' => BorderStyle::BORDER_MEDIUM,
  30              '2dash' => BorderStyle::BORDER_MEDIUMDASHED,
  31              '2dashdot' => BorderStyle::BORDER_MEDIUMDASHDOT,
  32              '2dashdotdot' => BorderStyle::BORDER_MEDIUMDASHDOTDOT,
  33              '2dot' => BorderStyle::BORDER_DOTTED,
  34              '2double' => BorderStyle::BORDER_DOUBLE,
  35              '3continuous' => BorderStyle::BORDER_THICK,
  36              '3dash' => BorderStyle::BORDER_MEDIUMDASHED,
  37              '3dashdot' => BorderStyle::BORDER_MEDIUMDASHDOT,
  38              '3dashdotdot' => BorderStyle::BORDER_MEDIUMDASHDOTDOT,
  39              '3dot' => BorderStyle::BORDER_DOTTED,
  40              '3double' => BorderStyle::BORDER_DOUBLE,
  41          ],
  42      ];
  43  
  44      public function parseStyle(SimpleXMLElement $styleData, array $namespaces): array
  45      {
  46          $style = [];
  47  
  48          $diagonalDirection = '';
  49          $borderPosition = '';
  50          foreach ($styleData->Border as $borderStyle) {
  51              $borderAttributes = self::getAttributes($borderStyle, $namespaces['ss']);
  52              $thisBorder = [];
  53              $styleType = (string) $borderAttributes->Weight;
  54              $styleType .= strtolower((string) $borderAttributes->LineStyle);
  55              $thisBorder['borderStyle'] = self::BORDER_MAPPINGS['borderStyle'][$styleType] ?? BorderStyle::BORDER_NONE;
  56  
  57              foreach ($borderAttributes as $borderStyleKey => $borderStyleValuex) {
  58                  $borderStyleValue = (string) $borderStyleValuex;
  59                  switch ($borderStyleKey) {
  60                      case 'Position':
  61                          [$borderPosition, $diagonalDirection] =
  62                              $this->parsePosition($borderStyleValue, $diagonalDirection);
  63  
  64                          break;
  65                      case 'Color':
  66                          $borderColour = substr($borderStyleValue, 1);
  67                          $thisBorder['color']['rgb'] = $borderColour;
  68  
  69                          break;
  70                  }
  71              }
  72  
  73              if ($borderPosition) {
  74                  $style['borders'][$borderPosition] = $thisBorder;
  75              } elseif ($diagonalDirection) {
  76                  $style['borders']['diagonalDirection'] = $diagonalDirection;
  77                  $style['borders']['diagonal'] = $thisBorder;
  78              }
  79          }
  80  
  81          return $style;
  82      }
  83  
  84      protected function parsePosition(string $borderStyleValue, string $diagonalDirection): array
  85      {
  86          $borderStyleValue = strtolower($borderStyleValue);
  87  
  88          if (in_array($borderStyleValue, self::BORDER_POSITIONS)) {
  89              $borderPosition = $borderStyleValue;
  90          } elseif ($borderStyleValue === 'diagonalleft') {
  91              $diagonalDirection = $diagonalDirection ? Borders::DIAGONAL_BOTH : Borders::DIAGONAL_DOWN;
  92          } elseif ($borderStyleValue === 'diagonalright') {
  93              $diagonalDirection = $diagonalDirection ? Borders::DIAGONAL_BOTH : Borders::DIAGONAL_UP;
  94          }
  95  
  96          return [$borderPosition ?? null, $diagonalDirection];
  97      }
  98  }