Skip to content

Commit cf9425e

Browse files
committed
Improve Component:fromUri implementation
1 parent e420b9f commit cf9425e

File tree

11 files changed

+73
-87
lines changed

11 files changed

+73
-87
lines changed

components/Components/Authority.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,30 @@ public static function tryNew(Stringable|string|null $uri = null): ?self
8686
public static function fromUri(WhatwgUrl|Rfc3986Uri|Stringable|string $uri): self
8787
{
8888
$uri = self::filterUri($uri);
89+
if ($uri instanceof Rfc3986Uri) {
90+
return new self($uri->getHost(), $uri->getPort(), $uri->getUserInfo());
91+
}
8992

90-
return match (true) {
91-
$uri instanceof UriInterface => self::new($uri->getAuthority()),
92-
default => self::new(Uri::new($uri)->getAuthority()),
93-
};
93+
if ($uri instanceof WhatWgUrl) {
94+
$userInfo = $uri->getUsername();
95+
if (null !== ($password = $uri->getPassword())) {
96+
$userInfo .= ':'.$password;
97+
}
98+
99+
return new self($uri->getUnicodeHost(), $uri->getPort(), $userInfo);
100+
}
101+
102+
if ($uri instanceof Psr7UriInterface) {
103+
$components = UriString::parse($uri);
104+
$userInfo = $components['user'];
105+
if (null !== ($password = $components['pass'])) {
106+
$userInfo .= ':'.$password;
107+
}
108+
109+
return new self($components['host'], $components['port'], $userInfo);
110+
}
111+
112+
return self::new($uri->getAuthority());
94113
}
95114

96115
/**

components/Components/Component.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,19 @@ public function getUriComponent(): string
5757

5858
final protected static function filterUri(WhatWgUrl|Rfc3986Uri|Stringable|string $uri): WhatWgUrl|Rfc3986Uri|UriInterface|Psr7UriInterface
5959
{
60-
return match (true) {
61-
$uri instanceof Modifier => $uri->unwrap(),
62-
$uri instanceof Psr7UriInterface, $uri instanceof UriInterface => $uri,
63-
$uri instanceof WhatWgUrl => Uri::new($uri->toAsciiString()),
64-
default => Uri::new($uri),
65-
};
60+
if ($uri instanceof Modifier) {
61+
return $uri->unwrap();
62+
}
63+
64+
if ($uri instanceof Rfc3986Uri
65+
|| $uri instanceof WhatWgUrl
66+
|| $uri instanceof PSR7UriInterface
67+
|| $uri instanceof UriInterface
68+
) {
69+
return $uri;
70+
}
71+
72+
return Uri::new($uri);
6673
}
6774

6875
/**

components/Components/Fragment.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use League\Uri\Contracts\UriInterface;
2121
use League\Uri\Encoder;
2222
use League\Uri\Uri;
23+
use League\Uri\UriString;
2324
use Psr\Http\Message\UriInterface as Psr7UriInterface;
2425
use Stringable;
2526
use Uri\Rfc3986\Uri as Rfc3986Uri;
@@ -62,19 +63,12 @@ public static function tryNew(Stringable|string|null $uri = null): ?self
6263
*/
6364
public static function fromUri(WhatWgUrl|Rfc3986Uri|Stringable|string $uri): self
6465
{
65-
if ($uri instanceof Rfc3986Uri) {
66-
return new self($uri->getRawFragment());
67-
}
68-
69-
if ($uri instanceof WhatWgUrl) {
70-
return new self($uri->getFragment());
71-
}
72-
7366
$uri = self::filterUri($uri);
7467

7568
return match (true) {
76-
$uri instanceof UriInterface => new self($uri->getFragment()),
77-
default => new self(Uri::new($uri)->getFragment()),
69+
$uri instanceof Rfc3986Uri => new self($uri->getRawFragment()),
70+
$uri instanceof Psr7UriInterface => new self(UriString::parse($uri)['fragment']),
71+
default => new self($uri->getFragment()),
7872
};
7973
}
8074

