1<?php
2
3namespace Elastica\Bulk;
4
5use Elastica\Response as BaseResponse;
6
7class ResponseSet extends BaseResponse implements \Iterator, \Countable
8{
9    /**
10     * @var Response[]
11     */
12    protected $_bulkResponses = [];
13
14    /**
15     * @var int
16     */
17    protected $_position = 0;
18
19    /**
20     * @param Response[] $bulkResponses
21     */
22    public function __construct(BaseResponse $response, array $bulkResponses)
23    {
24        parent::__construct($response->getData(), $response->getStatus());
25
26        $this->setQueryTime($response->getQueryTime());
27        $this->setTransferInfo($response->getTransferInfo());
28
29        $this->_bulkResponses = $bulkResponses;
30    }
31
32    /**
33     * @return Response[]
34     */
35    public function getBulkResponses(): array
36    {
37        return $this->_bulkResponses;
38    }
39
40    /**
41     * Returns first found error.
42     */
43    public function getError(): string
44    {
45        foreach ($this->getBulkResponses() as $bulkResponse) {
46            if ($bulkResponse->hasError()) {
47                return $bulkResponse->getError();
48            }
49        }
50
51        return '';
52    }
53
54    /**
55     * Returns first found error (full array).
56     *
57     * @return array|string
58     */
59    public function getFullError()
60    {
61        foreach ($this->getBulkResponses() as $bulkResponse) {
62            if ($bulkResponse->hasError()) {
63                return $bulkResponse->getFullError();
64            }
65        }
66
67        return '';
68    }
69
70    public function isOk(): bool
71    {
72        foreach ($this->getBulkResponses() as $bulkResponse) {
73            if (!$bulkResponse->isOk()) {
74                return false;
75            }
76        }
77
78        return true;
79    }
80
81    public function hasError(): bool
82    {
83        foreach ($this->getBulkResponses() as $bulkResponse) {
84            if ($bulkResponse->hasError()) {
85                return true;
86            }
87        }
88
89        return false;
90    }
91
92    public function current(): Response
93    {
94        return $this->_bulkResponses[$this->key()];
95    }
96
97    /**
98     * {@inheritdoc}
99     */
100    public function next(): void
101    {
102        ++$this->_position;
103    }
104
105    /**
106     * {@inheritdoc}
107     */
108    public function key(): int
109    {
110        return $this->_position;
111    }
112
113    /**
114     * {@inheritdoc}
115     */
116    public function valid(): bool
117    {
118        return isset($this->_bulkResponses[$this->key()]);
119    }
120
121    /**
122     * {@inheritdoc}
123     */
124    public function rewind(): void
125    {
126        $this->_position = 0;
127    }
128
129    /**
130     * {@inheritdoc}
131     */
132    public function count(): int
133    {
134        return \count($this->_bulkResponses);
135    }
136}
137