7
7
use Illuminate \Support \Collection ;
8
8
use Illuminate \Database \Eloquent \Relations \Relation ;
9
9
use Illuminate \Database \Eloquent \Relations \BelongsTo ;
10
- use Illuminate \Database \Eloquent \Relations \MorphToMany ;
11
10
use Illuminate \Database \Eloquent \Relations \HasOneOrMany ;
12
- use Illuminate \Database \Eloquent \Relations \BelongsToMany ;
13
11
14
12
class RelationFinder
15
13
{
@@ -18,21 +16,20 @@ class RelationFinder
18
16
*
19
17
* @param string $model
20
18
* @return Collection
19
+ * @throws \ReflectionException
21
20
*/
22
21
public function getModelRelations (string $ model )
23
22
{
24
23
$ class = new ReflectionClass ($ model );
25
24
26
- $ traitMethods = Collection::make ($ class ->getTraits ())->map (function ($ trait ) {
25
+ $ traitMethods = Collection::make ($ class ->getTraits ())->map (function (ReflectionClass $ trait ) {
27
26
return Collection::make ($ trait ->getMethods (ReflectionMethod::IS_PUBLIC ));
28
27
})->flatten ();
29
28
30
29
$ methods = Collection::make ($ class ->getMethods (ReflectionMethod::IS_PUBLIC ))
31
30
->merge ($ traitMethods )
32
31
->reject (function (ReflectionMethod $ method ) use ($ model ) {
33
- return $ method ->class !== $ model ;
34
- })->reject (function (ReflectionMethod $ method ) use ($ model ) {
35
- return $ method ->getNumberOfParameters () > 0 ;
32
+ return $ method ->class !== $ model || $ method ->getNumberOfParameters () > 0 ;
36
33
});
37
34
38
35
$ relations = Collection::make ();
@@ -46,13 +43,22 @@ public function getModelRelations(string $model)
46
43
return $ relations ;
47
44
}
48
45
46
+ /**
47
+ * @param string $qualifiedKeyName
48
+ * @return mixed
49
+ */
49
50
protected function getParentKey (string $ qualifiedKeyName )
50
51
{
51
52
$ segments = explode ('. ' , $ qualifiedKeyName );
52
53
53
54
return end ($ segments );
54
55
}
55
56
57
+ /**
58
+ * @param ReflectionMethod $method
59
+ * @param string $model
60
+ * @return array|null
61
+ */
56
62
protected function getRelationshipFromMethodAndModel (ReflectionMethod $ method , string $ model )
57
63
{
58
64
try {
0 commit comments