1: <?php
2: /**
3: * PHPExcel
4: *
5: * Copyright (c) 2006 - 2014 PHPExcel
6: *
7: * This library is free software; you can redistribute it and/or
8: * modify it under the terms of the GNU Lesser General Public
9: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11: *
12: * This library is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this library; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20: *
21: * @category PHPExcel
22: * @package PHPExcel_Worksheet
23: * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
24: * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25: * @version 1.8.0, 2014-03-02
26: */
27:
28:
29: /**
30: * PHPExcel_Worksheet_CellIterator
31: *
32: * Used to iterate rows in a PHPExcel_Worksheet
33: *
34: * @category PHPExcel
35: * @package PHPExcel_Worksheet
36: * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
37: */
38: class PHPExcel_Worksheet_CellIterator implements Iterator
39: {
40: /**
41: * PHPExcel_Worksheet to iterate
42: *
43: * @var PHPExcel_Worksheet
44: */
45: private $_subject;
46:
47: /**
48: * Row index
49: *
50: * @var int
51: */
52: private $_rowIndex;
53:
54: /**
55: * Current iterator position
56: *
57: * @var int
58: */
59: private $_position = 0;
60:
61: /**
62: * Loop only existing cells
63: *
64: * @var boolean
65: */
66: private $_onlyExistingCells = true;
67:
68: /**
69: * Create a new cell iterator
70: *
71: * @param PHPExcel_Worksheet $subject
72: * @param int $rowIndex
73: */
74: public function __construct(PHPExcel_Worksheet $subject = null, $rowIndex = 1) {
75: // Set subject and row index
76: $this->_subject = $subject;
77: $this->_rowIndex = $rowIndex;
78: }
79:
80: /**
81: * Destructor
82: */
83: public function __destruct() {
84: unset($this->_subject);
85: }
86:
87: /**
88: * Rewind iterator
89: */
90: public function rewind() {
91: $this->_position = 0;
92: }
93:
94: /**
95: * Current PHPExcel_Cell
96: *
97: * @return PHPExcel_Cell
98: */
99: public function current() {
100: return $this->_subject->getCellByColumnAndRow($this->_position, $this->_rowIndex);
101: }
102:
103: /**
104: * Current key
105: *
106: * @return int
107: */
108: public function key() {
109: return $this->_position;
110: }
111:
112: /**
113: * Next value
114: */
115: public function next() {
116: ++$this->_position;
117: }
118:
119: /**
120: * Are there any more PHPExcel_Cell instances available?
121: *
122: * @return boolean
123: */
124: public function valid() {
125: // columnIndexFromString() returns an index based at one,
126: // treat it as a count when comparing it to the base zero
127: // position.
128: $columnCount = PHPExcel_Cell::columnIndexFromString($this->_subject->getHighestColumn());
129:
130: if ($this->_onlyExistingCells) {
131: // If we aren't looking at an existing cell, either
132: // because the first column doesn't exist or next() has
133: // been called onto a nonexistent cell, then loop until we
134: // find one, or pass the last column.
135: while ($this->_position < $columnCount &&
136: !$this->_subject->cellExistsByColumnAndRow($this->_position, $this->_rowIndex)) {
137: ++$this->_position;
138: }
139: }
140:
141: return $this->_position < $columnCount;
142: }
143:
144: /**
145: * Get loop only existing cells
146: *
147: * @return boolean
148: */
149: public function getIterateOnlyExistingCells() {
150: return $this->_onlyExistingCells;
151: }
152:
153: /**
154: * Set the iterator to loop only existing cells
155: *
156: * @param boolean $value
157: */
158: public function setIterateOnlyExistingCells($value = true) {
159: $this->_onlyExistingCells = $value;
160: }
161: }
162: