body[$key] = $message; return $this; } /** Unset a key in the message array. */ public function forgetBodyKey(string $key): self { unset($this->body[$key]); return $this; } /** Set the message headers. */ public function withHeaders(array $headers = []): self { $this->headers = $headers; return $this; } public function onTopic(string $topic): self { $this->topicName = $topic; return $this; } /** Set the kafka message key. */ public function withKey(mixed $key): self { $this->key = $key; return $this; } #[ArrayShape(['payload' => 'array', 'key' => 'null|string', 'headers' => 'array'])] public function toArray(): array { return [ 'payload' => $this->body, 'key' => $this->key, 'headers' => $this->headers, ]; } public function withBody(mixed $body): ProducerMessage { $this->body = $body; return $this; } public function withHeader(string $key, string|int|float $value): ProducerMessage { $this->headers[$key] = $value; return $this; } public function getHeaders(): ?array { // Here we insert an uuid to be used to uniquely identify this message. If the // id is already set, then array_merge will override it. It's safe to do it // here because this class is used only when we produce a new message. return array_merge(parent::getHeaders(), [ config('kafka.message_id_key') => Str::uuid()->toString(), ]); } }