SearchResultIterator.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Data;
  7. use Magento\Framework\DB\QueryInterface;
  8. /**
  9. * Class SearchResultIterator
  10. */
  11. class SearchResultIterator implements \Iterator
  12. {
  13. /**
  14. * @var SearchResultInterface
  15. */
  16. protected $searchResult;
  17. /**
  18. * @var QueryInterface
  19. */
  20. protected $query;
  21. /**
  22. * @var array
  23. */
  24. protected $current;
  25. /**
  26. * @var int
  27. */
  28. protected $key = 0;
  29. /**
  30. * @param AbstractSearchResult $searchResult
  31. * @param QueryInterface $query
  32. */
  33. public function __construct(AbstractSearchResult $searchResult, QueryInterface $query)
  34. {
  35. $this->searchResult = $searchResult;
  36. $this->query = $query;
  37. }
  38. /**
  39. * @return array|mixed
  40. */
  41. public function current()
  42. {
  43. return $this->current;
  44. }
  45. /**
  46. * @return void
  47. */
  48. public function next()
  49. {
  50. ++$this->key;
  51. $this->current = $this->searchResult->createDataObject($this->query->fetchItem());
  52. }
  53. /**
  54. * @return int|mixed
  55. */
  56. public function key()
  57. {
  58. return $this->key;
  59. }
  60. /**
  61. * @return bool
  62. */
  63. public function valid()
  64. {
  65. return !empty($this->current);
  66. }
  67. /**
  68. * @return void
  69. */
  70. public function rewind()
  71. {
  72. $this->current = null;
  73. $this->key = 0;
  74. $this->query->reset();
  75. $this->next();
  76. }
  77. }