1 | <?php |
||
20 | final class EventStream implements Stream |
||
21 | { |
||
22 | /** |
||
23 | * @var Event[] |
||
24 | */ |
||
25 | private $events = []; |
||
26 | |||
27 | /** |
||
28 | * @var ArrayIterator |
||
29 | */ |
||
30 | private $iterator; |
||
31 | |||
32 | /** |
||
33 | * Creates an Event Stream |
||
34 | * |
||
35 | * @param array $events |
||
36 | * @throws \ReflectionException |
||
37 | */ |
||
38 | public function __construct(array $events = []) |
||
44 | |||
45 | /** |
||
46 | * iterator |
||
47 | * |
||
48 | * @return ArrayIterator |
||
49 | */ |
||
50 | private function iterator(): ArrayIterator |
||
57 | |||
58 | /** |
||
59 | * Adds an event to the stream |
||
60 | * |
||
61 | * @param Event|StoredEvent $event |
||
62 | * |
||
63 | * @return EventStream |
||
64 | * @throws \ReflectionException |
||
65 | */ |
||
66 | public function add(Event $event): EventStream |
||
73 | |||
74 | /** |
||
75 | * Returns the event list as an array |
||
76 | * |
||
77 | * @return Event[] |
||
78 | */ |
||
79 | public function asArray(): array |
||
83 | |||
84 | /** |
||
85 | * Count the events in this stream |
||
86 | * |
||
87 | * @return int |
||
88 | */ |
||
89 | public function count() |
||
93 | |||
94 | /** |
||
95 | * Returns true if this stream has no events |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | public function isEmpty(): bool |
||
103 | |||
104 | /** |
||
105 | * Returns the first event in stream or null if stream is empty |
||
106 | * |
||
107 | * @return null|Event |
||
108 | */ |
||
109 | public function first(): ?Event |
||
113 | |||
114 | /** |
||
115 | * Returns the first event in stream or null if stream is empty |
||
116 | * |
||
117 | * @return null|Event |
||
118 | */ |
||
119 | public function last(): ?Event |
||
123 | |||
124 | /** |
||
125 | * Return the current element |
||
126 | * |
||
127 | * @return mixed Can return any type. |
||
128 | */ |
||
129 | public function current() |
||
133 | |||
134 | /** |
||
135 | * Move forward to next element |
||
136 | * |
||
137 | * @return void Any returned value is ignored. |
||
138 | */ |
||
139 | public function next() |
||
143 | |||
144 | /** |
||
145 | * Return the key of the current element |
||
146 | * |
||
147 | * @return mixed scalar on success, or null on failure. |
||
148 | */ |
||
149 | public function key() |
||
153 | |||
154 | /** |
||
155 | * Checks if current position is valid |
||
156 | * |
||
157 | * @return boolean The return value will be casted to boolean and then evaluated. |
||
158 | * Returns true on success or false on failure. |
||
159 | */ |
||
160 | public function valid() |
||
164 | |||
165 | /** |
||
166 | * Rewind the Iterator to the first element |
||
167 | * |
||
168 | * @return void Any returned value is ignored. |
||
169 | */ |
||
170 | public function rewind() |
||
174 | } |
||
175 |