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 BaseResponse $response
21     * @param Response[]   $bulkResponses
22     */
23    public function __construct(BaseResponse $response, array $bulkResponses)
24    {
25        parent::__construct($response->getData());
26
27        $this->_bulkResponses = $bulkResponses;
28    }
29
30    /**
31     * @return Response[]
32     */
33    public function getBulkResponses(): array
34    {
35        return $this->_bulkResponses;
36    }
37
38    /**
39     * Returns first found error.
40     *
41     * @return string
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    /**
71     * @return bool
72     */
73    public function isOk(): bool
74    {
75        foreach ($this->getBulkResponses() as $bulkResponse) {
76            if (!$bulkResponse->isOk()) {
77                return false;
78            }
79        }
80
81        return true;
82    }
83
84    /**
85     * @return bool
86     */
87    public function hasError(): bool
88    {
89        foreach ($this->getBulkResponses() as $bulkResponse) {
90            if ($bulkResponse->hasError()) {
91                return true;
92            }
93        }
94
95        return false;
96    }
97
98    /**
99     * @return Response
100     */
101    public function current(): Response
102    {
103        return $this->_bulkResponses[$this->key()];
104    }
105
106    /**
107     * {@inheritdoc}
108     */
109    public function next()
110    {
111        ++$this->_position;
112    }
113
114    /**
115     * {@inheritdoc}
116     */
117    public function key(): int
118    {
119        return $this->_position;
120    }
121
122    /**
123     * {@inheritdoc}
124     */
125    public function valid(): bool
126    {
127        return isset($this->_bulkResponses[$this->key()]);
128    }
129
130    /**
131     * {@inheritdoc}
132     */
133    public function rewind()
134    {
135        $this->_position = 0;
136    }
137
138    /**
139     * {@inheritdoc}
140     */
141    public function count(): int
142    {
143        return \count($this->_bulkResponses);
144    }
145}
146