diff --git a/src/ResponseFactory.php b/src/ResponseFactory.php index d24feac9..5f6e2316 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; @@ -240,10 +241,19 @@ 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(string $component, $props = []): Response + public function render($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); + } }