| @@ 7-90 (lines=84) @@ | ||
| 4 | ||
| 5 | use RayRutjes\GetEventStore\EventRecord; |
|
| 6 | ||
| 7 | final class EventStreamIterator implements \Iterator |
|
| 8 | { |
|
| 9 | /** |
|
| 10 | * @var EventStreamFeedIterator |
|
| 11 | */ |
|
| 12 | private $feedIterator; |
|
| 13 | ||
| 14 | /** |
|
| 15 | * @var \ArrayIterator |
|
| 16 | */ |
|
| 17 | private $eventsIterator; |
|
| 18 | ||
| 19 | /** |
|
| 20 | * @var int |
|
| 21 | */ |
|
| 22 | private $currentKey; |
|
| 23 | ||
| 24 | /** |
|
| 25 | * @param EventStreamFeedIterator $feedIterator |
|
| 26 | */ |
|
| 27 | public function __construct(EventStreamFeedIterator $feedIterator) |
|
| 28 | { |
|
| 29 | $this->feedIterator = $feedIterator; |
|
| 30 | } |
|
| 31 | ||
| 32 | /** |
|
| 33 | * @return EventRecord |
|
| 34 | */ |
|
| 35 | public function current(): EventRecord |
|
| 36 | { |
|
| 37 | return $this->eventsIterator->current(); |
|
| 38 | } |
|
| 39 | ||
| 40 | public function next() |
|
| 41 | { |
|
| 42 | $this->eventsIterator->next(); |
|
| 43 | $this->currentKey++; |
|
| 44 | if ($this->eventsIterator->valid()) { |
|
| 45 | return; |
|
| 46 | } |
|
| 47 | ||
| 48 | $this->feedIterator->next(); |
|
| 49 | if ($this->feedIterator->valid()) { |
|
| 50 | $this->eventsIterator = $this->newEventsIterator(); |
|
| 51 | } else { |
|
| 52 | $this->feedIterator = null; |
|
| 53 | } |
|
| 54 | } |
|
| 55 | ||
| 56 | /** |
|
| 57 | * @return \ArrayIterator |
|
| 58 | */ |
|
| 59 | private function newEventsIterator() |
|
| 60 | { |
|
| 61 | $events = $this->feedIterator->current()->getEvents(); |
|
| 62 | if ($this->feedIterator->getReadDirection()->isForward()) { |
|
| 63 | $events = array_reverse($events); |
|
| 64 | } |
|
| 65 | ||
| 66 | return new \ArrayIterator($events); |
|
| 67 | } |
|
| 68 | ||
| 69 | /** |
|
| 70 | * @return int |
|
| 71 | */ |
|
| 72 | public function key() |
|
| 73 | { |
|
| 74 | return $this->currentKey; |
|
| 75 | } |
|
| 76 | ||
| 77 | /** |
|
| 78 | * @return bool |
|
| 79 | */ |
|
| 80 | public function valid(): bool |
|
| 81 | { |
|
| 82 | return $this->eventsIterator->valid(); |
|
| 83 | } |
|
| 84 | ||
| 85 | public function rewind() |
|
| 86 | { |
|
| 87 | $this->currentKey = 0; |
|
| 88 | $this->eventsIterator = $this->newEventsIterator(); |
|
| 89 | } |
|
| 90 | } |
|
| 91 | ||
| @@ 7-89 (lines=83) @@ | ||
| 4 | ||
| 5 | use RayRutjes\GetEventStore\EventRecord; |
|
| 6 | ||
| 7 | final class EventStreamViaPersistentSubscriptionIterator implements \Iterator |
|
| 8 | { |
|
| 9 | /** |
|
| 10 | * @var EventStreamViaPersistentSubscriptionFeedIterator |
|
| 11 | */ |
|
| 12 | private $feedIterator; |
|
| 13 | ||
| 14 | /** |
|
| 15 | * @var \ArrayIterator |
|
| 16 | */ |
|
| 17 | private $eventsIterator; |
|
| 18 | ||
| 19 | /** |
|
| 20 | * @var int |
|
| 21 | */ |
|
| 22 | private $currentKey; |
|
| 23 | ||
| 24 | /** |
|
| 25 | * @param EventStreamViaPersistentSubscriptionFeedIterator $feedIterator |
|
| 26 | */ |
|
| 27 | public function __construct(EventStreamViaPersistentSubscriptionFeedIterator $feedIterator) |
|
| 28 | { |
|
| 29 | $this->feedIterator = $feedIterator; |
|
| 30 | } |
|
| 31 | ||
| 32 | /** |
|
| 33 | * @return EventRecord |
|
| 34 | */ |
|
| 35 | public function current(): EventRecord |
|
| 36 | { |
|
| 37 | return $this->eventsIterator->current(); |
|
| 38 | } |
|
| 39 | ||
| 40 | public function next() |
|
| 41 | { |
|
| 42 | $this->eventsIterator->next(); |
|
| 43 | $this->currentKey++; |
|
| 44 | if ($this->eventsIterator->valid()) { |
|
| 45 | return; |
|
| 46 | } |
|
| 47 | ||
| 48 | $this->feedIterator->next(); |
|
| 49 | if ($this->feedIterator->valid()) { |
|
| 50 | $this->eventsIterator = $this->newEventsIterator(); |
|
| 51 | } else { |
|
| 52 | $this->feedIterator = null; |
|
| 53 | } |
|
| 54 | } |
|
| 55 | ||
| 56 | /** |
|
| 57 | * @return \ArrayIterator |
|
| 58 | */ |
|
| 59 | private function newEventsIterator() |
|
| 60 | { |
|
| 61 | $events = $this->feedIterator->current()->getEvents(); |
|
| 62 | ||
| 63 | $events = array_reverse($events); |
|
| 64 | ||
| 65 | return new \ArrayIterator($events); |
|
| 66 | } |
|
| 67 | ||
| 68 | /** |
|
| 69 | * @return int |
|
| 70 | */ |
|
| 71 | public function key() |
|
| 72 | { |
|
| 73 | return $this->currentKey; |
|
| 74 | } |
|
| 75 | ||
| 76 | /** |
|
| 77 | * @return bool |
|
| 78 | */ |
|
| 79 | public function valid(): bool |
|
| 80 | { |
|
| 81 | return $this->eventsIterator->valid(); |
|
| 82 | } |
|
| 83 | ||
| 84 | public function rewind() |
|
| 85 | { |
|
| 86 | $this->currentKey = 0; |
|
| 87 | $this->eventsIterator = $this->newEventsIterator(); |
|
| 88 | } |
|
| 89 | } |
|
| 90 | ||