|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Flugg\Responder; |
|
4
|
|
|
|
|
5
|
|
|
use League\Fractal\Scope; |
|
6
|
|
|
use League\Fractal\TransformerAbstract; |
|
7
|
|
|
use Flugg\Responder\Contracts\Transformable; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* An abstract base transformer. All transformers should extend this, and this class |
|
11
|
|
|
* itself extends the Fractal transformer. |
|
12
|
|
|
* |
|
13
|
|
|
* @package 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
|
|
|
* The transformable model associated with the transformer. |
|
21
|
|
|
* |
|
22
|
|
|
* @var Transformable |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $model; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param Transformable|null $model |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct( Transformable $model = null ) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->model = $model; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Get avilable includes. |
|
38
|
|
|
* |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getAvailableIncludes() |
|
42
|
|
|
{ |
|
43
|
|
|
return array_keys( $this->model->getRelations() ); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* This method is fired to loop through available includes, see if any of |
|
48
|
|
|
* them are requested and permitted for this scope. |
|
49
|
|
|
* |
|
50
|
|
|
* @param Scope $scope |
|
51
|
|
|
* @param mixed $data |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
public function processIncludedResources( Scope $scope, $data ) |
|
55
|
|
|
{ |
|
56
|
|
|
$includedData = [ ]; |
|
57
|
|
|
$includes = array_merge( $this->getDefaultIncludes(), $this->getAvailableIncludes() ); |
|
58
|
|
|
|
|
59
|
|
|
foreach ( $includes as $include ) { |
|
60
|
|
|
$includedData = $this->includeResourceIfAvailable( $scope, $data, $includedData, $include ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $includedData === [ ] ? false : $includedData; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Include a resource only if it is available on the method. |
|
68
|
|
|
* |
|
69
|
|
|
* @param Scope $scope |
|
70
|
|
|
* @param mixed $data |
|
71
|
|
|
* @param array $includedData |
|
72
|
|
|
* @param string $include |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function includeResourceIfAvailable( Scope $scope, $data, $includedData, $include ) |
|
76
|
|
|
{ |
|
77
|
|
|
if ( $resource = $this->callIncludeMethod( $scope, $include, $data ) ) { |
|
78
|
|
|
$childScope = $scope->embedChildScope( $include, $resource ); |
|
79
|
|
|
|
|
80
|
|
|
$includedData[ $include ] = $childScope->toArray(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $includedData; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Call Include Method. |
|
88
|
|
|
* |
|
89
|
|
|
* @param Scope $scope |
|
90
|
|
|
* @param string $includeName |
|
91
|
|
|
* @param mixed $data |
|
92
|
|
|
* @return \League\Fractal\Resource\ResourceInterface |
|
93
|
|
|
* @throws \Exception |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function callIncludeMethod( Scope $scope, $includeName, $data ) |
|
96
|
|
|
{ |
|
97
|
|
|
if ( ! $data instanceof Transformable || ! $data->relationLoaded( $includeName ) ) { |
|
98
|
|
|
return false; |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$data = $data->$includeName; |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
return app( Responder::class )->transform( $data ); |
|
104
|
|
|
} |
|
105
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.