Skip to content

Commit dbaa01e

Browse files
committed
wip
1 parent e306e3c commit dbaa01e

File tree

6 files changed

+26
-18
lines changed

6 files changed

+26
-18
lines changed

packages/database/src/IsDatabaseModel.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,17 @@
1111
use Tempest\Database\Exceptions\ValueWasMissing;
1212
use Tempest\Reflection\ClassReflector;
1313
use Tempest\Reflection\PropertyReflector;
14+
use Tempest\Router\IsBindingValue;
1415
use Tempest\Validation\SkipValidation;
1516

1617
use function Tempest\Support\arr;
1718
use function Tempest\Support\str;
1819

1920
trait IsDatabaseModel
2021
{
21-
#[SkipValidation]
22+
#[IsBindingValue, SkipValidation]
2223
public PrimaryKey $id;
2324

24-
#[Virtual]
25-
public int|string $bindingValue {
26-
get => $this->id->value;
27-
}
28-
2925
/**
3026
* Returns a builder for selecting records using this model's table.
3127
*

packages/router/src/Bindable.php

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

55
interface Bindable
66
{
7-
/**
8-
* @var int|string
9-
* The value being used to generate URIs when passed into the `\Tempest\Router\uri` function
10-
*/
11-
public int|string $bindingValue {
12-
get;
13-
}
14-
157
public static function resolve(string $input): self;
168
}

packages/router/src/GenericRouter.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use BackedEnum;
88
use Psr\Http\Message\ServerRequestInterface as PsrRequest;
9+
use ReflectionClass;
910
use Tempest\Container\Container;
1011
use Tempest\Core\AppConfig;
1112
use Tempest\Database\PrimaryKey;
@@ -128,7 +129,14 @@ public function toUri(array|string $action, ...$params): string
128129
if ($value instanceof BackedEnum) {
129130
$value = $value->value;
130131
} elseif ($value instanceof Bindable) {
131-
$value = $value->bindingValue;
132+
foreach (new ClassReflector($value)->getPublicProperties() as $property) {
133+
if (! $property->hasAttribute(IsBindingValue::class)) {
134+
continue;
135+
}
136+
137+
$value = $property->getValue($value);
138+
break;
139+
}
132140
}
133141

134142
$uri = $uri->replaceRegex(
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Tempest\Router;
4+
5+
use Attribute;
6+
7+
#[Attribute(Attribute::TARGET_PROPERTY)]
8+
final class IsBindingValue
9+
{
10+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ final class Book implements Bindable
1919
#[HasLength(min: 1, max: 120)]
2020
public string $title;
2121

22-
public Author $author;
22+
public ?Author $author = null;
2323

2424
/** @var \Tests\Tempest\Fixtures\Modules\Books\Models\Chapter[] */
25-
public array $chapters;
25+
public array $chapters = [];
2626

2727
#[HasOne]
28-
public Isbn $isbn;
28+
public ?Isbn $isbn = null;
2929
}

tests/Integration/Database/RefreshModelTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Tempest\Database\Migrations\CreateMigrationsTable;
66
use Tests\Tempest\Fixtures\Migrations\CreateAuthorTable;
77
use Tests\Tempest\Fixtures\Migrations\CreateBookTable;
8+
use Tests\Tempest\Fixtures\Migrations\CreateChapterTable;
89
use Tests\Tempest\Fixtures\Modules\Books\Models\Author;
910
use Tests\Tempest\Fixtures\Modules\Books\Models\Book;
1011
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
@@ -19,6 +20,7 @@ public function test_refresh_works_for_models_with_unloaded_relation(): void
1920
CreateMigrationsTable::class,
2021
CreateBookTable::class,
2122
CreateAuthorTable::class,
23+
CreateChapterTable::class,
2224
);
2325

2426
$author = Author::create(

0 commit comments

Comments
 (0)