PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.11.14
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.11.14
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 All 149 releases
visualizer / vendor / openspout / openspout / src / Reader / XLSX / SheetIterator.php

SheetIterator.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.11.14, at vendor/openspout/openspout/src/Reader/XLSX/SheetIterator.php

114 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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