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
|
<?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\AuthorityInterface;
use League\Uri\Contracts\UriComponentInterface;
use League\Uri\Contracts\UriInterface;
use League\Uri\Contracts\UserInfoInterface;
use League\Uri\Encoder;
use League\Uri\Exceptions\SyntaxError;
use League\Uri\Uri;
use Psr\Http\Message\UriInterface as Psr7UriInterface;
use SensitiveParameter;
use Stringable;
use function explode;
final class UserInfo extends Component implements UserInfoInterface
{
private readonly ?string $username;
private readonly ?string $password;
/**
* New instance.
*/
public function __construct(
Stringable|string|null $username,
#[SensitiveParameter]
Stringable|string|null $password = null
) {
$this->username = $this->validateComponent($username);
$password = $this->validateComponent($password);
if (null === $this->username && null !== $password) {
throw new SyntaxError('It is not possible to associated a password to an undefined user.');
}
$this->password = $password;
}
/**
* 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->getUsername(), $uri->getPassword()),
default => self::new(Uri::new($uri)->getUserInfo()),
};
}
/**
* Create a new instance from an Authority object.
*/
public static function fromAuthority(Stringable|string|null $authority): self
{
return match (true) {
$authority instanceof AuthorityInterface => self::new($authority->getUserInfo()),
default => self::new(Authority::new($authority)->getUserInfo()),
};
}
/**
* Create a new instance from a hash of parse_url parts.
*
* Create a new instance from a hash representation of the URI similar
* to PHP parse_url function result
*
* @param array{user? : ?string, pass? : ?string} $components
*/
public static function fromComponents(array $components): self
{
$components += ['user' => null, 'pass' => null];
return match (null) {
$components['user'] => new self(null),
default => new self($components['user'], $components['pass']),
};
}
/**
* Creates a new instance from an encoded string.
*/
public static function new(Stringable|string|null $value = null): self
{
if ($value instanceof UriComponentInterface) {
$value = $value->value();
}
if (null === $value) {
return new self(null);
}
$value = (string) $value;
[$user, $pass] = explode(':', $value, 2) + [1 => null];
return new self(Encoder::decodeAll($user), Encoder::decodeAll($pass));
}
public function value(): ?string
{
return match (true) {
null === $this->password => $this->getUsername(),
default => $this->getUsername().':'.$this->getPassword(),
};
}
public function getUriComponent(): string
{
return $this->value().(null === $this->username ? '' : '@');
}
public function getUser(): ?string
{
return $this->username;
}
public function getPass(): ?string
{
return $this->password;
}
public function getUsername(): ?string
{
return Encoder::encodeUser($this->username);
}
public function getPassword(): ?string
{
return Encoder::encodePassword($this->password);
}
/**
* @return array{user: ?string, pass: ?string}
*/
public function components(): array
{
return [
'user' => $this->username,
'pass' => $this->password,
];
}
public function withUser(Stringable|string|null $username): self
{
$username = $this->validateComponent($username);
return match ($this->username) {
$username => $this,
default => new self($username, $this->password),
};
}
public function withPass(#[SensitiveParameter] Stringable|string|null $password): self
{
$password = $this->validateComponent($password);
return match (true) {
$password === $this->password => $this,
null === $this->username => throw new SyntaxError('It is not possible to associated a password to an undefined user.'),
default => new self($this->username, $password),
};
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see UserInfo::fromUri()
*
* @codeCoverageIgnore
*
* Create a new instance from a URI object.
*/
#[Deprecated(message:'use League\Uri\Components\UserInfo::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 UserInfo::fromAuthority()
*
* @codeCoverageIgnore
*
* Create a new instance from an Authority object.
*/
#[Deprecated(message:'use League\Uri\Components\UserInfo::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 UserInfo::new()
*
* @codeCoverageIgnore
*
* Creates a new instance from an encoded string.
*/
#[Deprecated(message:'use League\Uri\Components\UserInfo::new() instead', since:'league/uri-components:7.0.0')]
public static function createFromString(Stringable|string $userInfo): self
{
return self::new($userInfo);
}
}
|