99use InvalidArgumentException ;
1010use LogicException ;
1111use Michalsn \CodeIgniterNestedModel \Enums \RelationTypes ;
12+ use Michalsn \CodeIgniterNestedModel \Exceptions \NestedModelException ;
1213use Michalsn \CodeIgniterNestedModel \Relation ;
1314use Michalsn \CodeIgniterNestedModel \With ;
1415use ReflectionClass ;
@@ -43,7 +44,7 @@ public function with(string $relation, ?Closure $closure = null): static
4344 [$ relation , $ name ] = explode ('. ' , $ relation , 2 );
4445
4546 if (! isset ($ this ->relations [$ relation ])) {
46- throw new InvalidArgumentException ( sprintf ( ' Parent relation "%s" has not been declared yet. ' , $ relation) );
47+ throw NestedModelException:: forParentRelationNotDeclared ( $ relation );
4748 }
4849
4950 $ this ->relations [$ relation ]->setWith (new With ($ name , $ closure ));
@@ -65,18 +66,18 @@ public function with(string $relation, ?Closure $closure = null): static
6566 private function checkReturnType (string $ methodName ): bool
6667 {
6768 if (! method_exists ($ this , $ methodName )) {
68- throw new InvalidArgumentException ( sprintf ( ' Relation "%s" is not defined. ' , $ methodName) );
69+ throw NestedModelException:: forRelationNotDefined ( $ methodName );
6970 }
7071
7172 $ reflectionMethod = new ReflectionMethod ($ this , $ methodName );
7273 $ returnType = $ reflectionMethod ->getReturnType ();
7374
7475 if (! $ returnType instanceof ReflectionNamedType) {
75- throw new LogicException ( sprintf ( ' Method "%s()" is missing a required return type declaration. ' , $ methodName) );
76+ throw NestedModelException:: forMissingReturnType ( $ methodName );
7677 }
7778
7879 if ($ returnType ->getName () !== Relation::class) {
79- throw new UnexpectedValueException ( sprintf ( ' Method "%s()" returned an incorrect type. ' , $ methodName) );
80+ throw NestedModelException:: forIncorrectReturnType ( $ methodName );
8081 }
8182
8283 return true ;
@@ -97,8 +98,8 @@ private function addRelation(Model|string $model, RelationTypes $relationType, ?
9798 $ this ->relations [$ relation ] = new Relation (
9899 $ relationType ,
99100 $ model ,
100- $ foreignKey ?? ($ relationType === RelationTypes::belongTo ? $ this ->relationsGetBelongForeignKey ($ model ) : $ this ->relationsGetHasForeignKey ()),
101- $ primaryKey ?? ($ relationType === RelationTypes::belongTo ? $ this ->relationsGetBelongPrimaryKey ($ model ) : $ this ->relationsGetHasPrimaryKey ())
101+ $ foreignKey ?? ($ relationType === RelationTypes::belongsTo ? $ this ->relationsGetBelongForeignKey ($ model ) : $ this ->relationsGetHasForeignKey ()),
102+ $ primaryKey ?? ($ relationType === RelationTypes::belongsTo ? $ this ->relationsGetBelongPrimaryKey ($ model ) : $ this ->relationsGetHasPrimaryKey ())
102103 );
103104
104105 return $ this ->relations [$ relation ];
@@ -123,9 +124,9 @@ protected function hasMany(Model|string $model, ?string $foreignKey = null, ?str
123124 /**
124125 * @throws ReflectionException
125126 */
126- protected function belongTo (Model |string $ model , ?string $ primaryKey = null , ?string $ foreignKey = null ): Relation
127+ protected function belongsTo (Model |string $ model , ?string $ primaryKey = null , ?string $ foreignKey = null ): Relation
127128 {
128- return $ this ->addRelation ($ model , RelationTypes::belongTo , $ foreignKey , $ primaryKey );
129+ return $ this ->addRelation ($ model , RelationTypes::belongsTo , $ foreignKey , $ primaryKey );
129130 }
130131
131132 /*
@@ -177,6 +178,7 @@ protected function belongsToMany(Model|string $model, Model|string $through, ?st
177178 $this->relations[$relation]->setThrough($through, $throughForeignKey, $throughPrimaryKey);
178179 }
179180 */
181+
180182 /**
181183 * @throws ReflectionException
182184 */
@@ -387,7 +389,7 @@ protected function getDataForRelationById(int|string $id, Relation $relation)
387389
388390 $ relation ->applyConditions ();
389391
390- return in_array ($ relation ->type , [RelationTypes::hasOne, RelationTypes::belongTo ], true ) ?
392+ return in_array ($ relation ->type , [RelationTypes::hasOne, RelationTypes::belongsTo ], true ) ?
391393 $ query ->first () :
392394 $ query ->findAll ();
393395 }
@@ -431,7 +433,7 @@ protected function getDataForRelationByIds(array $id, Relation $relation): array
431433
432434 $ relationData = [];
433435
434- if (in_array ($ relation ->type , [RelationTypes::hasOne, RelationTypes::belongTo ], true )) {
436+ if (in_array ($ relation ->type , [RelationTypes::hasOne, RelationTypes::belongsTo ], true )) {
435437 foreach ($ results as $ row ) {
436438 $ relationData [$ this ->tempReturnType === 'array ' ? $ row [$ relation ->foreignKey ] : $ row ->{$ relation ->foreignKey }] = $ row ;
437439 }
0 commit comments