diff options
Diffstat (limited to 'matrix-specification/Events')
-rw-r--r-- | matrix-specification/Events/ClientEvent.php | 32 | ||||
-rw-r--r-- | matrix-specification/Events/ClientEventWithoutRoomId.php | 34 | ||||
-rw-r--r-- | matrix-specification/Events/Event.php | 17 | ||||
-rw-r--r-- | matrix-specification/Events/PresenceEvent.php | 42 | ||||
-rw-r--r-- | matrix-specification/Events/SenderEvent.php | 20 | ||||
-rw-r--r-- | matrix-specification/Events/UnsignedData.php | 31 |
6 files changed, 176 insertions, 0 deletions
diff --git a/matrix-specification/Events/ClientEvent.php b/matrix-specification/Events/ClientEvent.php new file mode 100644 index 0000000..be1e354 --- /dev/null +++ b/matrix-specification/Events/ClientEvent.php @@ -0,0 +1,32 @@ +<?php + +namespace Matrix\Events; + +use Matrix\Enums\EventType; + +class ClientEvent extends ClientEventWithoutRoomId +{ + public function __construct( + array $content, + string $eventId, + int $originServerTimestamp, + protected string $roomId, + string $sender, + string $stateKey, + EventType $type, + ?UnsignedData $unsigned = null, + ) + { + parent::__construct($content, $eventId, $originServerTimestamp, $sender, $stateKey, $type); + } + + public function jsonSerialize(): array + { + $clientEvent = parent::jsonSerialize(); + $clientEvent += [ + "room_id" => $this->roomId, + ]; + + return $clientEvent; + } +} diff --git a/matrix-specification/Events/ClientEventWithoutRoomId.php b/matrix-specification/Events/ClientEventWithoutRoomId.php new file mode 100644 index 0000000..0410d5b --- /dev/null +++ b/matrix-specification/Events/ClientEventWithoutRoomId.php @@ -0,0 +1,34 @@ +<?php + +namespace Matrix\Events; + +use Matrix\Enums\EventType; + +class ClientEventWithoutRoomId extends SenderEvent +{ + public function __construct( + array $content, + protected string $eventId, + protected int $originServerTimestamp, + string $sender, + protected string $stateKey, + EventType $type, + protected ?UnsignedData $unsigned = null, + ) + { + parent::__construct($content, $sender, $type); + } + + public function jsonSerialize(): array + { + return [ + "content" => $this->content ?: new \stdClass, + "event_id" => $this->eventId, + "origin_server_ts" => $this->originServerTimestamp, + "sender" => $this->sender, + "state_key" => $this->stateKey, + "type" => $this->type, + "unsigned" => $this->unsigned ?? new \stdClass, + ]; + } +} diff --git a/matrix-specification/Events/Event.php b/matrix-specification/Events/Event.php new file mode 100644 index 0000000..fe85f48 --- /dev/null +++ b/matrix-specification/Events/Event.php @@ -0,0 +1,17 @@ +<?php + +namespace Matrix\Events; + +use Matrix\Enums\EventType; + +abstract class Event implements \JsonSerializable +{ + /** + * @param array<string, mixed> $content + */ + public function __construct( + protected array $content, + protected EventType $type, + ) + {} +} diff --git a/matrix-specification/Events/PresenceEvent.php b/matrix-specification/Events/PresenceEvent.php new file mode 100644 index 0000000..f71d4bd --- /dev/null +++ b/matrix-specification/Events/PresenceEvent.php @@ -0,0 +1,42 @@ +<?php + +namespace Matrix\Events; + +use Matrix\Enums\EventType; +use Matrix\Enums\PresenceState; + +class PresenceEvent extends SenderEvent +{ + public function __construct( + string $sender, + PresenceState $presence, + string $avatarUrl = "", + bool $currentlyActive = false, + string $displayName = "", + int $lastActiveAgo = 0, + string $statusMessage = "", + ) + { + parent::__construct( + [ + "avatar_url" => $avatarUrl, + "currently_active" => $currentlyActive, + "display_name" => $displayName, + "last_active_ago" => $lastActiveAgo, + "presence" => $presence, + "status_msg" => $statusMessage, + ], + $sender, + EventType::PRESENCE + ); + } + + public function jsonSerialize(): array + { + return [ + "content" => $this->content, + "sender" => $this->sender, + "type" => $this->type, + ]; + } +} diff --git a/matrix-specification/Events/SenderEvent.php b/matrix-specification/Events/SenderEvent.php new file mode 100644 index 0000000..f3d4ceb --- /dev/null +++ b/matrix-specification/Events/SenderEvent.php @@ -0,0 +1,20 @@ +<?php + +namespace Matrix\Events; + +use Matrix\Enums\EventType; + +abstract class SenderEvent extends Event +{ + /** + * @param array<string, mixed> $content + */ + public function __construct( + array $content, + protected string $sender, + EventType $type, + ) + { + parent::__construct($content, $type); + } +} diff --git a/matrix-specification/Events/UnsignedData.php b/matrix-specification/Events/UnsignedData.php new file mode 100644 index 0000000..924af29 --- /dev/null +++ b/matrix-specification/Events/UnsignedData.php @@ -0,0 +1,31 @@ +<?php + +namespace Matrix\Events; + +use Matrix\Enums\MembershipState; + +/** + * @see https://spec.matrix.org/v1.16/client-server-api/#definition-clientevent_unsigneddata + */ +class UnsignedData implements \JsonSerializable +{ + public function __construct( + private ?int $age = null, + private ?MembershipState $membership = null, + private ?array $previousContent = null, + private ?ClientEvent $redactedBecause = null, + private ?string $transactionId = null, + ) + {} + + public function jsonSerialize(): array + { + return array_filter([ + "age" => $this->age, + "membership" => $this->membership, + "prev_content" => $this->previousContent, + "redacted_because" => $this->redactedBecause, + "transaction_id" => $this->transactionId, + ], "is_null"); + } +} |