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
|
22 |
|
protected function parseRequestIncludeParameter() |
23
|
|
|
{ |
24
|
22 |
|
$request = request(); |
25
|
22 |
|
if (isset($request->include) && is_string($request->include)) { |
26
|
6 |
|
return explode(',', $request->include); |
27
|
|
|
} |
28
|
|
|
|
29
|
19 |
|
return []; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
22 |
|
public function compileRelations() |
36
|
|
|
{ |
37
|
22 |
|
$requestedRelations = $this->parseRequestIncludeParameter(); |
38
|
22 |
|
$relations = []; |
39
|
22 |
|
foreach ($requestedRelations as $requestedRelation) { |
40
|
6 |
|
if (isset($this->available[$requestedRelation])) { |
41
|
6 |
|
$relations[$requestedRelation] = $this->available[$requestedRelation]; |
42
|
|
|
} |
43
|
|
|
} |
44
|
22 |
|
$merge = array_merge($this->include, $relations); |
45
|
|
|
|
46
|
22 |
|
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
|
21 |
|
protected function includeRelations() |
66
|
|
|
{ |
67
|
21 |
|
$relations = []; |
68
|
21 |
|
if ($this->resource instanceof Model) { |
69
|
21 |
|
$relations = $this->compileRelations(); |
70
|
21 |
|
foreach ($relations as $relation => $transformer) { |
71
|
8 |
|
$relationMethodName = 'transform'.ucfirst(strtolower($relation)); |
72
|
8 |
|
if (method_exists($this, $relationMethodName)) { |
73
|
7 |
|
$relations[$relation] = $this->$relationMethodName($this->resource->$relation); |
74
|
|
|
} else { |
75
|
6 |
|
if ($this->resource->$relation instanceof Model) { |
76
|
5 |
|
$relations[$relation] = $transformer::resource($this->whenLoaded($relation)); |
|
|
|
|
77
|
1 |
|
} elseif ($this->resource->$relation instanceof Collection) { |
78
|
8 |
|
$relations[$relation] = $transformer::collection($this->whenLoaded($relation)); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
21 |
|
return $relations; |
85
|
|
|
} |
86
|
|
|
|
87
|
22 |
|
private static function loadRelations($resource, $extraRelations) |
88
|
|
|
{ |
89
|
22 |
|
if ($resource instanceof Model || $resource instanceof Collection) { |
90
|
22 |
|
$relations = (array)call_class_function(static::class, 'compileRelations'); |
91
|
22 |
|
$relations = array_merge($relations, $extraRelations); |
92
|
22 |
|
$resource->loadMissing(array_keys($relations)); |
93
|
|
|
} |
94
|
22 |
|
return $resource; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|