Skip to content

Commit 4d1c79f

Browse files
committed
wip
1 parent a28c943 commit 4d1c79f

File tree

7 files changed

+44
-3
lines changed

7 files changed

+44
-3
lines changed

packages/database/src/IsDatabaseModel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ public static function new(mixed ...$params): self
5858
/**
5959
* Finds a model instance by its ID.
6060
*/
61-
public static function findById(string|int|PrimaryKey $id): static
61+
public static function findById(string|int|PrimaryKey $id): self
6262
{
6363
return self::get($id);
6464
}
6565

6666
/**
6767
* Finds a model instance by its ID.
6868
*/
69-
public static function resolve(string|int|PrimaryKey $id): static
69+
public static function resolve(string|int|PrimaryKey $id): self
7070
{
7171
return query(self::class)->resolve($id);
7272
}

packages/router/src/Bindable.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@
44

55
interface Bindable
66
{
7+
public int|string $bindingValue {
8+
get;
9+
}
10+
711
public static function resolve(string $input): self;
812
}

packages/router/src/GenericRouter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Psr\Http\Message\ServerRequestInterface as PsrRequest;
99
use Tempest\Container\Container;
1010
use Tempest\Core\AppConfig;
11+
use Tempest\Database\PrimaryKey;
1112
use Tempest\Http\Mappers\PsrRequestToGenericRequestMapper;
1213
use Tempest\Http\Request;
1314
use Tempest\Http\Response;
@@ -126,6 +127,8 @@ public function toUri(array|string $action, ...$params): string
126127

127128
if ($value instanceof BackedEnum) {
128129
$value = $value->value;
130+
} elseif ($value instanceof Bindable) {
131+
$value = $value->bindingValue;
129132
}
130133

131134
$uri = $uri->replaceRegex(

packages/router/src/Routing/Construction/DiscoveredRoute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function fromRoute(Route $route, MethodReflector $methodReflector)
2424
self::getRouteParams($route->uri),
2525
$route->middleware,
2626
$methodReflector,
27-
$route->without,
27+
$route->without ?? [],
2828
);
2929
}
3030

tests/Fixtures/Modules/Books/Models/Author.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ public function __construct(
2424
public array $books = [],
2525
public ?Publisher $publisher = null,
2626
) {}
27+
28+
public int|string $bindingValue {
29+
get => $this->id->value;
30+
}
2731
}

tests/Fixtures/Modules/Books/Models/Book.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,8 @@ final class Book implements Bindable
2828

2929
#[HasOne]
3030
public ?Isbn $isbn = null;
31+
32+
public int|string $bindingValue {
33+
get => $this->id->value;
34+
}
3135
}

tests/Integration/Route/RouterTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use ReflectionException;
1212
use Tempest\Core\AppConfig;
1313
use Tempest\Database\Migrations\CreateMigrationsTable;
14+
use Tempest\Database\PrimaryKey;
1415
use Tempest\Http\HttpRequestFailed;
1516
use Tempest\Http\Responses\Ok;
1617
use Tempest\Http\Status;
@@ -26,6 +27,7 @@
2627
use Tests\Tempest\Fixtures\Migrations\CreateAuthorTable;
2728
use Tests\Tempest\Fixtures\Migrations\CreateBookTable;
2829
use Tests\Tempest\Fixtures\Migrations\CreatePublishersTable;
30+
use Tests\Tempest\Fixtures\Modules\Books\BookController;
2931
use Tests\Tempest\Fixtures\Modules\Books\Models\Author;
3032
use Tests\Tempest\Fixtures\Modules\Books\Models\Book;
3133
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
@@ -214,6 +216,30 @@ public function test_generate_uri_with_enum(): void
214216
);
215217
}
216218

219+
public function test_generate_uri_with_bindable_model(): void
220+
{
221+
$book = Book::new(
222+
id: new PrimaryKey('abc'),
223+
);
224+
225+
$this->assertSame(
226+
'/books/abc',
227+
uri([BookController::class, 'show'], book: $book),
228+
);
229+
}
230+
231+
public function test_generate_uri_with_primary_key(): void
232+
{
233+
$book = Book::new(
234+
id: new PrimaryKey('abc'),
235+
);
236+
237+
$this->assertSame(
238+
'/books/abc',
239+
uri([BookController::class, 'show'], book: $book->id),
240+
);
241+
}
242+
217243
public function test_uri_with_query_param_that_collides_partially_with_route_param(): void
218244
{
219245
$this->assertSame(

0 commit comments

Comments
 (0)