| 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\ReaderOptions $options Reader's current options |
| 26 |
* @param \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper $sharedStringsHelper |
| 27 |
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper |
| 28 |
* @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file |
| 29 |
*/ |
| 30 |
public function __construct($filePath, $options, $sharedStringsHelper, $globalFunctionsHelper) |
| 31 |
{ |
| 32 |
// Fetch all available sheets |
| 33 |
$sheetHelper = new SheetHelper($filePath, $options, $sharedStringsHelper, $globalFunctionsHelper); |
| 34 |
$this->sheets = $sheetHelper->getSheets(); |
| 35 |
|
| 36 |
if (count($this->sheets) === 0) { |
| 37 |
throw new NoSheetsFoundException('The file must contain at least one sheet.'); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Rewind the Iterator to the first element |
| 43 |
* @link http://php.net/manual/en/iterator.rewind.php |
| 44 |
* |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public function rewind() |
| 48 |
{ |
| 49 |
$this->currentSheetIndex = 0; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Checks if current position is valid |
| 54 |
* @link http://php.net/manual/en/iterator.valid.php |
| 55 |
* |
| 56 |
* @return bool |
| 57 |
*/ |
| 58 |
public function valid() |
| 59 |
{ |
| 60 |
return ($this->currentSheetIndex < count($this->sheets)); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Move forward to next element |
| 65 |
* @link http://php.net/manual/en/iterator.next.php |
| 66 |
* |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function next() |
| 70 |
{ |
| 71 |
// Using isset here because it is way faster than array_key_exists... |
| 72 |
if (isset($this->sheets[$this->currentSheetIndex])) { |
| 73 |
$currentSheet = $this->sheets[$this->currentSheetIndex]; |
| 74 |
$currentSheet->getRowIterator()->end(); |
| 75 |
|
| 76 |
$this->currentSheetIndex++; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Return the current element |
| 82 |
* @link http://php.net/manual/en/iterator.current.php |
| 83 |
* |
| 84 |
* @return \Box\Spout\Reader\XLSX\Sheet |
| 85 |
*/ |
| 86 |
public function current() |
| 87 |
{ |
| 88 |
return $this->sheets[$this->currentSheetIndex]; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Return the key of the current element |
| 93 |
* @link http://php.net/manual/en/iterator.key.php |
| 94 |
* |
| 95 |
* @return int |
| 96 |
*/ |
| 97 |
public function key() |
| 98 |
{ |
| 99 |
return $this->currentSheetIndex + 1; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Cleans up what was created to iterate over the object. |
| 104 |
* |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
public function end() |
| 108 |
{ |
| 109 |
// make sure we are not leaking memory in case the iteration stopped before the end |
| 110 |
foreach ($this->sheets as $sheet) { |
| 111 |
$sheet->getRowIterator()->end(); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|