1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: alex |
5
|
|
|
* Date: 13/02/20 |
6
|
|
|
* Time: 4:22 AM. |
7
|
|
|
*/ |
8
|
|
|
namespace AlgoWeb\PODataLaravel\Models; |
9
|
|
|
|
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
12
|
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough; |
13
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
14
|
|
|
use Mockery\Mock; |
15
|
|
|
use POData\Common\InvalidOperationException; |
16
|
|
|
|
17
|
|
|
trait MetadataKeyMethodNamesTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param Relation $foo |
21
|
|
|
* @throws InvalidOperationException |
22
|
|
|
* @return array|null |
23
|
|
|
*/ |
24
|
|
|
protected function getRelationsHasManyKeyNames(Relation $foo) |
25
|
|
|
{ |
26
|
|
|
$thruName = null; |
27
|
|
|
if ($foo instanceof HasManyThrough) { |
28
|
|
|
list($fkMethodName, $rkMethodName) = $this->polyglotKeyMethodBackupNames($foo, true); |
29
|
|
|
$thruName = $this->polyglotThroughKeyMethodNames($foo); |
30
|
|
|
return [$thruName, $fkMethodName, $rkMethodName]; |
31
|
|
|
} |
32
|
|
|
if ($foo instanceof BelongsToMany) { |
33
|
|
|
list($fkMethodName, $rkMethodName) = $this->polyglotKeyMethodNames($foo, true); |
34
|
|
|
return [$thruName, $fkMethodName, $rkMethodName]; |
35
|
|
|
} |
36
|
|
|
list($fkMethodName, $rkMethodName) = $this->polyglotKeyMethodBackupNames($foo, true); |
37
|
|
|
return [$thruName, $fkMethodName, $rkMethodName]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Relation $foo |
42
|
|
|
* @param mixed $condition |
43
|
|
|
* |
44
|
|
|
* @throws InvalidOperationException |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
protected function polyglotKeyMethodNames(Relation $foo, $condition = false) |
48
|
|
|
{ |
49
|
|
|
// if $condition is falsy, return quickly - don't muck around |
50
|
|
|
if (!$condition) { |
51
|
|
|
return [null, null]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$fkList = ['getQualifiedForeignPivotKeyName', 'getQualifiedForeignKeyName', 'getForeignKey']; |
55
|
|
|
$rkList = ['getQualifiedRelatedPivotKeyName', 'getQualifiedRelatedKeyName', 'getOtherKey', 'getOwnerKey', |
56
|
|
|
'getQualifiedOwnerKeyName']; |
57
|
|
|
|
58
|
|
|
$fkMethodName = null; |
59
|
|
|
$rkMethodName = null; |
60
|
|
|
|
61
|
|
|
foreach ($fkList as $methodName) { |
62
|
|
|
if (method_exists($foo, $methodName)) { |
63
|
|
|
$fkMethodName = $methodName; |
64
|
|
|
break; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (null === $fkMethodName) { |
69
|
|
|
$msg = 'Expected at least 1 element in foreign-key list, got 0'; |
70
|
|
|
throw new InvalidOperationException($msg); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
foreach ($rkList as $methodName) { |
74
|
|
|
if (method_exists($foo, $methodName)) { |
75
|
|
|
$rkMethodName = $methodName; |
76
|
|
|
break; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
if (null === $rkMethodName) { |
80
|
|
|
$msg = 'Expected at least 1 element in related-key list, got 0'; |
81
|
|
|
throw new InvalidOperationException($msg); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return [$fkMethodName, $rkMethodName]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param Relation $foo |
89
|
|
|
* @param bool $condition |
90
|
|
|
* @throws InvalidOperationException |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
protected function polyglotKeyMethodBackupNames(Relation $foo, $condition = false) |
94
|
|
|
{ |
95
|
|
|
// if $condition is falsy, return quickly - don't muck around |
96
|
|
|
if (!$condition) { |
97
|
|
|
return [null, null]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$fkList = ['getForeignKey', 'getForeignKeyName', 'getQualifiedFarKeyName']; |
101
|
|
|
$rkList = ['getOtherKey', 'getQualifiedParentKeyName']; |
102
|
|
|
|
103
|
|
|
$fkMethodName = null; |
104
|
|
|
$rkMethodName = null; |
105
|
|
|
|
106
|
|
|
foreach ($fkList as $methodName) { |
107
|
|
|
if (method_exists($foo, $methodName)) { |
108
|
|
|
$fkMethodName = $methodName; |
109
|
|
|
break; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (null === $fkMethodName) { |
114
|
|
|
$msg = 'Expected at least 1 element in foreign-key list, got 0'; |
115
|
|
|
throw new InvalidOperationException($msg); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
foreach ($rkList as $methodName) { |
119
|
|
|
if (method_exists($foo, $methodName)) { |
120
|
|
|
$rkMethodName = $methodName; |
121
|
|
|
break; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
if (null === $rkMethodName) { |
125
|
|
|
$msg = 'Expected at least 1 element in related-key list, got 0'; |
126
|
|
|
throw new InvalidOperationException($msg); |
127
|
|
|
} |
128
|
|
|
return [$fkMethodName, $rkMethodName]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function polyglotThroughKeyMethodNames(HasManyThrough $foo) |
132
|
|
|
{ |
133
|
|
|
$thruList = ['getThroughKey', 'getQualifiedFirstKeyName']; |
134
|
|
|
|
135
|
|
|
foreach ($thruList as $methodName) { |
136
|
|
|
if (method_exists($foo, $methodName)) { |
137
|
|
|
return $methodName; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param Model $model |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
protected function getModelClassMethods(Model $model) |
147
|
|
|
{ |
148
|
|
|
$methods = get_class_methods($model); |
149
|
|
|
$filter = function ($method) { |
150
|
|
|
return (!method_exists('Illuminate\Database\Eloquent\Model', $method) |
151
|
|
|
&& !method_exists(Mock::class, $method) |
152
|
|
|
&& !method_exists(MetadataTrait::class, $method) |
153
|
|
|
); |
154
|
|
|
}; |
155
|
|
|
$methods = array_filter($methods, $filter); |
156
|
|
|
|
157
|
|
|
return $methods; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|