1414namespace League \Uri ;
1515
1616use BackedEnum ;
17+ use DateTimeInterface ;
18+ use IntlDateFormatter ;
19+ use League \Uri \Contracts \FragmentDirective ;
1720use League \Uri \Contracts \UriComponentInterface ;
21+ use League \Uri \Contracts \UriInterface ;
1822use Stringable ;
1923use TypeError ;
24+ use Uri \Rfc3986 \Uri as Rfc3986Uri ;
25+ use Uri \WhatWg \Url as WhatWgUrl ;
2026
2127use function array_is_list ;
2228use function array_map ;
29+ use function extension_loaded ;
2330use function get_debug_type ;
2431use function implode ;
2532use 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 };
0 commit comments