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

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