ip = $ip ?? self::INADDR_ANY; $this->port = (int) $port; if (str_contains($port, ':')) { [$this->ip, $port] = explode(':', $port); $this->port = (int) $port; } throw_unless(filter_var($this->ip, FILTER_VALIDATE_IP) !== false, new InvalidArgumentException("$this->ip is an invalid IP address")); throw_unless($this->port > self::PORT_FLOOR, "Port $this->port must be greater than " . self::PORT_FLOOR); throw_unless($this->port < self::PORT_CEIL, "Port $this->port must be less than " . self::PORT_CEIL); } public function __toString(): string { $ip = $this->ip; if ($ip === self::INADDR_ANY) { return (string) $this->port; } if ($ip === self::INADDR_LOOPBACK) { $ip = 'localhost'; } return "$ip:$this->port"; } public function toJson($options = 0): string { return json_encode($this->__toString()); } }