This repository has been archived by the owner on Jul 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
EventSequence.php
253 lines (220 loc) · 5.97 KB
/
EventSequence.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
declare(strict_types=1);
namespace Eloquent\Phony\Event;
use ArrayIterator;
use Eloquent\Phony\Call\Call;
use Eloquent\Phony\Call\CallVerifierFactory;
use Eloquent\Phony\Call\Exception\UndefinedCallException;
use Eloquent\Phony\Event\Exception\UndefinedEventException;
use Iterator;
/**
* Represents a sequence of events.
*/
class EventSequence implements EventCollection
{
/**
* Construct a new event sequence.
*
* @param array<Event> $events The events.
* @param CallVerifierFactory $callVerifierFactory The call verifier factory to use.
*/
public function __construct(
array $events,
CallVerifierFactory $callVerifierFactory
) {
$calls = [];
foreach ($events as $event) {
if ($event instanceof Call) {
$calls[] = $event;
}
}
$this->events = $events;
$this->calls = $calls;
$this->eventCount = count($events);
$this->callCount = count($calls);
$this->callVerifierFactory = $callVerifierFactory;
}
/**
* Returns true if this collection contains any events.
*
* @return bool True if this collection contains any events.
*/
public function hasEvents(): bool
{
return $this->eventCount > 0;
}
/**
* Returns true if this collection contains any calls.
*
* @return bool True if this collection contains any calls.
*/
public function hasCalls(): bool
{
return $this->callCount > 0;
}
/**
* Get the number of events.
*
* @return int The event count.
*/
public function eventCount(): int
{
return $this->eventCount;
}
/**
* Get the number of calls.
*
* @return int The call count.
*/
public function callCount(): int
{
return $this->callCount;
}
/**
* Get the event count.
*
* @return int The event count.
*/
public function count(): int
{
return $this->eventCount;
}
/**
* Get all events as an array.
*
* @return array<Event> The events.
*/
public function allEvents(): array
{
return $this->events;
}
/**
* Get all calls as an array.
*
* @return array<Call> The calls.
*/
public function allCalls(): array
{
return $this->callVerifierFactory->fromCalls($this->calls);
}
/**
* Get the first event.
*
* @return Event The event.
* @throws UndefinedEventException If there are no events.
*/
public function firstEvent(): Event
{
if (empty($this->events)) {
throw new UndefinedEventException(0);
}
return $this->events[0];
}
/**
* Get the last event.
*
* @return Event The event.
* @throws UndefinedEventException If there are no events.
*/
public function lastEvent(): Event
{
if ($count = count($this->events)) {
return $this->events[$count - 1];
}
throw new UndefinedEventException(0);
}
/**
* Get an event by index.
*
* Negative indices are offset from the end of the list. That is, `-1`
* indicates the last element, and `-2` indicates the second last element.
*
* @param int $index The index.
*
* @return Event The event.
* @throws UndefinedEventException If the requested event is undefined, or there are no events.
*/
public function eventAt(int $index = 0): Event
{
if (!$this->normalizeIndex($this->eventCount, $index, $normalized)) {
throw new UndefinedEventException($index);
}
return $this->events[$normalized];
}
/**
* Get the first call.
*
* @return Call The call.
* @throws UndefinedCallException If there are no calls.
*/
public function firstCall(): Call
{
if (isset($this->calls[0])) {
return $this->callVerifierFactory->fromCall($this->calls[0]);
}
throw new UndefinedCallException(0);
}
/**
* Get the last call.
*
* @return Call The call.
* @throws UndefinedCallException If there are no calls.
*/
public function lastCall(): Call
{
if ($this->callCount) {
return $this->callVerifierFactory
->fromCall($this->calls[$this->callCount - 1]);
}
throw new UndefinedCallException(0);
}
/**
* Get a call by index.
*
* Negative indices are offset from the end of the list. That is, `-1`
* indicates the last element, and `-2` indicates the second last element.
*
* @param int $index The index.
*
* @return Call The call.
* @throws UndefinedCallException If the requested call is undefined, or there are no calls.
*/
public function callAt(int $index = 0): Call
{
if (!$this->normalizeIndex($this->callCount, $index, $normalized)) {
throw new UndefinedCallException($index);
}
return $this->callVerifierFactory->fromCall($this->calls[$normalized]);
}
/**
* Get an iterator for this collection.
*
* @return Iterator<int,Event> The iterator.
*/
public function getIterator(): Iterator
{
return new ArrayIterator($this->events);
}
private function normalizeIndex($size, $index, &$normalized = null)
{
$normalized = null;
if ($index < 0) {
$potential = $size + $index;
if ($potential < 0) {
return false;
}
} else {
$potential = $index;
}
if ($potential >= $size) {
return false;
}
$normalized = $potential;
return true;
}
private $events;
private $calls;
private $eventCount;
private $callCount;
private $callVerifierFactory;
}