Skip to content

Commit

Permalink
Allow implicit binding to have optional backed enums
Browse files Browse the repository at this point in the history
  • Loading branch information
Neol3108 committed Apr 22, 2024
1 parent 22f4864 commit 5505370
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Routing/ImplicitRouteBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ protected static function resolveBackedEnumsForRoute($route, $parameters)

$parameterValue = $parameters[$parameterName];

if ($parameterValue === null) {
continue;
}

$backedEnumClass = $parameter->getType()?->getName();

$backedEnum = $backedEnumClass::tryFrom((string) $parameterValue);
Expand Down
18 changes: 18 additions & 0 deletions tests/Routing/ImplicitRouteBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ public function test_it_can_resolve_the_implicit_backed_enum_route_bindings_for_
$this->assertSame('fruits', $route->parameter('category')->value);
}

public function test_it_handles_optional_implicit_backed_enum_route_bindings_for_the_given_route_with_optional_parameter()
{
$action = ['uses' => function (?CategoryBackedEnum $category = null) {
return $category->value;
}];

$route = new Route('GET', '/test', $action);
$route->parameters = ['category' => null];

$route->prepareForSerialization();

$container = Container::getInstance();

ImplicitRouteBinding::resolveForRoute($container, $route);

$this->assertNull($route->parameter('category'));
}

public function test_it_does_not_resolve_implicit_non_backed_enum_route_bindings_for_the_given_route()
{
$action = ['uses' => function (CategoryEnum $category) {
Expand Down

0 comments on commit 5505370

Please sign in to comment.