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