RowIterator.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. * PHPExcel_Worksheet_RowIterator
  29. *
  30. * Used to iterate rows in a PHPExcel_Worksheet
  31. *
  32. * @category PHPExcel
  33. * @package PHPExcel_Worksheet
  34. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  35. */
  36. class PHPExcel_Worksheet_RowIterator implements Iterator
  37. {
  38. /**
  39. * PHPExcel_Worksheet to iterate
  40. *
  41. * @var PHPExcel_Worksheet
  42. */
  43. private $_subject;
  44. /**
  45. * Current iterator position
  46. *
  47. * @var int
  48. */
  49. private $_position = 1;
  50. /**
  51. * Start position
  52. *
  53. * @var int
  54. */
  55. private $_startRow = 1;
  56. /**
  57. * Create a new row iterator
  58. *
  59. * @param PHPExcel_Worksheet $subject The worksheet to iterate over
  60. * @param integer $startRow The row number at which to start iterating
  61. */
  62. public function __construct(PHPExcel_Worksheet $subject = null, $startRow = 1) {
  63. // Set subject
  64. $this->_subject = $subject;
  65. $this->resetStart($startRow);
  66. }
  67. /**
  68. * Destructor
  69. */
  70. public function __destruct() {
  71. unset($this->_subject);
  72. }
  73. /**
  74. * (Re)Set the start row and the current row pointer
  75. *
  76. * @param integer $startRow The row number at which to start iterating
  77. */
  78. public function resetStart($startRow = 1) {
  79. $this->_startRow = $startRow;
  80. $this->seek($startRow);
  81. }
  82. /**
  83. * Set the row pointer to the selected row
  84. *
  85. * @param integer $row The row number to set the current pointer at
  86. */
  87. public function seek($row = 1) {
  88. $this->_position = $row;
  89. }
  90. /**
  91. * Rewind the iterator to the starting row
  92. */
  93. public function rewind() {
  94. $this->_position = $this->_startRow;
  95. }
  96. /**
  97. * Return the current row in this worksheet
  98. *
  99. * @return PHPExcel_Worksheet_Row
  100. */
  101. public function current() {
  102. return new PHPExcel_Worksheet_Row($this->_subject, $this->_position);
  103. }
  104. /**
  105. * Return the current iterator key
  106. *
  107. * @return int
  108. */
  109. public function key() {
  110. return $this->_position;
  111. }
  112. /**
  113. * Set the iterator to its next value
  114. */
  115. public function next() {
  116. ++$this->_position;
  117. }
  118. /**
  119. * Set the iterator to its previous value
  120. */
  121. public function prev() {
  122. if ($this->_position > 1)
  123. --$this->_position;
  124. }
  125. /**
  126. * Indicate if more rows exist in the worksheet
  127. *
  128. * @return boolean
  129. */
  130. public function valid() {
  131. return $this->_position <= $this->_subject->getHighestRow();
  132. }
  133. }