-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathGenericMessage.php
More file actions
52 lines (45 loc) · 1.56 KB
/
Copy pathGenericMessage.php
File metadata and controls
52 lines (45 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare(strict_types=1);
namespace Yiisoft\Queue\Message;
use InvalidArgumentException;
/**
* A general-purpose immutable {@see MessageInterface} implementation that holds a message type and its payload data.
*
* Prefer creating custom message classes that better express your domain.
*
* @psalm-import-type MessagePayload from MessageInterface
*/
final class GenericMessage extends Message
{
/**
* @param string $type A message type used to resolve the handler. Must be a non-empty string.
* @param bool|int|float|string|array|null $payload Message payload data. Must contain only `null`, scalars (`bool`,
* `int`, `float`, `string`), or arrays composed of the same types recursively.
*
* @psalm-param non-empty-string $type
* @psalm-param MessagePayload $payload
*/
public function __construct(
private readonly string $type,
private readonly bool|int|float|string|array|null $payload,
) {
/**
* @psalm-suppress TypeDoesNotContainType Guard against an empty type passed without static analysis.
*/
if ($this->type === '') {
throw new InvalidArgumentException('Message type must be a non-empty string.');
}
}
public static function fromPayload(string $type, bool|int|float|string|array|null $payload): static
{
return new self($type, $payload);
}
public function getType(): string
{
return $this->type;
}
public function getPayload(): bool|int|float|string|array|null
{
return $this->payload;
}
}