From a4c71776f4bd908c23ac3fb3227f7f102c56245a Mon Sep 17 00:00:00 2001 From: Marc Beinder Date: Tue, 12 Aug 2025 19:01:37 -0500 Subject: [PATCH 1/2] Add Backed Enum Support to ResponseFactory::render() --- src/ResponseFactory.php | 11 ++++++++++- tests/Enums/IntBackedEnum.php | 8 ++++++++ tests/Enums/StringBackedEnum.php | 8 ++++++++ tests/ResponseFactoryTest.php | 16 ++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/Enums/IntBackedEnum.php create mode 100644 tests/Enums/StringBackedEnum.php diff --git a/src/ResponseFactory.php b/src/ResponseFactory.php index d24feac9..695f4210 100644 --- a/src/ResponseFactory.php +++ b/src/ResponseFactory.php @@ -2,6 +2,7 @@ namespace Inertia; +use BackedEnum; use Closure; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Arr; @@ -242,8 +243,16 @@ protected function findComponentOrFail(string $component): void * * @param array|\Illuminate\Contracts\Support\Arrayable|ProvidesInertiaProperties $props */ - public function render(string $component, $props = []): Response + public function render(BackedEnum|string $component, $props = []): Response { + if($component instanceof BackedEnum) { + $component = $component->value; + + if(!is_string($component)) { + throw new InvalidArgumentException('Component argument must be of type string or a string BackedEnum'); + } + } + if (config('inertia.ensure_pages_exist', false)) { $this->findComponentOrFail($component); } diff --git a/tests/Enums/IntBackedEnum.php b/tests/Enums/IntBackedEnum.php new file mode 100644 index 00000000..19c56a86 --- /dev/null +++ b/tests/Enums/IntBackedEnum.php @@ -0,0 +1,8 @@ +render('foo'); $this->assertInstanceOf(\Inertia\Response::class, $response); } + + public function test_render_accepts_backed_enum(): void + { + $response = (new ResponseFactory)->render(StringBackedEnum::UsersIndex); + $this->assertInstanceOf(\Inertia\Response::class, $response); + } + + public function test_render_throws_for_non_string_backed_enum(): void + { + $factory = new ResponseFactory; + $this->expectException(InvalidArgumentException::class); + $factory->render(IntBackedEnum::Zero); + } } From 6f5ec57f95ea7d693bd0a0fd73907316720f18a0 Mon Sep 17 00:00:00 2001 From: Marc Beinder Date: Thu, 21 Aug 2025 08:03:04 -0500 Subject: [PATCH 2/2] Remove Type Hint in Favor of DocBlock --- src/ResponseFactory.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ResponseFactory.php b/src/ResponseFactory.php index 695f4210..5f6e2316 100644 --- a/src/ResponseFactory.php +++ b/src/ResponseFactory.php @@ -241,9 +241,10 @@ protected function findComponentOrFail(string $component): void /** * Create an Inertia response. * + * @param BackedEnum|string $component * @param array|\Illuminate\Contracts\Support\Arrayable|ProvidesInertiaProperties $props */ - public function render(BackedEnum|string $component, $props = []): Response + public function render($component, $props = []): Response { if($component instanceof BackedEnum) { $component = $component->value;