Skip to content

Commit f7e2fab

Browse files
authored
[11.x] Fixes handling Js::from(collect()); (#53206)
Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent 5c9cb78 commit f7e2fab

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
lines changed

src/Illuminate/Support/Js.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Contracts\Support\Jsonable;
88
use JsonSerializable;
99
use Stringable;
10+
use UnitEnum;
1011

1112
class Js implements Htmlable, Stringable
1213
{
@@ -70,7 +71,9 @@ protected function convertDataToJavaScriptExpression($data, $flags = 0, $depth =
7071
return $data->toHtml();
7172
}
7273

73-
$data = enum_value($data);
74+
if ($data instanceof UnitEnum) {
75+
$data = enum_value($data);
76+
}
7477

7578
$json = static::encode($data, $flags, $depth);
7679

src/Illuminate/Support/functions.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,12 @@ function defer(?callable $callback = null, ?string $name = null, bool $always =
4343
*/
4444
function enum_value($value, $default = null)
4545
{
46-
if (empty($value)) {
47-
return $value;
48-
}
49-
5046
return transform($value, fn ($value) => match (true) {
5147
$value instanceof \BackedEnum => $value->value,
5248
$value instanceof \UnitEnum => $value->name,
5349

5450
default => $value,
55-
}, $default);
51+
}, $default ?? $value);
5652
}
5753
}
5854

tests/Support/SupportEnumValueFunctionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ public static function scalarDataProvider()
4343
yield [true, true];
4444
yield [1337, 1337];
4545
yield [1.0, 1.0];
46+
yield [$collect = collect(), $collect];
4647
}
4748
}

tests/Support/SupportJsTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public function testScalars()
1919
$this->assertSame('1', (string) Js::from(1));
2020
$this->assertSame('1.1', (string) Js::from(1.1));
2121
$this->assertSame('[]', (string) Js::from([]));
22+
$this->assertSame('[]', (string) Js::from(collect()));
23+
$this->assertSame('null', (string) Js::from(null));
2224
$this->assertSame("'Hello world'", (string) Js::from('Hello world'));
2325
$this->assertEquals(
2426
"'\\u003Cdiv class=\\u0022foo\\u0022\\u003E\\u0027quoted html\\u0027\\u003C\\/div\\u003E'",

0 commit comments

Comments
 (0)