Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Fixes handling Js::from(collect()); #53206

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[11.x] Fixes handling Js::from(collect());
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Oct 17, 2024
commit 26fac8ac5ca3836379fc52009384958e70262a40
5 changes: 4 additions & 1 deletion src/Illuminate/Support/Js.php
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
use Illuminate\Contracts\Support\Jsonable;
use JsonSerializable;
use Stringable;
use UnitEnum;

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

$data = enum_value($data);
if ($data instanceof UnitEnum) {
$data = enum_value($data);
}

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

6 changes: 1 addition & 5 deletions src/Illuminate/Support/functions.php
Original file line number Diff line number Diff line change
@@ -43,16 +43,12 @@ function defer(?callable $callback = null, ?string $name = null, bool $always =
*/
function enum_value($value, $default = null)
{
if (empty($value)) {
return $value;
}

return transform($value, fn ($value) => match (true) {
$value instanceof \BackedEnum => $value->value,
$value instanceof \UnitEnum => $value->name,

default => $value,
}, $default);
}, $default ?? $value);
}
}

1 change: 1 addition & 0 deletions tests/Support/SupportEnumValueFunctionTest.php
Original file line number Diff line number Diff line change
@@ -43,5 +43,6 @@ public static function scalarDataProvider()
yield [true, true];
yield [1337, 1337];
yield [1.0, 1.0];
yield [$collect = collect(), $collect];
}
}
2 changes: 2 additions & 0 deletions tests/Support/SupportJsTest.php
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@ public function testScalars()
$this->assertSame('1', (string) Js::from(1));
$this->assertSame('1.1', (string) Js::from(1.1));
$this->assertSame('[]', (string) Js::from([]));
$this->assertSame('[]', (string) Js::from(collect()));
$this->assertSame('null', (string) Js::from(null));
$this->assertSame("'Hello world'", (string) Js::from('Hello world'));
$this->assertEquals(
"'\\u003Cdiv class=\\u0022foo\\u0022\\u003E\\u0027quoted html\\u0027\\u003C\\/div\\u003E'",