1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Transformers\Concerns; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A trait to be used by a transformer to handle relations |
9
|
|
|
* |
10
|
|
|
* @package flugger/laravel-responder |
11
|
|
|
* @author Alexander Tømmerås <[email protected]> |
12
|
|
|
* @license The MIT License |
13
|
|
|
*/ |
14
|
|
|
trait HasRelationships |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* List of available relations. |
18
|
|
|
* |
19
|
|
|
* @var string[] |
20
|
|
|
*/ |
21
|
|
|
protected $relations = ['*']; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A list of autoloaded default relations. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $load = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Indicates if the transformer has whitelisted all relations. |
32
|
|
|
* |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
3 |
|
public function allRelationsAllowed(): bool |
36
|
|
|
{ |
37
|
3 |
|
return $this->relations == ['*']; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get a list of whitelisted relations. |
42
|
|
|
* |
43
|
|
|
* @return string[] |
44
|
|
|
*/ |
45
|
|
|
public function getRelations(): array |
46
|
|
|
{ |
47
|
|
|
return array_filter($this->relations, function ($relation) { |
48
|
|
|
return $relation !== '*'; |
49
|
|
|
}); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get a list of default relations. |
54
|
|
|
* |
55
|
|
|
* @return string[] |
56
|
|
|
*/ |
57
|
|
|
public function getDefaultRelations(): array |
58
|
|
|
{ |
59
|
|
|
return collect($this->load)->keys()->mapWithKeys(function ($relation) { |
60
|
|
|
if (method_exists($this, $method = 'load' . ucfirst($relation))) { |
61
|
|
|
return [$relation => $this->makeEagerLoadCallback($method)]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $relation; |
65
|
|
|
}); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Extract a deep list of default relations, recursively. |
70
|
|
|
* |
71
|
|
|
* @return string[] |
72
|
|
|
*/ |
73
|
|
|
public function extractDefaultRelations(): array |
74
|
|
|
{ |
75
|
|
|
return collect($this->getDefaultRelations())->merge($this->load->map(function ($transformer, $relation) { |
76
|
|
|
return collect($this->resolveTransformer($transformer)->extractDefaultRelations()) |
|
|
|
|
77
|
|
|
->keys() |
78
|
|
|
->map(function ($nestedRelation) use ($relation) { |
79
|
|
|
return "$relation.$nestedRelation"; |
|
|
|
|
80
|
|
|
}); |
81
|
|
|
}))->all(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Extract a deep list of default relations, recursively. |
86
|
|
|
* |
87
|
|
|
* @param string $method |
88
|
|
|
* @return \Closure |
89
|
|
|
*/ |
90
|
|
|
public function makeEagerLoadCallback(string $method): Closure |
91
|
|
|
{ |
92
|
|
|
return function ($query) use ($method) { |
93
|
|
|
return $this->$method($query); |
94
|
|
|
}; |
95
|
|
|
} |
96
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.