ResultSet.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace Elastica\Multi;
  3. use Elastica\Response;
  4. /**
  5. * Elastica multi search result set
  6. * List of result sets for each search request.
  7. *
  8. * @author munkie
  9. */
  10. class ResultSet implements \Iterator, \ArrayAccess, \Countable
  11. {
  12. /**
  13. * Result Sets.
  14. *
  15. * @var array|\Elastica\ResultSet[] Result Sets
  16. */
  17. protected $_resultSets = [];
  18. /**
  19. * Current position.
  20. *
  21. * @var int Current position
  22. */
  23. protected $_position = 0;
  24. /**
  25. * Response.
  26. *
  27. * @var \Elastica\Response Response object
  28. */
  29. protected $_response;
  30. /**
  31. * Constructs ResultSet object.
  32. *
  33. * @param \Elastica\Response $response
  34. * @param \Elastica\ResultSet[] $resultSets
  35. */
  36. public function __construct(Response $response, $resultSets)
  37. {
  38. $this->_response = $response;
  39. $this->_resultSets = $resultSets;
  40. }
  41. /**
  42. * @return array|\Elastica\ResultSet[]
  43. */
  44. public function getResultSets()
  45. {
  46. return $this->_resultSets;
  47. }
  48. /**
  49. * Returns response object.
  50. *
  51. * @return \Elastica\Response Response object
  52. */
  53. public function getResponse()
  54. {
  55. return $this->_response;
  56. }
  57. /**
  58. * There is at least one result set with error.
  59. *
  60. * @return bool
  61. */
  62. public function hasError()
  63. {
  64. foreach ($this->getResultSets() as $resultSet) {
  65. if ($resultSet->getResponse()->hasError()) {
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. /**
  72. * @return \Elastica\ResultSet
  73. */
  74. public function current()
  75. {
  76. return $this->_resultSets[$this->key()];
  77. }
  78. public function next()
  79. {
  80. ++$this->_position;
  81. }
  82. /**
  83. * @return int
  84. */
  85. public function key()
  86. {
  87. return $this->_position;
  88. }
  89. /**
  90. * @return bool
  91. */
  92. public function valid()
  93. {
  94. return isset($this->_resultSets[$this->key()]);
  95. }
  96. public function rewind()
  97. {
  98. $this->_position = 0;
  99. }
  100. /**
  101. * @return int
  102. */
  103. public function count()
  104. {
  105. return count($this->_resultSets);
  106. }
  107. /**
  108. * @param string|int $offset
  109. *
  110. * @return bool true on success or false on failure
  111. */
  112. public function offsetExists($offset)
  113. {
  114. return isset($this->_resultSets[$offset]);
  115. }
  116. /**
  117. * @param mixed $offset
  118. *
  119. * @return mixed can return all value types
  120. */
  121. public function offsetGet($offset)
  122. {
  123. return $this->_resultSets[$offset] ?? null;
  124. }
  125. /**
  126. * @param mixed $offset
  127. * @param mixed $value
  128. */
  129. public function offsetSet($offset, $value)
  130. {
  131. if (is_null($offset)) {
  132. $this->_resultSets[] = $value;
  133. } else {
  134. $this->_resultSets[$offset] = $value;
  135. }
  136. }
  137. /**
  138. * @param mixed $offset
  139. */
  140. public function offsetUnset($offset)
  141. {
  142. unset($this->_resultSets[$offset]);
  143. }
  144. }