|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: arthur |
|
5
|
|
|
* Date: 04.11.18 |
|
6
|
|
|
* Time: 19:59. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Foundation\Abstracts\Transformers; |
|
10
|
|
|
|
|
11
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
12
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Trait IncludesRelations. |
|
16
|
|
|
*/ |
|
17
|
|
|
trait IncludesRelations |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected function parseRequestIncludeParameter() |
|
23
|
|
|
{ |
|
24
|
|
|
$request = request(); |
|
25
|
|
|
if (isset($request->include) && is_string($request->include)) { |
|
26
|
|
|
return explode(',', $request->include); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return []; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
|
|
public function compileRelations() |
|
36
|
|
|
{ |
|
37
|
|
|
$requestedRelations = $this->parseRequestIncludeParameter(); |
|
38
|
|
|
$relations = []; |
|
39
|
|
|
foreach ($requestedRelations as $requestedRelation) { |
|
40
|
|
|
if (isset($this->available[$requestedRelation])) { |
|
41
|
|
|
$relations[$requestedRelation] = $this->available[$requestedRelation]; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
$merge = array_merge($this->include, $relations); |
|
45
|
|
|
|
|
46
|
|
|
return array_unique($merge); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param $relation |
|
51
|
|
|
* |
|
52
|
|
|
* @return $this |
|
53
|
|
|
*/ |
|
54
|
|
|
public function include($relation) |
|
55
|
|
|
{ |
|
56
|
|
|
if (is_array($relation)) { |
|
57
|
|
|
$this->include = array_merge($this->include, $relation); |
|
|
|
|
|
|
58
|
|
|
} else { |
|
59
|
|
|
$this->include[] = $relation; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function includeRelations() |
|
66
|
|
|
{ |
|
67
|
|
|
$relations = []; |
|
68
|
|
|
if ($this->resource instanceof Model) { |
|
69
|
|
|
$relations = $this->compileRelations(); |
|
70
|
|
|
foreach ($relations as $relation => $transformer) { |
|
71
|
|
|
$relationMethodName = 'transform'.ucfirst(strtolower($relation)); |
|
72
|
|
|
if (method_exists($this, $relationMethodName)) { |
|
73
|
|
|
$relations[$relation] = $this->$relationMethodName($this->resource->$relation); |
|
74
|
|
|
} else { |
|
75
|
|
|
if ($this->resource->$relation instanceof Model) { |
|
76
|
|
|
$relations[$relation] = $transformer::resource($this->whenLoaded($relation)); |
|
|
|
|
|
|
77
|
|
|
} elseif ($this->resource->$relation instanceof Collection) { |
|
78
|
|
|
$relations[$relation] = $transformer::collection($this->whenLoaded($relation)); |
|
79
|
|
|
} |
|
80
|
|
|
if ($this->resource->$relation == null) { |
|
81
|
|
|
$relations[$relation] = null; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $relations; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private static function loadRelations($resource, $extraRelations) |
|
91
|
|
|
{ |
|
92
|
|
|
if ($resource instanceof Model || $resource instanceof Collection) { |
|
93
|
|
|
$relations = (array) call_class_function(static::class, 'compileRelations'); |
|
94
|
|
|
$relations = array_merge($relations, $extraRelations); |
|
95
|
|
|
$resource->loadMissing(array_keys($relations)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $resource; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|