1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Transformers\Concerns; |
4
|
|
|
|
5
|
|
|
use Flugg\Responder\Contracts\Resources\ResourceFactory; |
6
|
|
|
use Illuminate\Contracts\Container\Container; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use League\Fractal\Resource\ResourceInterface; |
9
|
|
|
use LogicException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A trait to be used by a transformer to make related resources. |
13
|
|
|
* |
14
|
|
|
* @package flugger/laravel-responder |
15
|
|
|
* @author Alexander Tømmerås <[email protected]> |
16
|
|
|
* @license The MIT License |
17
|
|
|
*/ |
18
|
|
|
trait MakesResources |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* A list of cached related resources. |
22
|
|
|
* |
23
|
|
|
* @var \League\Fractal\ResourceInterface[] |
24
|
|
|
*/ |
25
|
|
|
protected $resources = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Make a resource. |
29
|
|
|
* |
30
|
|
|
* @param null $data |
31
|
|
|
* @param \Flugg\Responder\Transformers\Transformer|string|callable|null $transformer |
32
|
|
|
* @param string|null $resourceKey |
33
|
|
|
* @return \League\Fractal\Resource\ResourceInterface |
34
|
|
|
*/ |
35
|
2 |
|
protected function resource($data = null, $transformer = null, string $resourceKey = null): ResourceInterface |
36
|
|
|
{ |
37
|
2 |
|
$resourceFactory = $this->resolveContainer()->make(ResourceFactory::class); |
38
|
|
|
|
39
|
2 |
|
return $resourceFactory->make($data, $transformer, $resourceKey); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Include a related resource. |
44
|
|
|
* |
45
|
|
|
* @param string $identifier |
46
|
|
|
* @param mixed $data |
47
|
|
|
* @param array $parameters |
48
|
|
|
* @return \League\Fractal\Resource\ResourceInterface |
49
|
|
|
* @throws \LogicException |
50
|
|
|
*/ |
51
|
3 |
|
protected function includeResource(string $identifier, $data, array $parameters): ResourceInterface |
52
|
|
|
{ |
53
|
3 |
|
if (method_exists($this, $method = 'include' . ucfirst($identifier))) { |
54
|
2 |
|
$resource = $this->$method($data, $parameters); |
55
|
|
|
} elseif ($data instanceof Model) { |
56
|
|
|
$resource = $this->includeResourceFromModel($data, $identifier); |
57
|
|
|
} else { |
58
|
1 |
|
throw new LogicException('Relation [' . $identifier . '] not found in [' . get_class($this) . '].'); |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
return $resource; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Include a related resource from a model. |
66
|
|
|
* |
67
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
68
|
|
|
* @param string $identifier |
69
|
|
|
* @return \League\Fractal\Resource\ResourceInterface |
70
|
|
|
*/ |
71
|
|
|
protected function includeResourceFromModel(Model $model, string $identifier): ResourceInterface |
72
|
|
|
{ |
73
|
|
|
$data = $this->resolveRelation($model, $identifier); |
74
|
|
|
|
75
|
|
|
if (! count($data)) { |
76
|
|
|
return $this->resource($data, null, $identifier); |
77
|
|
|
} elseif (key_exists($identifier, $this->resources)) { |
78
|
|
|
return $this->resources[$identifier]->setData($data); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->resources[$identifier] = $this->resource($data, null, $identifier); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Resolve a container using the resolver callback. |
86
|
|
|
* |
87
|
|
|
* @return \Illuminate\Contracts\Container\Container |
88
|
|
|
*/ |
89
|
|
|
protected abstract function resolveContainer(): Container; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Resolve relation data from a model. |
93
|
|
|
* |
94
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
95
|
|
|
* @param string $identifier |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
|
|
protected abstract function resolveRelation(Model $model, string $identifier); |
99
|
|
|
} |