|
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
|
|
|
protected function polyglotFkKey(Relation $rel) |
|
22
|
|
|
{ |
|
23
|
|
|
switch (true) { |
|
24
|
|
|
case $rel instanceof BelongsTo: |
|
25
|
|
|
$key = $rel->{$this->checkMethodNameList($rel, ['getForeignKeyName', 'getForeignKey'])}(); |
|
26
|
|
|
break; |
|
27
|
|
|
case $rel instanceof BelongsToMany: |
|
28
|
|
|
$key = $rel->getForeignPivotKeyName(); |
|
29
|
|
|
break; |
|
30
|
|
|
case $rel instanceof HasOneOrMany: |
|
31
|
|
|
$key = $rel->getForeignKeyName(); |
|
32
|
|
|
break; |
|
33
|
|
|
case $rel instanceof HasManyThrough: |
|
34
|
|
|
$key = $rel->getQualifiedFarKeyName(); |
|
35
|
|
|
break; |
|
36
|
|
|
default: |
|
37
|
|
|
$msg = sprintf('Unknown Relationship Type %s', get_class($rel)); |
|
38
|
|
|
throw new InvalidOperationException($msg); |
|
39
|
|
|
} |
|
40
|
|
|
$segments = explode('.', $key); |
|
41
|
|
|
return end($segments); |
|
42
|
|
|
} |
|
43
|
|
|
protected function polyglotRkKey(Relation $rel) |
|
44
|
|
|
{ |
|
45
|
|
|
switch (true) { |
|
46
|
|
|
case $rel instanceof BelongsTo: |
|
47
|
|
|
$key = $rel->{$this->checkMethodNameList($rel, ['getOwnerKey', 'getOwnerKeyName'])}(); |
|
48
|
|
|
break; |
|
49
|
|
|
case $rel instanceof BelongsToMany: |
|
50
|
|
|
$key = $rel->getRelatedPivotKeyName(); |
|
51
|
|
|
break; |
|
52
|
|
|
case $rel instanceof HasOneOrMany: |
|
53
|
|
|
$key = $rel->{$this->checkMethodNameList($rel, ['getLocalKeyName', 'getQualifiedParentKeyName'])}(); |
|
54
|
|
|
break; |
|
55
|
|
|
case $rel instanceof HasManyThrough: |
|
56
|
|
|
$key = $rel->getQualifiedParentKeyName(); |
|
57
|
|
|
break; |
|
58
|
|
|
default: |
|
59
|
|
|
$msg = sprintf('Unknown Relationship Type %s', get_class($rel)); |
|
60
|
|
|
throw new InvalidOperationException($msg); |
|
61
|
|
|
} |
|
62
|
|
|
$segments = explode('.', $key); |
|
63
|
|
|
return end($segments); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function polyglotThroughKey(Relation $rel) |
|
67
|
|
|
{ |
|
68
|
|
|
$key = $rel->{$this->checkMethodNameList($rel, ['getThroughKey', 'getQualifiedFirstKeyName'])}(); |
|
69
|
|
|
$segments = explode('.', $key); |
|
70
|
|
|
return end($segments); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param Model $model |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function getModelClassMethods(Model $model) |
|
78
|
|
|
{ |
|
79
|
|
|
return array_diff( |
|
80
|
|
|
get_class_methods($model), |
|
81
|
|
|
get_class_methods(\Illuminate\Database\Eloquent\Model::class), |
|
82
|
|
|
//TODO: sandi what will happen if Mock is not installed (I.e. Production?) |
|
83
|
|
|
get_class_methods(Mock::class), |
|
84
|
|
|
get_class_methods(MetadataTrait::class) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param Relation $foo |
|
90
|
|
|
* @param array $methodList |
|
91
|
|
|
* @throws InvalidOperationException |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function checkMethodNameList(Relation $foo, array $methodList) |
|
95
|
|
|
{ |
|
96
|
|
|
foreach ($methodList as $methodName) { |
|
97
|
|
|
if (method_exists($foo, $methodName)) { |
|
98
|
|
|
return $methodName; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
$msg = 'Expected at least 1 element in related-key list, got 0 for relation %s'; |
|
102
|
|
|
throw new InvalidOperationException(sprintf($msg, get_class($foo))); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|