-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChatMessage.php
More file actions
52 lines (46 loc) · 1.22 KB
/
Copy pathChatMessage.php
File metadata and controls
52 lines (46 loc) · 1.22 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
<?php
declare(strict_types=1);
namespace Micilini\PhpSockets\Chat;
use DateTimeImmutable;
final readonly class ChatMessage
{
/**
* @param array<string, mixed> $metadata
*/
public function __construct(
public string $id,
public string $roomId,
public string $fromUserId,
public string $kind,
public ?string $body,
public DateTimeImmutable $createdAt,
public array $metadata = [],
) {
}
public static function text(string $roomId, string $fromUserId, string $text): self
{
return new self(
id: 'msg_' . bin2hex(random_bytes(16)),
roomId: $roomId,
fromUserId: $fromUserId,
kind: 'text',
body: $text,
createdAt: new DateTimeImmutable(),
);
}
/**
* @return array<string, mixed>
*/
public function toArray(): array
{
return [
'id' => $this->id,
'roomId' => $this->roomId,
'fromUserId' => $this->fromUserId,
'kind' => $this->kind,
'body' => $this->body,
'metadata' => $this->metadata,
'createdAt' => $this->createdAt->format(DATE_ATOM),
];
}
}