IterableResult.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Internal\Hydration;
  20. /**
  21. * Represents a result structure that can be iterated over, hydrating row-by-row
  22. * during the iteration. An IterableResult is obtained by AbstractHydrator#iterate().
  23. *
  24. * @author robo
  25. * @since 2.0
  26. */
  27. class IterableResult implements \Iterator
  28. {
  29. /**
  30. * @var \Doctrine\ORM\Internal\Hydration\AbstractHydrator
  31. */
  32. private $_hydrator;
  33. /**
  34. * @var boolean
  35. */
  36. private $_rewinded = false;
  37. /**
  38. * @var integer
  39. */
  40. private $_key = -1;
  41. /**
  42. * @var object
  43. */
  44. private $_current = null;
  45. /**
  46. * @param \Doctrine\ORM\Internal\Hydration\AbstractHydrator $hydrator
  47. */
  48. public function __construct($hydrator)
  49. {
  50. $this->_hydrator = $hydrator;
  51. }
  52. public function rewind()
  53. {
  54. if ($this->_rewinded == true) {
  55. throw new HydrationException("Can only iterate a Result once.");
  56. } else {
  57. $this->_current = $this->next();
  58. $this->_rewinded = true;
  59. }
  60. }
  61. /**
  62. * Gets the next set of results.
  63. *
  64. * @return array
  65. */
  66. public function next()
  67. {
  68. $this->_current = $this->_hydrator->hydrateRow();
  69. $this->_key++;
  70. return $this->_current;
  71. }
  72. /**
  73. * @return mixed
  74. */
  75. public function current()
  76. {
  77. return $this->_current;
  78. }
  79. /**
  80. * @return int
  81. */
  82. public function key()
  83. {
  84. return $this->_key;
  85. }
  86. /**
  87. * @return bool
  88. */
  89. public function valid()
  90. {
  91. return ($this->_current!=false);
  92. }
  93. }