vendor/jackalope/jackalope/src/Jackalope/Query/RowIterator.php line 86

Open in your IDE?
  1. <?php
  2. namespace Jackalope\Query;
  3. use Countable;
  4. use SeekableIterator;
  5. use OutOfBoundsException;
  6. use Jackalope\ObjectManager;
  7. use Jackalope\FactoryInterface;
  8. /**
  9. * Iterator to efficiently iterate over the raw query result.
  10. *
  11. * @license http://www.apache.org/licenses Apache License Version 2.0, January 2004
  12. * @license http://opensource.org/licenses/MIT MIT License
  13. */
  14. class RowIterator implements SeekableIterator, Countable
  15. {
  16. /**
  17. * @var ObjectManager
  18. */
  19. protected $objectManager;
  20. /**
  21. * @var FactoryInterface
  22. */
  23. protected $factory;
  24. /**
  25. * @var array
  26. */
  27. protected $rows;
  28. /**
  29. * @var integer
  30. */
  31. protected $position = 0;
  32. /**
  33. * Create the iterator.
  34. *
  35. * @param FactoryInterface $factory the object factory
  36. * @param ObjectManager $objectManager
  37. * @param array $rows Raw data as described in QueryResult and \Jackalope\Transport\TransportInterface
  38. */
  39. public function __construct(FactoryInterface $factory, ObjectManager $objectManager, $rows)
  40. {
  41. $this->factory = $factory;
  42. $this->objectManager = $objectManager;
  43. $this->rows = $rows;
  44. }
  45. /**
  46. * @param int $position
  47. *
  48. * @return void
  49. *
  50. * @throws OutOfBoundsException
  51. */
  52. #[\ReturnTypeWillChange]
  53. public function seek($position)
  54. {
  55. $this->position = $position;
  56. if (!$this->valid()) {
  57. throw new OutOfBoundsException("invalid seek position ($position)");
  58. }
  59. }
  60. /**
  61. * @return integer
  62. */
  63. #[\ReturnTypeWillChange]
  64. public function count()
  65. {
  66. return count($this->rows);
  67. }
  68. #[\ReturnTypeWillChange]
  69. public function rewind()
  70. {
  71. $this->position = 0;
  72. }
  73. #[\ReturnTypeWillChange]
  74. public function current()
  75. {
  76. if (!$this->valid()) {
  77. return null;
  78. }
  79. return $this->factory->get(Row::class, [$this->objectManager, $this->rows[$this->position]]);
  80. }
  81. #[\ReturnTypeWillChange]
  82. public function key()
  83. {
  84. return $this->position;
  85. }
  86. #[\ReturnTypeWillChange]
  87. public function next()
  88. {
  89. ++$this->position;
  90. }
  91. /**
  92. * @return boolean
  93. */
  94. #[\ReturnTypeWillChange]
  95. public function valid()
  96. {
  97. return isset($this->rows[$this->position]);
  98. }
  99. }