Skip to content

Commit de59914

Browse files
committed
Improve and fix the Modifier mechanism
1 parent f37dcc3 commit de59914

File tree

6 files changed

+244
-212
lines changed

6 files changed

+244
-212
lines changed

components/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ All Notable changes to `League\Uri\Components` will be documented in this file
1515
- `Modifier::normalizeIp` returns the host normalized for IPv6 and IPv4 addresses
1616
- `Modifier::normalizeHost` returns the host as normalized by the WHATWG algorithm
1717
- `Modifier::with*` method from the underlying `Uri` object are proxy to improve DX.
18-
- `Modifier::uri` method which returns the underlying URI object used by the Modifier class.
18+
- `Modifier::wrap` static method which wraps the underlying URI object used by the Modifier class.
19+
- `Modifier::unwrap` method which returns the underlying URI object used by the Modifier class.
1920
- `Modifier::prefixQueryPairs` and `Modifier::prefixQueryParameters` to prefix Query using the pair key or the parameter names
2021
- `tryNew` named constructor added to all classes to return a new instance on success or `null` on failure.
2122
- `Query::decoded` the string representation of the component decoded.
@@ -39,6 +40,7 @@ All Notable changes to `League\Uri\Components` will be documented in this file
3940
- `Modifier::getIdnUriString` use `Modifier::toDisplayString` instead
4041
- `Modifier::getUriString` use `Modifier::toString` instead
4142
- `Modifier::getUri` use `Modifier::uri` instead
43+
- `Modifier::from` use `Modifier::wrap` instead
4244

4345
### Removed
4446