components/Components/FragmentDirectives.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use League\Uri\Exceptions\SyntaxError;
2626
use League\Uri\Modifier;
2727
use League\Uri\Uri;
28+
use League\Uri\UriString;
2829
use Psr\Http\Message\UriInterface as Psr7UriInterface;
2930
use Stringable;
3031
use Throwable;
@@ -107,21 +108,12 @@ public static function fromUri(WhatWgUrl|Rfc3986Uri|Stringable|string $uri): sel
107108
$uri = $uri->unwrap();
108109
}
109110

110-
if ($uri instanceof Rfc3986Uri) {
111-
return self::new($uri->getRawFragment());
112-
}
113-
114-
if ($uri instanceof Psr7UriInterface) {
115-
$fragment = $uri->getFragment();
116-
117-
return self::new('' === $fragment ? null : $fragment);
118-
}
119-
120-
if ($uri instanceof UriInterface || $uri instanceof WhatWgUrl) {
121-
return self::new($uri->getFragment());
122-
}
123-
124-
return self::new(Uri::new($uri)->getFragment());
111+
return match (true) {
112+
$uri instanceof Psr7UriInterface => self::new(UriString::parse($uri)['fragment']),
113+
$uri instanceof Rfc3986Uri => self::new($uri->getRawFragment()),
114+
$uri instanceof UriInterface, $uri instanceof WhatWgUrl => self::new($uri->getFragment()),
115+
default => self::new(Uri::new($uri)->getFragment()),
116+
};
125117
}
126118

127119
public function count(): int

components/Components/Host.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use League\Uri\IPv4\Converter as IPv4Converter;
2727
use League\Uri\IPv4Normalizer;
2828
use League\Uri\Uri;
29+
use League\Uri\UriString;
2930
use Psr\Http\Message\UriInterface as Psr7UriInterface;
3031
use Stringable;
3132
use Uri\Rfc3986\Uri as Rfc3986Uri;
@@ -315,19 +316,13 @@ public static function fromIp(Stringable|string $ip, string $version = ''): self
315316
*/
316317
public static function fromUri(WhatWgUrl|Rfc3986Uri|Stringable|string $uri): self
317318
{
318-
if ($uri instanceof Rfc3986Uri) {
319-
return new self($uri->getRawHost());
320-
}
321-
322-
if ($uri instanceof WhatWgUrl) {
323-
return new self($uri->getAsciiHost());
324-
}
325-
326319
$uri = self::filterUri($uri);
327320

328321
return match (true) {
329-
$uri instanceof UriInterface => new self($uri->getHost()),
330-
default => new self(Uri::new($uri)->getHost()),
322+
$uri instanceof Rfc3986Uri => new self($uri->getRawHost()),
323+
$uri instanceof WhatWgUrl => new self($uri->getAsciiHost()),
324+
$uri instanceof Psr7UriInterface => new self(UriString::parse($uri)['host']),
325+
default => new self($uri->getHost()),
331326
};
332327
}
333328

components/Components/Path.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public static function tryNew(Stringable|string $uri = ''): ?self
7676
*/
7777
public static function fromUri(WhatwgUrl|Rfc3986Uri|Stringable|string $uri): self
7878
{
79+
$uri = self::filterUri($uri);
7980
if ($uri instanceof Rfc3986Uri) {
8081
return self::new($uri->getRawPath());
8182
}
@@ -84,9 +85,6 @@ public static function fromUri(WhatwgUrl|Rfc3986Uri|Stringable|string $uri): sel
8485
return self::new($uri->getPath());
8586
}
8687

87-
if (!$uri instanceof UriInterface) {
88-
$uri = Uri::new($uri);
89-
}
9088
$path = $uri->getPath();
9189
$authority = $uri->getAuthority();
9290

components/Components/Port.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ public static function tryNew(Stringable|string|null $uri = null): ?self
6464
*/
6565
public static function fromUri(WhatWgUrl|Rfc3986Uri|Stringable|string $uri): self
6666
{
67-
if ($uri instanceof Rfc3986Uri || $uri instanceof WhatWgUrl || $uri instanceof UriInterface || $uri instanceof Psr7UriInterface) {
68-
return new self($uri->getPort());
69-
}
70-
7167
return new self(self::filterUri($uri)->getPort());
7268
}
7369

