1 | <?php |
||
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 | * Get a list of whitelisted relations, including nested relations. |
||
32 | * |
||
33 | * @return array |
||
34 | */ |
||
35 | public function whitelistedRelations(): array |
||
43 | |||
44 | /** |
||
45 | * Get a list of default relations, including nested relations. |
||
46 | * |
||
47 | * @return array |
||
48 | */ |
||
49 | public function defaultRelations(): array |
||
50 | { |
||
51 | 36 | $nestedRelations = $this->getNestedRelations($this->load, function ($transformer) { |
|
52 | 2 | return $transformer->defaultRelations(); |
|
53 | 36 | }); |
|
54 | |||
55 | 36 | return array_merge($this->normalizeRelations($this->load), $nestedRelations); |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * Extract a list of nested relations from the transformers provided in the |
||
60 | * list of relations. |
||
61 | * |
||
62 | * @param array $relations |
||
63 | * @param callable $nestedCallback |
||
64 | * @return array |
||
65 | */ |
||
66 | protected function getNestedRelations(array $relations, callable $nestedCallback): array |
||
80 | |||
81 | /** |
||
82 | * Normalize relations to force a key value structure. |
||
83 | * |
||
84 | * @param array $relations |
||
85 | * @return array |
||
86 | */ |
||
87 | protected function normalizeRelations(array $relations): array |
||
97 | |||
98 | /** |
||
99 | * Normalize relations to force a key value structure. |
||
100 | * |
||
101 | * @param string $relation |
||
102 | * @return \Closure|null |
||
103 | */ |
||
104 | 20 | protected function getQueryConstraint(string $relation) |
|
105 | { |
||
106 | 20 | if (! method_exists($this, $method = 'load' . ucfirst($relation))) { |
|
107 | 20 | return null; |
|
108 | } |
||
109 | |||
110 | 1 | return function ($query) use ($method) { |
|
111 | 1 | return $this->$method($query); |
|
112 | 1 | }; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * Resolve a relationship from a model instance. |
||
117 | * |
||
118 | * @param \Illuminate\Database\Eloquent\Model $model |
||
119 | * @param string $identifier |
||
120 | * @return mixed |
||
121 | */ |
||
122 | 14 | protected function resolveRelation(Model $model, string $identifier) |
|
123 | { |
||
124 | 14 | $relation = $model->$identifier; |
|
125 | |||
126 | 14 | if (method_exists($this, $method = 'filter' . ucfirst($identifier))) { |
|
127 | 1 | return $this->$method($relation); |
|
128 | } |
||
129 | |||
130 | 13 | return $relation; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Get a related transformer class mapped to a relation identifier. |
||
135 | * |
||
136 | * @param string $identifier |
||
137 | * @return string|null |
||
138 | */ |
||
139 | 17 | protected function getRelatedTransformerName(string $identifier) |
|
145 | |||
146 | /** |
||
147 | * Resolve a transformer from a class name string. |
||
148 | * |
||
149 | * @param string $transformer |
||
150 | * @return mixed |
||
151 | */ |
||
152 | protected abstract function resolveTransformer(string $transformer); |
||
153 | } |