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
|
5 |
|
public function getAvailableIncludes() |
24
|
|
|
{ |
25
|
5 |
|
if ($this->relations == ['*']) { |
26
|
1 |
|
return $this->resolveScopedIncludes($this->getCurrentScope()); |
27
|
|
|
} |
28
|
|
|
|
29
|
4 |
|
return array_filter($this->relations, function ($relation) { |
30
|
4 |
|
return $relation != '*'; |
31
|
4 |
|
}); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Overrides Fractal's getter for default includes. |
36
|
|
|
* |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function getDefaultIncludes() |
40
|
|
|
{ |
41
|
4 |
|
return Collection::make($this->load)->map(function ($transformer, $relation) { |
42
|
1 |
|
return is_numeric($relation) ? $transformer : $relation; |
43
|
4 |
|
})->values()->all(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Overrides Fractal's method for including a relation. |
48
|
|
|
* |
49
|
|
|
* @param \League\Fractal\Scope $scope |
50
|
|
|
* @param string $identifier |
51
|
|
|
* @param mixed $data |
52
|
|
|
* @return \League\Fractal\Resource\ResourceInterface |
53
|
|
|
*/ |
54
|
3 |
|
protected function callIncludeMethod(Scope $scope, $identifier, $data) |
55
|
|
|
{ |
56
|
3 |
|
$parameters = $this->resolveScopedParameters($scope, $identifier); |
57
|
|
|
|
58
|
3 |
|
return $this->includeResource($identifier, $data, $parameters); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Resolve scoped includes for the given scope. |
63
|
|
|
* |
64
|
|
|
* @param \League\Fractal\Scope $scope |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
1 |
|
protected function resolveScopedIncludes(Scope $scope): array |
68
|
|
|
{ |
69
|
1 |
|
$level = count($scope->getParentScopes()); |
70
|
1 |
|
$includes = $scope->getManager()->getRequestedIncludes(); |
71
|
|
|
|
72
|
1 |
|
return Collection::make($includes)->map(function ($include) { |
73
|
1 |
|
return explode('.', $include); |
74
|
|
|
})->filter(function ($include) use ($level) { |
75
|
1 |
|
return count($include) > $level; |
76
|
1 |
|
})->pluck($level)->unique()->all(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Resolve scoped parameters for the given scope. |
81
|
|
|
* |
82
|
|
|
* @param \League\Fractal\Scope $scope |
83
|
|
|
* @param string $identifier |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
3 |
|
protected function resolveScopedParameters(Scope $scope, string $identifier): array |
87
|
|
|
{ |
88
|
3 |
|
return iterator_to_array($scope->getManager()->getIncludeParams($scope->getIdentifier($identifier))); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the current scope of the transformer. |
93
|
|
|
* |
94
|
|
|
* @return \League\Fractal\Scope |
95
|
|
|
*/ |
96
|
|
|
public abstract function getCurrentScope(); |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Include a related resource. |
100
|
|
|
* |
101
|
|
|
* @param string $identifier |
102
|
|
|
* @param mixed $data |
103
|
|
|
* @param array $parameters |
104
|
|
|
* @return \League\Fractal\Resource\ResourceInterface |
105
|
|
|
*/ |
106
|
|
|
protected abstract function includeResource(string $identifier, $data, array $parameters): ResourceInterface; |
107
|
|
|
} |