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