|
4 | 4 |
|
5 | 5 | namespace Tempest\Database\Builder; |
6 | 6 |
|
| 7 | +use ReflectionException; |
7 | 8 | use Tempest\Database\BelongsTo; |
8 | 9 | use Tempest\Database\Builder\Relations\BelongsToRelation; |
9 | 10 | use Tempest\Database\Builder\Relations\HasManyRelation; |
10 | 11 | use Tempest\Database\Builder\Relations\HasOneRelation; |
| 12 | +use Tempest\Database\Config\DatabaseConfig; |
11 | 13 | use Tempest\Database\Eager; |
12 | 14 | use Tempest\Database\HasMany; |
13 | 15 | use Tempest\Database\HasOne; |
| 16 | +use Tempest\Database\TableName; |
14 | 17 | use Tempest\Reflection\ClassReflector; |
| 18 | +use Tempest\Support\Arr\ImmutableArray; |
15 | 19 |
|
16 | | -use function Tempest\reflect; |
| 20 | +use function Tempest\get; |
17 | 21 |
|
18 | 22 | final readonly class ModelDefinition |
19 | 23 | { |
20 | | - public function __construct( |
21 | | - /** @var class-string<\Tempest\Database\DatabaseModel> $modelClass */ |
22 | | - private string $modelClass, |
23 | | - ) {} |
| 24 | + private ClassReflector $modelClass; |
| 25 | + |
| 26 | + public static function tryFrom(string|object $model): ?self |
| 27 | + { |
| 28 | + try { |
| 29 | + return new self($model); |
| 30 | + } catch (ReflectionException) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public function __construct(string|object $model) |
| 36 | + { |
| 37 | + if ($model instanceof ClassReflector) { |
| 38 | + $this->modelClass = $model; |
| 39 | + } else { |
| 40 | + $this->modelClass = new ClassReflector($model); |
| 41 | + } |
| 42 | + } |
24 | 43 |
|
25 | 44 | /** @return \Tempest\Database\Builder\Relations\Relation[] */ |
26 | 45 | public function getRelations(string $relationName): array |
27 | 46 | { |
28 | 47 | $relations = []; |
29 | | - $class = reflect($this->modelClass); |
30 | 48 | $relationNames = explode('.', $relationName); |
31 | | - $alias = TableName::for($class)->tableName; |
| 49 | + $alias = $this->getTableDefinition()->name; |
| 50 | + $class = $this->modelClass; |
32 | 51 |
|
33 | 52 | foreach ($relationNames as $relationNamePart) { |
34 | 53 | $property = $class->getProperty($relationNamePart); |
@@ -68,7 +87,7 @@ public function getEagerRelations(): array |
68 | 87 | { |
69 | 88 | $relations = []; |
70 | 89 |
|
71 | | - foreach ($this->buildEagerRelationNames(reflect($this->modelClass)) as $relationName) { |
| 90 | + foreach ($this->buildEagerRelationNames($this->modelClass) as $relationName) { |
72 | 91 | foreach ($this->getRelations($relationName) as $relation) { |
73 | 92 | $relations[$relation->getRelationName()] = $relation; |
74 | 93 | } |
@@ -96,22 +115,30 @@ private function buildEagerRelationNames(ClassReflector $class): array |
96 | 115 | return $relations; |
97 | 116 | } |
98 | 117 |
|
99 | | - public function getTableName(): TableName |
| 118 | + public function getTableDefinition(): TableDefinition |
100 | 119 | { |
101 | | - return $this->modelClass::table(); |
| 120 | + $specificName = $this->modelClass |
| 121 | + ->getAttribute(TableName::class) |
| 122 | + ?->name; |
| 123 | + |
| 124 | + $conventionalName = get(DatabaseConfig::class) |
| 125 | + ->namingStrategy |
| 126 | + ->getName($this->modelClass->getName()); |
| 127 | + |
| 128 | + return new TableDefinition($specificName ?? $conventionalName); |
102 | 129 | } |
103 | 130 |
|
104 | | - public function getFieldName(string $fieldName): FieldName |
| 131 | + public function getFieldDefinition(string $name): FieldDefinition |
105 | 132 | { |
106 | | - return new FieldName( |
107 | | - tableName: $this->getTableName(), |
108 | | - fieldName: $fieldName, |
| 133 | + return new FieldDefinition( |
| 134 | + tableDefinition: $this->getTableDefinition(), |
| 135 | + name: $name, |
109 | 136 | ); |
110 | 137 | } |
111 | 138 |
|
112 | | - /** @return \Tempest\Database\Builder\FieldName[] */ |
113 | | - public function getFieldNames(): array |
| 139 | + /** @return ImmutableArray<array-key, \Tempest\Database\Builder\FieldDefinition> */ |
| 140 | + public function getFieldDefinitions(): ImmutableArray |
114 | 141 | { |
115 | | - return FieldName::make(reflect($this->modelClass)); |
| 142 | + return FieldDefinition::all($this->modelClass); |
116 | 143 | } |
117 | 144 | } |
0 commit comments