1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* event-sourcing (https://github.com/phpgears/event-sourcing). |
5
|
|
|
* Event Sourcing base. |
6
|
|
|
* |
7
|
|
|
* @license MIT |
8
|
|
|
* @link https://github.com/phpgears/event-sourcing |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Gears\EventSourcing\Event; |
15
|
|
|
|
16
|
|
|
use Gears\EventSourcing\Event\Exception\InvalidAggregateEventException; |
17
|
|
|
|
18
|
|
|
final class AggregateEventIteratorStream implements AggregateEventStream |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \Iterator |
22
|
|
|
*/ |
23
|
|
|
private $iterator; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* AggregateEventIteratorStream constructor. |
27
|
|
|
* |
28
|
|
|
* @param \Iterator $iterator |
29
|
|
|
*/ |
30
|
|
|
public function __construct(\Iterator $iterator) |
31
|
|
|
{ |
32
|
|
|
$this->iterator = $iterator; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
* |
38
|
|
|
* @return AggregateEvent |
39
|
|
|
*/ |
40
|
|
|
public function current(): AggregateEvent |
41
|
|
|
{ |
42
|
|
|
$event = $this->iterator->current(); |
43
|
|
|
|
44
|
|
|
if (!$event instanceof AggregateEvent) { |
45
|
|
|
throw new InvalidAggregateEventException(\sprintf( |
46
|
|
|
'Aggregate event stream only accepts %s, %s given', |
47
|
|
|
AggregateEvent::class, |
48
|
|
|
\is_object($event) ? \get_class($event) : \gettype($event) |
49
|
|
|
)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $event; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function next(): void |
59
|
|
|
{ |
60
|
|
|
$this->iterator->next(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
* |
66
|
|
|
* @return string|int|null |
|
|
|
|
67
|
|
|
*/ |
68
|
|
|
public function key() |
69
|
|
|
{ |
70
|
|
|
return $this->iterator->key(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function valid(): bool |
77
|
|
|
{ |
78
|
|
|
return $this->iterator->valid(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function rewind(): void |
85
|
|
|
{ |
86
|
|
|
$this->iterator->rewind(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function count(): int |
93
|
|
|
{ |
94
|
|
|
$currentKey = $this->iterator->key(); |
95
|
|
|
$this->iterator->rewind(); |
96
|
|
|
|
97
|
|
|
$count = 0; |
98
|
|
|
while ($this->iterator->valid()) { |
99
|
|
|
$count++; |
100
|
|
|
|
101
|
|
|
$this->iterator->next(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->iterator->rewind(); |
105
|
|
|
while ($this->iterator->key() !== $currentKey) { |
106
|
|
|
$this->iterator->next(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $count; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.