[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/spout/src/Spout/Reader/XLSX/ -> SheetIterator.php (source)

   1  <?php
   2  
   3  namespace Box\Spout\Reader\XLSX;
   4  
   5  use Box\Spout\Reader\IteratorInterface;
   6  use Box\Spout\Reader\XLSX\Helper\SheetHelper;
   7  use Box\Spout\Reader\Exception\NoSheetsFoundException;
   8  
   9  /**
  10   * Class SheetIterator
  11   * Iterate over XLSX sheet.
  12   *
  13   * @package Box\Spout\Reader\XLSX
  14   */
  15  class SheetIterator implements IteratorInterface
  16  {
  17      /** @var \Box\Spout\Reader\XLSX\Sheet[] The list of sheet present in the file */
  18      protected $sheets;
  19  
  20      /** @var int The index of the sheet being read (zero-based) */
  21      protected $currentSheetIndex;
  22  
  23      /**
  24       * @param string $filePath Path of the file to be read
  25       * @param \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper $sharedStringsHelper
  26       * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
  27       * @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file
  28       */
  29      public function __construct($filePath, $sharedStringsHelper, $globalFunctionsHelper)
  30      {
  31          // Fetch all available sheets
  32          $sheetHelper = new SheetHelper($filePath, $sharedStringsHelper, $globalFunctionsHelper);
  33          $this->sheets = $sheetHelper->getSheets();
  34  
  35          if (count($this->sheets) === 0) {
  36              throw new NoSheetsFoundException('The file must contain at least one sheet.');
  37          }
  38      }
  39  
  40      /**
  41       * Rewind the Iterator to the first element
  42       * @link http://php.net/manual/en/iterator.rewind.php
  43       *
  44       * @return void
  45       */
  46      public function rewind()
  47      {
  48          $this->currentSheetIndex = 0;
  49      }
  50  
  51      /**
  52       * Checks if current position is valid
  53       * @link http://php.net/manual/en/iterator.valid.php
  54       *
  55       * @return boolean
  56       */
  57      public function valid()
  58      {
  59          return ($this->currentSheetIndex < count($this->sheets));
  60      }
  61  
  62      /**
  63       * Move forward to next element
  64       * @link http://php.net/manual/en/iterator.next.php
  65       *
  66       * @return void
  67       */
  68      public function next()
  69      {
  70          // Using isset here because it is way faster than array_key_exists...
  71          if (isset($this->sheets[$this->currentSheetIndex])) {
  72              $currentSheet = $this->sheets[$this->currentSheetIndex];
  73              $currentSheet->getRowIterator()->end();
  74  
  75              $this->currentSheetIndex++;
  76          }
  77      }
  78  
  79      /**
  80       * Return the current element
  81       * @link http://php.net/manual/en/iterator.current.php
  82       *
  83       * @return \Box\Spout\Reader\XLSX\Sheet
  84       */
  85      public function current()
  86      {
  87          return $this->sheets[$this->currentSheetIndex];
  88      }
  89  
  90      /**
  91       * Return the key of the current element
  92       * @link http://php.net/manual/en/iterator.key.php
  93       *
  94       * @return int
  95       */
  96      public function key()
  97      {
  98          return $this->currentSheetIndex + 1;
  99      }
 100  
 101      /**
 102       * Cleans up what was created to iterate over the object.
 103       *
 104       * @return void
 105       */
 106      public function end()
 107      {
 108          // make sure we are not leaking memory in case the iteration stopped before the end
 109          foreach ($this->sheets as $sheet) {
 110              $sheet->getRowIterator()->end();
 111          }
 112      }
 113  }


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