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