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 = $this->checkMethodNameList($foo, $fkList); |
59
|
|
|
|
60
|
|
|
$rkMethodName = $this->checkMethodNameList($foo, $rkList); |
61
|
|
|
|
62
|
|
|
return [$fkMethodName, $rkMethodName]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Relation $foo |
67
|
|
|
* @param bool $condition |
68
|
|
|
* @throws InvalidOperationException |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
protected function polyglotKeyMethodBackupNames(Relation $foo, $condition = false) |
72
|
|
|
{ |
73
|
|
|
// if $condition is falsy, return quickly - don't muck around |
74
|
|
|
if (!$condition) { |
75
|
|
|
return [null, null]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$fkList = ['getForeignKey', 'getForeignKeyName', 'getQualifiedFarKeyName']; |
79
|
|
|
$rkList = ['getOtherKey', 'getQualifiedParentKeyName']; |
80
|
|
|
|
81
|
|
|
$fkMethodName = $this->checkMethodNameList($foo, $fkList); |
82
|
|
|
|
83
|
|
|
$rkMethodName = $this->checkMethodNameList($foo, $rkList); |
84
|
|
|
return [$fkMethodName, $rkMethodName]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param HasManyThrough $foo |
89
|
|
|
* @return string |
90
|
|
|
* @throws InvalidOperationException |
91
|
|
|
*/ |
92
|
|
|
protected function polyglotThroughKeyMethodNames(HasManyThrough $foo) |
93
|
|
|
{ |
94
|
|
|
$thruList = ['getThroughKey', 'getQualifiedFirstKeyName']; |
95
|
|
|
|
96
|
|
|
return $this->checkMethodNameList($foo, $thruList); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Model $model |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
protected function getModelClassMethods(Model $model) |
104
|
|
|
{ |
105
|
|
|
$methods = get_class_methods($model); |
106
|
|
|
$filter = function ($method) { |
107
|
|
|
return (!method_exists('Illuminate\Database\Eloquent\Model', $method) |
108
|
|
|
&& !method_exists(Mock::class, $method) |
109
|
|
|
&& !method_exists(MetadataTrait::class, $method) |
110
|
|
|
); |
111
|
|
|
}; |
112
|
|
|
$methods = array_filter($methods, $filter); |
113
|
|
|
|
114
|
|
|
return $methods; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param Relation $foo |
119
|
|
|
* @param array $methodList |
120
|
|
|
* @return string |
121
|
|
|
* @throws InvalidOperationException |
122
|
|
|
*/ |
123
|
|
|
protected function checkMethodNameList(Relation $foo, array $methodList) |
124
|
|
|
{ |
125
|
|
|
foreach ($methodList as $methodName) { |
126
|
|
|
if (method_exists($foo, $methodName)) { |
127
|
|
|
return $methodName; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
$msg = 'Expected at least 1 element in related-key list, got 0'; |
131
|
|
|
throw new InvalidOperationException($msg); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|