|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: alex |
|
5
|
|
|
* Date: 13/02/20 |
|
6
|
|
|
* Time: 4:22 AM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace AlgoWeb\PODataLaravel\Models; |
|
10
|
|
|
|
|
11
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
12
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
13
|
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough; |
|
14
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
|
15
|
|
|
use Mockery\Mock; |
|
16
|
|
|
use POData\Common\InvalidOperationException; |
|
17
|
|
|
|
|
18
|
|
|
trait MetadataKeyMethodNamesTrait |
|
19
|
|
|
{ |
|
20
|
|
|
protected static $methodAlternate = []; |
|
21
|
|
|
protected static $methodPrimary = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param Relation $foo |
|
25
|
|
|
* @return array|null |
|
26
|
|
|
* @throws InvalidOperationException |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function getRelationsHasManyKeyNames(Relation $foo) |
|
29
|
|
|
{ |
|
30
|
|
|
$thruName = null; |
|
31
|
|
|
if ($foo instanceof HasManyThrough) { |
|
32
|
|
|
list($fkMethodName, $rkMethodName) = $this->polyglotKeyMethodBackupNames($foo, true); |
|
33
|
|
|
$thruName = $this->polyglotThroughKeyMethodNames($foo); |
|
34
|
|
|
return [$thruName, $fkMethodName, $rkMethodName]; |
|
35
|
|
|
} |
|
36
|
|
|
if ($foo instanceof BelongsToMany) { |
|
37
|
|
|
list($fkMethodName, $rkMethodName) = $this->polyglotKeyMethodNames($foo, true); |
|
38
|
|
|
return [$thruName, $fkMethodName, $rkMethodName]; |
|
39
|
|
|
} |
|
40
|
|
|
list($fkMethodName, $rkMethodName) = $this->polyglotKeyMethodBackupNames($foo, true); |
|
41
|
|
|
return [$thruName, $fkMethodName, $rkMethodName]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param Relation $foo |
|
46
|
|
|
* @param mixed $condition |
|
47
|
|
|
* |
|
48
|
|
|
* @return array |
|
49
|
|
|
* @throws InvalidOperationException |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function polyglotKeyMethodNames(Relation $foo, $condition = false) |
|
52
|
|
|
{ |
|
53
|
|
|
// if $condition is falsy, return quickly - don't muck around |
|
54
|
|
|
if (!$condition) { |
|
55
|
|
|
return [null, null]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$fkList = ['getQualifiedForeignKeyName', 'getForeignKey']; |
|
59
|
|
|
$rkList = ['getQualifiedRelatedKeyName', 'getOtherKey', 'getOwnerKey', 'getQualifiedOwnerKeyName']; |
|
60
|
|
|
|
|
61
|
|
|
$fkMethodName = null; |
|
62
|
|
|
$rkMethodName = null; |
|
63
|
|
|
|
|
64
|
|
|
if (array_key_exists(get_class($foo), static::$methodPrimary)) { |
|
65
|
|
|
$line = static::$methodPrimary[get_class($foo)]; |
|
66
|
|
|
$fkMethodName = $line['fk']; |
|
67
|
|
|
$rkMethodName = $line['rk']; |
|
68
|
|
|
} else { |
|
69
|
|
|
$methodList = $this->getRelationClassMethods($foo); |
|
70
|
|
|
$fkMethodName = 'getQualifiedForeignPivotKeyName'; |
|
71
|
|
|
$fkIntersect = array_values(array_intersect($fkList, $methodList)); |
|
72
|
|
|
$fkMethodName = (0 < count($fkIntersect)) ? $fkIntersect[0] : $fkMethodName; |
|
73
|
|
|
if (!(in_array($fkMethodName, $methodList))) { |
|
74
|
|
|
$msg = 'Selected method, ' . $fkMethodName . ', not in method list'; |
|
75
|
|
|
throw new InvalidOperationException($msg); |
|
76
|
|
|
} |
|
77
|
|
|
$rkMethodName = 'getQualifiedRelatedPivotKeyName'; |
|
78
|
|
|
$rkIntersect = array_values(array_intersect($rkList, $methodList)); |
|
79
|
|
|
$rkMethodName = (0 < count($rkIntersect)) ? $rkIntersect[0] : $rkMethodName; |
|
80
|
|
|
if (!(in_array($rkMethodName, $methodList))) { |
|
81
|
|
|
$msg = 'Selected method, ' . $rkMethodName . ', not in method list'; |
|
82
|
|
|
throw new InvalidOperationException($msg); |
|
83
|
|
|
} |
|
84
|
|
|
$line = ['fk' => $fkMethodName, 'rk' => $rkMethodName]; |
|
85
|
|
|
static::$methodPrimary[get_class($foo)] = $line; |
|
86
|
|
|
} |
|
87
|
|
|
return [$fkMethodName, $rkMethodName]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param Relation $foo |
|
92
|
|
|
* @param bool $condition |
|
93
|
|
|
* @return array |
|
94
|
|
|
* @throws InvalidOperationException |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function polyglotKeyMethodBackupNames(Relation $foo, $condition = false) |
|
97
|
|
|
{ |
|
98
|
|
|
// if $condition is falsy, return quickly - don't muck around |
|
99
|
|
|
if (!$condition) { |
|
100
|
|
|
return [null, null]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$fkList = ['getForeignKey', 'getForeignKeyName', 'getQualifiedFarKeyName']; |
|
104
|
|
|
$rkList = ['getOtherKey', 'getQualifiedParentKeyName']; |
|
105
|
|
|
|
|
106
|
|
|
$fkMethodName = null; |
|
107
|
|
|
$rkMethodName = null; |
|
108
|
|
|
|
|
109
|
|
|
if (array_key_exists(get_class($foo), static::$methodAlternate)) { |
|
110
|
|
|
$line = static::$methodAlternate[get_class($foo)]; |
|
111
|
|
|
$fkMethodName = $line['fk']; |
|
112
|
|
|
$rkMethodName = $line['rk']; |
|
113
|
|
|
} else { |
|
114
|
|
|
$methodList = $this->getRelationClassMethods($foo); |
|
115
|
|
|
$fkCombo = array_values(array_intersect($fkList, $methodList)); |
|
116
|
|
|
if (!(1 <= count($fkCombo))) { |
|
117
|
|
|
$msg = 'Expected at least 1 element in foreign-key list, got ' . count($fkCombo); |
|
118
|
|
|
throw new InvalidOperationException($msg); |
|
119
|
|
|
} |
|
120
|
|
|
$fkMethodName = $fkCombo[0]; |
|
121
|
|
|
$rkCombo = array_values(array_intersect($rkList, $methodList)); |
|
122
|
|
|
if (!(1 <= count($rkCombo))) { |
|
123
|
|
|
$msg = 'Expected at least 1 element in related-key list, got ' . count($rkCombo); |
|
124
|
|
|
throw new InvalidOperationException($msg); |
|
125
|
|
|
} |
|
126
|
|
|
$rkMethodName = $rkCombo[0]; |
|
127
|
|
|
$line = ['fk' => $fkMethodName, 'rk' => $rkMethodName]; |
|
128
|
|
|
static::$methodAlternate[get_class($foo)] = $line; |
|
129
|
|
|
} |
|
130
|
|
|
return [$fkMethodName, $rkMethodName]; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
protected function polyglotThroughKeyMethodNames(HasManyThrough $foo) |
|
134
|
|
|
{ |
|
135
|
|
|
$thruList = ['getThroughKey', 'getQualifiedFirstKeyName']; |
|
136
|
|
|
|
|
137
|
|
|
$methodList = $this->getRelationClassMethods($foo); |
|
138
|
|
|
$thruCombo = array_values(array_intersect($thruList, $methodList)); |
|
139
|
|
|
return $thruCombo[0]; |
|
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
|
|
|
/** |
|
161
|
|
|
* @param Relation $rel |
|
162
|
|
|
* @return array |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function getRelationClassMethods(Relation $rel) |
|
165
|
|
|
{ |
|
166
|
|
|
$methods = get_class_methods($rel); |
|
167
|
|
|
|
|
168
|
|
|
return $methods; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
protected function resetKeyMethod() |
|
172
|
|
|
{ |
|
173
|
|
|
static::$methodPrimary = []; |
|
174
|
|
|
static::$methodAlternate = []; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|