88use Tempest \Database \Eager ;
99use Tempest \Database \HasMany ;
1010use Tempest \Database \HasOne ;
11+ use Tempest \Database \Id ;
1112use Tempest \Database \Relation ;
1213use Tempest \Database \Table ;
1314use Tempest \Database \Virtual ;
14- use Tempest \Mapper \CastWith ;
1515use Tempest \Mapper \SerializeWith ;
1616use Tempest \Reflection \ClassReflector ;
1717use Tempest \Reflection \PropertyReflector ;
2727
2828final class ModelInspector
2929{
30- private ?ClassReflector $ modelClass ;
30+ private(set) ?ClassReflector $ reflector ;
3131
32- private object |string $ model ;
32+ private(set) object |string $ instance ;
3333
3434 public function __construct (object |string $ model )
3535 {
3636 if ($ model instanceof HasMany) {
3737 $ model = $ model ->property ->getIterableType ()->asClass ();
38- $ this ->modelClass = $ model ;
38+ $ this ->reflector = $ model ;
3939 } elseif ($ model instanceof BelongsTo || $ model instanceof HasOne) {
4040 $ model = $ model ->property ->getType ()->asClass ();
41- $ this ->modelClass = $ model ;
41+ $ this ->reflector = $ model ;
4242 } elseif ($ model instanceof ClassReflector) {
43- $ this ->modelClass = $ model ;
43+ $ this ->reflector = $ model ;
4444 } else {
4545 try {
46- $ this ->modelClass = new ClassReflector ($ model );
46+ $ this ->reflector = new ClassReflector ($ model );
4747 } catch (ReflectionException ) {
48- $ this ->modelClass = null ;
48+ $ this ->reflector = null ;
4949 }
5050 }
5151
52- $ this ->model = $ model ;
52+ $ this ->instance = $ model ;
5353 }
5454
5555 public function isObjectModel (): bool
5656 {
57- return $ this ->modelClass !== null ;
57+ return $ this ->reflector !== null ;
5858 }
5959
6060 public function getTableDefinition (): TableDefinition
6161 {
6262 if (! $ this ->isObjectModel ()) {
63- return new TableDefinition ($ this ->model );
63+ return new TableDefinition ($ this ->instance );
6464 }
6565
66- $ specificName = $ this ->modelClass
66+ $ specificName = $ this ->reflector
6767 ->getAttribute (Table::class)
6868 ?->name;
6969
7070 $ conventionalName = get (DatabaseConfig::class)
7171 ->namingStrategy
72- ->getName ($ this ->modelClass ->getName ());
72+ ->getName ($ this ->reflector ->getName ());
7373
7474 return new TableDefinition ($ specificName ?? $ conventionalName );
7575 }
7676
77+ public function getFieldDefinition (string $ field ): FieldDefinition
78+ {
79+ return new FieldDefinition (
80+ $ this ->getTableDefinition (),
81+ $ field ,
82+ );
83+ }
84+
7785 public function getTableName (): string
7886 {
7987 return $ this ->getTableDefinition ()->name ;
@@ -85,14 +93,14 @@ public function getPropertyValues(): array
8593 return [];
8694 }
8795
88- if (! is_object ($ this ->model )) {
96+ if (! is_object ($ this ->instance )) {
8997 return [];
9098 }
9199
92100 $ values = [];
93101
94- foreach ($ this ->modelClass ->getProperties () as $ property ) {
95- if (! $ property ->isInitialized ($ this ->model )) {
102+ foreach ($ this ->reflector ->getProperties () as $ property ) {
103+ if (! $ property ->isInitialized ($ this ->instance )) {
96104 continue ;
97105 }
98106
@@ -102,7 +110,7 @@ public function getPropertyValues(): array
102110
103111 $ name = $ property ->getName ();
104112
105- $ values [$ name ] = $ property ->getValue ($ this ->model );
113+ $ values [$ name ] = $ property ->getValue ($ this ->instance );
106114 }
107115
108116 return $ values ;
@@ -122,11 +130,11 @@ public function getBelongsTo(string $name): ?BelongsTo
122130 return $ this ->getBelongsTo ($ singularizedName );
123131 }
124132
125- if (! $ this ->modelClass ->hasProperty ($ name )) {
133+ if (! $ this ->reflector ->hasProperty ($ name )) {
126134 return null ;
127135 }
128136
129- $ property = $ this ->modelClass ->getProperty ($ name );
137+ $ property = $ this ->reflector ->getProperty ($ name );
130138
131139 if ($ belongsTo = $ property ->getAttribute (BelongsTo::class)) {
132140 return $ belongsTo ;
@@ -168,11 +176,11 @@ public function getHasOne(string $name): ?HasOne
168176 return $ this ->getHasOne ($ singularizedName );
169177 }
170178
171- if (! $ this ->modelClass ->hasProperty ($ name )) {
179+ if (! $ this ->reflector ->hasProperty ($ name )) {
172180 return null ;
173181 }
174182
175- $ property = $ this ->modelClass ->getProperty ($ name );
183+ $ property = $ this ->reflector ->getProperty ($ name );
176184
177185 if ($ hasOne = $ property ->getAttribute (HasOne::class)) {
178186 return $ hasOne ;
@@ -189,11 +197,11 @@ public function getHasMany(string $name): ?HasMany
189197
190198 $ name = str ($ name )->camel ();
191199
192- if (! $ this ->modelClass ->hasProperty ($ name )) {
200+ if (! $ this ->reflector ->hasProperty ($ name )) {
193201 return null ;
194202 }
195203
196- $ property = $ this ->modelClass ->getProperty ($ name );
204+ $ property = $ this ->reflector ->getProperty ($ name );
197205
198206 if ($ hasMany = $ property ->getAttribute (HasMany::class)) {
199207 return $ hasMany ;
@@ -235,7 +243,7 @@ public function getSelectFields(): ImmutableArray
235243
236244 $ selectFields = arr ();
237245
238- foreach ($ this ->modelClass ->getPublicProperties () as $ property ) {
246+ foreach ($ this ->reflector ->getPublicProperties () as $ property ) {
239247 $ relation = $ this ->getRelation ($ property ->getName ());
240248
241249 if ($ relation instanceof HasMany || $ relation instanceof HasOne) {
@@ -297,7 +305,7 @@ public function resolveEagerRelations(string $parent = ''): array
297305
298306 $ relations = [];
299307
300- foreach ($ this ->modelClass ->getPublicProperties () as $ property ) {
308+ foreach ($ this ->reflector ->getPublicProperties () as $ property ) {
301309 if (! $ property ->hasAttribute (Eager::class)) {
302310 continue ;
303311 }
@@ -334,7 +342,7 @@ public function validate(mixed ...$data): void
334342 $ failingRules = [];
335343
336344 foreach ($ data as $ key => $ value ) {
337- $ property = $ this ->modelClass ->getProperty ($ key );
345+ $ property = $ this ->reflector ->getProperty ($ key );
338346
339347 if ($ property ->hasAttribute (SkipValidation::class)) {
340348 continue ;
@@ -351,26 +359,39 @@ public function validate(mixed ...$data): void
351359 }
352360
353361 if ($ failingRules !== []) {
354- throw new ValidationFailed ($ this ->modelClass ->getName (), $ failingRules );
362+ throw new ValidationFailed ($ this ->reflector ->getName (), $ failingRules );
355363 }
356364 }
357365
358366 public function getName (): string
359367 {
360- if ($ this ->isObjectModel () ) {
361- return $ this ->modelClass ->getName ();
368+ if ($ this ->reflector ) {
369+ return $ this ->reflector ->getName ();
362370 }
363371
364- return $ this ->modelClass ;
372+ return $ this ->instance ;
373+ }
374+
375+ public function getPrimaryFieldName (): string
376+ {
377+ return $ this ->getTableDefinition ()->name . '. ' . $ this ->getPrimaryKey ();
365378 }
366379
367380 public function getPrimaryKey (): string
368381 {
369382 return 'id ' ;
370383 }
371384
372- public function getPrimaryField (): string
385+ public function getPrimaryKeyValue (): ? Id
373386 {
374- return $ this ->getTableDefinition ()->name . '. ' . $ this ->getPrimaryKey ();
387+ if (! $ this ->isObjectModel ()) {
388+ return null ;
389+ }
390+
391+ if (! is_object ($ this ->instance )) {
392+ return null ;
393+ }
394+
395+ return $ this ->instance ->{$ this ->getPrimaryKey ()};
375396 }
376397}
0 commit comments