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 PhpOffice\PhpSpreadsheet\Cell\Coordinate;
   6  use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
   7  
   8  class ColumnCellIterator extends CellIterator
   9  {
  10      /**
  11       * Current iterator position.
  12       *
  13       * @var int
  14       */
  15      private $currentRow;
  16  
  17      /**
  18       * Column index.
  19       *
  20       * @var string
  21       */
  22      private $columnIndex;
  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 string $columnIndex The column that we want to iterate
  43       * @param int $startRow The row number at which to start iterating
  44       * @param int $endRow Optionally, the row number at which to stop iterating
  45       */
  46      public function __construct(?Worksheet $subject = null, $columnIndex = 'A', $startRow = 1, $endRow = null)
  47      {
  48          // Set subject
  49          $this->worksheet = $subject;
  50          $this->columnIndex = Coordinate::columnIndexFromString($columnIndex);
  51          $this->resetEnd($endRow);
  52          $this->resetStart($startRow);
  53      }
  54  
  55      /**
  56       * (Re)Set the start row and the current row pointer.
  57       *
  58       * @param int $startRow The row number at which to start iterating
  59       *
  60       * @return $this
  61       */
  62      public function resetStart($startRow = 1)
  63      {
  64          $this->startRow = $startRow;
  65          $this->adjustForExistingOnlyRange();
  66          $this->seek($startRow);
  67  
  68          return $this;
  69      }
  70  
  71      /**
  72       * (Re)Set the end row.
  73       *
  74       * @param int $endRow The row number at which to stop iterating
  75       *
  76       * @return $this
  77       */
  78      public function resetEnd($endRow = null)
  79      {
  80          $this->endRow = ($endRow) ? $endRow : $this->worksheet->getHighestRow();
  81          $this->adjustForExistingOnlyRange();
  82  
  83          return $this;
  84      }
  85  
  86      /**
  87       * Set the row pointer to the selected row.
  88       *
  89       * @param int $row The row number to set the current pointer at
  90       *
  91       * @return $this
  92       */
  93      public function seek($row = 1)
  94      {
  95          if ($this->onlyExistingCells && !($this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $row))) {
  96              throw new PhpSpreadsheetException('In "IterateOnlyExistingCells" mode and Cell does not exist');
  97          }
  98          if (($row < $this->startRow) || ($row > $this->endRow)) {
  99              throw new PhpSpreadsheetException("Row $row is out of range ({$this->startRow} - {$this->endRow})");
 100          }
 101          $this->currentRow = $row;
 102  
 103          return $this;
 104      }
 105  
 106      /**
 107       * Rewind the iterator to the starting row.
 108       */
 109      public function rewind(): void
 110      {
 111          $this->currentRow = $this->startRow;
 112      }
 113  
 114      /**
 115       * Return the current cell in this worksheet column.
 116       *
 117       * @return \PhpOffice\PhpSpreadsheet\Cell\Cell
 118       */
 119      public function current()
 120      {
 121          return $this->worksheet->getCellByColumnAndRow($this->columnIndex, $this->currentRow);
 122      }
 123  
 124      /**
 125       * Return the current iterator key.
 126       *
 127       * @return int
 128       */
 129      public function key()
 130      {
 131          return $this->currentRow;
 132      }
 133  
 134      /**
 135       * Set the iterator to its next value.
 136       */
 137      public function next(): void
 138      {
 139          do {
 140              ++$this->currentRow;
 141          } while (
 142              ($this->onlyExistingCells) &&
 143              (!$this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $this->currentRow)) &&
 144              ($this->currentRow <= $this->endRow)
 145          );
 146      }
 147  
 148      /**
 149       * Set the iterator to its previous value.
 150       */
 151      public function prev(): void
 152      {
 153          do {
 154              --$this->currentRow;
 155          } while (
 156              ($this->onlyExistingCells) &&
 157              (!$this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $this->currentRow)) &&
 158              ($this->currentRow >= $this->startRow)
 159          );
 160      }
 161  
 162      /**
 163       * Indicate if more rows exist in the worksheet range of rows that we're iterating.
 164       *
 165       * @return bool
 166       */
 167      public function valid()
 168      {
 169          return $this->currentRow <= $this->endRow && $this->currentRow >= $this->startRow;
 170      }
 171  
 172      /**
 173       * Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary.
 174       */
 175      protected function adjustForExistingOnlyRange(): void
 176      {
 177          if ($this->onlyExistingCells) {
 178              while (
 179                  (!$this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $this->startRow)) &&
 180                  ($this->startRow <= $this->endRow)
 181              ) {
 182                  ++$this->startRow;
 183              }
 184              while (
 185                  (!$this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $this->endRow)) &&
 186                  ($this->endRow >= $this->startRow)
 187              ) {
 188                  --$this->endRow;
 189              }
 190          }
 191      }
 192  }