Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

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