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     * @var Response
32     */
33    protected $_response;
34
35    /**
36     * @param BaseResultSet[] $resultSets
37     */
38    public function __construct(Response $response, array $resultSets)
39    {
40        $this->_response = $response;
41        $this->_resultSets = $resultSets;
42    }
43
44    /**
45     * @return BaseResultSet[]
46     */
47    public function getResultSets(): array
48    {
49        return $this->_resultSets;
50    }
51
52    /**
53     * Returns response object.
54     *
55     * @return Response Response object
56     */
57    public function getResponse(): Response
58    {
59        return $this->_response;
60    }
61
62    /**
63     * There is at least one result set with error.
64     */
65    public function hasError(): bool
66    {
67        foreach ($this->getResultSets() as $resultSet) {
68            if ($resultSet->getResponse()->hasError()) {
69                return true;
70            }
71        }
72
73        return false;
74    }
75
76    /**
77     * {@inheritdoc}
78     *
79     * @return mixed
80     */
81    #[\ReturnTypeWillChange]
82    public function current()
83    {
84        return $this->_resultSets[$this->key()];
85    }
86
87    /**
88     * {@inheritdoc}
89     */
90    public function next(): void
91    {
92        ++$this->_position;
93    }
94
95    /**
96     * {@inheritdoc}
97     */
98    public function key(): int
99    {
100        return $this->_position;
101    }
102
103    /**
104     * {@inheritdoc}
105     */
106    public function valid(): bool
107    {
108        return isset($this->_resultSets[$this->key()]);
109    }
110
111    /**
112     * {@inheritdoc}
113     */
114    public function rewind(): void
115    {
116        $this->_position = 0;
117    }
118
119    /**
120     * {@inheritdoc}
121     */
122    public function count(): int
123    {
124        return \count($this->_resultSets);
125    }
126
127    /**
128     * {@inheritdoc}
129     */
130    public function offsetExists($offset): bool
131    {
132        return isset($this->_resultSets[$offset]);
133    }
134
135    /**
136     * {@inheritdoc}
137     *
138     * @return mixed
139     */
140    #[\ReturnTypeWillChange]
141    public function offsetGet($offset)
142    {
143        return $this->_resultSets[$offset] ?? null;
144    }
145
146    /**
147     * {@inheritdoc}
148     */
149    public function offsetSet($offset, $value): void
150    {
151        if (null === $offset) {
152            $this->_resultSets[] = $value;
153        } else {
154            $this->_resultSets[$offset] = $value;
155        }
156    }
157
158    /**
159     * {@inheritdoc}
160     */
161    public function offsetUnset($offset): void
162    {
163        unset($this->_resultSets[$offset]);
164    }
165}
166