Completed
Pull Request — master (#387)
by
unknown
06:30
created

SheetIterator::getActiveSheet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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