Skip to content

Commit c0855e4

Browse files
committed
Adding support for DateTimeInterface in StringCoercionMode::Ecmascript mode
1 parent 310c01b commit c0855e4

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

StringCoercionMode.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace League\Uri;
1515

1616
use BackedEnum;
17+
use DateTimeInterface;
1718
use League\Uri\Contracts\UriComponentInterface;
1819
use Stringable;
1920
use TypeError;
@@ -42,8 +43,7 @@ enum StringCoercionMode
4243
* PHP conversion mode.
4344
*
4445
* Guarantees that only scalar values, BackedEnum, and null are accepted.
45-
* Any object, UnitEnum, resource, or recursive structure
46-
* results in an error.
46+
* Any object, Non-backed enums, resource, or recursive structure results in an error.
4747
*
4848
* - null: is not converted and stays the `null` value
4949
* - string: used as-is
@@ -67,7 +67,8 @@ enum StringCoercionMode
6767
* - float: converted to decimal string (3.14 -> “3.14”), "NaN", "-Infinity" or "Infinity"
6868
* - Backed Enum: converted to their backing value and then stringify see int and string
6969
* - Array as list are flatten into a string list using the "," character as separator
70-
* - Associative array, Unit Enum, any object without stringification semantics is coerced to "[object Object]".
70+
* - Associative array, Non-backed enums, any object without stringification semantics is coerced to "[object Object]".
71+
* - DateTimeInterface implementing object are coerce to their string representation using DateTimeInterface::RFC2822 format
7172
*/
7273
case Ecmascript;
7374

@@ -79,11 +80,11 @@ public function isCoercible(mixed $value): bool
7980
? !is_resource($value)
8081
: match (true) {
8182
$value instanceof Rfc3986Uri,
82-
$value instanceof WhatWgUrl,
83-
$value instanceof BackedEnum,
84-
$value instanceof Stringable,
83+
$value instanceof WhatWgUrl,
84+
$value instanceof BackedEnum,
85+
$value instanceof Stringable,
8586
is_scalar($value),
86-
null === $value => true,
87+
null === $value => true,
8788
default => false,
8889
};
8990
}
@@ -98,6 +99,7 @@ public function coerce(mixed $value): ?string
9899
self::Ecmascript => match (true) {
99100
$value instanceof Rfc3986Uri => $value->toString(),
100101
$value instanceof WhatWgUrl => $value->toAsciiString(),
102+
$value instanceof DateTimeInterface => $value->format(DateTimeInterface::RFC2822),
101103
$value instanceof BackedEnum => (string) $value->value,
102104
$value instanceof Stringable => $value->__toString(),
103105
is_object($value) => '[object Object]',

0 commit comments

Comments
 (0)