From d6eb49a1e9e1ecff0a563b44ea671be24a18f96f Mon Sep 17 00:00:00 2001 From: chipslays Date: Fri, 17 Feb 2023 11:26:04 +0400 Subject: [PATCH] feat: add `except` method to connections class --- src/Connections.php | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/Connections.php b/src/Connections.php index 3cd8fe8..ad3a7f6 100644 --- a/src/Connections.php +++ b/src/Connections.php @@ -38,13 +38,7 @@ public function __construct(array $connections = []) */ public function broadcast(string $event, array $data = [], array|TcpConnection|Connection $excepts = []): self { - foreach ((array) $excepts as &$value) { - if ($value instanceof TcpConnection || $value instanceof Connection) { - $value = $value->id; - } - } - - $targets = $this->filter(fn (Connection $connection) => !in_array($connection->id, $excepts)); + $targets = $this->except($excepts); Server::getInstance()->to($targets, $event, $data); @@ -295,4 +289,29 @@ public function random(): ?Connection return $this->connections[array_rand($this->connections)]; } + + /** + * Get connections without excepted connections. + * + * @param TcpConnection|Connection|array $connections + * @return static + */ + public function except(TcpConnection|Connection|Connections|array $connections): static + { + if ($connections instanceof Connections) { + $connections = $connections->all(); + } + + if (!is_array($connections)) { + $connections = [$connections]; + } + + foreach ($connections as &$value) { + if ($value instanceof TcpConnection || $value instanceof Connection) { + $value = $value->id; + } + } + + return $this->filter(fn (Connection $connection) => !in_array($connection->id, $connections)); + } } \ No newline at end of file