1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flugg\Responder; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\Pivot; |
6
|
|
|
use League\Fractal\Scope; |
7
|
|
|
use League\Fractal\TransformerAbstract; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* An abstract base transformer. Your transformers should extend this class, and this |
11
|
|
|
* class itself extends Fractal's transformer. |
12
|
|
|
* |
13
|
|
|
* @package flugger/laravel-responder |
14
|
|
|
* @author Alexander Tømmerås <[email protected]> |
15
|
|
|
* @license The MIT License |
16
|
|
|
*/ |
17
|
|
|
abstract class Transformer extends TransformerAbstract |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* A list of all available relations. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $relations = ['*']; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get relations set on the transformer. |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public function getRelations():array |
32
|
|
|
{ |
33
|
|
|
$relations = array_unique(array_merge($this->getAvailableIncludes(), $this->relations)); |
34
|
|
|
|
35
|
|
|
return array_filter($relations, function($relation) { |
36
|
|
|
return $relation !== '*'; |
37
|
|
|
}); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set relations on the transformer. |
42
|
|
|
* |
43
|
|
|
* @param array|string $relations |
44
|
|
|
* @return self |
45
|
|
|
*/ |
46
|
|
|
public function setRelations($relations) |
47
|
|
|
{ |
48
|
|
|
$this->setAvailableIncludes(array_merge($this->availableIncludes, (array) $relations)); |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Check if the transformer has whitelisted all relations. |
55
|
|
|
* |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function allRelationsAllowed():bool |
59
|
|
|
{ |
60
|
|
|
return $this->relations == ['*']; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Call method for retrieving a relation. This method overrides Fractal's own |
65
|
|
|
* [callIncludeMethod] method to load relations directly from your models. |
66
|
|
|
* |
67
|
|
|
* @param Scope $scope |
68
|
|
|
* @param string $includeName |
69
|
|
|
* @param mixed $data |
70
|
|
|
* @return \League\Fractal\Resource\ResourceInterface|bool |
71
|
|
|
* @throws \Exception |
72
|
|
|
*/ |
73
|
|
|
protected function callIncludeMethod(Scope $scope, $includeName, $data) |
74
|
|
|
{ |
75
|
|
|
if ($includeName === 'pivot') { |
76
|
|
|
return $this->includePivot($data->$includeName); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$params = $scope->getManager()->getIncludeParams($scope->getIdentifier($includeName)); |
80
|
|
|
|
81
|
|
|
if (method_exists($this, $includeName)) { |
82
|
|
|
return call_user_func([$this, $includeName], $data, $params); |
83
|
|
|
} else { |
84
|
|
|
return app(Responder::class)->transform($data->$includeName)->getResource(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Include pivot table data to the response. |
90
|
|
|
* |
91
|
|
|
* @param Pivot $pivot |
92
|
|
|
* @return \League\Fractal\Resource\ResourceInterface|bool |
93
|
|
|
*/ |
94
|
|
|
protected function includePivot(Pivot $pivot) |
95
|
|
|
{ |
96
|
|
|
if (! method_exists($this, 'transformPivot')) { |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return app(Responder::class)->transform($pivot, function ($pivot) { |
101
|
|
|
return $this->transformPivot($pivot); |
|
|
|
|
102
|
|
|
})->getResource(); |
103
|
|
|
} |
104
|
|
|
} |