1 | <?php |
||
19 | class RelationHelper |
||
20 | { |
||
21 | /** |
||
22 | * List of all relations defined on a model class. |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected static $relations = []; |
||
27 | |||
28 | /** |
||
29 | * Laravel's available relation types (classes|methods). |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected static $relationTypes = [ |
||
34 | 'hasOne', |
||
35 | 'hasMany', |
||
36 | 'hasManyThrough', |
||
37 | 'belongsTo', |
||
38 | 'belongsToMany', |
||
39 | 'morphOne', |
||
40 | 'morphMany', |
||
41 | 'morphTo', |
||
42 | 'morphToMany', |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * All available Laravel's direct relations. |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected static $directRelations = [ |
||
51 | HasOne::class, |
||
52 | MorphOne::class, |
||
53 | HasMany::class, |
||
54 | MorphMany::class, |
||
55 | BelongsTo::class, |
||
56 | MorphTo::class, |
||
57 | ]; |
||
58 | |||
59 | /** |
||
60 | * All available Laravel's pivoted relations. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected static $pivotedRelations = [ |
||
65 | BelongsToMany::class, |
||
66 | MorphToMany::class, |
||
67 | ]; |
||
68 | |||
69 | /** |
||
70 | * All available Laravel's direct parent relations. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected static $parentRelations = [ |
||
75 | BelongsTo::class, |
||
76 | MorphTo::class, |
||
77 | ]; |
||
78 | |||
79 | /** |
||
80 | * All available Laravel's direct child relations. |
||
81 | * |
||
82 | * @var array |
||
83 | */ |
||
84 | protected static $childRelations = [ |
||
85 | HasOne::class, |
||
86 | MorphOne::class, |
||
87 | HasMany::class, |
||
88 | MorphMany::class, |
||
89 | ]; |
||
90 | |||
91 | /** |
||
92 | * All available Laravel's direct single child relations. |
||
93 | * |
||
94 | * @var array |
||
95 | */ |
||
96 | protected static $childRelationsSingle = [ |
||
97 | HasOne::class, |
||
98 | MorphOne::class, |
||
99 | ]; |
||
100 | |||
101 | /** |
||
102 | * All available Laravel's direct multiple children relations. |
||
103 | * |
||
104 | * @var array |
||
105 | */ |
||
106 | protected static $childRelationsMultiple = [ |
||
107 | HasMany::class, |
||
108 | MorphMany::class, |
||
109 | ]; |
||
110 | |||
111 | /** |
||
112 | * Verify if a given relation is direct or not. |
||
113 | * |
||
114 | * @param string $relation |
||
115 | * @return bool |
||
116 | */ |
||
117 | public static function isDirect(string $relation): bool |
||
121 | |||
122 | /** |
||
123 | * Verify if a given relation is pivoted or not. |
||
124 | * |
||
125 | * @param string $relation |
||
126 | * @return bool |
||
127 | */ |
||
128 | public static function isPivoted(string $relation): bool |
||
132 | |||
133 | /** |
||
134 | * Verify if a given direct relation is of type parent. |
||
135 | * |
||
136 | * @param string $relation |
||
137 | * @return bool |
||
138 | */ |
||
139 | public static function isParent(string $relation): bool |
||
143 | |||
144 | /** |
||
145 | * Verify if a given direct relation is of type child. |
||
146 | * |
||
147 | * @param string $relation |
||
148 | * @return bool |
||
149 | */ |
||
150 | public static function isChild(string $relation): bool |
||
154 | |||
155 | /** |
||
156 | * Verify if a given direct relation is of type single child. |
||
157 | * Ex: hasOne, morphOne. |
||
158 | * |
||
159 | * @param string $relation |
||
160 | * @return bool |
||
161 | */ |
||
162 | public static function isChildSingle(string $relation): bool |
||
166 | |||
167 | /** |
||
168 | * Verify if a given direct relation is of type single child. |
||
169 | * Ex: hasMany, morphMany. |
||
170 | * |
||
171 | * @param string $relation |
||
172 | * @return bool |
||
173 | */ |
||
174 | public static function isChildMultiple(string $relation): bool |
||
178 | |||
179 | /** |
||
180 | * Get all the defined model class relations. |
||
181 | * Not just the eager loaded ones present in the $relations Eloquent property. |
||
182 | * |
||
183 | * @param Model $model |
||
184 | * @return array |
||
185 | * @throws ReflectionException |
||
186 | */ |
||
187 | public static function getModelRelations(Model $model): array |
||
224 | } |
||
225 |