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