Skip to content

Commit 0fbc4e6

Browse files
committed
Simplify coercion mode support
1 parent 9e8877f commit 0fbc4e6

File tree

14 files changed

+62
-73
lines changed

14 files changed

+62
-73
lines changed

Components/Authority.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@
2323
use League\Uri\Contracts\UriInterface;
2424
use League\Uri\Contracts\UserInfoInterface;
2525
use League\Uri\Exceptions\SyntaxError;
26+
use League\Uri\StringCoercionMode;
2627
use League\Uri\UriString;
2728
use Psr\Http\Message\UriInterface as Psr7UriInterface;
2829
use SensitiveParameter;
2930
use Stringable;
3031
use Uri\Rfc3986\Uri as Rfc3986Uri;
3132
use Uri\WhatWg\Url as WhatWgUrl;
3233

33-
use function is_string;
34-
3534
final class Authority extends Component implements AuthorityInterface
3635
{
3736
private readonly HostInterface $host;
@@ -185,12 +184,12 @@ public function getUserInfo(): ?string
185184

186185
public function equals(mixed $value): bool
187186
{
188-
if (!$value instanceof BackedEnum && !$value instanceof Stringable && !is_string($value) && null !== $value) {
187+
if (!StringCoercionMode::Native->isCoercible($value)) {
189188
return false;
190189
}
191190

192191
if (!$value instanceof UriComponentInterface) {
193-
$value = self::tryNew($value);
192+
$value = self::tryNew(StringCoercionMode::Native->coerce($value));
194193
if (null === $value) {
195194
return false;
196195
}

Components/Component.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use League\Uri\Encoder;
2222
use League\Uri\Exceptions\SyntaxError;
2323
use League\Uri\Modifier;
24+
use League\Uri\StringCoercionMode;
2425
use League\Uri\Uri;
2526
use Psr\Http\Message\UriInterface as Psr7UriInterface;
2627
use Stringable;
@@ -89,18 +90,12 @@ protected function validateComponent(BackedEnum|Stringable|int|string|null $comp
8990
*/
9091
final protected static function filterComponent(BackedEnum|Stringable|int|string|null $component): ?string
9192
{
92-
if ($component instanceof UriComponentInterface) {
93-
$component = $component->value();
94-
}
95-
96-
if ($component instanceof BackedEnum) {
97-
$component = (string) $component->value;
98-
}
93+
$component = StringCoercionMode::Native->coerce($component);
9994

10095
return match (true) {
10196
null === $component => null,
102-
1 === preg_match(self::REGEXP_INVALID_URI_CHARS, (string) $component) => throw new SyntaxError(sprintf('Invalid component string: %s.', $component)),
103-
default => (string) $component,
97+
1 === preg_match(self::REGEXP_INVALID_URI_CHARS, $component) => throw new SyntaxError(sprintf('Invalid component string: %s.', $component)),
98+
default => $component,
10499
};
105100
}
106101

Components/DataPath.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use League\Uri\Contracts\UriInterface;
2222
use League\Uri\Exceptions\SyntaxError;
2323
use League\Uri\FeatureDetection;
24+
use League\Uri\StringCoercionMode;
2425
use Psr\Http\Message\UriInterface as Psr7UriInterface;
2526
use SplFileObject;
2627
use Stringable;
@@ -391,10 +392,7 @@ public function withTrailingSlash(): PathInterface
391392

392393
public function withParameters(BackedEnum|Stringable|string $parameters): DataPathInterface
393394
{
394-
if ($parameters instanceof BackedEnum) {
395-
$parameters = $parameters->value;
396-
}
397-
$parameters = (string) $parameters;
395+
$parameters = (string) StringCoercionMode::Native->coerce($parameters);
398396

399397
return match ($this->getParameters()) {
400398
$parameters => $this,

Components/Fragment.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
use League\Uri\Contracts\UriException;
2121
use League\Uri\Contracts\UriInterface;
2222
use League\Uri\Encoder;
23+
use League\Uri\StringCoercionMode;
2324
use League\Uri\UriString;
2425
use Psr\Http\Message\UriInterface as Psr7UriInterface;
2526
use Stringable;
2627
use Uri\Rfc3986\Uri as Rfc3986Uri;
2728
use Uri\WhatWg\Url as WhatWgUrl;
2829

29-
use function is_string;
3030
use function str_replace;
3131

3232
final class Fragment extends Component implements FragmentInterface
@@ -96,12 +96,12 @@ public function decoded(): ?string
9696

9797
public function equals(mixed $value): bool
9898
{
99-
if (!$value instanceof BackedEnum && !$value instanceof Stringable && !is_string($value) && null !== $value) {
99+
if (!StringCoercionMode::Native->isCoercible($value)) {
100100
return false;
101101
}
102102

103103
if (!$value instanceof UriComponentInterface) {
104-
$value = self::tryNew($value);
104+
$value = self::tryNew(StringCoercionMode::Native->coerce($value));
105105
if (null === $value) {
106106
return false;
107107
}

Components/FragmentDirectives.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use League\Uri\Encoder;
2727
use League\Uri\Exceptions\OffsetOutOfBounds;
2828
use League\Uri\Modifier;
29+
use League\Uri\StringCoercionMode;
2930
use League\Uri\Uri;
3031
use League\Uri\UriString;
3132
use Psr\Http\Message\UriInterface as Psr7UriInterface;
@@ -46,7 +47,6 @@
4647
use function implode;
4748
use function in_array;
4849
use function is_bool;
49-
use function is_string;
5050
use function sprintf;
5151
use function str_replace;
5252
use function strpos;
@@ -79,19 +79,11 @@ public function __construct(FragmentDirective|BackedEnum|Stringable|string ...$d
7979
*/
8080
public static function fromFragment(BackedEnum|Stringable|string|null $fragment): self
8181
{
82-
if ($fragment instanceof UriComponentInterface) {
83-
$fragment = $fragment->value();
84-
}
85-
86-
if ($fragment instanceof BackedEnum) {
87-
$fragment = $fragment->value;
88-
}
89-
82+
$fragment = StringCoercionMode::Native->coerce($fragment);
9083
if (null === $fragment) {
9184
return new self();
9285
}
9386

94-
$fragment = (string) $fragment;
9587
$pos = strpos($fragment, self::DELIMITER);
9688
if (false === $pos) {
9789
return new self();
@@ -252,12 +244,12 @@ public function isEmpty(): bool
252244

253245
public function equals(mixed $value): bool
254246
{
255-
if (!$value instanceof BackedEnum && !$value instanceof Stringable && !is_string($value) && null !== $value) {
247+
if (!StringCoercionMode::Native->isCoercible($value)) {
256248
return false;
257249
}
258250

259251
if (!$value instanceof UriComponentInterface) {
260-
$value = self::tryNew($value);
252+
$value = self::tryNew(StringCoercionMode::Native->coerce($value));
261253
if (null === $value) {
262254
return false;
263255
}

Components/FragmentDirectives/GenericDirective.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use League\Uri\Contracts\FragmentDirective;
1818
use League\Uri\Encoder;
1919
use League\Uri\Exceptions\SyntaxError;
20+
use League\Uri\StringCoercionMode;
2021
use Stringable;
2122
use Throwable;
2223

@@ -89,13 +90,13 @@ public function toFragmentValue(): string
8990

9091
public function equals(mixed $directive): bool
9192
{
92-
if (!$directive instanceof Stringable && !is_string($directive)) {
93+
if (null === $directive || ! StringCoercionMode::Native->isCoercible($directive)) {
9394
return false;
9495
}
9596

9697
if (!$directive instanceof FragmentDirective) {
9798
try {
98-
$directive = self::fromString($directive);
99+
$directive = self::fromString((string) StringCoercionMode::Native->coerce($directive));
99100
} catch (Throwable) {
100101
return false;
101102
}

Components/FragmentDirectives/TextDirective.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
use League\Uri\Contracts\FragmentDirective;
1818
use League\Uri\Encoder;
1919
use League\Uri\Exceptions\SyntaxError;
20+
use League\Uri\StringCoercionMode;
2021
use Stringable;
2122
use Throwable;
2223

2324
use function explode;
24-
use function is_string;
2525
use function preg_match;
2626
use function str_replace;
2727

@@ -169,13 +169,13 @@ public function toFragmentValue(): string
169169

170170
public function equals(mixed $directive): bool
171171
{
172-
if (!$directive instanceof Stringable && !$directive instanceof BackedEnum && !is_string($directive)) {
172+
if (null === $directive || ! StringCoercionMode::Native->isCoercible($directive)) {
173173
return false;
174174
}
175175

176176
if (!$directive instanceof FragmentDirective) {
177177
try {
178-
$directive = self::fromString($directive);
178+
$directive = self::fromString((string) StringCoercionMode::Native->coerce($directive));
179179
} catch (Throwable) {
180180
return false;
181181
}

Components/Host.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use League\Uri\Idna\Converter as IdnConverter;
2828
use League\Uri\IPv4\Converter as IPv4Converter;
2929
use League\Uri\IPv4Normalizer;
30+
use League\Uri\StringCoercionMode;
3031
use League\Uri\UriString;
3132
use Psr\Http\Message\UriInterface as Psr7UriInterface;
3233
use Stringable;
@@ -35,10 +36,10 @@
3536

3637
use function explode;
3738
use function filter_var;
38-
use function is_string;
3939
use function preg_replace_callback;
4040
use function rawurldecode;
4141
use function rawurlencode;
42+
use function rtrim;
4243
use function sprintf;
4344
use function strtolower;
4445
use function strtoupper;
@@ -143,12 +144,12 @@ public function value(): ?string
143144

144145
public function equals(mixed $value): bool
145146
{
146-
if (!$value instanceof BackedEnum && !$value instanceof Stringable && !is_string($value) && null !== $value) {
147+
if (!StringCoercionMode::Native->isCoercible($value)) {
147148
return false;
148149
}
149150

150151
if (!$value instanceof UriComponentInterface) {
151-
$value = self::tryNew($value);
152+
$value = self::tryNew(StringCoercionMode::Native->coerce($value));
152153
if (null === $value) {
153154
return false;
154155
}

Components/Path.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
use League\Uri\Contracts\UriComponentInterface;
2020
use League\Uri\Contracts\UriInterface;
2121
use League\Uri\Encoder;
22+
use League\Uri\StringCoercionMode;
2223
use League\Uri\UriString;
2324
use Psr\Http\Message\UriInterface as Psr7UriInterface;
2425
use Stringable;
2526
use Throwable;
2627
use Uri\Rfc3986\Uri as Rfc3986Uri;
2728
use Uri\WhatWg\Url as WhatWgUrl;
2829

29-
use function is_string;
3030
use function substr;
3131

3232
final class Path extends Component implements PathInterface
@@ -101,12 +101,12 @@ public function value(): string
101101

102102
public function equals(mixed $value): bool
103103
{
104-
if (!$value instanceof BackedEnum && !$value instanceof Stringable && !is_string($value)) {
104+
if (null === $value || !StringCoercionMode::Native->isCoercible($value)) {
105105
return false;
106106
}
107107

108108
if (!$value instanceof UriComponentInterface) {
109-
$value = self::tryNew($value);
109+
$value = self::tryNew((string) StringCoercionMode::Native->coerce($value));
110110
if (null === $value) {
111111
return false;
112112
}

Components/Port.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
use League\Uri\Contracts\UriException;
2222
use League\Uri\Contracts\UriInterface;
2323
use League\Uri\Exceptions\SyntaxError;
24+
use League\Uri\StringCoercionMode;
2425
use League\Uri\UriScheme;
2526
use Psr\Http\Message\UriInterface as Psr7UriInterface;
2627
use Stringable;
2728
use Uri\Rfc3986\Uri as Rfc3986Uri;
2829
use Uri\WhatWg\Url as WhatWgUrl;
2930

31+
use function array_map;
3032
use function filter_var;
31-
use function is_string;
3233

3334
use const FILTER_VALIDATE_INT;
3435

@@ -111,12 +112,12 @@ public function value(): ?string
111112

112113
public function equals(mixed $value): bool
113114
{
114-
if (!$value instanceof BackedEnum && !$value instanceof Stringable && !is_string($value) && null !== $value) {
115+
if (!StringCoercionMode::Native->isCoercible($value)) {
115116
return false;
116117
}
117118

118119
if (!$value instanceof UriComponentInterface) {
119-
$value = self::tryNew($value);
120+
$value = self::tryNew(StringCoercionMode::Native->coerce($value));
120121
if (null === $value) {
121122
return false;
122123
}

0 commit comments

Comments
 (0)