| 1 |
<?php |
| 2 |
|
| 3 |
namespace OpenSpout\Reader\XLSX; |
| 4 |
|
| 5 |
use OpenSpout\Reader\Exception\NoSheetsFoundException; |
| 6 |
use OpenSpout\Reader\IteratorInterface; |
| 7 |
use OpenSpout\Reader\XLSX\Manager\SheetManager; |
| 8 |
|
| 9 |
/** |
| 10 |
* Iterate over XLSX sheet. |
| 11 |
*/ |
| 12 |
class SheetIterator implements IteratorInterface |
| 13 |
{ |
| 14 |
/** @var \OpenSpout\Reader\XLSX\Sheet[] The list of sheet present in the file */ |
| 15 |
protected $sheets; |
| 16 |
|
| 17 |
/** @var int The index of the sheet being read (zero-based) */ |
| 18 |
protected $currentSheetIndex; |
| 19 |
|
| 20 |
/** |
| 21 |
* @param SheetManager $sheetManager Manages sheets |
| 22 |
* |
| 23 |
* @throws \OpenSpout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file |
| 24 |
*/ |
| 25 |
public function __construct($sheetManager) |
| 26 |
{ |
| 27 |
// Fetch all available sheets |
| 28 |
$this->sheets = $sheetManager->getSheets(); |
| 29 |
|
| 30 |
if (0 === \count($this->sheets)) { |
| 31 |
throw new NoSheetsFoundException('The file must contain at least one sheet.'); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Rewind the Iterator to the first element. |
| 37 |
* |
| 38 |
* @see http://php.net/manual/en/iterator.rewind.php |
| 39 |
*/ |
| 40 |
#[\ReturnTypeWillChange] |
| 41 |
public function rewind() |
| 42 |
{ |
| 43 |
$this->currentSheetIndex = 0; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Checks if current position is valid. |
| 48 |
* |
| 49 |
* @see http://php.net/manual/en/iterator.valid.php |
| 50 |
* |
| 51 |
* @return bool |
| 52 |
*/ |
| 53 |
#[\ReturnTypeWillChange] |
| 54 |
public function valid() |
| 55 |
{ |
| 56 |
return $this->currentSheetIndex < \count($this->sheets); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Move forward to next element. |
| 61 |
* |
| 62 |
* @see http://php.net/manual/en/iterator.next.php |
| 63 |
*/ |
| 64 |
#[\ReturnTypeWillChange] |
| 65 |
public function next() |
| 66 |
{ |
| 67 |
// Using isset here because it is way faster than array_key_exists... |
| 68 |
if (isset($this->sheets[$this->currentSheetIndex])) { |
| 69 |
$currentSheet = $this->sheets[$this->currentSheetIndex]; |
| 70 |
$currentSheet->getRowIterator()->end(); |
| 71 |
|
| 72 |
++$this->currentSheetIndex; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Return the current element. |
| 78 |
* |
| 79 |
* @see http://php.net/manual/en/iterator.current.php |
| 80 |
* |
| 81 |
* @return \OpenSpout\Reader\XLSX\Sheet |
| 82 |
*/ |
| 83 |
#[\ReturnTypeWillChange] |
| 84 |
public function current() |
| 85 |
{ |
| 86 |
return $this->sheets[$this->currentSheetIndex]; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Return the key of the current element. |
| 91 |
* |
| 92 |
* @see http://php.net/manual/en/iterator.key.php |
| 93 |
* |
| 94 |
* @return int |
| 95 |
*/ |
| 96 |
#[\ReturnTypeWillChange] |
| 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 |
#[\ReturnTypeWillChange] |
| 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 |
} |
| 114 |
|