CellIterator.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 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 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /**
  28. * PHPExcel_Worksheet_CellIterator
  29. *
  30. * Used to iterate rows in a PHPExcel_Worksheet
  31. *
  32. * @category PHPExcel
  33. * @package PHPExcel_Worksheet
  34. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  35. */
  36. class PHPExcel_Worksheet_CellIterator implements Iterator
  37. {
  38. /**
  39. * PHPExcel_Worksheet to iterate
  40. *
  41. * @var PHPExcel_Worksheet
  42. */
  43. private $_subject;
  44. /**
  45. * Row index
  46. *
  47. * @var int
  48. */
  49. private $_rowIndex;
  50. /**
  51. * Current iterator position
  52. *
  53. * @var int
  54. */
  55. private $_position = 0;
  56. /**
  57. * Loop only existing cells
  58. *
  59. * @var boolean
  60. */
  61. private $_onlyExistingCells = true;
  62. /**
  63. * Create a new cell iterator
  64. *
  65. * @param PHPExcel_Worksheet $subject
  66. * @param int $rowIndex
  67. */
  68. public function __construct(PHPExcel_Worksheet $subject = null, $rowIndex = 1) {
  69. // Set subject and row index
  70. $this->_subject = $subject;
  71. $this->_rowIndex = $rowIndex;
  72. }
  73. /**
  74. * Destructor
  75. */
  76. public function __destruct() {
  77. unset($this->_subject);
  78. }
  79. /**
  80. * Rewind iterator
  81. */
  82. public function rewind() {
  83. $this->_position = 0;
  84. }
  85. /**
  86. * Current PHPExcel_Cell
  87. *
  88. * @return PHPExcel_Cell
  89. */
  90. public function current() {
  91. return $this->_subject->getCellByColumnAndRow($this->_position, $this->_rowIndex);
  92. }
  93. /**
  94. * Current key
  95. *
  96. * @return int
  97. */
  98. public function key() {
  99. return $this->_position;
  100. }
  101. /**
  102. * Next value
  103. */
  104. public function next() {
  105. ++$this->_position;
  106. }
  107. /**
  108. * Are there any more PHPExcel_Cell instances available?
  109. *
  110. * @return boolean
  111. */
  112. public function valid() {
  113. // columnIndexFromString() returns an index based at one,
  114. // treat it as a count when comparing it to the base zero
  115. // position.
  116. $columnCount = PHPExcel_Cell::columnIndexFromString($this->_subject->getHighestColumn());
  117. if ($this->_onlyExistingCells) {
  118. // If we aren't looking at an existing cell, either
  119. // because the first column doesn't exist or next() has
  120. // been called onto a nonexistent cell, then loop until we
  121. // find one, or pass the last column.
  122. while ($this->_position < $columnCount &&
  123. !$this->_subject->cellExistsByColumnAndRow($this->_position, $this->_rowIndex)) {
  124. ++$this->_position;
  125. }
  126. }
  127. return $this->_position < $columnCount;
  128. }
  129. /**
  130. * Get loop only existing cells
  131. *
  132. * @return boolean
  133. */
  134. public function getIterateOnlyExistingCells() {
  135. return $this->_onlyExistingCells;
  136. }
  137. /**
  138. * Set the iterator to loop only existing cells
  139. *
  140. * @param boolean $value
  141. */
  142. public function setIterateOnlyExistingCells($value = true) {
  143. $this->_onlyExistingCells = $value;
  144. }
  145. }