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

   1  <?php
   2  
   3  namespace Box\Spout\Writer\Common\Manager\Style;
   4  
   5  use Box\Spout\Common\Entity\Cell;
   6  use Box\Spout\Common\Entity\Style\Style;
   7  
   8  /**
   9   * Class StyleManager
  10   * Manages styles to be applied to a cell
  11   */
  12  class StyleManager implements StyleManagerInterface
  13  {
  14      /** @var StyleRegistry Registry for all used styles */
  15      protected $styleRegistry;
  16  
  17      /**
  18       * @param StyleRegistry $styleRegistry
  19       */
  20      public function __construct(StyleRegistry $styleRegistry)
  21      {
  22          $this->styleRegistry = $styleRegistry;
  23      }
  24  
  25      /**
  26       * Returns the default style
  27       *
  28       * @return Style Default style
  29       */
  30      protected function getDefaultStyle()
  31      {
  32          // By construction, the default style has ID 0
  33          return $this->styleRegistry->getRegisteredStyles()[0];
  34      }
  35  
  36      /**
  37       * Registers the given style as a used style.
  38       * Duplicate styles won't be registered more than once.
  39       *
  40       * @param Style $style The style to be registered
  41       * @return Style The registered style, updated with an internal ID.
  42       */
  43      public function registerStyle($style)
  44      {
  45          return $this->styleRegistry->registerStyle($style);
  46      }
  47  
  48      /**
  49       * Apply additional styles if the given row needs it.
  50       * Typically, set "wrap text" if a cell contains a new line.
  51       *
  52       * @param Cell $cell
  53       * @return PossiblyUpdatedStyle The eventually updated style
  54       */
  55      public function applyExtraStylesIfNeeded(Cell $cell) : PossiblyUpdatedStyle
  56      {
  57          return $this->applyWrapTextIfCellContainsNewLine($cell);
  58      }
  59  
  60      /**
  61       * Set the "wrap text" option if a cell of the given row contains a new line.
  62       *
  63       * @NOTE: There is a bug on the Mac version of Excel (2011 and below) where new lines
  64       *        are ignored even when the "wrap text" option is set. This only occurs with
  65       *        inline strings (shared strings do work fine).
  66       *        A workaround would be to encode "\n" as "_x000D_" but it does not work
  67       *        on the Windows version of Excel...
  68       *
  69       * @param Cell $cell The cell the style should be applied to
  70       * @return PossiblyUpdatedStyle The eventually updated style
  71       */
  72      protected function applyWrapTextIfCellContainsNewLine(Cell $cell) : PossiblyUpdatedStyle
  73      {
  74          $cellStyle = $cell->getStyle();
  75  
  76          // if the "wrap text" option is already set, no-op
  77          if (!$cellStyle->hasSetWrapText() && $cell->isString() && \strpos($cell->getValue(), "\n") !== false) {
  78              $cellStyle->setShouldWrapText();
  79  
  80              return new PossiblyUpdatedStyle($cellStyle, true);
  81          }
  82  
  83          return new PossiblyUpdatedStyle($cellStyle, false);
  84      }
  85  }