|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations; |
|
4
|
|
|
|
|
5
|
|
|
abstract class AssociationStubBase |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var AssociationStubRelationType |
|
9
|
|
|
*/ |
|
10
|
|
|
protected $multiplicity; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Foreign key field of this end of relation. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string|null |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $keyField; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* A list of fields to Traverse between Keyfield and foreignField. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string[] |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $throughFieldChain; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Foreign key field of other end of relation. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string|null |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $foreignField; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string|null |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $relationName; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Target type this relation points to, if known. Is null for known-side polymorphic relations. |
|
40
|
|
|
* |
|
41
|
|
|
* @var string|null |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $targType; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Base type this relation is attached to. |
|
47
|
|
|
* |
|
48
|
|
|
* @var string|null |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $baseType; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getRelationName() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->relationName; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $relationName |
|
62
|
|
|
*/ |
|
63
|
|
|
public function setRelationName($relationName) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->relationName = $this->checkStringInput($relationName) ? $relationName : $this->relationName; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return AssociationStubRelationType |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getMultiplicity() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->multiplicity; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param AssociationStubRelationType $multiplicity |
|
78
|
|
|
*/ |
|
79
|
|
|
public function setMultiplicity(AssociationStubRelationType $multiplicity) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->multiplicity = $multiplicity; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getKeyField() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->keyField; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $keyField |
|
94
|
|
|
*/ |
|
95
|
|
|
public function setKeyField($keyField) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->keyField = $this->checkStringInput($keyField) ? $keyField : $this->keyField; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function isCompatible(AssociationStubBase $otherStub) |
|
101
|
|
|
{ |
|
102
|
|
|
if ($this->morphicType() != $otherStub->morphicType()) { |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (!$this->isOk()) { |
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
if (!$otherStub->isOk()) { |
|
110
|
|
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
$thisMult = $this->getMultiplicity(); |
|
113
|
|
|
$thatMult = $otherStub->getMultiplicity(); |
|
114
|
|
|
return (AssociationStubRelationType::MANY() == $thisMult |
|
115
|
|
|
|| $thisMult != $thatMult); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Is this AssociationStub sane? |
|
120
|
|
|
*/ |
|
121
|
|
|
public function isOk() |
|
122
|
|
|
{ |
|
123
|
|
|
if (null === $this->multiplicity) { |
|
124
|
|
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
if (null === $this->relationName) { |
|
127
|
|
|
return false; |
|
128
|
|
|
} |
|
129
|
|
|
if (null === $this->keyField) { |
|
130
|
|
|
return false; |
|
131
|
|
|
} |
|
132
|
|
|
if (null === $this->baseType) { |
|
133
|
|
|
return false; |
|
134
|
|
|
} |
|
135
|
|
|
$targType = $this->targType; |
|
136
|
|
|
if ($this instanceof AssociationStubMonomorphic && null === $targType) { |
|
137
|
|
|
return false; |
|
138
|
|
|
} |
|
139
|
|
|
$foreignField = $this->foreignField; |
|
140
|
|
|
if (null !== $targType) { |
|
141
|
|
|
if (!$this->checkStringInput($targType)) { |
|
142
|
|
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
if (!$this->checkStringInput($foreignField)) { |
|
145
|
|
|
return false; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
return (null === $targType) === (null === $foreignField); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getTargType() |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->targType; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param string $targType |
|
161
|
|
|
*/ |
|
162
|
|
|
public function setTargType($targType) |
|
163
|
|
|
{ |
|
164
|
|
|
$this->targType = $targType; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @return string |
|
169
|
|
|
*/ |
|
170
|
|
|
public function getBaseType() |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->baseType; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param string $baseType |
|
177
|
|
|
*/ |
|
178
|
|
|
public function setBaseType($baseType) |
|
179
|
|
|
{ |
|
180
|
|
|
$this->baseType = $this->checkStringInput($baseType) ? $baseType : $this->baseType; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getForeignField() |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->foreignField; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param string $foreignField |
|
193
|
|
|
*/ |
|
194
|
|
|
public function setForeignField($foreignField) |
|
195
|
|
|
{ |
|
196
|
|
|
$this->foreignField = $foreignField; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @return string[]|null |
|
201
|
|
|
*/ |
|
202
|
|
|
public function getThroughFieldChain(): array |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->throughFieldChain; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @param string[]|null $keyChain |
|
209
|
|
|
*/ |
|
210
|
|
|
public function setThroughFieldChain(?array $keyChain) |
|
211
|
|
|
{ |
|
212
|
|
|
$this->throughFieldChain = $keyChain; |
|
213
|
|
|
} |
|
214
|
|
|
/** |
|
215
|
|
|
* Supply a canonical sort ordering to determine order in associations. |
|
216
|
|
|
* |
|
217
|
|
|
* @param AssociationStubBase $other |
|
218
|
|
|
* |
|
219
|
|
|
* @return int |
|
220
|
|
|
*/ |
|
221
|
|
|
public function compare(AssociationStubBase $other) |
|
222
|
|
|
{ |
|
223
|
|
|
$thisClass = get_class($this); |
|
224
|
|
|
$otherClass = get_class($other); |
|
225
|
|
|
$classComp = strcmp($thisClass, $otherClass); |
|
226
|
|
|
if (0 !== $classComp) { |
|
227
|
|
|
return $classComp / abs($classComp); |
|
228
|
|
|
} |
|
229
|
|
|
$thisBase = $this->getBaseType(); |
|
230
|
|
|
$otherBase = $other->getBaseType(); |
|
231
|
|
|
$baseComp = strcmp($thisBase, $otherBase); |
|
232
|
|
|
if (0 !== $baseComp) { |
|
233
|
|
|
return $baseComp / abs($baseComp); |
|
234
|
|
|
} |
|
235
|
|
|
$thisMethod = $this->getRelationName(); |
|
236
|
|
|
$otherMethod = $other->getRelationName(); |
|
237
|
|
|
$methodComp = strcmp($thisMethod, $otherMethod); |
|
238
|
|
|
return 0 === $methodComp ? 0 : $methodComp / abs($methodComp); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Return what type of stub this is - polymorphic, monomorphic, or something else. |
|
243
|
|
|
* |
|
244
|
|
|
* @return string |
|
245
|
|
|
*/ |
|
246
|
|
|
abstract public function morphicType(); |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @param $input |
|
250
|
|
|
* @return bool |
|
251
|
|
|
*/ |
|
252
|
|
|
private function checkStringInput($input) |
|
253
|
|
|
{ |
|
254
|
|
|
if (null === $input || !is_string($input) || empty($input)) { |
|
255
|
|
|
return false; |
|
256
|
|
|
} |
|
257
|
|
|
return true; |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|