1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\AMQP\Model\Basic\Message; |
5
|
|
|
|
6
|
|
|
use Innmind\AMQP\Model\Basic\Message; |
7
|
|
|
use Innmind\TimeContinuum\{ |
8
|
|
|
PointInTimeInterface, |
9
|
|
|
ElapsedPeriod |
10
|
|
|
}; |
11
|
|
|
use Innmind\Immutable\{ |
12
|
|
|
MapInterface, |
13
|
|
|
Str |
14
|
|
|
}; |
15
|
|
|
|
16
|
|
|
final class Generic implements Message |
17
|
|
|
{ |
18
|
|
|
private $contentType; |
19
|
|
|
private $contentEncoding; |
20
|
|
|
private $headers; |
21
|
|
|
private $deliveryMode; |
22
|
|
|
private $priority; |
23
|
|
|
private $correlationId; |
24
|
|
|
private $replyTo; |
25
|
|
|
private $expiration; |
26
|
|
|
private $id; |
27
|
|
|
private $timestamp; |
28
|
|
|
private $type; |
29
|
|
|
private $userId; |
30
|
|
|
private $appId; |
31
|
|
|
private $body; |
32
|
|
|
|
33
|
22 |
|
public function __construct(Str $body) |
34
|
|
|
{ |
35
|
22 |
|
$this->body = $body->toEncoding('ASCII'); |
36
|
22 |
|
} |
37
|
|
|
|
38
|
9 |
|
public function hasContentType(): bool |
39
|
|
|
{ |
40
|
9 |
|
return $this->contentType instanceof ContentType; |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
public function contentType(): ContentType |
44
|
|
|
{ |
45
|
2 |
|
return $this->contentType; |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
public function withContentType(ContentType $contentType): Message |
49
|
|
|
{ |
50
|
2 |
|
$self = clone $this; |
51
|
2 |
|
$self->contentType = $contentType; |
52
|
|
|
|
53
|
2 |
|
return $self; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
9 |
|
public function hasContentEncoding(): bool |
57
|
|
|
{ |
58
|
9 |
|
return $this->contentEncoding instanceof ContentEncoding; |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
public function contentEncoding(): ContentEncoding |
62
|
|
|
{ |
63
|
2 |
|
return $this->contentEncoding; |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
public function withContentEncoding(ContentEncoding $contentEncoding): Message |
67
|
|
|
{ |
68
|
2 |
|
$self = clone $this; |
69
|
2 |
|
$self->contentEncoding = $contentEncoding; |
70
|
|
|
|
71
|
2 |
|
return $self; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
9 |
|
public function hasHeaders(): bool |
75
|
|
|
{ |
76
|
9 |
|
return $this->headers instanceof MapInterface; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
2 |
|
public function headers(): MapInterface |
83
|
|
|
{ |
84
|
2 |
|
return $this->headers; |
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
public function withHeaders(MapInterface $headers): Message |
88
|
|
|
{ |
89
|
|
|
if ( |
90
|
3 |
|
(string) $headers->keyType() !== 'string' || |
91
|
3 |
|
(string) $headers->valueType() !== 'mixed' |
92
|
|
|
) { |
93
|
1 |
|
throw new \TypeError('Argument 1 must be of type MapInterface<string, mixed>'); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
$self = clone $this; |
97
|
2 |
|
$self->headers = $headers; |
98
|
|
|
|
99
|
2 |
|
return $self; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
9 |
|
public function hasDeliveryMode(): bool |
103
|
|
|
{ |
104
|
9 |
|
return $this->deliveryMode instanceof DeliveryMode; |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
public function deliveryMode(): DeliveryMode |
108
|
|
|
{ |
109
|
2 |
|
return $this->deliveryMode; |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
public function withDeliveryMode(DeliveryMode $deliveryMode): Message |
113
|
|
|
{ |
114
|
2 |
|
$self = clone $this; |
115
|
2 |
|
$self->deliveryMode = $deliveryMode; |
116
|
|
|
|
117
|
2 |
|
return $self; |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
9 |
|
public function hasPriority(): bool |
121
|
|
|
{ |
122
|
9 |
|
return $this->priority instanceof Priority; |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
public function priority(): Priority |
126
|
|
|
{ |
127
|
2 |
|
return $this->priority; |
128
|
|
|
} |
129
|
|
|
|
130
|
2 |
|
public function withPriority(Priority $priority): Message |
131
|
|
|
{ |
132
|
2 |
|
$self = clone $this; |
133
|
2 |
|
$self->priority = $priority; |
134
|
|
|
|
135
|
2 |
|
return $self; |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
9 |
|
public function hasCorrelationId(): bool |
139
|
|
|
{ |
140
|
9 |
|
return $this->correlationId instanceof CorrelationId; |
141
|
|
|
} |
142
|
|
|
|
143
|
2 |
|
public function correlationId(): CorrelationId |
144
|
|
|
{ |
145
|
2 |
|
return $this->correlationId; |
146
|
|
|
} |
147
|
|
|
|
148
|
2 |
|
public function withCorrelationId(CorrelationId $correlationId): Message |
149
|
|
|
{ |
150
|
2 |
|
$self = clone $this; |
151
|
2 |
|
$self->correlationId = $correlationId; |
152
|
|
|
|
153
|
2 |
|
return $self; |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
9 |
|
public function hasReplyTo(): bool |
157
|
|
|
{ |
158
|
9 |
|
return $this->replyTo instanceof ReplyTo; |
159
|
|
|
} |
160
|
|
|
|
161
|
2 |
|
public function replyTo(): ReplyTo |
162
|
|
|
{ |
163
|
2 |
|
return $this->replyTo; |
164
|
|
|
} |
165
|
|
|
|
166
|
2 |
|
public function withReplyTo(ReplyTo $replyTo): Message |
167
|
|
|
{ |
168
|
2 |
|
$self = clone $this; |
169
|
2 |
|
$self->replyTo = $replyTo; |
170
|
|
|
|
171
|
2 |
|
return $self; |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
9 |
|
public function hasExpiration(): bool |
175
|
|
|
{ |
176
|
9 |
|
return $this->expiration instanceof ElapsedPeriod; |
177
|
|
|
} |
178
|
|
|
|
179
|
2 |
|
public function expiration(): ElapsedPeriod |
180
|
|
|
{ |
181
|
2 |
|
return $this->expiration; |
182
|
|
|
} |
183
|
|
|
|
184
|
2 |
|
public function withExpiration(ElapsedPeriod $expiration): Message |
185
|
|
|
{ |
186
|
2 |
|
$self = clone $this; |
187
|
2 |
|
$self->expiration = $expiration; |
188
|
|
|
|
189
|
2 |
|
return $self; |
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
192
|
9 |
|
public function hasId(): bool |
193
|
|
|
{ |
194
|
9 |
|
return $this->id instanceof Id; |
195
|
|
|
} |
196
|
|
|
|
197
|
2 |
|
public function id(): Id |
198
|
|
|
{ |
199
|
2 |
|
return $this->id; |
200
|
|
|
} |
201
|
|
|
|
202
|
2 |
|
public function withId(Id $id): Message |
203
|
|
|
{ |
204
|
2 |
|
$self = clone $this; |
205
|
2 |
|
$self->id = $id; |
206
|
|
|
|
207
|
2 |
|
return $self; |
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
9 |
|
public function hasTimestamp(): bool |
211
|
|
|
{ |
212
|
9 |
|
return $this->timestamp instanceof PointInTimeInterface; |
213
|
|
|
} |
214
|
|
|
|
215
|
2 |
|
public function timestamp(): PointInTimeInterface |
216
|
|
|
{ |
217
|
2 |
|
return $this->timestamp; |
218
|
|
|
} |
219
|
|
|
|
220
|
2 |
|
public function withTimestamp(PointInTimeInterface $timestamp): Message |
221
|
|
|
{ |
222
|
2 |
|
$self = clone $this; |
223
|
2 |
|
$self->timestamp = $timestamp; |
224
|
|
|
|
225
|
2 |
|
return $self; |
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
9 |
|
public function hasType(): bool |
229
|
|
|
{ |
230
|
9 |
|
return $this->type instanceof Type; |
231
|
|
|
} |
232
|
|
|
|
233
|
2 |
|
public function type(): Type |
234
|
|
|
{ |
235
|
2 |
|
return $this->type; |
236
|
|
|
} |
237
|
|
|
|
238
|
2 |
|
public function withType(Type $type): Message |
239
|
|
|
{ |
240
|
2 |
|
$self = clone $this; |
241
|
2 |
|
$self->type = $type; |
242
|
|
|
|
243
|
2 |
|
return $self; |
|
|
|
|
244
|
|
|
} |
245
|
|
|
|
246
|
9 |
|
public function hasUserId(): bool |
247
|
|
|
{ |
248
|
9 |
|
return $this->userId instanceof UserId; |
249
|
|
|
} |
250
|
|
|
|
251
|
2 |
|
public function userId(): UserId |
252
|
|
|
{ |
253
|
2 |
|
return $this->userId; |
254
|
|
|
} |
255
|
|
|
|
256
|
2 |
|
public function withUserId(UserId $userId): Message |
257
|
|
|
{ |
258
|
2 |
|
$self = clone $this; |
259
|
2 |
|
$self->userId = $userId; |
260
|
|
|
|
261
|
2 |
|
return $self; |
|
|
|
|
262
|
|
|
} |
263
|
|
|
|
264
|
9 |
|
public function hasAppId(): bool |
265
|
|
|
{ |
266
|
9 |
|
return $this->appId instanceof AppId; |
267
|
|
|
} |
268
|
|
|
|
269
|
2 |
|
public function appId(): AppId |
270
|
|
|
{ |
271
|
2 |
|
return $this->appId; |
272
|
|
|
} |
273
|
|
|
|
274
|
2 |
|
public function withAppId(AppId $appId): Message |
275
|
|
|
{ |
276
|
2 |
|
$self = clone $this; |
277
|
2 |
|
$self->appId = $appId; |
278
|
|
|
|
279
|
2 |
|
return $self; |
|
|
|
|
280
|
|
|
} |
281
|
|
|
|
282
|
8 |
|
public function body(): Str |
283
|
|
|
{ |
284
|
8 |
|
return $this->body; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.