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 401 and 402] [Versions 401 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Worksheet;
   4  
   5  use Iterator;
   6  use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
   7  
   8  /**
   9   * @implements Iterator<int, Row>
  10   */
  11  class RowIterator implements Iterator
  12  {
  13      /**
  14       * Worksheet to iterate.
  15       *
  16       * @var Worksheet
  17       */
  18      private $subject;
  19  
  20      /**
  21       * Current iterator position.
  22       *
  23       * @var int
  24       */
  25      private $position = 1;
  26  
  27      /**
  28       * Start position.
  29       *
  30       * @var int
  31       */
  32      private $startRow = 1;
  33  
  34      /**
  35       * End position.
  36       *
  37       * @var int
  38       */
  39      private $endRow = 1;
  40  
  41      /**
  42       * Create a new row iterator.
  43       *
  44       * @param Worksheet $subject The worksheet to iterate over
  45       * @param int $startRow The row number at which to start iterating
  46       * @param int $endRow Optionally, the row number at which to stop iterating
  47       */
  48      public function __construct(Worksheet $subject, $startRow = 1, $endRow = null)
  49      {
  50          // Set subject
  51          $this->subject = $subject;
  52          $this->resetEnd($endRow);
  53          $this->resetStart($startRow);
  54      }
  55  
  56      /**
  57       * (Re)Set the start row and the current row pointer.
  58       *
  59       * @param int $startRow The row number at which to start iterating
  60       *
  61       * @return $this
  62       */
  63      public function resetStart(int $startRow = 1)
  64      {
  65          if ($startRow > $this->subject->getHighestRow()) {
  66              throw new PhpSpreadsheetException(
  67                  "Start row ({$startRow}) is beyond highest row ({$this->subject->getHighestRow()})"
  68              );
  69          }
  70  
  71          $this->startRow = $startRow;
  72          if ($this->endRow < $this->startRow) {
  73              $this->endRow = $this->startRow;
  74          }
  75          $this->seek($startRow);
  76  
  77          return $this;
  78      }
  79  
  80      /**
  81       * (Re)Set the end row.
  82       *
  83       * @param int $endRow The row number at which to stop iterating
  84       *
  85       * @return $this
  86       */
  87      public function resetEnd($endRow = null)
  88      {
  89          $this->endRow = $endRow ?: $this->subject->getHighestRow();
  90  
  91          return $this;
  92      }
  93  
  94      /**
  95       * Set the row pointer to the selected row.
  96       *
  97       * @param int $row The row number to set the current pointer at
  98       *
  99       * @return $this
 100       */
 101      public function seek(int $row = 1)
 102      {
 103          if (($row < $this->startRow) || ($row > $this->endRow)) {
 104              throw new PhpSpreadsheetException("Row $row is out of range ({$this->startRow} - {$this->endRow})");
 105          }
 106          $this->position = $row;
 107  
 108          return $this;
 109      }
 110  
 111      /**
 112       * Rewind the iterator to the starting row.
 113       */
 114      public function rewind(): void
 115      {
 116          $this->position = $this->startRow;
 117      }
 118  
 119      /**
 120       * Return the current row in this worksheet.
 121       */
 122      public function current(): Row
 123      {
 124          return new Row($this->subject, $this->position);
 125      }
 126  
 127      /**
 128       * Return the current iterator key.
 129       */
 130      public function key(): int
 131      {
 132          return $this->position;
 133      }
 134  
 135      /**
 136       * Set the iterator to its next value.
 137       */
 138      public function next(): void
 139      {
 140          ++$this->position;
 141      }
 142  
 143      /**
 144       * Set the iterator to its previous value.
 145       */
 146      public function prev(): void
 147      {
 148          --$this->position;
 149      }
 150  
 151      /**
 152       * Indicate if more rows exist in the worksheet range of rows that we're iterating.
 153       */
 154      public function valid(): bool
 155      {
 156          return $this->position <= $this->endRow && $this->position >= $this->startRow;
 157      }
 158  }