|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: arthur |
|
5
|
|
|
* Date: 29.10.18 |
|
6
|
|
|
* Time: 09:35. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Foundation\Abstracts\Transformers; |
|
10
|
|
|
|
|
11
|
|
|
use Illuminate\Http\Resources\Json\JsonResource; |
|
12
|
|
|
|
|
13
|
|
|
abstract class Transformer extends JsonResource |
|
14
|
|
|
{ |
|
15
|
|
|
protected $include = []; |
|
16
|
|
|
|
|
17
|
|
|
protected $available = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Resolve the resource to an array. |
|
21
|
|
|
* |
|
22
|
|
|
* @param \Illuminate\Http\Request|null $request |
|
23
|
|
|
* |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
10 |
|
public function resolve($request = null) |
|
27
|
|
|
{ |
|
28
|
10 |
|
$includedRequestRelations = $this->parseRequestIncludeParameter($request); |
|
29
|
10 |
|
return array_merge(parent::resolve($request), $this->filter($this->includeRelations($includedRequestRelations))); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
10 |
|
protected function parseRequestIncludeParameter($request) |
|
33
|
|
|
{ |
|
34
|
10 |
|
if (isset($request->include) && is_string($request->include)) |
|
35
|
1 |
|
return explode(',', $request->include); |
|
36
|
|
|
|
|
37
|
10 |
|
return []; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
10 |
|
protected function compileRelations(array $includedRequestRelations) |
|
41
|
|
|
{ |
|
42
|
10 |
|
return array_unique(array_merge($this->include, array_intersect($this->available, $includedRequestRelations))); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
10 |
|
protected function includeRelations($requestedRelations) |
|
46
|
|
|
{ |
|
47
|
10 |
|
$relations = []; |
|
48
|
10 |
|
foreach ($this->compileRelations($requestedRelations) as $relation) { |
|
49
|
5 |
|
if (is_string($relation) && method_exists($this, 'transform' . ucfirst(strtolower($relation)))) { |
|
50
|
5 |
|
$data = null; |
|
51
|
5 |
|
if ($this->resource !== null) { |
|
52
|
5 |
|
$method = 'transform' . ucfirst(strtolower($relation)); |
|
53
|
5 |
|
$data = $this->$method($this->resource); |
|
54
|
|
|
} |
|
55
|
5 |
|
if ($data instanceof JsonResource) { |
|
56
|
4 |
|
if ($data->resource === null) |
|
57
|
1 |
|
$data = null; |
|
58
|
|
|
else |
|
59
|
3 |
|
$data->jsonSerialize(); |
|
60
|
|
|
} |
|
61
|
5 |
|
$relations[strtolower($relation)] = $data; |
|
62
|
|
|
} else { |
|
63
|
5 |
|
throw new \Exception('invalid relation or not relation_transform_method given in ' . get_short_class_name(static::class)); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
10 |
|
return $relations; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param $relations |
|
72
|
|
|
*/ |
|
73
|
2 |
|
public function include($relations) |
|
74
|
|
|
{ |
|
75
|
2 |
|
if (is_string($relations)) |
|
76
|
2 |
|
$relations = [$relations]; |
|
77
|
2 |
|
$this->include = array_unique(array_merge($this->include, $relations)); |
|
78
|
2 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param $relations |
|
83
|
|
|
*/ |
|
84
|
|
|
public function available($relations) |
|
85
|
|
|
{ |
|
86
|
|
|
if (is_string($relations)) |
|
87
|
|
|
$relations = [$relations]; |
|
88
|
|
|
$this->available = array_unique(array_merge($this->available, $relations)); |
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
10 |
|
public static function resource($model): self |
|
93
|
|
|
{ |
|
94
|
10 |
|
return new static($model); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
6 |
|
public static function collection($resource) |
|
98
|
|
|
{ |
|
99
|
6 |
|
return new TransformerCollection($resource, get_called_class()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
6 |
|
public function serialize() |
|
103
|
|
|
{ |
|
104
|
6 |
|
return json_decode(json_encode($this->jsonSerialize()), true); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|