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 39 and 311]

   1  <?php
   2  
   3  namespace Box\Spout\Writer\Common\Manager\Style;
   4  
   5  use Box\Spout\Common\Entity\Style\Style;
   6  
   7  /**
   8   * Class StyleMerger
   9   * Takes care of merging styles together
  10   */
  11  class StyleMerger
  12  {
  13      /**
  14       * Merges the current style with the given style, using the given style as a base. This means that:
  15       *   - if current style and base style both have property A set, use current style property's value
  16       *   - if current style has property A set but base style does not, use current style property's value
  17       *   - if base style has property A set but current style does not, use base style property's value
  18       *
  19       * @NOTE: This function returns a new style.
  20       *
  21       * @param Style $style
  22       * @param Style $baseStyle
  23       * @return Style New style corresponding to the merge of the 2 styles
  24       */
  25      public function merge(Style $style, Style $baseStyle)
  26      {
  27          $mergedStyle = clone $style;
  28  
  29          $this->mergeFontStyles($mergedStyle, $style, $baseStyle);
  30          $this->mergeOtherFontProperties($mergedStyle, $style, $baseStyle);
  31          $this->mergeCellProperties($mergedStyle, $style, $baseStyle);
  32  
  33          return $mergedStyle;
  34      }
  35  
  36      /**
  37       * @param Style $styleToUpdate (passed as reference)
  38       * @param Style $style
  39       * @param Style $baseStyle
  40       * @return void
  41       */
  42      private function mergeFontStyles(Style $styleToUpdate, Style $style, Style $baseStyle)
  43      {
  44          if (!$style->hasSetFontBold() && $baseStyle->isFontBold()) {
  45              $styleToUpdate->setFontBold();
  46          }
  47          if (!$style->hasSetFontItalic() && $baseStyle->isFontItalic()) {
  48              $styleToUpdate->setFontItalic();
  49          }
  50          if (!$style->hasSetFontUnderline() && $baseStyle->isFontUnderline()) {
  51              $styleToUpdate->setFontUnderline();
  52          }
  53          if (!$style->hasSetFontStrikethrough() && $baseStyle->isFontStrikethrough()) {
  54              $styleToUpdate->setFontStrikethrough();
  55          }
  56      }
  57  
  58      /**
  59       * @param Style $styleToUpdate Style to update (passed as reference)
  60       * @param Style $style
  61       * @param Style $baseStyle
  62       * @return void
  63       */
  64      private function mergeOtherFontProperties(Style $styleToUpdate, Style $style, Style $baseStyle)
  65      {
  66          if (!$style->hasSetFontSize() && $baseStyle->getFontSize() !== Style::DEFAULT_FONT_SIZE) {
  67              $styleToUpdate->setFontSize($baseStyle->getFontSize());
  68          }
  69          if (!$style->hasSetFontColor() && $baseStyle->getFontColor() !== Style::DEFAULT_FONT_COLOR) {
  70              $styleToUpdate->setFontColor($baseStyle->getFontColor());
  71          }
  72          if (!$style->hasSetFontName() && $baseStyle->getFontName() !== Style::DEFAULT_FONT_NAME) {
  73              $styleToUpdate->setFontName($baseStyle->getFontName());
  74          }
  75      }
  76  
  77      /**
  78       * @param Style $styleToUpdate Style to update (passed as reference)
  79       * @param Style $style
  80       * @param Style $baseStyle
  81       * @return void
  82       */
  83      private function mergeCellProperties(Style $styleToUpdate, Style $style, Style $baseStyle)
  84      {
  85          if (!$style->hasSetWrapText() && $baseStyle->shouldWrapText()) {
  86              $styleToUpdate->setShouldWrapText();
  87          }
  88          if (!$style->hasSetCellAlignment() && $baseStyle->shouldApplyCellAlignment()) {
  89              $styleToUpdate->setCellAlignment($baseStyle->getCellAlignment());
  90          }
  91          if (!$style->getBorder() && $baseStyle->shouldApplyBorder()) {
  92              $styleToUpdate->setBorder($baseStyle->getBorder());
  93          }
  94          if (!$style->getFormat() && $baseStyle->shouldApplyFormat()) {
  95              $styleToUpdate->setFormat($baseStyle->getFormat());
  96          }
  97          if (!$style->shouldApplyBackgroundColor() && $baseStyle->shouldApplyBackgroundColor()) {
  98              $styleToUpdate->setBackgroundColor($baseStyle->getBackgroundColor());
  99          }
 100      }
 101  }