11
11
12
12
class Relation
13
13
{
14
+ private static array $ relationMap = [
15
+ \Awobaz \Compoships \Database \Eloquent \Relations \BelongsTo::class => BelongsTo::class,
16
+ \Awobaz \Compoships \Database \Eloquent \Relations \HasOne::class => HasOne::class,
17
+ \Awobaz \Compoships \Database \Eloquent \Relations \HasMany::class => HasMany::class,
18
+ ];
14
19
private array $ attributes ;
15
20
16
21
public function __construct (array $ attributes )
@@ -20,7 +25,7 @@ public function __construct(array $attributes)
20
25
21
26
public function type (): string
22
27
{
23
- return $ this ->attributes ['type ' ];
28
+ return self :: $ relationMap [ $ this -> attributes [ ' type ' ]] ?? $ this ->attributes ['type ' ];
24
29
}
25
30
26
31
public function related (): string
@@ -33,34 +38,38 @@ public function parent(): string
33
38
return $ this ->attributes ['parent ' ];
34
39
}
35
40
36
- public function localKey (): string
41
+ public function localKeys (): array
37
42
{
38
- return $ this ->attributes ['local_key ' ];
43
+ return ( array ) $ this ->attributes ['local_key ' ];
39
44
}
40
45
41
46
public function localTable (): string
42
47
{
43
- return Helpers::getTableName ($ this ->localKey () );
48
+ return Helpers::getTableName ($ this ->localKeys ()[ 0 ] );
44
49
}
45
50
46
- public function localColumn (): string
51
+ public function localColumns (): array
47
52
{
48
- return Helpers::getColumnName ($ this ->localKey ());
53
+ return array_map (static function (string $ column ) {
54
+ return Helpers::getColumnName ($ column );
55
+ }, $ this ->localKeys ());
49
56
}
50
57
51
- public function foreignKey (): string
58
+ public function foreignKeys (): array
52
59
{
53
- return $ this ->attributes ['foreign_key ' ];
60
+ return ( array ) $ this ->attributes ['foreign_key ' ];
54
61
}
55
62
56
63
public function foreignTable (): string
57
64
{
58
- return Helpers::getTableName ($ this ->foreignKey () );
65
+ return Helpers::getTableName ($ this ->foreignKeys ()[ 0 ] );
59
66
}
60
67
61
- public function foreignColumn (): string
68
+ public function foreignColumns (): array
62
69
{
63
- return Helpers::getColumnName ($ this ->foreignKey ());
70
+ return array_map (static function (string $ column ) {
71
+ return Helpers::getColumnName ($ column );
72
+ }, $ this ->foreignKeys ());
64
73
}
65
74
66
75
public function morphClass (): ?string
@@ -119,8 +128,8 @@ public function relatedRelation(): Relation
119
128
'type ' => $ reverseLookup [$ type ] ?? $ type ,
120
129
'related ' => $ this ->parent (),
121
130
'parent ' => $ this ->related (),
122
- 'local_key ' => $ this ->foreignKey (),
123
- 'foreign_key ' => $ this ->localKey (),
131
+ 'local_key ' => $ this ->foreignKeys (),
132
+ 'foreign_key ' => $ this ->localKeys (),
124
133
'pivot ' => $ this ->attributes ['pivot ' ] ?? null ,
125
134
'morph_class ' => $ this ->morphClass (),
126
135
'morph_type ' => $ this ->morphType (),
@@ -149,15 +158,19 @@ public function sortByRelation(): int
149
158
*/
150
159
public function sortByKeys (): array
151
160
{
152
- return [$ this ->type (), $ this ->localKey (), $ this ->foreignKey ()];
161
+ return [$ this ->type (), $ this ->localKeys (), $ this ->foreignKeys ()];
153
162
}
154
163
155
164
public function uniqueId (): string
156
165
{
157
- $ localKey = Helpers::getTableName ($ this ->localKey ());
158
- $ foreignKey = Helpers::getTableName ($ this ->foreignKey ());
166
+ $ sortBy = [];
167
+ foreach ($ this ->localKeys () as $ localKey ) {
168
+ $ sortBy [] = Helpers::getTableName ($ localKey );
169
+ }
170
+ foreach ($ this ->foreignKeys () as $ foreignKey ) {
171
+ $ sortBy [] = Helpers::getTableName ($ foreignKey );
172
+ }
159
173
160
- $ sortBy = [$ localKey , $ foreignKey ];
161
174
sort ($ sortBy );
162
175
163
176
return implode (':: ' , $ sortBy );
0 commit comments