1 | <?php |
||
18 | final class DomainEventSequence implements IteratorAggregate, Countable |
||
19 | { |
||
20 | /** @var Vector */ |
||
21 | private $compositeVector; |
||
22 | |||
23 | 1 | public static function fromArray(array $eventsArray): DomainEventSequence |
|
24 | { |
||
25 | return new static(array_map(function (array $eventState): DomainEventInterface { |
||
26 | 1 | $eventFqcn = self::resolveEventFqcn($eventState); |
|
27 | 1 | return call_user_func([ $eventFqcn, 'fromArray' ], $eventState); |
|
28 | 1 | }, $eventsArray)); |
|
29 | } |
||
30 | |||
31 | 6 | public static function makeEmpty(): DomainEventSequence |
|
35 | |||
36 | public function __construct(array $events = []) |
||
42 | |||
43 | 3 | public function push(DomainEventInterface $event): DomainEventSequence |
|
57 | |||
58 | public function append(DomainEventSequence $events): DomainEventSequence |
||
66 | |||
67 | 1 | public function toNative(): array |
|
68 | { |
||
69 | 1 | $nativeList = []; |
|
70 | 1 | foreach ($this as $event) { |
|
71 | 1 | $nativeRep = $event->toArray(); |
|
72 | 1 | $nativeRep['@type'] = get_class($event); |
|
73 | 1 | $nativeList[] = $nativeRep; |
|
74 | } |
||
75 | 1 | return $nativeList; |
|
76 | } |
||
77 | |||
78 | 3 | public function getHeadRevision(): AggregateRevision |
|
82 | |||
83 | public function getTailRevision(): AggregateRevision |
||
84 | { |
||
85 | return $this->isEmpty() ? AggregateRevision::makeEmpty() : $this->getTail()->getAggregateRevision(); |
||
86 | } |
||
87 | |||
88 | public function getTail(): ?DomainEventInterface |
||
89 | { |
||
90 | return $this->compositeVector->first(); |
||
91 | } |
||
92 | |||
93 | public function getHead(): ?DomainEventInterface |
||
94 | { |
||
95 | return $this->compositeVector->last(); |
||
96 | } |
||
97 | |||
98 | public function getLength(): int |
||
102 | |||
103 | 3 | public function isEmpty(): bool |
|
107 | |||
108 | public function indexOf(DomainEventInterface $event): int |
||
112 | |||
113 | 2 | public function count(): int |
|
117 | |||
118 | 4 | public function getIterator(): Iterator |
|
122 | |||
123 | 1 | private static function resolveEventFqcn(array $eventState): string |
|
124 | { |
||
125 | 1 | if (!isset($eventState['@type'])) { |
|
126 | throw new \Exception('Missing expected typeinfo for event at key "@type" within given state-array.'); |
||
127 | } |
||
128 | 1 | $eventFqcn = $eventState['@type']; |
|
129 | 1 | if (!class_exists($eventFqcn)) { |
|
130 | throw new \Exception(sprintf('Can not load event-class "%s" given within state-array.', $eventFqcn)); |
||
131 | } |
||
132 | 1 | return $eventFqcn; |
|
133 | } |
||
134 | |||
135 | 3 | private function __clone() |
|
139 | } |
||
140 |