Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Worksheet;
   4  
   5  use Iterator as NativeIterator;
   6  use PhpOffice\PhpSpreadsheet\Cell\Cell;
   7  use PhpOffice\PhpSpreadsheet\Collection\Cells;
   8  
   9  /**
  10   * @template TKey
  11   *
  12   * @implements NativeIterator<TKey, Cell>
  13   */
  14  abstract class CellIterator implements NativeIterator
  15  {
  16      public const TREAT_NULL_VALUE_AS_EMPTY_CELL = 1;
  17  
  18      public const TREAT_EMPTY_STRING_AS_EMPTY_CELL = 2;
  19  
  20      public const IF_NOT_EXISTS_RETURN_NULL = false;
  21  
  22      public const IF_NOT_EXISTS_CREATE_NEW = true;
  23  
  24      /**
  25       * Worksheet to iterate.
  26       *
  27       * @var Worksheet
  28       */
  29      protected $worksheet;
  30  
  31      /**
  32       * Cell Collection to iterate.
  33       *
  34       * @var Cells
  35       */
  36      protected $cellCollection;
  37  
  38      /**
  39       * Iterate only existing cells.
  40       *
  41       * @var bool
  42       */
  43      protected $onlyExistingCells = false;
  44  
  45      /**
  46       * If iterating all cells, and a cell doesn't exist, identifies whether a new cell should be created,
  47       *    or if the iterator should return a null value.
  48       *
  49       * @var bool
  50       */
  51      protected $ifNotExists = self::IF_NOT_EXISTS_CREATE_NEW;
  52  
  53      /**
  54       * Destructor.
  55       */
  56      public function __destruct()
  57      {
  58          // @phpstan-ignore-next-line
  59          $this->worksheet = $this->cellCollection = null;
  60      }
  61  
  62      public function getIfNotExists(): bool
  63      {
  64          return $this->ifNotExists;
  65      }
  66  
  67      public function setIfNotExists(bool $ifNotExists = self::IF_NOT_EXISTS_CREATE_NEW): void
  68      {
  69          $this->ifNotExists = $ifNotExists;
  70      }
  71  
  72      /**
  73       * Get loop only existing cells.
  74       */
  75      public function getIterateOnlyExistingCells(): bool
  76      {
  77          return $this->onlyExistingCells;
  78      }
  79  
  80      /**
  81       * Validate start/end values for 'IterateOnlyExistingCells' mode, and adjust if necessary.
  82       */
  83      abstract protected function adjustForExistingOnlyRange(): void;
  84  
  85      /**
  86       * Set the iterator to loop only existing cells.
  87       */
  88      public function setIterateOnlyExistingCells(bool $value): void
  89      {
  90          $this->onlyExistingCells = (bool) $value;
  91  
  92          $this->adjustForExistingOnlyRange();
  93      }
  94  }