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.
   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace OpenSpout\Reader\XLSX;
   6  
   7  use OpenSpout\Reader\Exception\NoSheetsFoundException;
   8  use OpenSpout\Reader\SheetIteratorInterface;
   9  use OpenSpout\Reader\XLSX\Manager\SheetManager;
  10  
  11  /**
  12   * @implements SheetIteratorInterface<Sheet>
  13   */
  14  final class SheetIterator implements SheetIteratorInterface
  15  {
  16      /** @var Sheet[] The list of sheet present in the file */
  17      private array $sheets;
  18  
  19      /** @var int The index of the sheet being read (zero-based) */
  20      private int $currentSheetIndex = 0;
  21  
  22      /**
  23       * @param SheetManager $sheetManager Manages sheets
  24       *
  25       * @throws NoSheetsFoundException If there are no sheets in the file
  26       */
  27      public function __construct(SheetManager $sheetManager)
  28      {
  29          // Fetch all available sheets
  30          $this->sheets = $sheetManager->getSheets();
  31  
  32          if (0 === \count($this->sheets)) {
  33              throw new NoSheetsFoundException('The file must contain at least one sheet.');
  34          }
  35      }
  36  
  37      /**
  38       * Rewind the Iterator to the first element.
  39       *
  40       * @see http://php.net/manual/en/iterator.rewind.php
  41       */
  42      public function rewind(): void
  43      {
  44          $this->currentSheetIndex = 0;
  45      }
  46  
  47      /**
  48       * Checks if current position is valid.
  49       *
  50       * @see http://php.net/manual/en/iterator.valid.php
  51       */
  52      public function valid(): bool
  53      {
  54          return $this->currentSheetIndex < \count($this->sheets);
  55      }
  56  
  57      /**
  58       * Move forward to next element.
  59       *
  60       * @see http://php.net/manual/en/iterator.next.php
  61       */
  62      public function next(): void
  63      {
  64          ++$this->currentSheetIndex;
  65      }
  66  
  67      /**
  68       * Return the current element.
  69       *
  70       * @see http://php.net/manual/en/iterator.current.php
  71       */
  72      public function current(): Sheet
  73      {
  74          return $this->sheets[$this->currentSheetIndex];
  75      }
  76  
  77      /**
  78       * Return the key of the current element.
  79       *
  80       * @see http://php.net/manual/en/iterator.key.php
  81       */
  82      public function key(): int
  83      {
  84          return $this->currentSheetIndex + 1;
  85      }
  86  }