1
|
|
|
<?php |
2
|
|
|
namespace AlgoWeb\PODataLaravel\Models; |
3
|
|
|
|
4
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
5
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
9
|
|
|
use Illuminate\Support\Facades\App as App; |
10
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
11
|
|
|
use POData\Providers\Metadata\ResourceEntityType; |
12
|
|
|
use POData\Providers\Metadata\ResourceStreamInfo; |
13
|
|
|
use POData\Providers\Metadata\Type\EdmPrimitiveType; |
14
|
|
|
use Illuminate\Database\Eloquent\Model; |
15
|
|
|
|
16
|
|
|
trait MetadataTrait |
17
|
|
|
{ |
18
|
|
|
protected static $relationHooks = []; |
19
|
|
|
protected static $relationCategories = []; |
20
|
|
|
protected static $methodPrimary = []; |
21
|
|
|
protected static $methodAlternate = []; |
22
|
|
|
protected $loadEagerRelations = []; |
23
|
|
|
|
24
|
|
|
/* |
25
|
|
|
* Array to record mapping between doctrine types and OData types |
26
|
|
|
*/ |
27
|
|
|
protected $mapping = [ |
28
|
3 |
|
'integer' => EdmPrimitiveType::INT32, |
29
|
|
|
'string' => EdmPrimitiveType::STRING, |
30
|
3 |
|
'datetime' => EdmPrimitiveType::DATETIME, |
31
|
|
|
'float' => EdmPrimitiveType::SINGLE, |
32
|
|
|
'decimal' => EdmPrimitiveType::DECIMAL, |
33
|
2 |
|
'text' => EdmPrimitiveType::STRING, |
34
|
2 |
|
'boolean' => EdmPrimitiveType::BOOLEAN, |
35
|
|
|
'blob' => 'stream' |
36
|
2 |
|
]; |
37
|
|
|
|
38
|
2 |
|
/* |
39
|
1 |
|
* Retrieve and assemble this model's metadata for OData packaging |
40
|
|
|
*/ |
41
|
|
|
public function metadata() |
42
|
1 |
|
{ |
43
|
1 |
|
assert($this instanceof Model, get_class($this)); |
44
|
1 |
|
|
45
|
|
|
// Break these out separately to enable separate reuse |
46
|
1 |
|
$connect = $this->getConnection(); |
47
|
|
|
$builder = $connect->getSchemaBuilder(); |
48
|
1 |
|
|
49
|
1 |
|
$table = $this->getTable(); |
50
|
1 |
|
|
51
|
|
|
if (!$builder->hasTable($table)) { |
52
|
1 |
|
return []; |
53
|
1 |
|
} |
54
|
1 |
|
|
55
|
|
|
$columns = $builder->getColumnListing($table); |
56
|
1 |
|
$mask = $this->metadataMask(); |
57
|
|
|
$columns = array_intersect($columns, $mask); |
58
|
1 |
|
|
59
|
1 |
|
$tableData = []; |
60
|
1 |
|
|
61
|
1 |
|
$rawFoo = $connect->getDoctrineSchemaManager()->listTableColumns($table); |
62
|
1 |
|
$foo = []; |
63
|
1 |
|
$getters = $this->collectGetters(); |
64
|
1 |
|
|
65
|
|
|
foreach ($rawFoo as $key => $val) { |
66
|
1 |
|
// Work around glitch in Doctrine when reading from MariaDB which added ` characters to root key value |
67
|
|
|
$key = trim($key, '`'); |
68
|
|
|
$foo[$key] = $val; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
foreach ($columns as $column) { |
72
|
|
|
// Doctrine schema manager returns columns with lowercased names |
73
|
4 |
|
$rawColumn = $foo[strtolower($column)]; |
74
|
|
|
$nullable = !($rawColumn->getNotNull()); |
75
|
4 |
|
$fillable = in_array($column, $this->getFillable()); |
76
|
|
|
$rawType = $rawColumn->getType(); |
77
|
4 |
|
$type = $rawType->getName(); |
78
|
4 |
|
$default = $this->$column; |
79
|
4 |
|
$tableData[$column] = ['type' => $type, |
80
|
2 |
|
'nullable' => $nullable, |
81
|
4 |
|
'fillable' => $fillable, |
82
|
1 |
|
'default' => $default |
83
|
1 |
|
]; |
84
|
|
|
} |
85
|
4 |
|
|
86
|
|
|
foreach ($getters as $get) { |
87
|
|
|
if (isset($tableData[$get])) { |
88
|
|
|
continue; |
89
|
|
|
} |
90
|
|
|
$default = $this->$get; |
91
|
5 |
|
$tableData[$get] = ['type' => 'text', 'nullable' => true, 'fillable' => false, 'default' => $default]; |
92
|
|
|
} |
93
|
5 |
|
|
94
|
5 |
|
return $tableData; |
95
|
1 |
|
} |
96
|
|
|
|
97
|
|
|
/* |
98
|
4 |
|
* Return the set of fields that are permitted to be in metadata |
99
|
|
|
* - following same visible-trumps-hidden guideline as Laravel |
100
|
4 |
|
*/ |
101
|
|
|
public function metadataMask() |
102
|
4 |
|
{ |
103
|
4 |
|
$attribs = array_keys($this->getAllAttributes()); |
104
|
4 |
|
|
105
|
4 |
|
$visible = $this->getVisible(); |
106
|
4 |
|
$hidden = $this->getHidden(); |
107
|
4 |
|
if (0 < count($visible)) { |
108
|
|
|
assert(!empty($visible)); |
109
|
4 |
|
$attribs = array_intersect($visible, $attribs); |
110
|
1 |
|
} elseif (0 < count($hidden)) { |
111
|
1 |
|
assert(!empty($hidden)); |
112
|
1 |
|
$attribs = array_diff($attribs, $hidden); |
113
|
1 |
|
} |
114
|
|
|
|
115
|
4 |
|
return $attribs; |
116
|
4 |
|
} |
117
|
|
|
|
118
|
4 |
|
/* |
119
|
|
|
* Get the endpoint name being exposed |
120
|
|
|
* |
121
|
|
|
*/ |
122
|
|
|
public function getEndpointName() |
123
|
|
|
{ |
124
|
|
|
$endpoint = isset($this->endpoint) ? $this->endpoint : null; |
125
|
|
|
|
126
|
|
|
if (!isset($endpoint)) { |
127
|
|
|
$bitter = get_class(); |
128
|
|
|
$name = substr($bitter, strrpos($bitter, '\\')+1); |
129
|
|
|
return strtolower($name); |
130
|
|
|
} |
131
|
|
|
return strtolower($endpoint); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/* |
135
|
|
|
* Assemble this model's OData metadata as xml schema |
136
|
|
|
* |
137
|
|
|
* @return ResourceEntityType |
138
|
|
|
*/ |
139
|
|
|
public function getXmlSchema() |
140
|
|
|
{ |
141
|
|
|
$raw = $this->metadata(); |
142
|
|
|
if ([] == $raw) { |
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$metadata = App::make('metadata'); |
147
|
|
|
|
148
|
|
|
$isKnown = $this->isKnownPolymorphSide(); |
149
|
|
|
$isAbstract = false; |
150
|
|
|
if ($isKnown) { |
151
|
|
|
$baseType = $metadata->resolveResourceType('polyMorphicPlaceholder'); |
152
|
|
|
assert($baseType instanceof ResourceEntityType); |
153
|
|
|
assert($baseType->isAbstract()); |
154
|
|
|
$isAbstract = true; |
155
|
|
|
} else { |
156
|
|
|
$baseType = null; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$reflec = new \ReflectionClass(get_class($this)); |
160
|
|
|
$complex = $metadata->addEntityType($reflec, $reflec->getShortName(), false, $baseType); |
161
|
|
|
$keyName = $this->getKeyName(); |
162
|
|
|
|
163
|
|
|
if (null !== $keyName && !$isAbstract) { |
164
|
|
|
$metadata->addKeyProperty($complex, $keyName, $this->mapping[$raw[$keyName]['type']]); |
165
|
|
|
} |
166
|
|
|
assert(0 < count($complex->getKeyProperties()), get_class($this) . ' has no effective keys'); |
167
|
|
|
|
168
|
|
|
foreach ($raw as $key => $secret) { |
169
|
|
|
if ($key == $keyName) { |
170
|
|
|
continue; |
171
|
|
|
} |
172
|
|
|
if ($secret['type'] == 'blob') { |
173
|
|
|
$complex->setMediaLinkEntry(true); |
174
|
|
|
$streamInfo = new ResourceStreamInfo($key); |
175
|
3 |
|
assert($complex->isMediaLinkEntry()); |
176
|
|
|
$complex->addNamedStream($streamInfo); |
177
|
3 |
|
continue; |
178
|
|
|
} |
179
|
3 |
|
$nullable = $secret['nullable']; |
180
|
3 |
|
$default = $secret['default']; |
181
|
3 |
|
// tag as isBag? |
182
|
3 |
|
$metadata->addPrimitiveProperty( |
183
|
3 |
|
$complex, |
184
|
3 |
|
$key, |
185
|
3 |
|
$this->mapping[$secret['type']], |
186
|
3 |
|
false, |
187
|
3 |
|
$default, |
188
|
3 |
|
$nullable |
189
|
|
|
); |
190
|
3 |
|
} |
191
|
|
|
|
192
|
3 |
|
return $complex; |
193
|
3 |
|
} |
194
|
3 |
|
|
195
|
3 |
|
/** |
196
|
3 |
|
* @param $entityTypes |
197
|
3 |
|
* @param $resourceSets |
198
|
3 |
|
* @return array |
199
|
3 |
|
*/ |
200
|
3 |
|
public function hookUpRelationships($entityTypes, $resourceSets) |
201
|
3 |
|
{ |
202
|
|
|
assert(is_array($entityTypes) && is_array($resourceSets), 'Both entityTypes and resourceSets must be arrays'); |
203
|
3 |
|
$metadata = App::make('metadata'); |
204
|
3 |
|
$rel = $this->getRelationshipsFromMethods(); |
205
|
3 |
|
$thisClass = get_class($this); |
206
|
3 |
|
$thisInTypes = array_key_exists($thisClass, $entityTypes); |
207
|
3 |
|
$thisInSets = array_key_exists($thisClass, $resourceSets); |
208
|
3 |
|
|
209
|
3 |
|
if (!($thisInSets && $thisInTypes)) { |
210
|
3 |
|
return $rel; |
211
|
|
|
} |
212
|
3 |
|
|
213
|
3 |
|
$resourceType = $entityTypes[$thisClass]; |
214
|
3 |
|
// if $r is in $combined keys, then its in keyspaces of both $entityTypes and $resourceSets |
215
|
|
|
$combinedKeys = array_intersect(array_keys($entityTypes), array_keys($resourceSets)); |
216
|
3 |
View Code Duplication |
foreach ($rel['HasOne'] as $n => $r) { |
217
|
3 |
|
$r = trim($r, '\\'); |
218
|
3 |
|
if (in_array($r, $combinedKeys)) { |
219
|
3 |
|
$targResourceSet = $resourceSets[$r]; |
220
|
3 |
|
$metadata->addResourceReferenceProperty($resourceType, $n, $targResourceSet); |
221
|
|
|
} |
222
|
1 |
|
} |
223
|
3 |
View Code Duplication |
foreach ($rel['HasMany'] as $n => $r) { |
224
|
|
|
$r = trim($r, '\\'); |
225
|
1 |
|
if (in_array($r, $combinedKeys)) { |
226
|
|
|
$targResourceSet = $resourceSets[$r]; |
227
|
1 |
|
$metadata->addResourceSetReferenceProperty($resourceType, $n, $targResourceSet); |
228
|
|
|
} |
229
|
1 |
|
} |
230
|
|
|
return $rel; |
231
|
3 |
|
} |
232
|
2 |
|
|
233
|
2 |
|
/** |
234
|
3 |
|
* Get model's relationships |
235
|
3 |
|
* |
236
|
3 |
|
* @return array |
237
|
3 |
|
*/ |
238
|
3 |
|
public function getRelationships() |
239
|
3 |
|
{ |
240
|
3 |
|
if (empty(static::$relationHooks)) { |
241
|
|
|
$hooks = []; |
242
|
|
|
|
243
|
|
|
$rels = $this->getRelationshipsFromMethods(true); |
244
|
|
|
|
245
|
|
|
$this->getRelationshipsUnknownPolyMorph($rels, $hooks); |
246
|
|
|
|
247
|
|
|
$this->getRelationshipsKnownPolyMorph($rels, $hooks); |
248
|
|
|
|
249
|
|
|
$this->getRelationshipsHasOne($rels, $hooks); |
250
|
|
|
|
251
|
|
|
$this->getRelationshipsHasMany($rels, $hooks); |
252
|
|
|
|
253
|
|
|
static::$relationHooks = $hooks; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return static::$relationHooks; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
protected function getAllAttributes() |
260
|
|
|
{ |
261
|
|
|
// Adapted from http://stackoverflow.com/a/33514981 |
262
|
|
|
// $columns = $this->getFillable(); |
263
|
|
|
// Another option is to get all columns for the table like so: |
264
|
|
|
$builder = $this->getConnection()->getSchemaBuilder(); |
265
|
|
|
$columns = $builder->getColumnListing($this->getTable()); |
266
|
|
|
// but it's safer to just get the fillable fields |
267
|
|
|
|
268
|
|
|
$attributes = $this->getAttributes(); |
269
|
|
|
|
270
|
|
|
foreach ($columns as $column) { |
271
|
|
|
if (!array_key_exists($column, $attributes)) { |
272
|
|
|
$attributes[$column] = null; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$methods = $this->collectGetters(); |
277
|
|
|
|
278
|
|
|
foreach ($methods as $method) { |
279
|
|
|
$attributes[$method] = null; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $attributes; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param bool $biDir |
287
|
|
|
* @return array |
288
|
|
|
*/ |
289
|
|
|
protected function getRelationshipsFromMethods($biDir = false) |
290
|
|
|
{ |
291
|
|
|
$biDirVal = intval($biDir); |
292
|
|
|
$isCached = isset(static::$relationCategories[$biDirVal]) && !empty(static::$relationCategories[$biDirVal]); |
293
|
|
|
if (!$isCached) { |
294
|
|
|
$model = $this; |
295
|
|
|
$relationships = [ |
296
|
|
|
'HasOne' => [], |
297
|
|
|
'UnknownPolyMorphSide' => [], |
298
|
|
|
'HasMany' => [], |
299
|
|
|
'KnownPolyMorphSide' => [] |
300
|
|
|
]; |
301
|
|
|
$methods = get_class_methods($model); |
302
|
|
|
if (!empty($methods)) { |
303
|
|
|
foreach ($methods as $method) { |
304
|
|
|
if (!method_exists('Illuminate\Database\Eloquent\Model', $method) |
305
|
|
|
) { |
306
|
|
|
//Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php |
307
|
|
|
$reflection = new \ReflectionMethod($model, $method); |
308
|
|
|
|
309
|
|
|
$file = new \SplFileObject($reflection->getFileName()); |
310
|
|
|
$file->seek($reflection->getStartLine() - 1); |
311
|
|
|
$code = ''; |
312
|
|
|
while ($file->key() < $reflection->getEndLine()) { |
313
|
|
|
$code .= $file->current(); |
314
|
|
|
$file->next(); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
$code = trim(preg_replace('/\s\s+/', '', $code)); |
318
|
|
|
assert( |
319
|
|
|
false !== stripos($code, 'function'), |
320
|
|
|
'Function definition must have keyword \'function\'' |
321
|
|
|
); |
322
|
|
|
$begin = strpos($code, 'function('); |
323
|
|
|
$code = substr($code, $begin, strrpos($code, '}') - $begin + 1); |
324
|
|
|
$lastCode = $code[strlen($code) - 1]; |
325
|
|
|
assert('}' == $lastCode, 'Final character of function definition must be closing brace'); |
326
|
|
|
foreach ([ |
327
|
|
|
'hasMany', |
328
|
|
|
'hasManyThrough', |
329
|
|
|
'belongsToMany', |
330
|
|
|
'hasOne', |
331
|
|
|
'belongsTo', |
332
|
|
|
'morphOne', |
333
|
|
|
'morphTo', |
334
|
|
|
'morphMany', |
335
|
|
|
'morphToMany', |
336
|
|
|
'morphedByMany' |
337
|
|
|
] as $relation) { |
338
|
|
|
$search = '$this->' . $relation . '('; |
339
|
|
|
if ($pos = stripos($code, $search)) { |
340
|
|
|
//Resolve the relation's model to a Relation object. |
341
|
|
|
$relationObj = $model->$method(); |
342
|
|
|
if ($relationObj instanceof Relation) { |
343
|
|
|
$relObject = $relationObj->getRelated(); |
344
|
|
|
$relatedModel = '\\' . get_class($relObject); |
345
|
|
|
if (in_array(MetadataTrait::class, class_uses($relatedModel))) { |
346
|
|
|
$relations = [ |
347
|
|
|
'hasManyThrough', |
348
|
|
|
'belongsToMany', |
349
|
|
|
'hasMany', |
350
|
|
|
'morphMany', |
351
|
|
|
'morphToMany', |
352
|
|
|
'morphedByMany' |
353
|
|
|
]; |
354
|
|
|
if (in_array($relation, $relations)) { |
355
|
|
|
//Collection or array of models (because Collection is Arrayable) |
356
|
|
|
$relationships['HasMany'][$method] = $biDir ? $relationObj : $relatedModel; |
357
|
|
|
} elseif ('morphTo' === $relation) { |
358
|
|
|
// Model isn't specified because relation is polymorphic |
359
|
|
|
$relationships['UnknownPolyMorphSide'][$method] = |
360
|
|
|
$biDir ? $relationObj : '\Illuminate\Database\Eloquent\Model|\Eloquent'; |
361
|
|
|
} else { |
362
|
|
|
//Single model is returned |
363
|
|
|
$relationships['HasOne'][$method] = $biDir ? $relationObj : $relatedModel; |
364
|
|
|
} |
365
|
|
|
if (in_array($relation, ['morphMany', 'morphOne', 'morphToMany'])) { |
366
|
|
|
$relationships['KnownPolyMorphSide'][$method] = |
367
|
|
|
$biDir ? $relationObj : $relatedModel; |
368
|
|
|
} |
369
|
|
|
if (in_array($relation, ['morphedByMany'])) { |
370
|
|
|
$relationships['UnknownPolyMorphSide'][$method] = |
371
|
|
|
$biDir ? $relationObj : $relatedModel; |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
static::$relationCategories[$biDirVal] = $relationships; |
381
|
|
|
} |
382
|
|
|
return static::$relationCategories[$biDirVal]; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Get the visible attributes for the model. |
387
|
|
|
* |
388
|
|
|
* @return array |
389
|
|
|
*/ |
390
|
|
|
abstract public function getVisible(); |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Get the hidden attributes for the model. |
394
|
|
|
* |
395
|
|
|
* @return array |
396
|
|
|
*/ |
397
|
|
|
abstract public function getHidden(); |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Get the primary key for the model. |
401
|
|
|
* |
402
|
|
|
* @return string |
403
|
|
|
*/ |
404
|
|
|
abstract public function getKeyName(); |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Get the current connection name for the model. |
408
|
|
|
* |
409
|
|
|
* @return string |
410
|
|
|
*/ |
411
|
|
|
abstract public function getConnectionName(); |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* Get the database connection for the model. |
415
|
|
|
* |
416
|
|
|
* @return \Illuminate\Database\Connection |
417
|
|
|
*/ |
418
|
|
|
abstract public function getConnection(); |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Get all of the current attributes on the model. |
422
|
|
|
* |
423
|
|
|
* @return array |
424
|
|
|
*/ |
425
|
|
|
abstract public function getAttributes(); |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Get the table associated with the model. |
429
|
|
|
* |
430
|
|
|
* @return string |
431
|
|
|
*/ |
432
|
|
|
abstract public function getTable(); |
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* Get the fillable attributes for the model. |
436
|
|
|
* |
437
|
|
|
* @return array |
438
|
|
|
*/ |
439
|
|
|
abstract public function getFillable(); |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Dig up all defined getters on the model |
443
|
|
|
* |
444
|
|
|
* @return array |
445
|
|
|
*/ |
446
|
|
|
protected function collectGetters() |
447
|
|
|
{ |
448
|
|
|
$getterz = []; |
449
|
|
|
$methods = get_class_methods($this); |
450
|
|
|
foreach ($methods as $method) { |
451
|
|
|
if (12 < strlen($method) && 'get' == substr($method, 0, 3)) { |
452
|
|
|
if ('Attribute' == substr($method, -9)) { |
453
|
|
|
$getterz[] = $method; |
454
|
|
|
} |
455
|
|
|
} |
456
|
|
|
} |
457
|
|
|
$methods = []; |
458
|
|
|
|
459
|
|
|
foreach ($getterz as $getter) { |
460
|
|
|
$residual = substr($getter, 3); |
461
|
|
|
$residual = substr($residual, 0, -9); |
462
|
|
|
$methods[] = $residual; |
463
|
|
|
} |
464
|
|
|
return $methods; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* @param $foo |
469
|
|
|
* @return array |
470
|
|
|
*/ |
471
|
|
|
private function polyglotKeyMethodNames($foo, $condition = false) |
472
|
|
|
{ |
473
|
|
|
$fkList = ['getQualifiedForeignKeyName', 'getForeignKey']; |
474
|
|
|
$rkList = ['getQualifiedRelatedKeyName', 'getOtherKey', 'getOwnerKey']; |
475
|
|
|
|
476
|
|
|
$fkMethodName = null; |
477
|
|
|
$rkMethodName = null; |
478
|
|
|
if ($condition) { |
479
|
|
|
if (array_key_exists(get_class($foo), static::$methodPrimary)) { |
480
|
|
|
$line = static::$methodPrimary[get_class($foo)]; |
481
|
|
|
$fkMethodName = $line['fk']; |
482
|
|
|
$rkMethodName = $line['rk']; |
483
|
|
|
} else { |
484
|
|
|
$methodList = get_class_methods(get_class($foo)); |
485
|
|
|
$fkMethodName = 'getQualifiedForeignPivotKeyName'; |
486
|
|
|
foreach ($fkList as $option) { |
487
|
|
|
if (in_array($option, $methodList)) { |
488
|
|
|
$fkMethodName = $option; |
489
|
|
|
break; |
490
|
|
|
} |
491
|
|
|
} |
492
|
|
|
assert(in_array($fkMethodName, $methodList), 'Selected method, '.$fkMethodName.', not in method list'); |
493
|
|
|
$rkMethodName = 'getQualifiedRelatedPivotKeyName'; |
494
|
|
|
foreach ($rkList as $option) { |
495
|
|
|
if (in_array($option, $methodList)) { |
496
|
|
|
$rkMethodName = $option; |
497
|
|
|
break; |
498
|
|
|
} |
499
|
|
|
} |
500
|
|
|
assert(in_array($rkMethodName, $methodList), 'Selected method, '.$rkMethodName.', not in method list'); |
501
|
|
|
$line = ['fk' => $fkMethodName, 'rk' => $rkMethodName]; |
502
|
|
|
static::$methodPrimary[get_class($foo)] = $line; |
503
|
|
|
} |
504
|
|
|
} |
505
|
|
|
return [$fkMethodName, $rkMethodName]; |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
private function polyglotKeyMethodBackupNames($foo, $condition = false) |
509
|
|
|
{ |
510
|
|
|
$fkList = ['getForeignKey', 'getForeignKeyName']; |
511
|
|
|
$rkList = ['getOtherKey', 'getQualifiedParentKeyName']; |
512
|
|
|
|
513
|
|
|
$fkMethodName = null; |
514
|
|
|
$rkMethodName = null; |
515
|
|
|
if ($condition) { |
516
|
|
|
if (array_key_exists(get_class($foo), static::$methodAlternate)) { |
517
|
|
|
$line = static::$methodAlternate[get_class($foo)]; |
518
|
|
|
$fkMethodName = $line['fk']; |
519
|
|
|
$rkMethodName = $line['rk']; |
520
|
|
|
} else { |
521
|
|
|
$methodList = get_class_methods(get_class($foo)); |
522
|
|
|
$fkCombo = array_values(array_intersect($fkList, $methodList)); |
523
|
|
|
assert(1 <= count($fkCombo), 'Expected at least 1 element in foreign-key list, got '.count($fkCombo)); |
524
|
|
|
$fkMethodName = $fkCombo[0]; |
525
|
|
|
assert(in_array($fkMethodName, $methodList), 'Selected method, '.$fkMethodName.', not in method list'); |
526
|
|
|
$rkCombo = array_values(array_intersect($rkList, $methodList)); |
527
|
|
|
assert(1 <= count($rkCombo), 'Expected at least 1 element in related-key list, got '.count($rkCombo)); |
528
|
|
|
$rkMethodName = $rkCombo[0]; |
529
|
|
|
assert(in_array($rkMethodName, $methodList), 'Selected method, '.$rkMethodName.', not in method list'); |
530
|
|
|
$line = ['fk' => $fkMethodName, 'rk' => $rkMethodName]; |
531
|
|
|
static::$methodAlternate[get_class($foo)] = $line; |
532
|
|
|
} |
533
|
|
|
} |
534
|
|
|
return [$fkMethodName, $rkMethodName]; |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* @param $hooks |
539
|
|
|
* @param $first |
540
|
|
|
* @param $property |
541
|
|
|
* @param $last |
542
|
|
|
* @param $mult |
543
|
|
|
* @param $targ |
544
|
|
|
* @param string|null $targ |
545
|
|
|
*/ |
546
|
|
|
private function addRelationsHook(&$hooks, $first, $property, $last, $mult, $targ, $type = null) |
547
|
|
|
{ |
548
|
|
|
if (!isset($hooks[$first])) { |
549
|
|
|
$hooks[$first] = []; |
550
|
|
|
} |
551
|
|
|
if (!isset($hooks[$first][$targ])) { |
552
|
|
|
$hooks[$first][$targ] = []; |
553
|
|
|
} |
554
|
|
|
$hooks[$first][$targ][$property] = [ |
555
|
|
|
'property' => $property, |
556
|
|
|
'local' => $last, |
557
|
|
|
'multiplicity' => $mult, |
558
|
|
|
'type' => $type |
559
|
|
|
]; |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* @param $rels |
564
|
|
|
* @param $hooks |
565
|
|
|
*/ |
566
|
|
|
private function getRelationshipsHasMany($rels, &$hooks) |
567
|
|
|
{ |
568
|
|
|
foreach ($rels['HasMany'] as $property => $foo) { |
569
|
|
|
if ($foo instanceof MorphMany || $foo instanceof MorphToMany) { |
570
|
|
|
continue; |
571
|
|
|
} |
572
|
|
|
$isBelong = $foo instanceof BelongsToMany; |
573
|
|
|
$mult = '*'; |
574
|
|
|
$targ = get_class($foo->getRelated()); |
575
|
|
|
|
576
|
|
|
list($fkMethodName, $rkMethodName) = $this->polyglotKeyMethodNames($foo, $isBelong); |
577
|
|
|
list($fkMethodAlternate, $rkMethodAlternate) = $this->polyglotKeyMethodBackupNames($foo, !$isBelong); |
578
|
|
|
|
579
|
|
|
$keyRaw = $isBelong ? $foo->$fkMethodName() : $foo->$fkMethodAlternate(); |
580
|
|
|
$keySegments = explode('.', $keyRaw); |
581
|
|
|
$keyName = $keySegments[count($keySegments) - 1]; |
582
|
|
|
$localRaw = $isBelong ? $foo->$rkMethodName() : $foo->$rkMethodAlternate(); |
583
|
|
|
$localSegments = explode('.', $localRaw); |
584
|
|
|
$localName = $localSegments[count($localSegments) - 1]; |
585
|
|
|
$first = $keyName; |
586
|
|
|
$last = $localName; |
587
|
|
|
$this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ); |
588
|
|
|
} |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
/** |
592
|
|
|
* @param $rels |
593
|
|
|
* @param $hooks |
594
|
|
|
*/ |
595
|
|
|
private function getRelationshipsHasOne($rels, &$hooks) |
596
|
|
|
{ |
597
|
|
|
foreach ($rels['HasOne'] as $property => $foo) { |
598
|
|
|
if ($foo instanceof MorphOne) { |
599
|
|
|
continue; |
600
|
|
|
} |
601
|
|
|
$isBelong = $foo instanceof BelongsTo; |
602
|
|
|
$mult = $isBelong ? '1' : '0..1'; |
603
|
|
|
$targ = get_class($foo->getRelated()); |
604
|
|
|
|
605
|
|
|
list($fkMethodName, $rkMethodName) = $this->polyglotKeyMethodNames($foo, $isBelong); |
606
|
|
|
list($fkMethodAlternate, $rkMethodAlternate) = $this->polyglotKeyMethodBackupNames($foo, !$isBelong); |
607
|
|
|
|
608
|
|
|
$keyName = $isBelong ? $foo->$fkMethodName() : $foo->$fkMethodAlternate(); |
609
|
|
|
$keySegments = explode('.', $keyName); |
610
|
|
|
$keyName = $keySegments[count($keySegments) - 1]; |
611
|
|
|
$localRaw = $isBelong ? $foo->$rkMethodName() : $foo->$rkMethodAlternate(); |
612
|
|
|
$localSegments = explode('.', $localRaw); |
613
|
|
|
$localName = $localSegments[count($localSegments) - 1]; |
614
|
|
|
$first = $isBelong ? $localName : $keyName; |
615
|
|
|
$last = $isBelong ? $keyName : $localName; |
616
|
|
|
$this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ); |
617
|
|
|
} |
618
|
|
|
} |
619
|
|
|
|
620
|
|
|
/** |
621
|
|
|
* @param $rels |
622
|
|
|
* @param $hooks |
623
|
|
|
*/ |
624
|
|
|
private function getRelationshipsKnownPolyMorph($rels, &$hooks) |
625
|
|
|
{ |
626
|
|
|
foreach ($rels['KnownPolyMorphSide'] as $property => $foo) { |
627
|
|
|
$isMany = $foo instanceof MorphToMany; |
628
|
|
|
$targ = get_class($foo->getRelated()); |
629
|
|
|
$mult = $isMany ? '*' : $foo instanceof MorphMany ? '*' : '1'; |
630
|
|
|
$mult = $foo instanceof MorphOne ? '0..1' : $mult; |
631
|
|
|
|
632
|
|
|
list($fkMethodName, $rkMethodName) = $this->polyglotKeyMethodNames($foo, $isMany); |
633
|
|
|
list($fkMethodAlternate, $rkMethodAlternate) = $this->polyglotKeyMethodBackupNames($foo, !$isMany); |
634
|
|
|
|
635
|
|
|
$keyRaw = $isMany ? $foo->$fkMethodName() : $foo->$fkMethodAlternate(); |
636
|
|
|
$keySegments = explode('.', $keyRaw); |
637
|
|
|
$keyName = $keySegments[count($keySegments) - 1]; |
638
|
|
|
$localRaw = $isMany ? $foo->$rkMethodName() : $foo->$rkMethodAlternate(); |
639
|
|
|
$localSegments = explode('.', $localRaw); |
640
|
|
|
$localName = $localSegments[count($localSegments) - 1]; |
641
|
|
|
$first = $isMany ? $keyName : $localName; |
642
|
|
|
$last = $isMany ? $localName : $keyName; |
643
|
|
|
$this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ, 'unknown'); |
644
|
|
|
} |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
/** |
648
|
|
|
* @param $rels |
649
|
|
|
* @param $hooks |
650
|
|
|
*/ |
651
|
|
|
private function getRelationshipsUnknownPolyMorph($rels, &$hooks) |
652
|
|
|
{ |
653
|
|
|
foreach ($rels['UnknownPolyMorphSide'] as $property => $foo) { |
654
|
|
|
$isMany = $foo instanceof MorphToMany; |
655
|
|
|
$targ = get_class($foo->getRelated()); |
656
|
|
|
$mult = $isMany ? '*' : '1'; |
657
|
|
|
|
658
|
|
|
list($fkMethodName, $rkMethodName) = $this->polyglotKeyMethodNames($foo, $isMany); |
659
|
|
|
list($fkMethodAlternate, $rkMethodAlternate) = $this->polyglotKeyMethodBackupNames($foo, !$isMany); |
660
|
|
|
|
661
|
|
|
$keyRaw = $isMany ? $foo->$fkMethodName() : $foo->$fkMethodAlternate(); |
662
|
|
|
$keySegments = explode('.', $keyRaw); |
663
|
|
|
$keyName = $keySegments[count($keySegments) - 1]; |
664
|
|
|
$localRaw = $isMany ? $foo->$rkMethodName() : $foo->$rkMethodAlternate(); |
665
|
|
|
$localSegments = explode('.', $localRaw); |
666
|
|
|
$localName = $localSegments[count($localSegments) - 1]; |
667
|
|
|
|
668
|
|
|
$first = $keyName; |
669
|
|
|
$last = (isset($localName) && '' != $localName) ? $localName : $foo->getRelated()->getKeyName(); |
670
|
|
|
$this->addRelationsHook($hooks, $first, $property, $last, $mult, $targ, 'known'); |
671
|
|
|
} |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
/** |
675
|
|
|
* SUpplemental function to retrieve cast array for Laravel versions that do not supply hasCasts |
676
|
|
|
* |
677
|
|
|
* @return array |
678
|
|
|
*/ |
679
|
|
|
public function retrieveCasts() |
680
|
|
|
{ |
681
|
|
|
return $this->casts; |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
/** |
685
|
|
|
* Return list of relations to be eager-loaded by Laravel query provider |
686
|
|
|
* |
687
|
|
|
* @return array |
688
|
|
|
*/ |
689
|
|
|
public function getEagerLoad() |
690
|
|
|
{ |
691
|
|
|
assert(is_array($this->loadEagerRelations)); |
692
|
|
|
return $this->loadEagerRelations; |
693
|
|
|
} |
694
|
|
|
|
695
|
|
|
/** |
696
|
|
|
* Set list of relations to be eager-loaded |
697
|
|
|
* |
698
|
|
|
* @param array $relations |
699
|
|
|
*/ |
700
|
|
|
public function setEagerLoad(array $relations) |
701
|
|
|
{ |
702
|
|
|
$check = array_map('strval', $relations); |
703
|
|
|
assert($relations == $check, 'All supplied relations must be resolvable to strings'); |
704
|
|
|
$this->loadEagerRelations = $relations; |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
/* |
708
|
|
|
* Is this model the known side of at least one polymorphic relation? |
709
|
|
|
*/ |
710
|
|
|
public function isKnownPolymorphSide() |
711
|
|
|
{ |
712
|
|
|
// isKnownPolymorph needs to be checking KnownPolymorphSide results - if you're checking UnknownPolymorphSide, |
713
|
|
|
// you're turned around |
714
|
|
|
$rels = $this->getRelationshipsFromMethods(); |
715
|
|
|
return !empty($rels['KnownPolyMorphSide']); |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
/* |
719
|
|
|
* Is this model on the unknown side of at least one polymorphic relation? |
720
|
|
|
*/ |
721
|
|
|
public function isUnknownPolymorphSide() |
722
|
|
|
{ |
723
|
|
|
// isUnknownPolymorph needs to be checking UnknownPolymorphSide results - if you're checking KnownPolymorphSide, |
724
|
|
|
// you're turned around |
725
|
|
|
$rels = $this->getRelationshipsFromMethods(); |
726
|
|
|
return !empty($rels['UnknownPolyMorphSide']); |
727
|
|
|
} |
728
|
|
|
|
729
|
|
|
/** |
730
|
|
|
* Extract entity gubbins detail for later downstream use |
731
|
|
|
* |
732
|
|
|
* @return EntityGubbins |
733
|
|
|
*/ |
734
|
|
|
public function extractGubbins() |
735
|
|
|
{ |
736
|
|
|
$multArray = [ |
737
|
|
|
'*' => AssociationStubRelationType::MANY(), |
738
|
|
|
'1' => AssociationStubRelationType::ONE(), |
739
|
|
|
'0..1' => AssociationStubRelationType::NULL_ONE() |
740
|
|
|
]; |
741
|
|
|
|
742
|
|
|
$gubbins = new EntityGubbins(); |
743
|
|
|
$gubbins->setName($this->getEndpointName()); |
744
|
|
|
$gubbins->setClassName(get_class($this)); |
745
|
|
|
|
746
|
|
|
$fields = $this->metadata(); |
747
|
|
|
$entityFields = []; |
748
|
|
|
foreach ($fields as $name => $field) { |
749
|
|
|
$nuField = new EntityField(); |
750
|
|
|
$nuField->setName($name); |
751
|
|
|
$nuField->setIsNullable($field['nullable']); |
752
|
|
|
$nuField->setReadOnly(false); |
753
|
|
|
$nuField->setCreateOnly(false); |
754
|
|
|
$nuField->setDefaultValue($field['default']); |
755
|
|
|
$nuField->setIsKeyField($this->getKeyName() == $name); |
756
|
|
|
$nuField->setFieldType(EntityFieldType::PRIMITIVE()); |
757
|
|
|
$entityFields[$name] = $nuField; |
758
|
|
|
} |
759
|
|
|
$gubbins->setFields($entityFields); |
760
|
|
|
|
761
|
|
|
$rawRels = $this->getRelationships(); |
762
|
|
|
$stubs = []; |
763
|
|
|
foreach ($rawRels as $key => $rel) { |
764
|
|
|
foreach ($rel as $rawName => $deets) { |
765
|
|
|
foreach ($deets as $relName => $relGubbins) { |
766
|
|
|
$gubbinsType = $relGubbins['type']; |
767
|
|
|
$property = $relGubbins['property']; |
768
|
|
|
$isPoly = isset($gubbinsType); |
769
|
|
|
$targType = 'known' != $gubbinsType ? $rawName : null; |
770
|
|
|
$stub = $isPoly ? new AssociationStubPolymorphic() : new AssociationStubMonomorphic(); |
771
|
|
|
$stub->setBaseType(get_class($this)); |
772
|
|
|
$stub->setRelationName($property); |
773
|
|
|
$stub->setKeyField($relGubbins['local']); |
774
|
|
|
$stub->setForeignField($targType ? $key : null); |
775
|
|
|
$stub->setMultiplicity($multArray[$relGubbins['multiplicity']]); |
776
|
|
|
$stub->setTargType($targType); |
777
|
|
|
assert($stub->isOk()); |
778
|
|
|
$stubs[$property] = $stub; |
779
|
|
|
} |
780
|
|
|
} |
781
|
|
|
} |
782
|
|
|
|
783
|
|
|
$gubbins->setStubs($stubs); |
784
|
|
|
|
785
|
|
|
return $gubbins; |
786
|
|
|
} |
787
|
|
|
} |
788
|
|
|
|