Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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      public function rewind()
  42      {
  43          $this->currentSheetIndex = 0;
  44      }
  45  
  46      /**
  47       * Checks if current position is valid
  48       * @see http://php.net/manual/en/iterator.valid.php
  49       *
  50       * @return bool
  51       */
  52      public function valid()
  53      {
  54          return ($this->currentSheetIndex < \count($this->sheets));
  55      }
  56  
  57      /**
  58       * Move forward to next element
  59       * @see http://php.net/manual/en/iterator.next.php
  60       *
  61       * @return void
  62       */
  63      public function next()
  64      {
  65          // Using isset here because it is way faster than array_key_exists...
  66          if (isset($this->sheets[$this->currentSheetIndex])) {
  67              $currentSheet = $this->sheets[$this->currentSheetIndex];
  68              $currentSheet->getRowIterator()->end();
  69  
  70              $this->currentSheetIndex++;
  71          }
  72      }
  73  
  74      /**
  75       * Return the current element
  76       * @see http://php.net/manual/en/iterator.current.php
  77       *
  78       * @return \Box\Spout\Reader\XLSX\Sheet
  79       */
  80      public function current()
  81      {
  82          return $this->sheets[$this->currentSheetIndex];
  83      }
  84  
  85      /**
  86       * Return the key of the current element
  87       * @see http://php.net/manual/en/iterator.key.php
  88       *
  89       * @return int
  90       */
  91      public function key()
  92      {
  93          return $this->currentSheetIndex + 1;
  94      }
  95  
  96      /**
  97       * Cleans up what was created to iterate over the object.
  98       *
  99       * @return void
 100       */
 101      public function end()
 102      {
 103          // make sure we are not leaking memory in case the iteration stopped before the end
 104          foreach ($this->sheets as $sheet) {
 105              $sheet->getRowIterator()->end();
 106          }
 107      }
 108  }