1818use Tempest \Validation \SkipValidation ;
1919use Tempest \Validation \Validator ;
2020
21+ use function Tempest \Database \model ;
2122use function Tempest \get ;
2223use function Tempest \Support \arr ;
2324use function Tempest \Support \str ;
@@ -28,7 +29,8 @@ final class ModelInspector
2829
2930 public function __construct (
3031 private object |string $ model ,
31- ) {
32+ )
33+ {
3234 if ($ this ->model instanceof ClassReflector) {
3335 $ this ->modelClass = $ this ->model ;
3436 } else {
@@ -219,6 +221,38 @@ public function getRelation(string|PropertyReflector $name): ?Relation
219221 return $ this ->getBelongsTo ($ name ) ?? $ this ->getHasOne ($ name ) ?? $ this ->getHasMany ($ name );
220222 }
221223
224+ public function resolveRelations (string $ relationString , string $ parent = '' ): array
225+ {
226+ if ($ relationString === '' ) {
227+ return [];
228+ }
229+
230+ $ relationNames = explode ('. ' , $ relationString );
231+
232+ $ currentRelationName = $ relationNames [0 ];
233+
234+ $ currentRelation = $ this ->getRelation ($ currentRelationName );
235+
236+ if ($ currentRelation === null ) {
237+ return [];
238+ }
239+
240+ unset($ relationNames [0 ]);
241+
242+ $ newRelationString = implode ('. ' , $ relationNames );
243+ $ currentRelation ->setParent ($ parent );
244+ $ newParent = ltrim (sprintf (
245+ '%s.%s ' ,
246+ $ parent ,
247+ $ currentRelationName ,
248+ ), '. ' );
249+
250+ return [
251+ $ currentRelation ,
252+ ...model ($ currentRelation ->property ->getType ()->asClass ())->resolveRelations ($ newRelationString , $ newParent )
253+ ];
254+ }
255+
222256 public function getEagerRelations (): array
223257 {
224258 if (! $ this ->isObjectModel ()) {
0 commit comments