components/Components/Component.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function getUriComponent(): string
5858
final protected static function filterUri(WhatWgUrl|Rfc3986Uri|Stringable|string $uri): WhatWgUrl|Rfc3986Uri|UriInterface|Psr7UriInterface
5959
{
6060
return match (true) {
61-
$uri instanceof Modifier => $uri->uri(),
61+
$uri instanceof Modifier => $uri->unwrap(),
6262
$uri instanceof Psr7UriInterface, $uri instanceof UriInterface => $uri,
6363
$uri instanceof WhatWgUrl => Uri::new($uri->toAsciiString()),
6464
default => Uri::new($uri),

components/Components/FragmentDirectives.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public static function tryNew(Stringable|string|null $value): ?self
104104
public static function fromUri(WhatWgUrl|Rfc3986Uri|Stringable|string $uri): self
105105
{
106106
if ($uri instanceof Modifier) {
107-
$uri = $uri->uri();
107+
$uri = $uri->unwrap();
108108
}
109109

110110
if ($uri instanceof Rfc3986Uri) {

components/Modifier.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,19 @@ final public function __construct(protected readonly Rfc3986Uri|WhatWgUrl|Psr7Ur
7070
{
7171
}
7272

73-
/**
74-
* @param UriFactoryInterface|null $uriFactory deprecated, will be removed in the next major release
75-
*/
76-
public static function from(Rfc3986Uri|WhatWgUrl|Stringable|string $uri, ?UriFactoryInterface $uriFactory = null): static
73+
public static function wrap(Rfc3986Uri|WhatWgUrl|Stringable|string $uri): static
7774
{
7875
return new static(match (true) {
7976
$uri instanceof self => $uri->uri,
8077
$uri instanceof Psr7UriInterface,
8178
$uri instanceof UriInterface,
8279
$uri instanceof Rfc3986Uri,
8380
$uri instanceof WhatWgUrl => $uri,
84-
$uriFactory instanceof UriFactoryInterface => $uriFactory->createUri((string) $uri), // using UriFactoryInterface is deprecated
8581
default => Uri::new($uri),
8682
});
8783
}
8884

89-
public function uri(): Rfc3986Uri|WhatWgUrl|Psr7UriInterface|UriInterface
85+
public function unwrap(): Rfc3986Uri|WhatWgUrl|Psr7UriInterface|UriInterface
9086
{
9187
return $this->uri;
9288
}
@@ -1151,32 +1147,56 @@ public function getIdnUriString(): string
11511147

11521148
public function appendFragmentDirectives(FragmentDirectives|Directive|Stringable|string ...$directives): static
11531149
{
1154-
return $this->withFragment(FragmentDirectives::fromUri($this->uri())->append(...$directives));
1150+
return $this->withFragment(FragmentDirectives::fromUri($this->unwrap())->append(...$directives));
11551151
}
11561152

11571153
public function prependFragmentDirectives(FragmentDirectives|Directive|Stringable|string ...$directives): static
11581154
{
1159-
return $this->withFragment(FragmentDirectives::fromUri($this->uri())->prepend(...$directives));
1155+
return $this->withFragment(FragmentDirectives::fromUri($this->unwrap())->prepend(...$directives));
11601156
}
11611157

11621158
public function removeFragmentDirectives(int ...$offset): static
11631159
{
1164-
return $this->withFragment(FragmentDirectives::fromUri($this->uri())->remove(...$offset));
1160+
return $this->withFragment(FragmentDirectives::fromUri($this->unwrap())->remove(...$offset));
11651161
}
11661162

11671163
public function replaceFragmentDirective(int $offset, Directive|Stringable|string $directive): static
11681164
{
1169-
return $this->withFragment(FragmentDirectives::fromUri($this->uri())->replace($offset, $directive));
1165+
return $this->withFragment(FragmentDirectives::fromUri($this->unwrap())->replace($offset, $directive));
11701166
}
11711167

11721168
public function sliceFragmentDirectives(int $offset, ?int $length): static
11731169
{
1174-
return $this->withFragment(FragmentDirectives::fromUri($this->uri())->slice($offset, $length));
1170+
return $this->withFragment(FragmentDirectives::fromUri($this->unwrap())->slice($offset, $length));
11751171
}
11761172

11771173
public function filterFragmentDirectives(callable $callback): static
11781174
{
1179-
return $this->withFragment(FragmentDirectives::fromUri($this->uri())->filter($callback));
1175+
return $this->withFragment(FragmentDirectives::fromUri($this->unwrap())->filter($callback));
1176+
}
1177+
1178+
1179+
/**
1180+
* DEPRECATION WARNING! This method will be removed in the next major point release.
1181+
*
1182+
* @deprecated Since version 7.6.0
1183+
* @codeCoverageIgnore
1184+
* @see Modifier::wrap()
1185+
*
1186+
* @param UriFactoryInterface|null $uriFactory deprecated, will be removed in the next major release
1187+
*/
1188+
#[Deprecated(message:'use League\Uri\Modifier::wrap() instead', since:'league/uri-components:7.6.0')]
1189+
public static function from(Rfc3986Uri|WhatWgUrl|Stringable|string $uri, ?UriFactoryInterface $uriFactory = null): static
1190+
{
1191+
return new static(match (true) {
1192+
$uri instanceof self => $uri->uri,
1193+
$uri instanceof Psr7UriInterface,
1194+
$uri instanceof UriInterface,
1195+
$uri instanceof Rfc3986Uri,
1196+
$uri instanceof WhatWgUrl => $uri,
1197+
$uriFactory instanceof UriFactoryInterface => $uriFactory->createUri((string) $uri), // using UriFactoryInterface is deprecated
1198+
default => Uri::new($uri),
1199+
});
11801200
}
11811201

11821202
/**
@@ -1247,7 +1267,7 @@ public function removeQueryPairs(string ...$keys): static
12471267
*
12481268
* @deprecated Since version 7.6.0
12491269
* @codeCoverageIgnore
1250-
* @see Modifier::uri()
1270+
* @see Modifier::unwrap()
12511271
*
12521272
* Remove query data according to their key name.
12531273
*/

0 commit comments

Comments
 (0)