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
|
<?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 League\Uri\Contracts\UriInterface;
use League\Uri\Exceptions\SyntaxError;
use League\Uri\Uri;
use Psr\Http\Message\UriInterface as Psr7UriInterface;
use Stringable;
use function array_key_exists;
use function in_array;
use function preg_match;
use function sprintf;
use function strtolower;
final class Scheme extends Component
{
/**
* Supported schemes and corresponding default port.
*
* @var array<string, int|null>
*/
private const SCHEME_DEFAULT_PORT = [
'data' => null,
'file' => null,
'ftp' => 21,
'gopher' => 70,
'http' => 80,
'https' => 443,
'ws' => 80,
'wss' => 443,
];
private const REGEXP_SCHEME = ',^[a-z]([-a-z0-9+.]+)?$,i';
private readonly ?string $scheme;
private function __construct(Stringable|string|null $scheme)
{
$this->scheme = $this->validate($scheme);
}
public function isWebsocket(): bool
{
return in_array($this->scheme, ['ws', 'wss'], true);
}
public function isHttp(): bool
{
return in_array($this->scheme, ['http', 'https'], true);
}
public function isSsl(): bool
{
return in_array($this->scheme, ['https', 'wss'], true);
}
public function isSpecial(): bool
{
return null !== $this->scheme
&& array_key_exists($this->scheme, self::SCHEME_DEFAULT_PORT);
}
public function defaultPort(): Port
{
return Port::new(self::SCHEME_DEFAULT_PORT[$this->scheme] ?? null);
}
/**
* Validate a scheme.
*
* @throws SyntaxError if the scheme is invalid
*/
private function validate(Stringable|string|null $scheme): ?string
{
$scheme = self::filterComponent($scheme);
if (null === $scheme) {
return null;
}
$fScheme = strtolower($scheme);
static $inMemoryCache = [];
if (isset($inMemoryCache[$fScheme])) {
return $fScheme;
}
if (1 !== preg_match(self::REGEXP_SCHEME, $fScheme)) {
throw new SyntaxError(sprintf("The scheme '%s' is invalid.", $scheme));
}
if (100 < count($inMemoryCache)) {
unset($inMemoryCache[array_key_first($inMemoryCache)]);
}
$inMemoryCache[$fScheme] = 1;
return $fScheme;
}
public static function new(Stringable|string|null $value = null): self
{
return new self($value);
}
/**
* Create a new instance from a URI object.
*/
public static function fromUri(Stringable|string $uri): self
{
$uri = self::filterUri($uri);
return match (true) {
$uri instanceof UriInterface => new self($uri->getScheme()),
default => new self(Uri::new($uri)->getScheme()),
};
}
public function value(): ?string
{
return $this->scheme;
}
public function getUriComponent(): string
{
return $this->value().(null === $this->scheme ? '' : ':');
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see Scheme::new()
*
* @codeCoverageIgnore
*/
#[Deprecated(message:'use League\Uri\Components\Scheme::new() instead', since:'league/uri-components:7.0.0')]
public static function createFromString(Stringable|string $scheme): self
{
return self::new($scheme);
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see Scheme::fromUri()
*
* @codeCoverageIgnore
*
* Create a new instance from a URI object.
*/
#[Deprecated(message:'use League\Uri\Components\Scheme::fromUri() instead', since:'league/uri-components:7.0.0')]
public static function createFromUri(Psr7UriInterface|UriInterface $uri): self
{
return self::fromUri($uri);
}
}
|