The topic, if more than one the first is canonical, but all receive delivery */ public readonly array $topic; /** @var string|null The SSE event type */ public readonly ?string $type; /** @var string|null Message data */ public readonly ?string $data; /** @var bool|null Private update*/ public readonly ?bool $private; /** @var string|null Message ID */ public readonly ?string $id; /** @var int SSE retry interval */ public readonly ?int $retry; private readonly int $created; public function __construct( array $topic, ?string $data=null, ?string $type=null, ?bool $private=null, ?string $id=null, ?int $retry=null ) { $this->topic = $topic; $this->type = $type; $this->data = $data; $this->private = $private; $this->id = $id; $this->retry = $retry; $this->created = time(); } public function getAge(): int { return time() - $this->created; } public function toString(): string { $msg = []; if ($this->type) $msg[] = "event: ".$this->type; if ($this->retry) $msg[] = "retry: ".$this->retry; if ($this->id) $msg[] = "id: ".$this->id; if ($this->data) $msg[] = "data: ".$this->data; return join("\n", $msg)."\n\n"; } public static function fromData(array $data): Message { return new Message( topic: (array)$data['topic'], type: $data['type']??null, data: $data['data']??null, private: match ($data['private']??null) { "on" => true, true => true, null => null, default => false }, id: $data['id']??null, retry: $data['retry']??null, ); } }