Skip to content

Commit 3ced68a

Browse files
committed
Fix saucer typo
1 parent a986b61 commit 3ced68a

23 files changed

+1298
-120
lines changed

src/Component/Authority.php

Lines changed: 119 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,27 @@
44

55
namespace Boson\Component\Uri\Component;
66

7+
use Boson\Component\Uri\Exception\InvalidHostArgumentException;
8+
use Boson\Component\Uri\Exception\InvalidPasswordArgumentException;
9+
use Boson\Component\Uri\Exception\InvalidPortArgumentException;
10+
use Boson\Component\Uri\Exception\InvalidUserArgumentException;
711
use Boson\Contracts\Uri\Component\AuthorityInterface;
812
use Boson\Contracts\Uri\Component\UserInfoInterface;
913

10-
final class Authority implements AuthorityInterface
14+
/**
15+
* @phpstan-sealed MutableAuthority
16+
*
17+
* @phpstan-consistent-constructor
18+
*/
19+
class Authority implements AuthorityInterface
1120
{
1221
/**
1322
* Gets the user component of the URI.
1423
*
1524
* @var non-empty-string|null
1625
*/
1726
public ?string $user {
18-
get => $this->userInfo?->user;
27+
get => $this->userInfo?->username;
1928
}
2029

2130
/**
@@ -27,34 +36,127 @@ final class Authority implements AuthorityInterface
2736
get => $this->userInfo?->password;
2837
}
2938

39+
public protected(set) string $host;
40+
41+
public protected(set) ?int $port;
42+
43+
/**
44+
* Gets the userinfo URI component with a specific {@see UserInfo}
45+
* implementation.
46+
*
47+
* @var UserInfo|null
48+
*/
49+
public protected(set) ?UserInfoInterface $userInfo;
50+
51+
/**
52+
* @param \Stringable|non-empty-string $host
53+
* @param int<0, max>|null $port
54+
*
55+
* @throws InvalidHostArgumentException if an invalid authority host is provided
56+
* @throws InvalidUserArgumentException if an invalid user info's username is provided
57+
* @throws InvalidPasswordArgumentException if an invalid user info's password is provided
58+
*/
3059
public function __construct(
31-
/**
32-
* @var non-empty-string
33-
*/
34-
public readonly string $host,
35-
/**
36-
* @var int<0, 65535>|null
37-
*/
38-
public readonly ?int $port = null,
39-
public readonly ?UserInfoInterface $userInfo = null,
40-
) {}
41-
42-
public function equals(mixed $other): bool
60+
\Stringable|string $host,
61+
?int $port = null,
62+
?UserInfoInterface $info = null,
63+
) {
64+
$this->host = $this->formatHost($host);
65+
$this->port = $this->formatPort($port);
66+
$this->userInfo = $this->formatUserInfo($info);
67+
}
68+
69+
/**
70+
* Returns an authority instance from another one
71+
*
72+
* @api
73+
*
74+
* @throws InvalidHostArgumentException if an invalid authority host is provided
75+
* @throws InvalidUserArgumentException if an invalid user info's username is provided
76+
* @throws InvalidPasswordArgumentException if an invalid user info's password is provided
77+
*/
78+
final public static function from(AuthorityInterface $authority): static
79+
{
80+
return new static(
81+
host: $authority->host,
82+
port: $authority->port,
83+
info: $authority->userInfo,
84+
);
85+
}
86+
87+
/**
88+
* Returns an authority instance from another one
89+
*
90+
* @api
91+
*
92+
* @throws InvalidHostArgumentException if an invalid authority host is provided
93+
* @throws InvalidUserArgumentException if an invalid user info's username is provided
94+
* @throws InvalidPasswordArgumentException if an invalid user info's password is provided
95+
*/
96+
final public static function tryFrom(?AuthorityInterface $authority): ?static
97+
{
98+
if ($authority === null) {
99+
return null;
100+
}
101+
102+
return static::from($authority);
103+
}
104+
105+
/**
106+
* @return non-empty-string
107+
* @throws InvalidHostArgumentException if an invalid authority host is provided
108+
*/
109+
protected function formatHost(\Stringable|string $host): string
110+
{
111+
if ($host instanceof \Stringable) {
112+
try {
113+
$host = (string) $host;
114+
/** @phpstan-ignore-next-line : This is not a dead catch */
115+
} catch (\Throwable $e) {
116+
throw InvalidHostArgumentException::becauseStringableErrorOccurs($e);
117+
}
118+
}
119+
120+
if ($host === '') {
121+
throw InvalidHostArgumentException::becauseComponentIsEmpty();
122+
}
123+
124+
return $host;
125+
}
126+
127+
/**
128+
* @return int<0, max>|null
129+
*/
130+
protected function formatPort(?int $port): ?int
131+
{
132+
return $port;
133+
}
134+
135+
/**
136+
* @throws InvalidUserArgumentException if an invalid user info's username is provided
137+
* @throws InvalidPasswordArgumentException if an invalid user info's password is provided
138+
*/
139+
protected function formatUserInfo(?UserInfoInterface $info): ?UserInfo
140+
{
141+
return UserInfo::tryFrom($info);
142+
}
143+
144+
final public function equals(mixed $other): bool
43145
{
44146
return $other === $this
45-
|| ($other instanceof self
147+
|| ($other instanceof AuthorityInterface
46148
&& $this->host === $other->host
47149
&& $this->port === $other->port
48150
&& ($other->userInfo === $this->userInfo
49151
|| $other->userInfo?->equals($this->userInfo) === true));
50152
}
51153

52-
public function toString(): string
154+
final public function toString(): string
53155
{
54156
return (string) $this;
55157
}
56158

57-
public function __toString(): string
159+
final public function __toString(): string
58160
{
59161
$result = $this->host;
60162

src/Component/MutableAuthority.php

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Boson\Component\Uri\Component;
6+
7+
use Boson\Component\Uri\Exception\InvalidHostArgumentException;
8+
use Boson\Component\Uri\Exception\InvalidPasswordArgumentException;
9+
use Boson\Component\Uri\Exception\InvalidPortArgumentException;
10+
use Boson\Component\Uri\Exception\InvalidUserArgumentException;
11+
use Boson\Contracts\Uri\Component\AuthorityInterface;
12+
use Boson\Contracts\Uri\Component\MutableAuthorityInterface;
13+
use Boson\Contracts\Uri\Component\UserInfoInterface;
14+
15+
/**
16+
* @phpstan-consistent-constructor
17+
*/
18+
final class MutableAuthority extends Authority implements
19+
MutableAuthorityInterface
20+
{
21+
/**
22+
* Gets or updates the user of the {@see MutableUserInfo} URI component
23+
*
24+
* @var non-empty-string|null
25+
*/
26+
public ?string $user {
27+
get => $this->userInfo?->username;
28+
/**
29+
* Updates a user of the {@see MutableUserInfo} URI component
30+
*
31+
* @throws InvalidUserArgumentException if an invalid user info's username is provided
32+
* @throws InvalidPasswordArgumentException if an invalid user info's password is provided
33+
*/
34+
set(\Stringable|string|null $user) {
35+
if ($user === null) {
36+
$this->userInfo = null;
37+
38+
return;
39+
}
40+
41+
if ($this->userInfo === null) {
42+
$this->userInfo = new MutableUserInfo($user);
43+
44+
return;
45+
}
46+
47+
$this->userInfo->username = $user;
48+
}
49+
}
50+
51+
/**
52+
* Gets or updates the password of the {@see MutableUserInfo} URI component
53+
*
54+
* @var non-empty-string|null
55+
*/
56+
public ?string $password {
57+
get => $this->userInfo?->password;
58+
/**
59+
* Updates a password of the {@see MutableUserInfo} URI component
60+
*
61+
* @throws InvalidPasswordArgumentException in case of invalid password value passed
62+
*/
63+
set(#[\SensitiveParameter] \Stringable|string|null $password) {
64+
if ($this->userInfo === null) {
65+
if ($password === null) {
66+
return;
67+
}
68+
69+
throw InvalidPasswordArgumentException::becauseUserNotDefined();
70+
}
71+
72+
$this->userInfo->password = $password;
73+
}
74+
}
75+
76+
public string $host {
77+
get => $this->host;
78+
/**
79+
* @throws InvalidHostArgumentException if an invalid authority host is provided
80+
*/
81+
set(\Stringable|string $host) => $this->formatHost($host);
82+
}
83+
84+
public ?int $port = null {
85+
get => $this->port;
86+
/**
87+
* @throws InvalidPortArgumentException if an invalid authority port is provided
88+
*/
89+
set(?int $port) => $this->formatPort($port);
90+
}
91+
92+
/**
93+
* Gets a mutable userinfo URI component with a
94+
* specific {@see MutableUserInfo} implementation.
95+
*
96+
* @var MutableUserInfo|null
97+
*
98+
* @phpstan-ignore-next-line This is a valid docblock type
99+
*/
100+
public ?UserInfoInterface $userInfo = null {
101+
get => $this->userInfo;
102+
/**
103+
* @throws InvalidUserArgumentException if an invalid user info's username is provided
104+
* @throws InvalidPasswordArgumentException if an invalid user info's password is provided
105+
*/
106+
set(?UserInfoInterface $info) => $this->formatUserInfo($info);
107+
}
108+
109+
/**
110+
* Unlike the parent {@see parent::formatUserInfo()} method, it
111+
* returns a mutable implementation of user info
112+
*
113+
* @throws InvalidUserArgumentException if an invalid user info's username is provided
114+
* @throws InvalidPasswordArgumentException if an invalid user info's password is provided
115+
*/
116+
#[\Override]
117+
protected function formatUserInfo(?UserInfoInterface $info): ?MutableUserInfo
118+
{
119+
return MutableUserInfo::tryFrom($info);
120+
}
121+
122+
/**
123+
* Returns mutable authority instance from immutable one
124+
*
125+
* @api
126+
*
127+
* @throws InvalidHostArgumentException if an invalid authority host is provided
128+
* @throws InvalidPortArgumentException if an invalid authority port is provided
129+
* @throws InvalidUserArgumentException if an invalid user info's username is provided
130+
* @throws InvalidPasswordArgumentException if an invalid user info's password is provided
131+
*/
132+
public static function fromImmutable(AuthorityInterface $authority): self
133+
{
134+
if ($authority instanceof self) {
135+
return clone $authority;
136+
}
137+
138+
return new self(
139+
host: $authority->host,
140+
port: $authority->port,
141+
info: $authority->userInfo,
142+
);
143+
}
144+
145+
/**
146+
* Returns optional mutable authority instance from immutable one
147+
*
148+
* @api
149+
*
150+
* @throws InvalidHostArgumentException if an invalid authority host is provided
151+
* @throws InvalidPortArgumentException if an invalid authority port is provided
152+
* @throws InvalidUserArgumentException if an invalid user info's username is provided
153+
* @throws InvalidPasswordArgumentException if an invalid user info's password is provided
154+
*/
155+
public static function tryFromImmutable(?AuthorityInterface $authority): ?self
156+
{
157+
if ($authority === null) {
158+
return null;
159+
}
160+
161+
return self::fromImmutable($authority);
162+
}
163+
}

0 commit comments

Comments
 (0)