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\Creator\Style;
   4  
   5  use Box\Spout\Common\Entity\Style\Border;
   6  use Box\Spout\Common\Entity\Style\CellAlignment;
   7  use Box\Spout\Common\Entity\Style\Style;
   8  use Box\Spout\Common\Exception\InvalidArgumentException;
   9  
  10  /**
  11   * Class StyleBuilder
  12   * Builder to create new styles
  13   */
  14  class StyleBuilder
  15  {
  16      /** @var Style Style to be created */
  17      protected $style;
  18  
  19      /**
  20       *
  21       */
  22      public function __construct()
  23      {
  24          $this->style = new Style();
  25      }
  26  
  27      /**
  28       * Makes the font bold.
  29       *
  30       * @return StyleBuilder
  31       */
  32      public function setFontBold()
  33      {
  34          $this->style->setFontBold();
  35  
  36          return $this;
  37      }
  38  
  39      /**
  40       * Makes the font italic.
  41       *
  42       * @return StyleBuilder
  43       */
  44      public function setFontItalic()
  45      {
  46          $this->style->setFontItalic();
  47  
  48          return $this;
  49      }
  50  
  51      /**
  52       * Makes the font underlined.
  53       *
  54       * @return StyleBuilder
  55       */
  56      public function setFontUnderline()
  57      {
  58          $this->style->setFontUnderline();
  59  
  60          return $this;
  61      }
  62  
  63      /**
  64       * Makes the font struck through.
  65       *
  66       * @return StyleBuilder
  67       */
  68      public function setFontStrikethrough()
  69      {
  70          $this->style->setFontStrikethrough();
  71  
  72          return $this;
  73      }
  74  
  75      /**
  76       * Sets the font size.
  77       *
  78       * @param int $fontSize Font size, in pixels
  79       * @return StyleBuilder
  80       */
  81      public function setFontSize($fontSize)
  82      {
  83          $this->style->setFontSize($fontSize);
  84  
  85          return $this;
  86      }
  87  
  88      /**
  89       * Sets the font color.
  90       *
  91       * @param string $fontColor ARGB color (@see Color)
  92       * @return StyleBuilder
  93       */
  94      public function setFontColor($fontColor)
  95      {
  96          $this->style->setFontColor($fontColor);
  97  
  98          return $this;
  99      }
 100  
 101      /**
 102       * Sets the font name.
 103       *
 104       * @param string $fontName Name of the font to use
 105       * @return StyleBuilder
 106       */
 107      public function setFontName($fontName)
 108      {
 109          $this->style->setFontName($fontName);
 110  
 111          return $this;
 112      }
 113  
 114      /**
 115       * Makes the text wrap in the cell if requested
 116       *
 117       * @param bool $shouldWrap Should the text be wrapped
 118       * @return StyleBuilder
 119       */
 120      public function setShouldWrapText($shouldWrap = true)
 121      {
 122          $this->style->setShouldWrapText($shouldWrap);
 123  
 124          return $this;
 125      }
 126  
 127      /**
 128       * Sets the cell alignment.
 129       *
 130       * @param string $cellAlignment The cell alignment
 131       *
 132       * @throws InvalidArgumentException If the given cell alignment is not valid
 133       * @return StyleBuilder
 134       */
 135      public function setCellAlignment($cellAlignment)
 136      {
 137          if (!CellAlignment::isValid($cellAlignment)) {
 138              throw new InvalidArgumentException('Invalid cell alignment value');
 139          }
 140  
 141          $this->style->setCellAlignment($cellAlignment);
 142  
 143          return $this;
 144      }
 145  
 146      /**
 147       * Set a border
 148       *
 149       * @param Border $border
 150       * @return $this
 151       */
 152      public function setBorder(Border $border)
 153      {
 154          $this->style->setBorder($border);
 155  
 156          return $this;
 157      }
 158  
 159      /**
 160       *  Sets a background color
 161       *
 162       * @param string $color ARGB color (@see Color)
 163       * @return StyleBuilder
 164       */
 165      public function setBackgroundColor($color)
 166      {
 167          $this->style->setBackgroundColor($color);
 168  
 169          return $this;
 170      }
 171  
 172      /**
 173       *  Sets a format
 174       *
 175       * @param string $format Format
 176       * @return StyleBuilder
 177       * @api
 178       */
 179      public function setFormat($format)
 180      {
 181          $this->style->setFormat($format);
 182  
 183          return $this;
 184      }
 185  
 186      /**
 187       * Returns the configured style. The style is cached and can be reused.
 188       *
 189       * @return Style
 190       */
 191      public function build()
 192      {
 193          return $this->style;
 194      }
 195  }