1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace League\Uri\Components;
use Deprecated;
use Iterator;
use League\Uri\Contracts\AuthorityInterface;
use League\Uri\Contracts\DomainHostInterface;
use League\Uri\Contracts\HostInterface;
use League\Uri\Contracts\UriComponentInterface;
use League\Uri\Contracts\UriInterface;
use League\Uri\Exceptions\OffsetOutOfBounds;
use League\Uri\Exceptions\SyntaxError;
use Psr\Http\Message\UriInterface as Psr7UriInterface;
use Stringable;
use TypeError;
use function array_count_values;
use function array_filter;
use function array_keys;
use function array_reverse;
use function array_shift;
use function count;
use function explode;
use function implode;
use function sprintf;
final class Domain extends Component implements DomainHostInterface
{
private const SEPARATOR = '.';
private readonly HostInterface $host;
/** @var string[] */
private readonly array $labels;
private function __construct(Stringable|string|null $host)
{
$host = match (true) {
$host instanceof HostInterface => $host,
$host instanceof UriComponentInterface => Host::new($host->value()),
default => Host::new($host),
};
if (!$host->isDomain()) {
throw new SyntaxError(sprintf('`%s` is an invalid domain name.', $host->value() ?? 'null'));
}
$this->host = $host;
$this->labels = array_reverse(explode(self::SEPARATOR, $this->host->value() ?? ''));
}
/**
* Returns a new instance from a string or a stringable object.
*/
public static function new(Stringable|string|null $value = null): self
{
return new self($value);
}
/**
* Returns a new instance from an iterable structure.
*/
public static function fromLabels(Stringable|string ...$labels): self
{
return new self(match ([]) {
$labels => null,
default => implode(self::SEPARATOR, array_reverse(array_map(
fn ($label) => self::filterComponent($label),
$labels
))),
});
}
/**
* Create a new instance from a URI object.
*/
public static function fromUri(Stringable|string $uri): self
{
return self::new(Host::fromUri($uri));
}
/**
* Create a new instance from an Authority object.
*/
public static function fromAuthority(Stringable|string $authority): self
{
return self::new(Host::fromAuthority($authority));
}
public function value(): ?string
{
return $this->host->value();
}
public function toAscii(): ?string
{
return $this->host->toAscii();
}
public function toUnicode(): ?string
{
return $this->host->toUnicode();
}
public function isIp(): bool
{
return false;
}
public function isDomain(): bool
{
return true;
}
public function isRegisteredName(): bool
{
return true;
}
public function getIpVersion(): ?string
{
return null;
}
public function getIp(): ?string
{
return null;
}
public function count(): int
{
return count($this->labels);
}
public function getIterator(): Iterator
{
yield from $this->labels;
}
public function get(int $offset): ?string
{
if ($offset < 0) {
$offset += count($this->labels);
}
return $this->labels[$offset] ?? null;
}
public function keys(?string $label = null): array
{
return match (null) {
$label => array_keys($this->labels),
default => array_keys($this->labels, $label, true),
};
}
public function isAbsolute(): bool
{
return count($this->labels) > 1 && '' === $this->labels[array_key_first($this->labels)];
}
public function prepend(Stringable|string|int|null $label): DomainHostInterface
{
$label = self::filterComponent($label);
$value = $this->value();
return match (true) {
null === $label => $this,
null === $value => new self($label),
str_ends_with($label, self::SEPARATOR) => new self($label.$value),
default => new self($label.self::SEPARATOR.$value),
};
}
public function append(Stringable|string|int|null $label): DomainHostInterface
{
$label = self::filterComponent($label);
$value = $this->value();
return match (true) {
null === $label => $this,
null === $value => new self($label),
!$this->isAbsolute() => new self($value.self::SEPARATOR.$label),
str_ends_with($label, self::SEPARATOR) => new self($value.$label),
default => new self($value.$label.self::SEPARATOR),
};
}
public function withRootLabel(): DomainHostInterface
{
$key = array_key_first($this->labels);
return match ($this->labels[$key]) {
'' => $this,
default => $this->append(''),
};
}
public function slice(int $offset, ?int $length = null): self
{
$nbLabels = count($this->labels);
if ($offset < -$nbLabels || $offset > $nbLabels) {
throw new OffsetOutOfBounds(sprintf('No label can be found with at : `%s`.', $offset));
}
$labels = array_slice($this->labels, $offset, $length, true);
return match ($labels) {
$this->labels => $this,
default => self::fromLabels(...$labels),
};
}
public function withoutRootLabel(): DomainHostInterface
{
$key = array_key_first($this->labels);
if ('' !== $this->labels[$key]) {
return $this;
}
$labels = $this->labels;
array_shift($labels);
return self::fromLabels(...$labels);
}
/**
* @throws OffsetOutOfBounds
*/
public function withLabel(int $key, Stringable|string|int|null $label): DomainHostInterface
{
$nbLabels = count($this->labels);
if ($key < - $nbLabels - 1 || $key > $nbLabels) {
throw new OffsetOutOfBounds(sprintf('No label can be added with the submitted key : `%s`.', $key));
}
if (0 > $key) {
$key += $nbLabels;
}
if ($nbLabels === $key) {
return $this->append($label);
}
if (-1 === $key) {
return $this->prepend($label);
}
if (!$label instanceof HostInterface && null !== $label) {
$label = Host::new((string) $label)->value();
}
if ($label === $this->labels[$key]) {
return $this;
}
$labels = $this->labels;
$labels[$key] = $label;
return new self(implode(self::SEPARATOR, array_reverse($labels)));
}
public function withoutLabel(int ...$keys): DomainHostInterface
{
if ([] === $keys) {
return $this;
}
$nb_labels = count($this->labels);
foreach ($keys as &$offset) {
if (- $nb_labels > $offset || $nb_labels - 1 < $offset) {
throw new OffsetOutOfBounds(sprintf('No label can be removed with the submitted key : `%s`.', $offset));
}
if (0 > $offset) {
$offset += $nb_labels;
}
}
unset($offset);
$deleted_keys = array_keys(array_count_values($keys));
$filter = static fn ($key): bool => !in_array($key, $deleted_keys, true);
return self::fromLabels(...array_filter($this->labels, $filter, ARRAY_FILTER_USE_KEY));
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see Domain::getIterator()
*
* @codeCoverageIgnore
*
* Returns a new instance from a string or a stringable object.
*/
#[Deprecated(message:'use League\Uri\Components\Domain::getIterator() instead', since:'league/uri-components:7.0.0')]
public function labels(): array
{
return $this->labels;
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see Domain::new()
*
* @codeCoverageIgnore
*
* Returns a new instance from a string or a stringable object.
*/
#[Deprecated(message:'use League\Uri\Components\Domain::new() instead', since:'league/uri-components:7.0.0')]
public static function createFromString(Stringable|string $host): self
{
return self::new($host);
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see Domain::fromLabels()
*
* @codeCoverageIgnore
*
* Returns a new instance from an iterable structure.
*
* @throws TypeError If a label is the null value
*/
#[Deprecated(message:'use League\Uri\Components\Domain::fromLabels() instead', since:'league/uri-components:7.0.0')]
public static function createFromLabels(iterable $labels): self
{
return self::fromLabels(...$labels);
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see Domain::fromUri()
*
* @codeCoverageIgnore
*
* Create a new instance from a URI object.
*/
#[Deprecated(message:'use League\Uri\Components\Domain::fromUri() instead', since:'league/uri-components:7.0.0')]
public static function createFromUri(Psr7UriInterface|UriInterface $uri): self
{
return self::fromUri($uri);
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see Domain::fromAuthority()
*
* @codeCoverageIgnore
*
* Create a new instance from an Authority object.
*/
#[Deprecated(message:'use League\Uri\Components\Domain::fromAuthority() instead', since:'league/uri-components:7.0.0')]
public static function createFromAuthority(AuthorityInterface|Stringable|string $authority): self
{
return self::fromAuthority($authority);
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see Domain::new()
*
* @codeCoverageIgnore
*
* Returns a new instance from an iterable structure.
*/
#[Deprecated(message:'use League\Uri\Components\Domain::new() instead', since:'league/uri-components:7.0.0')]
public static function createFromHost(HostInterface $host): self
{
return self::new($host);
}
}
|