1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/cqrs-tools |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\CQRSTools\Domain\Event; |
11
|
|
|
|
12
|
|
|
use ArrayIterator; |
13
|
|
|
use Countable; |
14
|
|
|
use IteratorAggregate; |
15
|
|
|
use Slick\CQRSTools\Event; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* EventStream |
19
|
|
|
* |
20
|
|
|
* @package Slick\CQRSTools\Domain\Event |
21
|
|
|
*/ |
22
|
|
|
final class EventStream implements IteratorAggregate, Countable |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Event[] |
26
|
|
|
*/ |
27
|
|
|
private $events = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Creates an Event Stream |
31
|
|
|
* |
32
|
|
|
* @param array $events |
33
|
|
|
* @throws \ReflectionException |
34
|
|
|
*/ |
35
|
|
|
public function __construct(array $events = []) |
36
|
|
|
{ |
37
|
|
|
foreach ($events as $event) { |
38
|
|
|
$this->add($event); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Adds an event to the stream |
44
|
|
|
* |
45
|
|
|
* @param Event|StoredEvent $event |
46
|
|
|
* |
47
|
|
|
* @return EventStream |
48
|
|
|
* @throws \ReflectionException |
49
|
|
|
*/ |
50
|
|
|
public function add(Event $event): EventStream |
51
|
|
|
{ |
52
|
|
|
$event = $event instanceof StoredEvent ? $event->event() : $event; |
53
|
|
|
array_push($this->events, $event); |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the event list as an array |
59
|
|
|
* |
60
|
|
|
* @return Event[] |
61
|
|
|
*/ |
62
|
|
|
public function asArray(): array |
63
|
|
|
{ |
64
|
|
|
return $this->events; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Retrieve an external iterator |
69
|
|
|
* |
70
|
|
|
* @return ArrayIterator |
71
|
|
|
*/ |
72
|
|
|
public function getIterator() |
73
|
|
|
{ |
74
|
|
|
return new ArrayIterator($this->events); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Count the events in this stream |
79
|
|
|
* |
80
|
|
|
* @return int |
81
|
|
|
*/ |
82
|
|
|
public function count() |
83
|
|
|
{ |
84
|
|
|
return count($this->events); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns true if this stream has no events |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function isEmpty(): bool |
93
|
|
|
{ |
94
|
|
|
return empty($this->events); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the first event in stream or null if stream is empty |
99
|
|
|
* |
100
|
|
|
* @return null|Event |
101
|
|
|
*/ |
102
|
|
|
public function first(): ?Event |
103
|
|
|
{ |
104
|
|
|
return reset($this->events) ?: null; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns the first event in stream or null if stream is empty |
109
|
|
|
* |
110
|
|
|
* @return null|Event |
111
|
|
|
*/ |
112
|
|
|
public function last(): ?Event |
113
|
|
|
{ |
114
|
|
|
return end($this->events) ?: null; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|