Skip to content

Commit

Permalink
feat: add except method to connections class
Browse files Browse the repository at this point in the history
  • Loading branch information
chipslays committed Feb 17, 2023
1 parent 4577263 commit d6eb49a
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/Connections.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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));
}
}

0 comments on commit d6eb49a

Please sign in to comment.