|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Gica\Cqrs\EventStore\InMemory; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Gica\Cqrs\EventStore\ByClassNamesEventStream; |
|
8
|
|
|
use Gica\Iterator\IteratorTransformer\IteratorExpander; |
|
9
|
|
|
|
|
10
|
|
|
class RawEventStream implements ByClassNamesEventStream |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
private $groupedEventsArray = []; |
|
14
|
|
|
|
|
15
|
|
|
/** @var int|null */ |
|
16
|
|
|
private $limit; |
|
17
|
|
|
|
|
18
|
|
|
/** @var int|null */ |
|
19
|
|
|
private $afterSequenceNumber; |
|
20
|
|
|
|
|
21
|
3 |
|
public function __construct($groupedEventsArray) |
|
22
|
|
|
{ |
|
23
|
3 |
|
$this->groupedEventsArray = $groupedEventsArray; |
|
24
|
3 |
|
} |
|
25
|
|
|
|
|
26
|
2 |
|
public function getIterator() |
|
27
|
|
|
{ |
|
28
|
2 |
|
$groupedEvents = $this->fetchCommits(); |
|
29
|
|
|
|
|
30
|
2 |
|
$deGrouper = new IteratorExpander(function ($group) { |
|
31
|
2 |
|
foreach ($group as $event) { |
|
32
|
2 |
|
yield $event; |
|
33
|
|
|
} |
|
34
|
2 |
|
}); |
|
35
|
|
|
|
|
36
|
2 |
|
$events = iterator_to_array($deGrouper($groupedEvents)); |
|
37
|
|
|
|
|
38
|
2 |
|
return new \ArrayIterator($events); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return array|\ArrayIterator |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function fetchCommits() |
|
45
|
|
|
{ |
|
46
|
2 |
|
$groupedEvents = $this->groupedEventsArray; |
|
47
|
|
|
|
|
48
|
2 |
|
if ($this->groupedEventsArray instanceof \Iterator || $this->groupedEventsArray instanceof \IteratorAggregate) { |
|
49
|
1 |
|
$groupedEvents = iterator_to_array($this->groupedEventsArray); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
if ($this->afterSequenceNumber) { |
|
|
|
|
|
|
53
|
1 |
|
$groupedEvents = array_slice($groupedEvents, $this->afterSequenceNumber); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
if ($this->limit) { |
|
|
|
|
|
|
57
|
1 |
|
$groupedEvents = array_slice($groupedEvents, 0, $this->limit); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
return $groupedEvents; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
public function limitCommits(int $limit) |
|
64
|
|
|
{ |
|
65
|
1 |
|
$this->limit = $limit; |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
public function afterSequence(int $sequenceNumber) |
|
69
|
|
|
{ |
|
70
|
1 |
|
$this->afterSequenceNumber = $sequenceNumber; |
|
71
|
1 |
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
public function countCommits(): int |
|
74
|
|
|
{ |
|
75
|
1 |
|
return count($this->groupedEventsArray); |
|
76
|
|
|
} |
|
77
|
|
|
} |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: