1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RayRutjes\GetEventStore\Client\Http\Feed; |
4
|
|
|
|
5
|
|
|
use RayRutjes\GetEventStore\EventRecord; |
6
|
|
|
|
7
|
|
|
class EventStreamFeed |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
private $events; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $links = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $eTag; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
private $isHeadOfStream; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $events |
31
|
|
|
* @param array $links |
32
|
|
|
* @param bool $isHeadOfStream |
33
|
|
|
* @param string $eTag |
34
|
|
|
*/ |
35
|
|
|
public function __construct(array $events, array $links, bool $isHeadOfStream, string $eTag = null) |
36
|
|
|
{ |
37
|
|
|
$this->validateEvents($events); |
38
|
|
|
$this->events = $events; |
39
|
|
|
|
40
|
|
|
foreach ($links as $link) { |
41
|
|
|
$this->validateLink($link); |
42
|
|
|
$this->links[$link->getRelation()] = $link; |
43
|
|
|
} |
44
|
|
|
$this->isHeadOfStream = $isHeadOfStream; |
45
|
|
|
$this->eTag = $eTag; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $events |
50
|
|
|
* |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
|
|
private function validateEvents(array $events) |
54
|
|
|
{ |
55
|
|
|
foreach ($events as $event) { |
56
|
|
|
if (!$event instanceof EventRecord) { |
57
|
|
|
throw new \InvalidArgumentException(sprintf('Expected EventRecord, got %s', get_class($event))); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $link |
64
|
|
|
*/ |
65
|
|
|
private function validateLink($link) |
66
|
|
|
{ |
67
|
|
|
if (!$link instanceof EventStreamFeedLink) { |
68
|
|
|
throw new \InvalidArgumentException('Invalid link type %s.', get_class($link)); |
69
|
|
|
} |
70
|
|
|
if (isset($this->links[$link->getRelation()])) { |
71
|
|
|
throw new \InvalidArgumentException(sprintf('Link relation %s already there.', $link->getRelation())); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function hasPreviousLink(): bool |
79
|
|
|
{ |
80
|
|
|
return isset($this->links[EventStreamFeedLink::LINK_PREVIOUS]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return EventStreamFeedLink |
85
|
|
|
*/ |
86
|
|
|
public function getPreviousLink(): EventStreamFeedLink |
87
|
|
|
{ |
88
|
|
|
return $this->links[EventStreamFeedLink::LINK_PREVIOUS]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function hasNextLink(): bool |
95
|
|
|
{ |
96
|
|
|
return isset($this->links[EventStreamFeedLink::LINK_NEXT]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return EventStreamFeedLink |
101
|
|
|
*/ |
102
|
|
|
public function getNextLink(): EventStreamFeedLink |
103
|
|
|
{ |
104
|
|
|
return $this->links[EventStreamFeedLink::LINK_NEXT]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function hasLastLink(): bool |
111
|
|
|
{ |
112
|
|
|
return isset($this->links[EventStreamFeedLink::LINK_LAST]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return EventStreamFeedLink |
117
|
|
|
*/ |
118
|
|
|
public function getLastLink(): EventStreamFeedLink |
119
|
|
|
{ |
120
|
|
|
return $this->links[EventStreamFeedLink::LINK_LAST]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getETag(): string |
127
|
|
|
{ |
128
|
|
|
return $this->eTag; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
public function getEvents(): array |
135
|
|
|
{ |
136
|
|
|
return $this->events; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
public function isHeadOfStream(): bool |
143
|
|
|
{ |
144
|
|
|
return $this->isHeadOfStream; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|