|
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\BelongsTo; |
|
12
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
13
|
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough; |
|
14
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOneOrMany; |
|
15
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
|
16
|
|
|
use Mockery\Mock; |
|
17
|
|
|
use POData\Common\InvalidOperationException; |
|
18
|
|
|
|
|
19
|
|
|
trait MetadataKeyMethodNamesTrait |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @param Relation $foo |
|
23
|
|
|
* @throws InvalidOperationException |
|
24
|
|
|
* @return array|null |
|
25
|
|
|
*/ |
|
26
|
|
|
protected function getRelationsHasManyKeyNames(Relation $foo) |
|
27
|
|
|
{ |
|
28
|
|
|
$thruName = $foo instanceof HasManyThrough ? |
|
29
|
|
|
$this->polyglotThroughKeyMethodNames($foo) : |
|
30
|
|
|
null; |
|
31
|
|
|
list($fkMethodName, $rkMethodName) = $this->polyglotKeyMethodNames($foo); |
|
32
|
|
|
return [$thruName, $fkMethodName, $rkMethodName]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Relation $foo |
|
37
|
|
|
* @param mixed $condition |
|
38
|
|
|
* |
|
39
|
|
|
* @throws InvalidOperationException |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function polyglotKeyMethodNames(Relation $foo) |
|
43
|
|
|
{ |
|
44
|
|
|
if ($foo instanceof BelongsTo) { |
|
45
|
|
|
// getForeignKey for laravel 5.5 |
|
46
|
|
|
$fkList = ['getForeignKeyName', 'getForeignKey']; |
|
47
|
|
|
// getOwnerKeyName for laravel 5.5 |
|
48
|
|
|
$rkList = ['getOwnerKey', 'getOwnerKeyName']; |
|
49
|
|
|
}elseif ($foo instanceof BelongsToMany) { |
|
50
|
|
|
$fkList = ['getForeignPivotKeyName']; |
|
51
|
|
|
$rkList = ['getRelatedPivotKeyName']; |
|
52
|
|
|
}elseif($foo instanceof HasOneOrMany){ |
|
53
|
|
|
$fkList = ['getForeignKeyName']; |
|
54
|
|
|
$rkList = ['getLocalKeyName', 'getQualifiedParentKeyName']; |
|
55
|
|
|
}elseif($foo instanceof HasManyThrough) { |
|
56
|
|
|
$fkList = ['getQualifiedFarKeyName']; |
|
57
|
|
|
$rkList = ['getQualifiedParentKeyName']; |
|
58
|
|
|
}else{ |
|
59
|
|
|
$msg = sprintf('Unknown Relationship Type %s', get_class($foo)); |
|
60
|
|
|
throw new InvalidOperationException($msg); |
|
61
|
|
|
} |
|
62
|
|
|
$fkMethodName = $this->checkMethodNameList($foo, $fkList); |
|
63
|
|
|
|
|
64
|
|
|
$rkMethodName = $this->checkMethodNameList($foo, $rkList); |
|
65
|
|
|
|
|
66
|
|
|
return [$fkMethodName, $rkMethodName]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param HasManyThrough $foo |
|
71
|
|
|
* @throws InvalidOperationException |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function polyglotThroughKeyMethodNames(HasManyThrough $foo) |
|
75
|
|
|
{ |
|
76
|
|
|
$thruList = ['getThroughKey', 'getQualifiedFirstKeyName']; |
|
77
|
|
|
|
|
78
|
|
|
return $this->checkMethodNameList($foo, $thruList); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param Model $model |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function getModelClassMethods(Model $model) |
|
86
|
|
|
{ |
|
87
|
|
|
return array_diff( |
|
88
|
|
|
get_class_methods($model), |
|
89
|
|
|
get_class_methods(\Illuminate\Database\Eloquent\Model::class), |
|
90
|
|
|
//TODO: sandi what will happen if Mock is not installed (I.e. Production?) |
|
91
|
|
|
get_class_methods(Mock::class), |
|
92
|
|
|
get_class_methods(MetadataTrait::class) |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param Relation $foo |
|
98
|
|
|
* @param array $methodList |
|
99
|
|
|
* @throws InvalidOperationException |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function checkMethodNameList(Relation $foo, array $methodList) |
|
103
|
|
|
{ |
|
104
|
|
|
foreach ($methodList as $methodName) { |
|
105
|
|
|
if (method_exists($foo, $methodName)) { |
|
106
|
|
|
return $methodName; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
$msg = 'Expected at least 1 element in related-key list, got 0 for relation %s'; |
|
110
|
|
|
throw new InvalidOperationException(sprintf($msg,get_class($foo))); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|