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