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;
   6  
   7  use DateInterval;
   8  use DateTimeInterface;
   9  use OpenSpout\Common\Entity\Cell\BooleanCell;
  10  use OpenSpout\Common\Entity\Cell\DateIntervalCell;
  11  use OpenSpout\Common\Entity\Cell\DateTimeCell;
  12  use OpenSpout\Common\Entity\Cell\EmptyCell;
  13  use OpenSpout\Common\Entity\Cell\FormulaCell;
  14  use OpenSpout\Common\Entity\Cell\NumericCell;
  15  use OpenSpout\Common\Entity\Cell\StringCell;
  16  use OpenSpout\Common\Entity\Comment\Comment;
  17  use OpenSpout\Common\Entity\Style\Style;
  18  
  19  abstract class Cell
  20  {
  21      public ?Comment $comment = null;
  22  
  23      private Style $style;
  24  
  25      public function __construct(?Style $style)
  26      {
  27          $this->setStyle($style);
  28      }
  29  
  30      abstract public function getValue(): null|bool|string|int|float|DateTimeInterface|DateInterval;
  31  
  32      final public function setStyle(?Style $style): void
  33      {
  34          $this->style = $style ?? new Style();
  35      }
  36  
  37      final public function getStyle(): Style
  38      {
  39          return $this->style;
  40      }
  41  
  42      final public static function fromValue(null|bool|string|int|float|DateTimeInterface|DateInterval $value, ?Style $style = null): self
  43      {
  44          if (\is_bool($value)) {
  45              return new BooleanCell($value, $style);
  46          }
  47          if (null === $value || '' === $value) {
  48              return new EmptyCell($value, $style);
  49          }
  50          if (\is_int($value) || \is_float($value)) {
  51              return new NumericCell($value, $style);
  52          }
  53          if ($value instanceof DateTimeInterface) {
  54              return new DateTimeCell($value, $style);
  55          }
  56          if ($value instanceof DateInterval) {
  57              return new DateIntervalCell($value, $style);
  58          }
  59          if (isset($value[0]) && '=' === $value[0]) {
  60              return new FormulaCell($value, $style);
  61          }
  62  
  63          return new StringCell($value, $style);
  64      }
  65  }