1 | <?php |
||
14 | final class AggregateResponse implements \IteratorAggregate |
||
15 | { |
||
16 | /** |
||
17 | * @var array|ResponseInterface[] |
||
18 | */ |
||
19 | private $lines = []; |
||
20 | |||
21 | /** |
||
22 | * @var Tag |
||
23 | */ |
||
24 | private $tag; |
||
25 | |||
26 | /** |
||
27 | * Reply constructor. |
||
28 | * @param Tag $tag |
||
29 | */ |
||
30 | 20 | public function __construct(Tag $tag) |
|
34 | |||
35 | /** |
||
36 | * @return \Iterator|ResponseInterface[] |
||
37 | */ |
||
38 | 1 | public function getIterator(): \Iterator |
|
39 | { |
||
40 | 1 | return new \ArrayIterator($this->lines); |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * @return ResponseInterface |
||
45 | */ |
||
46 | 12 | public function first(): ResponseInterface |
|
47 | { |
||
48 | 12 | if (empty($this->lines)) { |
|
49 | 1 | throw new \OutOfBoundsException('Cannot return item of empty response'); |
|
50 | } |
||
51 | |||
52 | 11 | return reset($this->lines); |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * @return ResponseInterface |
||
57 | */ |
||
58 | 9 | public function last(): ResponseInterface |
|
59 | { |
||
60 | 9 | if (empty($this->lines)) { |
|
61 | 1 | throw new \OutOfBoundsException('Cannot return item of empty response'); |
|
62 | } |
||
63 | |||
64 | 8 | return end($this->lines); |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param int $index |
||
69 | * @return ResponseInterface |
||
70 | */ |
||
71 | 2 | public function at(int $index): ResponseInterface |
|
72 | { |
||
73 | 2 | if (!isset($this->lines[$index])) { |
|
74 | 1 | throw new \OutOfBoundsException('GenericItem not in response'); |
|
75 | } |
||
76 | |||
77 | 1 | return $this->lines[$index]; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return bool |
||
82 | */ |
||
83 | 6 | public function hasCompleted(): bool |
|
104 | |||
105 | /** |
||
106 | * @param string $line |
||
107 | * @return AggregateResponse |
||
108 | */ |
||
109 | 16 | public function withLine(string $line): AggregateResponse |
|
141 | } |
||
142 |