Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace OpenSpout\Common\Entity\Style;
   6  
   7  use OpenSpout\Common\Exception\InvalidArgumentException;
   8  
   9  /**
  10   * Represents a style to be applied to a cell.
  11   */
  12  final class Style
  13  {
  14      /**
  15       * Default values.
  16       */
  17      public const DEFAULT_FONT_SIZE = 11;
  18      public const DEFAULT_FONT_COLOR = Color::BLACK;
  19      public const DEFAULT_FONT_NAME = 'Arial';
  20  
  21      /** @var int Style ID */
  22      private int $id = -1;
  23  
  24      /** @var bool Whether the font should be bold */
  25      private bool $fontBold = false;
  26  
  27      /** @var bool Whether the bold property was set */
  28      private bool $hasSetFontBold = false;
  29  
  30      /** @var bool Whether the font should be italic */
  31      private bool $fontItalic = false;
  32  
  33      /** @var bool Whether the italic property was set */
  34      private bool $hasSetFontItalic = false;
  35  
  36      /** @var bool Whether the font should be underlined */
  37      private bool $fontUnderline = false;
  38  
  39      /** @var bool Whether the underline property was set */
  40      private bool $hasSetFontUnderline = false;
  41  
  42      /** @var bool Whether the font should be struck through */
  43      private bool $fontStrikethrough = false;
  44  
  45      /** @var bool Whether the strikethrough property was set */
  46      private bool $hasSetFontStrikethrough = false;
  47  
  48      /** @var int Font size */
  49      private int $fontSize = self::DEFAULT_FONT_SIZE;
  50  
  51      /** @var bool Whether the font size property was set */
  52      private bool $hasSetFontSize = false;
  53  
  54      /** @var string Font color */
  55      private string $fontColor = self::DEFAULT_FONT_COLOR;
  56  
  57      /** @var bool Whether the font color property was set */
  58      private bool $hasSetFontColor = false;
  59  
  60      /** @var string Font name */
  61      private string $fontName = self::DEFAULT_FONT_NAME;
  62  
  63      /** @var bool Whether the font name property was set */
  64      private bool $hasSetFontName = false;
  65  
  66      /** @var bool Whether specific font properties should be applied */
  67      private bool $shouldApplyFont = false;
  68  
  69      /** @var bool Whether specific cell alignment should be applied */
  70      private bool $shouldApplyCellAlignment = false;
  71  
  72      /** @var string Cell alignment */
  73      private string $cellAlignment;
  74  
  75      /** @var bool Whether the cell alignment property was set */
  76      private bool $hasSetCellAlignment = false;
  77  
  78      /** @var bool Whether specific cell vertical alignment should be applied */
  79      private bool $shouldApplyCellVerticalAlignment = false;
  80  
  81      /** @var string Cell vertical alignment */
  82      private string $cellVerticalAlignment;
  83  
  84      /** @var bool Whether the cell vertical alignment property was set */
  85      private bool $hasSetCellVerticalAlignment = false;
  86  
  87      /** @var bool Whether the text should wrap in the cell (useful for long or multi-lines text) */
  88      private bool $shouldWrapText = false;
  89  
  90      /** @var bool Whether the wrap text property was set */
  91      private bool $hasSetWrapText = false;
  92  
  93      /** @var bool Whether the cell should shrink to fit to content */
  94      private bool $shouldShrinkToFit = false;
  95  
  96      /** @var bool Whether the shouldShrinkToFit text property was set */
  97      private bool $hasSetShrinkToFit = false;
  98  
  99      private ?Border $border = null;
 100  
 101      /** @var null|string Background color */
 102      private ?string $backgroundColor = null;
 103  
 104      /** @var null|string Format */
 105      private ?string $format = null;
 106  
 107      private bool $isRegistered = false;
 108  
 109      private bool $isEmpty = true;
 110  
 111      public function __sleep(): array
 112      {
 113          $vars = get_object_vars($this);
 114          unset($vars['id'], $vars['isRegistered']);
 115  
 116          return array_keys($vars);
 117      }
 118  
 119      public function getId(): int
 120      {
 121          \assert(0 <= $this->id);
 122  
 123          return $this->id;
 124      }
 125  
 126      public function setId(int $id): self
 127      {
 128          $this->id = $id;
 129  
 130          return $this;
 131      }
 132  
 133      public function getBorder(): ?Border
 134      {
 135          return $this->border;
 136      }
 137  
 138      public function setBorder(Border $border): self
 139      {
 140          $this->border = $border;
 141          $this->isEmpty = false;
 142  
 143          return $this;
 144      }
 145  
 146      public function isFontBold(): bool
 147      {
 148          return $this->fontBold;
 149      }
 150  
 151      public function setFontBold(): self
 152      {
 153          $this->fontBold = true;
 154          $this->hasSetFontBold = true;
 155          $this->shouldApplyFont = true;
 156          $this->isEmpty = false;
 157  
 158          return $this;
 159      }
 160  
 161      public function hasSetFontBold(): bool
 162      {
 163          return $this->hasSetFontBold;
 164      }
 165  
 166      public function isFontItalic(): bool
 167      {
 168          return $this->fontItalic;
 169      }
 170  
 171      public function setFontItalic(): self
 172      {
 173          $this->fontItalic = true;
 174          $this->hasSetFontItalic = true;
 175          $this->shouldApplyFont = true;
 176          $this->isEmpty = false;
 177  
 178          return $this;
 179      }
 180  
 181      public function hasSetFontItalic(): bool
 182      {
 183          return $this->hasSetFontItalic;
 184      }
 185  
 186      public function isFontUnderline(): bool
 187      {
 188          return $this->fontUnderline;
 189      }
 190  
 191      public function setFontUnderline(): self
 192      {
 193          $this->fontUnderline = true;
 194          $this->hasSetFontUnderline = true;
 195          $this->shouldApplyFont = true;
 196          $this->isEmpty = false;
 197  
 198          return $this;
 199      }
 200  
 201      public function hasSetFontUnderline(): bool
 202      {
 203          return $this->hasSetFontUnderline;
 204      }
 205  
 206      public function isFontStrikethrough(): bool
 207      {
 208          return $this->fontStrikethrough;
 209      }
 210  
 211      public function setFontStrikethrough(): self
 212      {
 213          $this->fontStrikethrough = true;
 214          $this->hasSetFontStrikethrough = true;
 215          $this->shouldApplyFont = true;
 216          $this->isEmpty = false;
 217  
 218          return $this;
 219      }
 220  
 221      public function hasSetFontStrikethrough(): bool
 222      {
 223          return $this->hasSetFontStrikethrough;
 224      }
 225  
 226      public function getFontSize(): int
 227      {
 228          return $this->fontSize;
 229      }
 230  
 231      /**
 232       * @param int $fontSize Font size, in pixels
 233       */
 234      public function setFontSize(int $fontSize): self
 235      {
 236          $this->fontSize = $fontSize;
 237          $this->hasSetFontSize = true;
 238          $this->shouldApplyFont = true;
 239          $this->isEmpty = false;
 240  
 241          return $this;
 242      }
 243  
 244      public function hasSetFontSize(): bool
 245      {
 246          return $this->hasSetFontSize;
 247      }
 248  
 249      public function getFontColor(): string
 250      {
 251          return $this->fontColor;
 252      }
 253  
 254      /**
 255       * Sets the font color.
 256       *
 257       * @param string $fontColor ARGB color (@see Color)
 258       */
 259      public function setFontColor(string $fontColor): self
 260      {
 261          $this->fontColor = $fontColor;
 262          $this->hasSetFontColor = true;
 263          $this->shouldApplyFont = true;
 264          $this->isEmpty = false;
 265  
 266          return $this;
 267      }
 268  
 269      public function hasSetFontColor(): bool
 270      {
 271          return $this->hasSetFontColor;
 272      }
 273  
 274      public function getFontName(): string
 275      {
 276          return $this->fontName;
 277      }
 278  
 279      /**
 280       * @param string $fontName Name of the font to use
 281       */
 282      public function setFontName(string $fontName): self
 283      {
 284          $this->fontName = $fontName;
 285          $this->hasSetFontName = true;
 286          $this->shouldApplyFont = true;
 287          $this->isEmpty = false;
 288  
 289          return $this;
 290      }
 291  
 292      public function hasSetFontName(): bool
 293      {
 294          return $this->hasSetFontName;
 295      }
 296  
 297      public function getCellAlignment(): string
 298      {
 299          return $this->cellAlignment;
 300      }
 301  
 302      public function getCellVerticalAlignment(): string
 303      {
 304          return $this->cellVerticalAlignment;
 305      }
 306  
 307      /**
 308       * @param string $cellAlignment The cell alignment
 309       */
 310      public function setCellAlignment(string $cellAlignment): self
 311      {
 312          if (!CellAlignment::isValid($cellAlignment)) {
 313              throw new InvalidArgumentException('Invalid cell alignment value');
 314          }
 315  
 316          $this->cellAlignment = $cellAlignment;
 317          $this->hasSetCellAlignment = true;
 318          $this->shouldApplyCellAlignment = true;
 319          $this->isEmpty = false;
 320  
 321          return $this;
 322      }
 323  
 324      /**
 325       * @param string $cellVerticalAlignment The cell vertical alignment
 326       */
 327      public function setCellVerticalAlignment(string $cellVerticalAlignment): self
 328      {
 329          if (!CellVerticalAlignment::isValid($cellVerticalAlignment)) {
 330              throw new InvalidArgumentException('Invalid cell vertical alignment value');
 331          }
 332  
 333          $this->cellVerticalAlignment = $cellVerticalAlignment;
 334          $this->hasSetCellVerticalAlignment = true;
 335          $this->shouldApplyCellVerticalAlignment = true;
 336          $this->isEmpty = false;
 337  
 338          return $this;
 339      }
 340  
 341      public function hasSetCellAlignment(): bool
 342      {
 343          return $this->hasSetCellAlignment;
 344      }
 345  
 346      public function hasSetCellVerticalAlignment(): bool
 347      {
 348          return $this->hasSetCellVerticalAlignment;
 349      }
 350  
 351      /**
 352       * @return bool Whether specific cell alignment should be applied
 353       */
 354      public function shouldApplyCellAlignment(): bool
 355      {
 356          return $this->shouldApplyCellAlignment;
 357      }
 358  
 359      public function shouldApplyCellVerticalAlignment(): bool
 360      {
 361          return $this->shouldApplyCellVerticalAlignment;
 362      }
 363  
 364      public function shouldWrapText(): bool
 365      {
 366          return $this->shouldWrapText;
 367      }
 368  
 369      /**
 370       * @param bool $shouldWrap Should the text be wrapped
 371       */
 372      public function setShouldWrapText(bool $shouldWrap = true): self
 373      {
 374          $this->shouldWrapText = $shouldWrap;
 375          $this->hasSetWrapText = true;
 376          $this->isEmpty = false;
 377  
 378          return $this;
 379      }
 380  
 381      public function hasSetWrapText(): bool
 382      {
 383          return $this->hasSetWrapText;
 384      }
 385  
 386      /**
 387       * @return bool Whether specific font properties should be applied
 388       */
 389      public function shouldApplyFont(): bool
 390      {
 391          return $this->shouldApplyFont;
 392      }
 393  
 394      /**
 395       * Sets the background color.
 396       *
 397       * @param string $color ARGB color (@see Color)
 398       */
 399      public function setBackgroundColor(string $color): self
 400      {
 401          $this->backgroundColor = $color;
 402          $this->isEmpty = false;
 403  
 404          return $this;
 405      }
 406  
 407      public function getBackgroundColor(): ?string
 408      {
 409          return $this->backgroundColor;
 410      }
 411  
 412      /**
 413       * Sets format.
 414       */
 415      public function setFormat(string $format): self
 416      {
 417          $this->format = $format;
 418          $this->isEmpty = false;
 419  
 420          return $this;
 421      }
 422  
 423      public function getFormat(): ?string
 424      {
 425          return $this->format;
 426      }
 427  
 428      public function isRegistered(): bool
 429      {
 430          return $this->isRegistered;
 431      }
 432  
 433      public function markAsRegistered(?int $id): void
 434      {
 435          $this->setId($id);
 436          $this->isRegistered = true;
 437      }
 438  
 439      public function isEmpty(): bool
 440      {
 441          return $this->isEmpty;
 442      }
 443  
 444      /**
 445       * Sets should shrink to fit.
 446       */
 447      public function setShouldShrinkToFit(bool $shrinkToFit = true): self
 448      {
 449          $this->hasSetShrinkToFit = true;
 450          $this->shouldShrinkToFit = $shrinkToFit;
 451  
 452          return $this;
 453      }
 454  
 455      /**
 456       * @return bool Whether format should be applied
 457       */
 458      public function shouldShrinkToFit(): bool
 459      {
 460          return $this->shouldShrinkToFit;
 461      }
 462  
 463      public function hasSetShrinkToFit(): bool
 464      {
 465          return $this->hasSetShrinkToFit;
 466      }
 467  }