-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvelope.php
133 lines (109 loc) · 3.06 KB
/
Envelope.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
declare(strict_types=1);
namespace Pandawa\Contracts\Bus;
/**
* @author Iqbal Maulana <iq.bluejack@gmail.com>
*/
final class Envelope
{
/**
* @var array<class-string<StampInterface>, list<StampInterface>>
*/
private readonly array $stamps;
public function __construct(public readonly object $message, array $stamps = [])
{
$normalizedStamps = [];
foreach ($stamps as $class => $stamp) {
if (is_string($class)) {
$normalizedStamps[$class] = $stamp;
continue;
}
$normalizedStamps[get_class($stamp)][] = $stamp;
}
$this->stamps = $normalizedStamps;
}
/**
* Write message with envelope.
*
* @param object $message
* @param array $stamps
*
* @return static
*/
public static function wrap(object $message, array $stamps = []): self
{
$envelope = $message instanceof self ? $message : new self($message);
return $envelope->with(...$stamps);
}
/**
* Add stamps to envelope.
*/
public function with(StampInterface ...$stamps): self
{
return new self($this->message, [...$this->all(), ...$stamps]);
}
/**
* Remove all stamps of given class.
*/
public function without(string $stampClass): self
{
$newStamps = $this->stamps;
unset($newStamps[$stampClass]);
return new self($this->message, $newStamps);
}
/**
* Remove all stamps that implement given type.
*/
public function withoutTypeOf(string $type): self
{
$newStamps = $this->stamps;
foreach (array_keys($newStamps) as $class) {
if ($class === $type || is_subclass_of($class, $type)) {
unset($newStamps[$class]);
}
}
return new self($this->message, $newStamps);
}
/**
* Get last stamp of the given class.
*
* @template TStamp of StampInterface
*
* @param class-string<TStamp> $stampClass
*
* @return TStamp|StampInterface
*/
public function last(string $stampClass): ?StampInterface
{
$stamps = $this->stamps[$stampClass] ?? null;
return $stamps ? end($stamps) : null;
}
/**
* Get all stamp with filterable of the given class.
*
* @template TStamp of StampInterface
*
* @param class-string<TStamp>|null $stampClass
*
* @return array<int, TStamp>|array<string, list<StampInterface>>
*/
public function all(?string $stampClass = null): array
{
if (null !== $stampClass) {
return $this->stamps[$stampClass] ?? [];
}
return $this->stamps;
}
public function __call(string $method, array $arguments): mixed
{
return $this->message->{$method}(...$arguments);
}
public function __get(string $property): mixed
{
return $this->message->{$property};
}
public function __set(string $property, mixed $value): void
{
$this->message->{$property} = $value;
}
}