1 | <?php |
||
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 (! isset($this->relations)) { |
||
26 | return []; |
||
27 | } elseif ($this->relations == ['*']) { |
||
28 | return $this->resolveScopedIncludes($this->getCurrentScope()); |
||
29 | } |
||
30 | |||
31 | return array_filter($this->relations, function ($relation) { |
||
32 | return $relation != '*'; |
||
33 | }); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Overrides Fractal's getter for default includes. |
||
38 | * |
||
39 | * @return array |
||
40 | */ |
||
41 | public function getDefaultIncludes() |
||
42 | { |
||
43 | return Collection::make($this->load)->map(function ($transformer, $relation) { |
||
44 | return is_numeric($relation) ? $transformer : $relation; |
||
45 | })->all(); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Overrides Fractal's method for including a relation. |
||
50 | * |
||
51 | * @param \League\Fractal\Scope $scope |
||
52 | * @param string $identifier |
||
53 | * @param mixed $data |
||
54 | * @return \League\Fractal\Resource\ResourceInterface |
||
55 | */ |
||
56 | protected function callIncludeMethod(Scope $scope, $identifier, $data) |
||
62 | |||
63 | /** |
||
64 | * Resolve scoped includes for the given scope. |
||
65 | * |
||
66 | * @param \League\Fractal\Scope $scope |
||
67 | * @return array |
||
68 | */ |
||
69 | protected function resolveScopedIncludes(Scope $scope): array |
||
80 | |||
81 | /** |
||
82 | * Resolve scoped parameters for the given scope. |
||
83 | * |
||
84 | * @param \League\Fractal\Scope $scope |
||
85 | * @param string $identifier |
||
86 | * @return array |
||
87 | */ |
||
88 | protected function resolveScopedParameters(Scope $scope, string $identifier): array |
||
92 | |||
93 | /** |
||
94 | * Get the current scope of the transformer. |
||
95 | * |
||
96 | * @return \League\Fractal\Scope |
||
97 | */ |
||
98 | public abstract function getCurrentScope(); |
||
99 | |||
100 | /** |
||
101 | * Include a related resource. |
||
102 | * |
||
103 | * @param string $identifier |
||
104 | * @param mixed $data |
||
105 | * @param array $parameters |
||
106 | * @return \League\Fractal\Resource\ResourceInterface |
||
107 | */ |
||
108 | protected abstract function includeResource(string $identifier, $data, array $parameters): ResourceInterface; |
||
109 | } |