1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flugg\Responder; |
4
|
|
|
|
5
|
|
|
use Flugg\Responder\Contracts\Transformable; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\Pivot; |
7
|
|
|
use League\Fractal\Scope; |
8
|
|
|
use League\Fractal\TransformerAbstract; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* An abstract base transformer. Yourr transformers should extend this, and this class |
12
|
|
|
* itself extends Fractal's transformer. |
13
|
|
|
* |
14
|
|
|
* @package Laravel Responder |
15
|
|
|
* @author Alexander Tømmerås <[email protected]> |
16
|
|
|
* @license The MIT License |
17
|
|
|
*/ |
18
|
|
|
abstract class Transformer extends TransformerAbstract |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The transformable model associated with the transformer. |
22
|
|
|
* |
23
|
|
|
* @var Transformable |
24
|
|
|
*/ |
25
|
|
|
protected $model; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Constructor. |
29
|
|
|
* |
30
|
|
|
* @param Transformable|null $model |
31
|
|
|
*/ |
32
|
|
|
public function __construct( Transformable $model = null ) |
33
|
|
|
{ |
34
|
|
|
$this->model = $model; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get avilable includes. |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function getAvailableIncludes() |
43
|
|
|
{ |
44
|
|
|
return array_keys( $this->model->getRelations() ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* This method is fired to loop through available includes, see if any of |
49
|
|
|
* them are requested and permitted for this scope. |
50
|
|
|
* |
51
|
|
|
* @param Scope $scope |
52
|
|
|
* @param mixed $data |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function processIncludedResources( Scope $scope, $data ) |
56
|
|
|
{ |
57
|
|
|
$includedData = [ ]; |
58
|
|
|
$includes = array_merge( $this->getDefaultIncludes(), $this->getAvailableIncludes() ); |
59
|
|
|
|
60
|
|
|
foreach ( $includes as $include ) { |
61
|
|
|
$includedData = $this->includeResourceIfAvailable( $scope, $data, $includedData, $include ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $includedData === [ ] ? false : $includedData; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Include a resource only if it is available on the method. |
69
|
|
|
* |
70
|
|
|
* @param Scope $scope |
71
|
|
|
* @param mixed $data |
72
|
|
|
* @param array $includedData |
73
|
|
|
* @param string $include |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
protected function includeResourceIfAvailable( Scope $scope, $data, $includedData, $include ) |
77
|
|
|
{ |
78
|
|
|
if ( $resource = $this->callIncludeMethod( $scope, $include, $data ) ) { |
79
|
|
|
$childScope = $scope->embedChildScope( $include, $resource ); |
|
|
|
|
80
|
|
|
|
81
|
|
|
$includedData[ $include ] = $childScope->toArray(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $includedData; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Call Include Method. |
89
|
|
|
* |
90
|
|
|
* @param Scope $scope |
91
|
|
|
* @param string $includeName |
92
|
|
|
* @param mixed $data |
93
|
|
|
* @return \League\Fractal\Resource\ResourceInterface|bool |
94
|
|
|
* @throws \Exception |
95
|
|
|
*/ |
96
|
|
|
protected function callIncludeMethod( Scope $scope, $includeName, $data ) |
97
|
|
|
{ |
98
|
|
|
if ( ! $data instanceof Transformable || ! $data->relationLoaded( $includeName ) ) { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$relation = $data->$includeName; |
103
|
|
|
|
104
|
|
|
if ( $relation instanceof Pivot ) { |
105
|
|
|
return $this->includePivot( $relation ); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return app( 'responder.success' )->transform( $relation ); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Include pivot table data to the response. |
113
|
|
|
* |
114
|
|
|
* @param Pivot $pivot |
115
|
|
|
* @return \League\Fractal\Resource\ResourceInterface|bool |
116
|
|
|
*/ |
117
|
|
|
protected function includePivot( Pivot $pivot ) |
118
|
|
|
{ |
119
|
|
|
if ( method_exists( $this, 'transformPivot' ) ) { |
120
|
|
|
$data = $this->transformPivot( $pivot ); |
|
|
|
|
121
|
|
|
|
122
|
|
|
return app( 'responder.success' )->transform( $pivot, function () use ( $data ) { |
123
|
|
|
return $data; |
124
|
|
|
} ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.