1 | <?php |
||
20 | trait MakesResources |
||
21 | { |
||
22 | /** |
||
23 | * A list of cached related resources. |
||
24 | * |
||
25 | * @var \League\Fractal\ResourceInterface[] |
||
26 | */ |
||
27 | protected $resources = []; |
||
28 | |||
29 | /** |
||
30 | * Make a resource. |
||
31 | * |
||
32 | * @param mixed $data |
||
33 | * @param \Flugg\Responder\Transformers\Transformer|string|callable|null $transformer |
||
34 | * @param string|null $resourceKey |
||
35 | * @return \League\Fractal\Resource\ResourceInterface |
||
36 | */ |
||
37 | 21 | protected function resource($data = null, $transformer = null, string $resourceKey = null): ResourceInterface |
|
47 | |||
48 | /** |
||
49 | * Include a related resource. |
||
50 | * |
||
51 | * @param string $identifier |
||
52 | * @param mixed $data |
||
53 | * @param array $parameters |
||
54 | * @return \League\Fractal\Resource\ResourceInterface |
||
55 | * @throws \LogicException |
||
56 | */ |
||
57 | 22 | protected function includeResource(string $identifier, $data, array $parameters): ResourceInterface |
|
58 | { |
||
59 | 22 | $transformer = $this->mappedTransformerClass($identifier); |
|
60 | |||
61 | 22 | if (method_exists($this, $method = 'include' . ucfirst(Str::camel($identifier)))) { |
|
62 | 4 | $resource = $this->resource($this->$method($data, collect($parameters)), $transformer, $identifier); |
|
63 | 19 | } elseif ($data instanceof Model) { |
|
64 | 18 | $resource = $this->includeResourceFromModel($data, $identifier, $transformer); |
|
65 | } else { |
||
66 | 1 | throw new LogicException('Relation [' . $identifier . '] not found in [' . get_class($this) . '].'); |
|
67 | } |
||
68 | |||
69 | 21 | return $resource; |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * Include a related resource from a model and cache the resource type for following calls. |
||
74 | * |
||
75 | * @param \Illuminate\Database\Eloquent\Model $model |
||
76 | * @param string $identifier |
||
77 | * @param \Flugg\Responder\Transformers\Transformer|string|callable|null $transformer |
||
78 | * @return \League\Fractal\Resource\ResourceInterface |
||
79 | */ |
||
80 | 18 | protected function includeResourceFromModel(Model $model, string $identifier, $transformer = null): ResourceInterface |
|
92 | |||
93 | /** |
||
94 | * Indicates if the resource should be cached. |
||
95 | * |
||
96 | * @param mixed $data |
||
97 | * @return bool |
||
98 | */ |
||
99 | 18 | protected function shouldCacheResource($data): bool |
|
103 | |||
104 | /** |
||
105 | * Resolve a container using the resolver callback. |
||
106 | * |
||
107 | * @return \Illuminate\Contracts\Container\Container |
||
108 | */ |
||
109 | protected abstract function resolveContainer(): Container; |
||
110 | |||
111 | /** |
||
112 | * Resolve relation data from a model. |
||
113 | * |
||
114 | * @param \Illuminate\Database\Eloquent\Model $model |
||
115 | * @param string $identifier |
||
116 | * @return mixed |
||
117 | */ |
||
118 | protected abstract function resolveRelation(Model $model, string $identifier); |
||
119 | |||
120 | /** |
||
121 | * Get a related transformer class mapped to a relation identifier. |
||
122 | * |
||
123 | * @param string $identifier |
||
124 | * @return string |
||
125 | */ |
||
126 | protected abstract function mappedTransformerClass(string $identifier); |
||
127 | } |