|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mpyw\EloquentHasByNonDependentSubquery; |
|
4
|
|
|
|
|
5
|
|
|
use DomainException; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOneOrMany; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany; |
|
11
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
|
12
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
|
13
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class Keys |
|
17
|
|
|
*/ |
|
18
|
|
|
class Keys |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var \Illuminate\Database\Eloquent\Builder |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $query; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $sourceKeyName; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $relatedKeyName; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var null|string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $relatedMorphType; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var null|string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $relatedMorphClass; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Keys constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param Relation $relation |
|
49
|
|
|
*/ |
|
50
|
23 |
|
public function __construct(Relation $relation) |
|
51
|
|
|
{ |
|
52
|
23 |
|
if ($relation instanceof HasOneOrMany) { |
|
53
|
4 |
|
$this->sourceKeyName = $this->ensureString($relation->getQualifiedParentKeyName()); |
|
54
|
4 |
|
$this->relatedKeyName = $this->ensureString($relation->getQualifiedForeignKeyName()); |
|
55
|
|
|
|
|
56
|
4 |
|
if ($relation instanceof MorphOneOrMany) { |
|
57
|
1 |
|
$this->relatedMorphType = $relation->getQualifiedMorphType(); |
|
58
|
1 |
|
$this->relatedMorphClass = $relation->getMorphClass(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
4 |
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
19 |
|
if ($relation instanceof BelongsTo && !$relation instanceof MorphTo) { |
|
65
|
12 |
|
$this->sourceKeyName = $this->ensureString($relation->getQualifiedForeignKeyName()); |
|
66
|
11 |
|
$this->relatedKeyName = $this->ensureString($relation->getQualifiedOwnerKeyName()); |
|
67
|
11 |
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
7 |
|
if ($relation instanceof BelongsToMany) { |
|
71
|
4 |
|
$this->sourceKeyName = $this->ensureString($relation->getQualifiedParentKeyName()); |
|
72
|
4 |
|
$this->relatedKeyName = $this->ensureString($relation->getQualifiedForeignPivotKeyName()); |
|
73
|
|
|
|
|
74
|
4 |
|
if ($relation instanceof MorphToMany) { |
|
75
|
2 |
|
$this->relatedMorphType = "{$relation->getTable()}.{$relation->getMorphType()}"; |
|
76
|
2 |
|
$this->relatedMorphClass = $relation->getMorphClass(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
4 |
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
3 |
|
if ($relation instanceof HasManyThrough) { |
|
83
|
2 |
|
$this->sourceKeyName = $this->ensureString($relation->getQualifiedLocalKeyName()); |
|
84
|
2 |
|
$this->relatedKeyName = $this->ensureString($relation->getQualifiedFirstKeyName()); |
|
85
|
2 |
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
throw new DomainException('Unsupported relation. Currently supported: HasOne, HasMany, BelongsTo, BelongsToMany, HasManyThrough, HasOneThrough, MorphOne, MorphMany and MorphToMany'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Ensure the provided key is single. |
|
93
|
|
|
* |
|
94
|
|
|
* @param mixed $key |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
22 |
|
protected function ensureString($key): string |
|
98
|
|
|
{ |
|
99
|
22 |
|
if (is_array($key)) { |
|
100
|
1 |
|
throw new DomainException('Multi-column relationships are not supported.'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
21 |
|
return (string)$key; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
21 |
|
public function getQualifiedSourceKeyName(): string |
|
110
|
|
|
{ |
|
111
|
21 |
|
return $this->sourceKeyName; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
21 |
|
public function getQualifiedRelatedKeyName(): string |
|
118
|
|
|
{ |
|
119
|
21 |
|
return $this->relatedKeyName; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return null|string |
|
124
|
|
|
*/ |
|
125
|
3 |
|
public function getQualifiedRelatedMorphType(): ?string |
|
126
|
|
|
{ |
|
127
|
3 |
|
return $this->relatedMorphType; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return null|string |
|
132
|
|
|
*/ |
|
133
|
3 |
|
public function getRelatedMorphClass(): ?string |
|
134
|
|
|
{ |
|
135
|
3 |
|
return $this->relatedMorphClass; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
21 |
|
public function needsPolymorphicRelatedConstraints(): bool |
|
142
|
|
|
{ |
|
143
|
21 |
|
return (bool)$this->relatedMorphClass; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|