Iterator.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Worksheet;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. class Iterator implements \Iterator
  5. {
  6. /**
  7. * Spreadsheet to iterate.
  8. *
  9. * @var Spreadsheet
  10. */
  11. private $subject;
  12. /**
  13. * Current iterator position.
  14. *
  15. * @var int
  16. */
  17. private $position = 0;
  18. /**
  19. * Create a new worksheet iterator.
  20. *
  21. * @param Spreadsheet $subject
  22. */
  23. public function __construct(Spreadsheet $subject = null)
  24. {
  25. // Set subject
  26. $this->subject = $subject;
  27. }
  28. /**
  29. * Destructor.
  30. */
  31. public function __destruct()
  32. {
  33. unset($this->subject);
  34. }
  35. /**
  36. * Rewind iterator.
  37. */
  38. public function rewind()
  39. {
  40. $this->position = 0;
  41. }
  42. /**
  43. * Current Worksheet.
  44. *
  45. * @return Worksheet
  46. */
  47. public function current()
  48. {
  49. return $this->subject->getSheet($this->position);
  50. }
  51. /**
  52. * Current key.
  53. *
  54. * @return int
  55. */
  56. public function key()
  57. {
  58. return $this->position;
  59. }
  60. /**
  61. * Next value.
  62. */
  63. public function next()
  64. {
  65. ++$this->position;
  66. }
  67. /**
  68. * Are there more Worksheet instances available?
  69. *
  70. * @return bool
  71. */
  72. public function valid()
  73. {
  74. return $this->position < $this->subject->getSheetCount();
  75. }
  76. }