1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Transformers\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use League\Fractal\Resource\ResourceInterface; |
7
|
|
|
use League\Fractal\Scope; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A trait to be used by a transformer to override Fractal's transformer methods. |
11
|
|
|
* |
12
|
|
|
* @package flugger/laravel-responder |
13
|
|
|
* @author Alexander Tømmerås <[email protected]> |
14
|
|
|
* @license The MIT License |
15
|
|
|
*/ |
16
|
|
|
trait OverridesFractal |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Overrides Fractal's getter for available includes. |
20
|
|
|
* |
21
|
|
|
* @return array |
22
|
|
|
*/ |
23
|
|
|
public function getAvailableIncludes() |
24
|
|
|
{ |
25
|
|
|
if ($this->allowsAllRelations()) { |
26
|
|
|
return $this->resolveScopedIncludes($this->getCurrentScope()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return $this->getRelations(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Overrides Fractal's getter for default includes. |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
public function getDefaultIncludes() |
38
|
|
|
{ |
39
|
|
|
return $this->getDefaultRelations(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Overrides Fractal's method for including a relation. |
44
|
|
|
* |
45
|
|
|
* @param \League\Fractal\Scope $scope |
46
|
|
|
* @param string $identifier |
47
|
|
|
* @param mixed $data |
48
|
|
|
* @return \League\Fractal\Resource\ResourceInterface |
49
|
|
|
*/ |
50
|
|
|
protected function callIncludeMethod(Scope $scope, $identifier, $data) |
51
|
|
|
{ |
52
|
|
|
$parameters = $this->resolveScopedParameters($scope, $identifier); |
53
|
|
|
|
54
|
|
|
return $this->includeResource($identifier, $data, $parameters); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Resolve scoped includes for the given scope. |
59
|
|
|
* |
60
|
|
|
* @param \League\Fractal\Scope $scope |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
protected function resolveScopedIncludes(Scope $scope): array |
64
|
|
|
{ |
65
|
|
|
$level = count($scope->getParentScopes()); |
66
|
|
|
$includes = $scope->getManager()->getRequestedIncludes(); |
67
|
|
|
|
68
|
|
|
return Collection::make($includes)->map(function ($include) { |
69
|
|
|
return explode('.', $include); |
70
|
|
|
})->filter(function ($include) use ($level) { |
71
|
|
|
return count($include) > $level; |
72
|
|
|
})->pluck($level)->unique()->all(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Resolve scoped parameters for the given scope. |
77
|
|
|
* |
78
|
|
|
* @param \League\Fractal\Scope $scope |
79
|
|
|
* @param string $identifier |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
protected function resolveScopedParameters(Scope $scope, string $identifier): array |
83
|
|
|
{ |
84
|
|
|
return iterator_to_array($scope->getManager()->getIncludeParams($scope->getIdentifier($identifier))); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the current scope of the transformer. |
89
|
|
|
* |
90
|
|
|
* @return \League\Fractal\Scope |
91
|
|
|
*/ |
92
|
|
|
public abstract function getCurrentScope(); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Indicates if all relations are allowed. |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public abstract function allowsAllRelations(): bool; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get a list of whitelisted relations. |
103
|
|
|
* |
104
|
|
|
* @return string[] |
105
|
|
|
*/ |
106
|
|
|
public abstract function getRelations(): array; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get a list of default relations. |
110
|
|
|
* |
111
|
|
|
* @return string[] |
112
|
|
|
*/ |
113
|
|
|
public abstract function getDefaultRelations(): array; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Include a related resource. |
117
|
|
|
* |
118
|
|
|
* @param string $identifier |
119
|
|
|
* @param mixed $data |
120
|
|
|
* @param array $parameters |
121
|
|
|
* @return \League\Fractal\Resource\ResourceInterface |
122
|
|
|
*/ |
123
|
|
|
protected abstract function includeResource(string $identifier, $data, array $parameters): ResourceInterface; |
124
|
|
|
} |