components/Components/Query.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use League\Uri\KeyValuePair\Converter;
2525
use League\Uri\QueryString;
2626
use League\Uri\Uri;
27+
use League\Uri\UriString;
2728
use Psr\Http\Message\UriInterface as Psr7UriInterface;
2829
use Stringable;
2930
use Traversable;
@@ -131,23 +132,12 @@ public static function fromPairs(iterable $pairs, string $separator = '&', strin
131132
*/
132133
public static function fromUri(WhatWgUrl|Rfc3986Uri|Stringable|string $uri): self
133134
{
134-
if ($uri instanceof Rfc3986Uri) {
135-
return new self($uri->getRawQuery());
136-
}
137-
138-
if ($uri instanceof WhatWgUrl) {
139-
return new self($uri->getQuery());
140-
}
141-
142-
if ($uri instanceof UriInterface) {
143-
return new self($uri->getQuery());
144-
}
145-
146135
$uri = self::filterUri($uri);
147136

148137
return match (true) {
149-
$uri instanceof UriInterface => new self($uri->getQuery()),
150-
default => new self(Uri::new($uri)->getQuery()),
138+
$uri instanceof Rfc3986Uri => new self($uri->getRawQuery()),
139+
$uri instanceof Psr7UriInterface => new self(UriString::parse($uri)['query']),
140+
default => new self($uri->getQuery()),
151141
};
152142
}
153143

components/Components/Scheme.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use League\Uri\Contracts\UriComponentInterface;
1818
use League\Uri\Contracts\UriInterface;
1919
use League\Uri\Exceptions\SyntaxError;
20-
use League\Uri\Uri;
20+
use League\Uri\UriString;
2121
use Psr\Http\Message\UriInterface as Psr7UriInterface;
2222
use Stringable;
2323
use Throwable;
@@ -77,6 +77,7 @@ final class Scheme extends Component
7777
'steam' => null,
7878
'svn' => 3690,
7979
'telnet' => 23,
80+
'tn3270' => 23,
8081
'ventrilo' => 3784,
8182
'vnc' => 5900,
8283
'wais' => 210,
@@ -183,16 +184,13 @@ public static function tryNew(Stringable|string|null $uri = null): ?self
183184
*/
184185
public static function fromUri(WhatWgUrl|Rfc3986Uri|Stringable|string $uri): self
185186
{
186-
if ($uri instanceof Rfc3986Uri || $uri instanceof WhatWgUrl) {
187-
return new self($uri->getScheme());
188-
}
189-
190187
$uri = self::filterUri($uri);
191188

192-
return match (true) {
193-
$uri instanceof UriInterface => new self($uri->getScheme()),
194-
default => new self(Uri::new($uri)->getScheme()),
195-
};
189+
return new self(
190+
$uri instanceof Psr7UriInterface
191+
? UriString::parse($uri)['scheme']
192+
: $uri->getScheme()
193+
);
196194
}
197195

198196
public function value(): ?string

components/Components/URLSearchParams.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use League\Uri\KeyValuePair\Converter;
2828
use League\Uri\QueryString;
2929
use League\Uri\Uri;
30+
use League\Uri\UriString;
31+
use Psr\Http\Message\UriInterface as Psr7UriInterface;
3032
use Stringable;
3133
use Uri\Rfc3986\Uri as Rfc3986Uri;
3234
use Uri\WhatWg\Url as WhatWgUrl;
@@ -217,13 +219,10 @@ public static function fromAssociative(object|array $associative): self
217219
*/
218220
public static function fromUri(WhatWgUrl|Rfc3986Uri|Stringable|string $uri): self
219221
{
220-
if ($uri instanceof WhatWgUrl) {
221-
return new self(Query::fromPairs(QueryString::parseFromValue($uri->getQuery(), Converter::fromFormData())));
222-
}
223-
224222
$query = match (true) {
225223
$uri instanceof Rfc3986Uri => $uri->getRawQuery(),
226-
$uri instanceof UriInterface => $uri->getQuery(),
224+
$uri instanceof WhatWgUrl, $uri instanceof UriInterface => $uri->getQuery(),
225+
$uri instanceof Psr7UriInterface => UriString::parse($uri)['query'],
227226
default => Uri::new($uri)->getQuery(),
228227
};
229228

0 commit comments

Comments
 (0)