61 lines
1.6 KiB
PHP
61 lines
1.6 KiB
PHP
|
<?php
|
||
|
// DnsEndPoint.php
|
||
|
// Created: 2021-05-02
|
||
|
// Updated: 2022-02-28
|
||
|
|
||
|
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.');
|
||
|
return new DnsEndPoint($parts[0], intval($parts[1]));
|
||
|
}
|
||
|
|
||
|
public function __toString(): string {
|
||
|
if($this->port < 1)
|
||
|
return $this->host;
|
||
|
return $this->host . ':' . $this->port;
|
||
|
}
|
||
|
}
|