2022-09-13 13:13:11 +00:00
|
|
|
<?php
|
|
|
|
// DnsEndPoint.php
|
|
|
|
// Created: 2021-05-02
|
2023-01-01 19:00:00 +00:00
|
|
|
// Updated: 2023-01-01
|
2022-09-13 13:13:11 +00:00
|
|
|
|
|
|
|
namespace Index\Net;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use Stringable;
|
|
|
|
|
|
|
|
class DnsEndPoint extends EndPoint implements Stringable {
|
|
|
|
private string $host;
|
|
|
|
private int $port;
|
|
|
|
|
|
|
|
public function __construct(string $host, int $port) {
|
|
|
|
if($port < 0 || $port > 0xFFFF)
|
|
|
|
throw new InvalidArgumentException('$port is not a valid port number.');
|
|
|
|
$this->host = $host;
|
|
|
|
$this->port = $port;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHost(): string {
|
|
|
|
return $this->host;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasPort(): bool {
|
|
|
|
return $this->port > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPort(): int {
|
|
|
|
return $this->port;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __serialize(): array {
|
|
|
|
return [$this->host, $this->port];
|
|
|
|
}
|
|
|
|
public function __unserialize(array $serialized): void {
|
|
|
|
$this->host = $serialized[0];
|
|
|
|
$this->port = $serialized[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function equals(mixed $other): bool {
|
|
|
|
return $other instanceof DnsEndPoint
|
|
|
|
&& $this->port === $other->port
|
|
|
|
&& $this->host === $other->host;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function parse(string $string): EndPoint {
|
|
|
|
$parts = explode(':', $string);
|
|
|
|
if(count($parts) < 2)
|
|
|
|
throw new InvalidArgumentException('$string is not a valid host and port combo.');
|
2023-01-01 19:00:00 +00:00
|
|
|
return new DnsEndPoint($parts[0], (int)$parts[1]);
|
2022-09-13 13:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __toString(): string {
|
|
|
|
if($this->port < 1)
|
|
|
|
return $this->host;
|
|
|
|
return $this->host . ':' . $this->port;
|
|
|
|
}
|
|
|
|
}
|