-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventEmit.php
More file actions
65 lines (58 loc) · 1.85 KB
/
EventEmit.php
File metadata and controls
65 lines (58 loc) · 1.85 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
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
namespace Arcp\Messages\Telemetry;
use Arcp\Envelope\MessageType;
use Arcp\Errors\InvalidArgumentException;
/**
* `event.emit` — generic typed event envelope (RFC §6.2).
*
* Used for ad-hoc structured events (subscription markers like
* `subscription.backfill_complete`, custom domain events). The runtime
* does not interpret `payload.type` beyond delivering it through
* subscriptions.
*
* @phpstan-type EventBody array<string, mixed>
*/
final readonly class EventEmit extends MessageType
{
/** @param EventBody $attributes */
public function __construct(
public string $eventType,
public array $attributes = [],
) {
if ($eventType === '') {
throw new InvalidArgumentException('event.emit type must be non-empty');
}
}
#[\Override]
public static function typeName(): string
{
return 'event.emit';
}
#[\Override]
public static function fromArray(array $data): static
{
$eventType = $data['type'] ?? throw new InvalidArgumentException('event.emit payload.type missing');
if (!\is_string($eventType)) {
throw new InvalidArgumentException('event.emit payload.type must be string');
}
$attributes = [];
if (isset($data['attributes'])) {
if (!\is_array($data['attributes'])) {
throw new InvalidArgumentException('event.emit payload.attributes must be object');
}
/** @var EventBody $attributes */
$attributes = $data['attributes'];
}
return new static($eventType, $attributes);
}
#[\Override]
public function toArray(): array
{
$out = ['type' => $this->eventType];
if ($this->attributes !== []) {
$out['attributes'] = $this->attributes;
}
return $out;
}
}