Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion src/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Inertia;

use BackedEnum;
use Closure;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -240,10 +241,19 @@ protected function findComponentOrFail(string $component): void
/**
* Create an Inertia response.
*
* @param BackedEnum|string $component
* @param array<array-key, mixed>|\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|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);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Enums/IntBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Inertia\Tests\Enums;

enum IntBackedEnum: int
{
case Zero = 0;
}
8 changes: 8 additions & 0 deletions tests/Enums/StringBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Inertia\Tests\Enums;

enum StringBackedEnum: string
{
case UsersIndex = "UsersPage/Index";
}
16 changes: 16 additions & 0 deletions tests/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
use Inertia\MergeProp;
use Inertia\OptionalProp;
use Inertia\ResponseFactory;
use Inertia\Tests\Enums\IntBackedEnum;
use Inertia\Tests\Enums\StringBackedEnum;
use Inertia\Tests\Stubs\ExampleInertiaPropsProvider;
use Inertia\Tests\Stubs\ExampleMiddleware;
use InvalidArgumentException;

class ResponseFactoryTest extends TestCase
{
Expand Down Expand Up @@ -489,4 +492,17 @@ public function test_will_not_throw_exception_if_component_does_not_exist_when_e
$response = (new ResponseFactory)->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);
}
}