Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   1  <?php
   2  
   3  namespace Box\Spout\Reader\XLSX;
   4  
   5  use Box\Spout\Reader\Exception\NoSheetsFoundException;
   6  use Box\Spout\Reader\IteratorInterface;
   7  use Box\Spout\Reader\XLSX\Manager\SheetManager;
   8  
   9  /**
  10   * Class SheetIterator
  11   * Iterate over XLSX sheet.
  12   */
  13  class SheetIterator implements IteratorInterface
  14  {
  15      /** @var \Box\Spout\Reader\XLSX\Sheet[] The list of sheet present in the file */
  16      protected $sheets;
  17  
  18      /** @var int The index of the sheet being read (zero-based) */
  19      protected $currentSheetIndex;
  20  
  21      /**
  22       * @param SheetManager $sheetManager Manages sheets
  23       * @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file
  24       */
  25      public function __construct($sheetManager)
  26      {
  27          // Fetch all available sheets
  28          $this->sheets = $sheetManager->getSheets();
  29  
  30          if (\count($this->sheets) === 0) {
  31              throw new NoSheetsFoundException('The file must contain at least one sheet.');
  32          }
  33      }
  34  
  35      /**
  36       * Rewind the Iterator to the first element
  37       * @see http://php.net/manual/en/iterator.rewind.php
  38       *
  39       * @return void
  40       */
  41      #[\ReturnTypeWillChange]
  42      public function rewind()
  43      {
  44          $this->currentSheetIndex = 0;
  45      }
  46  
  47      /**
  48       * Checks if current position is valid
  49       * @see http://php.net/manual/en/iterator.valid.php
  50       *
  51       * @return bool
  52       */
  53      #[\ReturnTypeWillChange]
  54      public function valid()
  55      {
  56          return ($this->currentSheetIndex < \count($this->sheets));
  57      }
  58  
  59      /**
  60       * Move forward to next element
  61       * @see http://php.net/manual/en/iterator.next.php
  62       *
  63       * @return void
  64       */
  65      #[\ReturnTypeWillChange]
  66      public function next()
  67      {
  68          // Using isset here because it is way faster than array_key_exists...
  69          if (isset($this->sheets[$this->currentSheetIndex])) {
  70              $currentSheet = $this->sheets[$this->currentSheetIndex];
  71              $currentSheet->getRowIterator()->end();
  72  
  73              $this->currentSheetIndex++;
  74          }
  75      }
  76  
  77      /**
  78       * Return the current element
  79       * @see http://php.net/manual/en/iterator.current.php
  80       *
  81       * @return \Box\Spout\Reader\XLSX\Sheet
  82       */
  83      #[\ReturnTypeWillChange]
  84      public function current()
  85      {
  86          return $this->sheets[$this->currentSheetIndex];
  87      }
  88  
  89      /**
  90       * Return the key of the current element
  91       * @see http://php.net/manual/en/iterator.key.php
  92       *
  93       * @return int
  94       */
  95      #[\ReturnTypeWillChange]
  96      public function key()
  97      {
  98          return $this->currentSheetIndex + 1;
  99      }
 100  
 101      /**
 102       * Cleans up what was created to iterate over the object.
 103       *
 104       * @return void
 105       */
 106      #[\ReturnTypeWillChange]
 107      public function end()
 108      {
 109          // make sure we are not leaking memory in case the iteration stopped before the end
 110          foreach ($this->sheets as $sheet) {
 111              $sheet->getRowIterator()->end();
 112          }
 113      }
 114  }