-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMessage.php
73 lines (58 loc) · 1.95 KB
/
Message.php
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
namespace PcComponentes\Ddd\Util\Message;
use PcComponentes\Ddd\Domain\Model\ValueObject\Uuid;
abstract class Message implements \JsonSerializable
{
private Uuid $messageId;
private array $payload;
protected function __construct(Uuid $messageId, array $payload)
{
$this->assertPrimitivePayload($payload);
$this->messageId = $messageId;
$this->payload = $payload;
}
abstract public static function messageName(): string;
abstract public static function messageVersion(): string;
abstract public static function messageType(): string;
abstract public function accept(MessageVisitor $visitor): void;
public function messageId(): Uuid
{
return $this->messageId;
}
public function messagePayload(): array
{
return $this->payload;
}
public function jsonSerialize(): array
{
return [
'message_id' => $this->messageId(),
'name' => static::messageName(),
'version' => static::messageVersion(),
'type' => static::messageType(),
'payload' => $this->messagePayload(),
];
}
private function assertPrimitivePayload(array &$payload, string $index = 'payload'): void
{
\array_walk(
$payload,
function ($item, $currentIndex) use ($index) {
$newIndex = "{$index}.{$currentIndex}";
if (true === \is_object($item)) {
throw new \InvalidArgumentException(
\sprintf(
'Attribute %s is an object. Payload parameters only can be primitive.',
$newIndex,
),
);
}
if (true !== \is_array($item)) {
return;
}
$this->assertPrimitivePayload($item, $newIndex);
},
);
}
}