0xFFFF) throw new InvalidArgumentException('$port is not a valid port number.'); $this->address = $address; $this->port = $port; } public function getAddress(): IPAddress { return $this->address; } public function hasPort(): bool { return $this->port > 0; } public function getPort(): int { return $this->port; } public function __serialize(): array { return [$this->address, $this->port]; } public function __unserialize(array $serialized): void { $this->address = $serialized[0]; $this->port = $serialized[1]; } public function equals(mixed $other): bool { return $other instanceof IPEndPoint && $this->port === $other->port && $this->address->equals($other->address); } public static function parse(string $string): EndPoint { if(str_starts_with($string, '[')) { // IPv6 $closing = strpos($string, ']'); if($closing === false) throw new InvalidArgumentException('$string is not a valid IP end point.'); $ip = substr($string, 1, $closing - 1); $port = (int)substr($string, $closing + 2); } else { // IPv4 $parts = explode(':', $string, 2); if(count($parts) < 2) throw new InvalidArgumentException('$string is not a valid IP end point.'); $ip = $parts[0]; $port = (int)$parts[1]; } return new IPEndPoint(IPAddress::parse($ip), $port); } public function __toString(): string { if($this->port < 1) return $this->address->getAddress(); return $this->address->getAddress() . ':' . $this->port; } }