[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/Worksheet/ -> ColumnIterator.php (source)

   1  <?php
   2  
   3  /**
   4   * PHPExcel_Worksheet_ColumnIterator
   5   *
   6   * Copyright (c) 2006 - 2015 PHPExcel
   7   *
   8   * This library is free software; you can redistribute it and/or
   9   * modify it under the terms of the GNU Lesser General Public
  10   * License as published by the Free Software Foundation; either
  11   * version 2.1 of the License, or (at your option) any later version.
  12   *
  13   * This library is distributed in the hope that it will be useful,
  14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16   * Lesser General Public License for more details.
  17   *
  18   * You should have received a copy of the GNU Lesser General Public
  19   * License along with this library; if not, write to the Free Software
  20   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  21   *
  22   * @category   PHPExcel
  23   * @package    PHPExcel_Worksheet
  24   * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  25   * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  26   * @version    ##VERSION##, ##DATE##
  27   */
  28  class PHPExcel_Worksheet_ColumnIterator implements Iterator
  29  {
  30      /**
  31       * PHPExcel_Worksheet to iterate
  32       *
  33       * @var PHPExcel_Worksheet
  34       */
  35      private $subject;
  36  
  37      /**
  38       * Current iterator position
  39       *
  40       * @var int
  41       */
  42      private $position = 0;
  43  
  44      /**
  45       * Start position
  46       *
  47       * @var int
  48       */
  49      private $startColumn = 0;
  50  
  51  
  52      /**
  53       * End position
  54       *
  55       * @var int
  56       */
  57      private $endColumn = 0;
  58  
  59  
  60      /**
  61       * Create a new column iterator
  62       *
  63       * @param    PHPExcel_Worksheet    $subject    The worksheet to iterate over
  64       * @param    string                $startColumn    The column address at which to start iterating
  65       * @param    string                $endColumn        Optionally, the column address at which to stop iterating
  66       */
  67      public function __construct(PHPExcel_Worksheet $subject = null, $startColumn = 'A', $endColumn = null)
  68      {
  69          // Set subject
  70          $this->subject = $subject;
  71          $this->resetEnd($endColumn);
  72          $this->resetStart($startColumn);
  73      }
  74  
  75      /**
  76       * Destructor
  77       */
  78      public function __destruct()
  79      {
  80          unset($this->subject);
  81      }
  82  
  83      /**
  84       * (Re)Set the start column and the current column pointer
  85       *
  86       * @param integer    $startColumn    The column address at which to start iterating
  87       * @return PHPExcel_Worksheet_ColumnIterator
  88       */
  89      public function resetStart($startColumn = 'A')
  90      {
  91          $startColumnIndex = PHPExcel_Cell::columnIndexFromString($startColumn) - 1;
  92          $this->startColumn = $startColumnIndex;
  93          $this->seek($startColumn);
  94  
  95          return $this;
  96      }
  97  
  98      /**
  99       * (Re)Set the end column
 100       *
 101       * @param string    $endColumn    The column address at which to stop iterating
 102       * @return PHPExcel_Worksheet_ColumnIterator
 103       */
 104      public function resetEnd($endColumn = null)
 105      {
 106          $endColumn = ($endColumn) ? $endColumn : $this->subject->getHighestColumn();
 107          $this->endColumn = PHPExcel_Cell::columnIndexFromString($endColumn) - 1;
 108  
 109          return $this;
 110      }
 111  
 112      /**
 113       * Set the column pointer to the selected column
 114       *
 115       * @param string    $column    The column address to set the current pointer at
 116       * @return PHPExcel_Worksheet_ColumnIterator
 117       * @throws PHPExcel_Exception
 118       */
 119      public function seek($column = 'A')
 120      {
 121          $column = PHPExcel_Cell::columnIndexFromString($column) - 1;
 122          if (($column < $this->startColumn) || ($column > $this->endColumn)) {
 123              throw new PHPExcel_Exception("Column $column is out of range ({$this->startColumn} - {$this->endColumn})");
 124          }
 125          $this->position = $column;
 126  
 127          return $this;
 128      }
 129  
 130      /**
 131       * Rewind the iterator to the starting column
 132       */
 133      public function rewind()
 134      {
 135          $this->position = $this->startColumn;
 136      }
 137  
 138      /**
 139       * Return the current column in this worksheet
 140       *
 141       * @return PHPExcel_Worksheet_Column
 142       */
 143      public function current()
 144      {
 145          return new PHPExcel_Worksheet_Column($this->subject, PHPExcel_Cell::stringFromColumnIndex($this->position));
 146      }
 147  
 148      /**
 149       * Return the current iterator key
 150       *
 151       * @return string
 152       */
 153      public function key()
 154      {
 155          return PHPExcel_Cell::stringFromColumnIndex($this->position);
 156      }
 157  
 158      /**
 159       * Set the iterator to its next value
 160       */
 161      public function next()
 162      {
 163          ++$this->position;
 164      }
 165  
 166      /**
 167       * Set the iterator to its previous value
 168       *
 169       * @throws PHPExcel_Exception
 170       */
 171      public function prev()
 172      {
 173          if ($this->position <= $this->startColumn) {
 174              throw new PHPExcel_Exception(
 175                  "Column is already at the beginning of range (" .
 176                  PHPExcel_Cell::stringFromColumnIndex($this->endColumn) . " - " .
 177                  PHPExcel_Cell::stringFromColumnIndex($this->endColumn) . ")"
 178              );
 179          }
 180  
 181          --$this->position;
 182      }
 183  
 184      /**
 185       * Indicate if more columns exist in the worksheet range of columns that we're iterating
 186       *
 187       * @return boolean
 188       */
 189      public function valid()
 190      {
 191          return $this->position <= $this->endColumn;
 192      }
 193  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1