diff --git a/src/Connection/Channels.php b/src/Connection/Channels.php index 14e0222..748c048 100644 --- a/src/Connection/Channels.php +++ b/src/Connection/Channels.php @@ -12,6 +12,11 @@ class Channels /** @var string[] */ protected array $channels = []; + /** + * Constructor. + * + * @param TcpConnection|Connection $connection + */ public function __construct(protected TcpConnection|Connection $connection) { // diff --git a/src/Connections.php b/src/Connections.php index 1bfb68a..3cd8fe8 100644 --- a/src/Connections.php +++ b/src/Connections.php @@ -111,7 +111,7 @@ public function get(int $id, mixed $default = null): ?Connection /** * Get specific connections. * - * @param array|integer $ids + * @param int[]|int $ids * @return static */ public function only(array|int $ids): static @@ -143,7 +143,7 @@ public function add(TcpConnection|Connection $connection): self /** * Remove connection from collection. * - * @param TcpConnection|Connection|integer $connection + * @param TcpConnection|Connection|int $connection * @return self */ public function remove(TcpConnection|Connection|int $connection): self @@ -166,7 +166,7 @@ public function first(): ?Connection } /** - * Get last connection from connection. + * Get last connection in this collection. * * @return Connection|null */ @@ -176,6 +176,8 @@ public function last(): ?Connection } /** + * Get a limited collection of connections. + * * @param int $count * @param int $offset * @return static @@ -186,6 +188,8 @@ public function limit(int $count, int $offset = 0): static } /** + * Get a filtered collection of connections. + * * @param callable|null $callback * @return static */ @@ -195,6 +199,8 @@ public function filter(callable $callback): static } /** + * Map each connection in collection. + * * @param callable $callback * @return static */ @@ -208,6 +214,8 @@ public function map(callable $callback): static } /** + * Do something on over each connection in collection. + * * @param callable $callback * @return self */ @@ -223,6 +231,8 @@ public function each(callable $callback): self } /** + * Get a empty collection of connections. + * * @return static */ public function clear(): static @@ -231,14 +241,18 @@ public function clear(): static } /** - * @return Connection + * Get a first connection and remove this from collection. + * + * @return Connection|null */ - public function shift(): Connection + public function shift(): ?Connection { return array_shift($this->connections); } /** + * Split a collection to array of chunks (each chunk is a collection of connections). + * * @param int $size * @return array */ @@ -269,21 +283,16 @@ public function tap(callable $callback): self } /** - * @param callable $callback - * @return mixed - */ - public function pipe(callable $callback): mixed - { - return call_user_func($callback, $this); - } - - /** - * Get random connection. + * Get a random connection. * - * @return Connection + * @return Connection|null */ - public function random(): Connection + public function random(): ?Connection { + if ($this->count() == 0) { + return null; + } + return $this->connections[array_rand($this->connections)]; } } \ No newline at end of file diff --git a/src/Events/AbstractEvent.php b/src/Events/AbstractEvent.php index c248867..cdaad72 100644 --- a/src/Events/AbstractEvent.php +++ b/src/Events/AbstractEvent.php @@ -30,14 +30,14 @@ abstract class AbstractEvent public readonly Payload $payload; /** - * Available if client passed `channelId`. + * Available if client passed `channel_id_`. * * @var Channel|null */ public readonly ?Channel $channel; /** - * Available if client passed `targetId`. + * Available if client passed `target_id`. * * @var Connection|null */ @@ -85,7 +85,7 @@ public function boot(Connection $connection, Payload $payload): self $this->server = Server::getInstance(); $this->data = &$this->payload->data; - $this->initMagicalVars(); + $this->setMagicalVariables(); return $this; } @@ -93,15 +93,17 @@ public function boot(Connection $connection, Payload $payload): self /** * @return void */ - protected function initMagicalVars(): void + protected function setMagicalVariables(): void { - // Get channel instance by `channelId` parameter. - $this->channel = $this->server->channel($this->payload('channelId', '')); + // Get channel instance by `channel_id_` parameter. + if ($this->data->has('channel_id')) { + $this->channel = $this->server->channel($this->payload('channel_id')); + } - // Get target connection instance by `targetId` parameter. - $this->target = $this->payload->has('targetId') - ? $this->server->connection((int) $this->payload('targetId')) - : null; + // Get target connection instance by `target_id` parameter. + if ($this->data->has('target_id')) { + $this->target = $this->server->connection((int) $this->payload('target_id')); + } } /** @@ -163,7 +165,7 @@ public function broadcast(string $event, array $data = [], array|TcpConnection|C } /** - * Getter for channel (available if client passed `channelId`). + * Getter for channel (available if client passed `channel_id_`). * * @return Channel|null */ @@ -173,7 +175,7 @@ public function channel(): ?Channel } /** - * Getter for target (available if client passed `targetId`). + * Getter for target (available if client passed `target_id`). * * @return Connection|null */ diff --git a/src/Events/Event.php b/src/Events/Event.php index cea83d7..b278d8a 100644 --- a/src/Events/Event.php +++ b/src/Events/Event.php @@ -10,8 +10,6 @@ class Event extends AbstractEvent { /** - * This method here is useless, but needed. - * * @return void */ public function handle(Connection $connection, Payload $payload, Server $server): void diff --git a/src/Traits/Rawable.php b/src/Traits/Rawable.php index 514df1e..6b686fc 100644 --- a/src/Traits/Rawable.php +++ b/src/Traits/Rawable.php @@ -6,6 +6,12 @@ trait Rawable { protected $onRawHandler; + /** + * Handle raw data from client. + * + * @param callable $handler + * @return void + */ public function onRaw(callable $handler): void { $this->onRawHandler = $handler;