SheetIterator::end()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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