1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DDDominio\EventSourcing\Common; |
4
|
|
|
|
5
|
|
|
use DDDominio\Common\EventInterface; |
6
|
|
|
|
7
|
|
|
class EventStream implements EventStreamInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var EventInterface[] |
11
|
|
|
*/ |
12
|
|
|
private $events; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param EventInterface[] $events |
16
|
|
|
*/ |
17
|
90 |
|
public function __construct(array $events) |
18
|
|
|
{ |
19
|
90 |
|
$this->events = $events; |
20
|
90 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return EventStream |
24
|
|
|
*/ |
25
|
29 |
|
public static function buildEmpty() |
26
|
|
|
{ |
27
|
29 |
|
return new self([]); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param EventInterface|EventInterface[] $events |
32
|
|
|
* @return EventStream |
33
|
|
|
*/ |
34
|
21 |
|
public function append($events) |
35
|
|
|
{ |
36
|
21 |
|
if (!is_array($events)) { |
37
|
16 |
|
$events = [$events]; |
38
|
|
|
} |
39
|
21 |
|
return new self(array_merge($this->events(), $events)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return EventInterface[] |
44
|
|
|
*/ |
45
|
36 |
|
public function events() |
46
|
|
|
{ |
47
|
36 |
|
return $this->events; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param int $offset |
52
|
|
|
* @return EventInterface |
53
|
|
|
* @throws \OutOfBoundsException |
54
|
|
|
*/ |
55
|
31 |
|
public function get($offset) |
56
|
|
|
{ |
57
|
31 |
|
if (!isset($this->events[$offset])) { |
58
|
2 |
|
throw new \OutOfBoundsException(); |
59
|
|
|
} |
60
|
29 |
|
return $this->events[$offset]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return EventInterface |
65
|
|
|
* @throws \OutOfBoundsException |
66
|
|
|
*/ |
67
|
8 |
|
public function last() |
68
|
|
|
{ |
69
|
8 |
|
if ($this->isEmpty()) { |
70
|
1 |
|
throw new \OutOfBoundsException(); |
71
|
|
|
} |
72
|
7 |
|
return end($this->events); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
18 |
|
public function isEmpty() |
79
|
|
|
{ |
80
|
18 |
|
return empty($this->events); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
51 |
|
public function count() |
87
|
|
|
{ |
88
|
51 |
|
return count($this->events); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
14 |
|
public function getIterator() |
95
|
|
|
{ |
96
|
14 |
|
return new \ArrayIterator($this->events()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param \Closure $closure |
101
|
|
|
*/ |
102
|
1 |
|
public function each(\Closure $closure) |
103
|
|
|
{ |
104
|
1 |
|
foreach ($this->events as $event) { |
105
|
1 |
|
$closure($event); |
106
|
|
|
} |
107
|
1 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param \Closure $closure |
111
|
|
|
* @return EventStream |
112
|
|
|
*/ |
113
|
4 |
|
public function filter(\Closure $closure) |
114
|
|
|
{ |
115
|
4 |
|
return new self(array_values(array_filter($this->events, $closure))); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param \Closure $closure |
120
|
|
|
* @return EventStream |
121
|
|
|
*/ |
122
|
44 |
|
public function map(\Closure $closure) |
123
|
|
|
{ |
124
|
44 |
|
return new self(array_map($closure, $this->events)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param int $offset |
129
|
|
|
* @param int|null $length |
130
|
|
|
* @return EventStream |
131
|
|
|
*/ |
132
|
6 |
|
public function slice($offset, $length = null) |
133
|
|
|
{ |
134
|
6 |
|
return new self(array_slice($this->events, $offset, $length)); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|