@@ 7-37 (lines=31) @@ | ||
4 | ||
5 | use RayRutjes\GetEventStore\Util\InternalIterator; |
|
6 | ||
7 | final class EventDataCollection extends InternalIterator implements \Iterator, \Countable |
|
8 | { |
|
9 | /** |
|
10 | * @param array $events |
|
11 | * |
|
12 | * @return EventDataCollection |
|
13 | */ |
|
14 | public static function fromArray(array $events = []): EventDataCollection |
|
15 | { |
|
16 | $indexed = []; |
|
17 | foreach ($events as $event) { |
|
18 | if (!$event instanceof EventData) { |
|
19 | throw new \InvalidArgumentException(sprintf('Invalid EventData, got: %s', get_class($event))); |
|
20 | } |
|
21 | $indexed[] = $event; |
|
22 | } |
|
23 | ||
24 | return new self(new \ArrayIterator($indexed)); |
|
25 | } |
|
26 | ||
27 | /** |
|
28 | * @return int |
|
29 | */ |
|
30 | public function count() |
|
31 | { |
|
32 | if ($this->iterator instanceof \Countable) { |
|
33 | return $this->iterator->count(); |
|
34 | } |
|
35 | ||
36 | return 0; |
|
37 | } |
|
38 | } |
|
39 |
@@ 7-37 (lines=31) @@ | ||
4 | ||
5 | use RayRutjes\GetEventStore\Util\InternalIterator; |
|
6 | ||
7 | final class EventRecordCollection extends InternalIterator implements \Iterator, \Countable |
|
8 | { |
|
9 | /** |
|
10 | * @param array $events |
|
11 | * |
|
12 | * @return EventRecordCollection |
|
13 | */ |
|
14 | public static function fromArray(array $events = []): EventRecordCollection |
|
15 | { |
|
16 | $indexed = []; |
|
17 | foreach ($events as $event) { |
|
18 | if (!$event instanceof EventRecord) { |
|
19 | throw new \InvalidArgumentException(sprintf('Invalid EventRecord, got: %s', get_class($event))); |
|
20 | } |
|
21 | $indexed[] = $event; |
|
22 | } |
|
23 | ||
24 | return new self(new \ArrayIterator($indexed)); |
|
25 | } |
|
26 | ||
27 | /** |
|
28 | * @return int |
|
29 | */ |
|
30 | public function count() |
|
31 | { |
|
32 | if ($this->iterator instanceof \Countable) { |
|
33 | return $this->iterator->count(); |
|
34 | } |
|
35 | ||
36 | return 0; |
|
37 | } |
|
38 | } |
|
39 |