Skip to content

Commit 321dfff

Browse files
committed
Improve coercion
1 parent f4f0e71 commit 321dfff

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

components/Modifier.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
use Psr\Http\Message\UriInterface as Psr7UriInterface;
4646
use SensitiveParameter;
4747
use Stringable;
48-
use Traversable;
4948
use Uri\Rfc3986\Uri as Rfc3986Uri;
5049
use Uri\WhatWg\Url as WhatWgUrl;
5150
use ValueError;

interfaces/FeatureDetection.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public static function supportsIdn(): void
4444
$isSupported = $isSupported ?? (function_exists('\idn_to_ascii') && defined('\INTL_IDNA_VARIANT_UTS46'));
4545

4646
$isSupported || throw new MissingFeature('Support for IDN host requires the `intl` extension for best performance or run "composer require symfony/polyfill-intl-idn" to install a polyfill.');
47-
4847
}
4948

5049
public static function supportsIPv4Conversion(): void

interfaces/StringCoercionMode.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@
1414
namespace League\Uri;
1515

1616
use BackedEnum;
17+
use DateTimeInterface;
18+
use IntlDateFormatter;
19+
use League\Uri\Contracts\FragmentDirective;
1720
use League\Uri\Contracts\UriComponentInterface;
21+
use League\Uri\Contracts\UriInterface;
1822
use Stringable;
1923
use TypeError;
24+
use Uri\Rfc3986\Uri as Rfc3986Uri;
25+
use Uri\WhatWg\Url as WhatWgUrl;
2026

2127
use function array_is_list;
2228
use function array_map;
29+
use function extension_loaded;
2330
use function get_debug_type;
2431
use function implode;
2532
use function is_array;
@@ -65,6 +72,7 @@ enum StringCoercionMode
6572
* - Backed Enum: converted to their backing value and then stringify see int and string
6673
* - Array as list are flatten into a string list using the "," character as separator
6774
* - Associative array, Unit Enum, any object without stringification semantics is coerced to "[object Object]".
75+
* - DateTimeInterface object are stringify following EcmaScript `Date.prototype.toString()` semantics
6876
*/
6977
case Ecmascript;
7078

@@ -75,6 +83,8 @@ public function isCoercible(mixed $value): bool
7583
return self::Ecmascript === $this
7684
? !is_resource($value)
7785
: match (true) {
86+
$value instanceof Rfc3986Uri,
87+
$value instanceof WhatWgUrl,
7888
$value instanceof BackedEnum,
7989
$value instanceof Stringable,
8090
is_scalar($value),
@@ -86,8 +96,12 @@ public function isCoercible(mixed $value): bool
8696
public function coerce(mixed $value): ?string
8797
{
8898
$value = match (true) {
99+
$value instanceof UriComponentInterface,
100+
$value instanceof FragmentDirective => $value->value(),
101+
$value instanceof UriInterface,
102+
$value instanceof WhatWgUrl => $value->toAsciiString(),
103+
$value instanceof Rfc3986Uri => $value->toString(),
89104
$value instanceof BackedEnum => $value->value,
90-
$value instanceof UriComponentInterface => $value->value(),
91105
$value instanceof Stringable => (string) $value,
92106
default => $value,
93107
};

interfaces/StringCoercionModeTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace League\Uri;
1515

16+
use League\Uri\Components\FragmentDirectives\TextDirective;
1617
use PHPUnit\Framework\Attributes\DataProvider;
1718
use PHPUnit\Framework\TestCase;
1819
use stdClass;
@@ -50,6 +51,9 @@ public function __toString(): string
5051
return 'ok';
5152
}
5253
}, 'ok'],
54+
55+
[new \Uri\Rfc3986\Uri('https://example.com'), 'https://example.com'],
56+
[new TextDirective('start', 'end'), 'start,end'],
5357
];
5458
}
5559

@@ -94,6 +98,9 @@ public function __toString(): string
9498
}, 'ok'],
9599
[fn () => 42, '[object Object]'],
96100
[TestUnitEnum::One, '[object Object]'],
101+
102+
[new \Uri\WhatWg\Url('https://0:0@0:0/0?0#0'), 'https://0:0@0.0.0.0:0/0?0#0'],
103+
[new TextDirective('start', 'end'), 'start,end'],
97104
];
98105
}
99106

0 commit comments

Comments
 (0)