1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webino™ (http://webino.sk) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/webino/event-emitter |
6
|
|
|
* @copyright Copyright (c) 2019 Webino, s.r.o. (http://webino.sk) |
7
|
|
|
* @author Peter Bačinský <[email protected]> |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Webino; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class EventResults |
15
|
|
|
* @package event-emitter |
16
|
|
|
*/ |
17
|
|
|
class EventResults implements |
18
|
|
|
EventResultsInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $results = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create event Results from array values. |
27
|
|
|
* |
28
|
|
|
* @param array $results Event Results values array |
29
|
|
|
*/ |
30
|
|
|
public function __construct(array $results = []) |
31
|
|
|
{ |
32
|
|
|
$this->results = $results; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Add new response. |
37
|
|
|
* |
38
|
|
|
* @internal |
39
|
|
|
* @param mixed $response Event response value |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function add($response): void |
43
|
|
|
{ |
44
|
|
|
$this->results[] = $response; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns first response. |
49
|
|
|
* |
50
|
|
|
* @api |
51
|
|
|
* @return mixed|null |
52
|
|
|
*/ |
53
|
|
|
public function first() |
54
|
|
|
{ |
55
|
|
|
return $this->results[0] ?: null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns last response. |
60
|
|
|
* |
61
|
|
|
* @api |
62
|
|
|
* @return mixed|null |
63
|
|
|
*/ |
64
|
|
|
public function last() |
65
|
|
|
{ |
66
|
|
|
$index = count($this->results) - 1; |
67
|
|
|
return $this->results[$index] ?: null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Return results as array. |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public function toArray(): array |
76
|
|
|
{ |
77
|
|
|
return $this->results; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Whether a offset exists. |
82
|
|
|
* |
83
|
|
|
* @param mixed $offset |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function offsetExists($offset) |
87
|
|
|
{ |
88
|
|
|
return array_key_exists($offset, $this->results); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Offset to retrieve. |
93
|
|
|
* |
94
|
|
|
* @param mixed $offset |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
public function offsetGet($offset) |
98
|
|
|
{ |
99
|
|
|
return $this->results[$offset]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Offset to set. |
104
|
|
|
* |
105
|
|
|
* @param mixed $offset |
106
|
|
|
* @param mixed $value |
107
|
|
|
*/ |
108
|
|
|
public function offsetSet($offset, $value) |
109
|
|
|
{ |
110
|
|
|
$this->results[$offset] = $value; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Offset to unset. |
115
|
|
|
* |
116
|
|
|
* @param mixed $offset |
117
|
|
|
*/ |
118
|
|
|
public function offsetUnset($offset) |
119
|
|
|
{ |
120
|
|
|
unset($this->results[$offset]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function __toString(): string |
127
|
|
|
{ |
128
|
|
|
return join('', $this->results); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|