| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Http\Resources;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Support\Arr;
|
|
|
6 |
use Illuminate\Support\Str;
|
|
|
7 |
|
|
|
8 |
trait ConditionallyLoadsAttributes
|
|
|
9 |
{
|
|
|
10 |
/**
|
|
|
11 |
* Filter the given data, removing any optional values.
|
|
|
12 |
*
|
|
|
13 |
* @param array $data
|
|
|
14 |
* @return array
|
|
|
15 |
*/
|
|
|
16 |
protected function filter($data)
|
|
|
17 |
{
|
|
|
18 |
$index = -1;
|
|
|
19 |
|
|
|
20 |
foreach ($data as $key => $value) {
|
|
|
21 |
$index++;
|
|
|
22 |
|
|
|
23 |
if (is_array($value)) {
|
|
|
24 |
$data[$key] = $this->filter($value);
|
|
|
25 |
|
|
|
26 |
continue;
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
if (is_numeric($key) && $value instanceof MergeValue) {
|
|
|
30 |
return $this->mergeData(
|
|
|
31 |
$data, $index, $this->filter($value->data),
|
|
|
32 |
array_values($value->data) === $value->data
|
|
|
33 |
);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
if ($value instanceof self && is_null($value->resource)) {
|
|
|
37 |
$data[$key] = null;
|
|
|
38 |
}
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
return $this->removeMissingValues($data);
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Merge the given data in at the given index.
|
|
|
46 |
*
|
|
|
47 |
* @param array $data
|
|
|
48 |
* @param int $index
|
|
|
49 |
* @param array $merge
|
|
|
50 |
* @param bool $numericKeys
|
|
|
51 |
* @return array
|
|
|
52 |
*/
|
|
|
53 |
protected function mergeData($data, $index, $merge, $numericKeys)
|
|
|
54 |
{
|
|
|
55 |
if ($numericKeys) {
|
|
|
56 |
return $this->removeMissingValues(array_merge(
|
|
|
57 |
array_merge(array_slice($data, 0, $index, true), $merge),
|
|
|
58 |
$this->filter(array_values(array_slice($data, $index + 1, null, true)))
|
|
|
59 |
));
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
return $this->removeMissingValues(array_slice($data, 0, $index, true) +
|
|
|
63 |
$merge +
|
|
|
64 |
$this->filter(array_slice($data, $index + 1, null, true)));
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Remove the missing values from the filtered data.
|
|
|
69 |
*
|
|
|
70 |
* @param array $data
|
|
|
71 |
* @return array
|
|
|
72 |
*/
|
|
|
73 |
protected function removeMissingValues($data)
|
|
|
74 |
{
|
|
|
75 |
$numericKeys = true;
|
|
|
76 |
|
|
|
77 |
foreach ($data as $key => $value) {
|
|
|
78 |
if (($value instanceof PotentiallyMissing && $value->isMissing()) ||
|
|
|
79 |
($value instanceof self &&
|
|
|
80 |
$value->resource instanceof PotentiallyMissing &&
|
|
|
81 |
$value->isMissing())) {
|
|
|
82 |
unset($data[$key]);
|
|
|
83 |
} else {
|
|
|
84 |
$numericKeys = $numericKeys && is_numeric($key);
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
if (property_exists($this, 'preserveKeys') && $this->preserveKeys === true) {
|
|
|
89 |
return $data;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
return $numericKeys ? array_values($data) : $data;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Retrieve a value if the given "condition" is truthy.
|
|
|
97 |
*
|
|
|
98 |
* @param bool $condition
|
|
|
99 |
* @param mixed $value
|
|
|
100 |
* @param mixed $default
|
|
|
101 |
* @return \Illuminate\Http\Resources\MissingValue|mixed
|
|
|
102 |
*/
|
|
|
103 |
protected function when($condition, $value, $default = null)
|
|
|
104 |
{
|
|
|
105 |
if ($condition) {
|
|
|
106 |
return value($value);
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
return func_num_args() === 3 ? value($default) : new MissingValue;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Retrieve a value if the given "condition" is falsy.
|
|
|
114 |
*
|
|
|
115 |
* @param bool $condition
|
|
|
116 |
* @param mixed $value
|
|
|
117 |
* @param mixed $default
|
|
|
118 |
* @return \Illuminate\Http\Resources\MissingValue|mixed
|
|
|
119 |
*/
|
|
|
120 |
public function unless($condition, $value, $default = null)
|
|
|
121 |
{
|
|
|
122 |
$arguments = func_num_args() === 2 ? [$value] : [$value, $default];
|
|
|
123 |
|
|
|
124 |
return $this->when(! $condition, ...$arguments);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Merge a value into the array.
|
|
|
129 |
*
|
|
|
130 |
* @param mixed $value
|
|
|
131 |
* @return \Illuminate\Http\Resources\MergeValue|mixed
|
|
|
132 |
*/
|
|
|
133 |
protected function merge($value)
|
|
|
134 |
{
|
|
|
135 |
return $this->mergeWhen(true, $value);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* Merge a value if the given condition is truthy.
|
|
|
140 |
*
|
|
|
141 |
* @param bool $condition
|
|
|
142 |
* @param mixed $value
|
|
|
143 |
* @return \Illuminate\Http\Resources\MergeValue|mixed
|
|
|
144 |
*/
|
|
|
145 |
protected function mergeWhen($condition, $value)
|
|
|
146 |
{
|
|
|
147 |
return $condition ? new MergeValue(value($value)) : new MissingValue;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Merge a value unless the given condition is truthy.
|
|
|
152 |
*
|
|
|
153 |
* @param bool $condition
|
|
|
154 |
* @param mixed $value
|
|
|
155 |
* @return \Illuminate\Http\Resources\MergeValue|mixed
|
|
|
156 |
*/
|
|
|
157 |
protected function mergeUnless($condition, $value)
|
|
|
158 |
{
|
|
|
159 |
return ! $condition ? new MergeValue(value($value)) : new MissingValue;
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
/**
|
|
|
163 |
* Merge the given attributes.
|
|
|
164 |
*
|
|
|
165 |
* @param array $attributes
|
|
|
166 |
* @return \Illuminate\Http\Resources\MergeValue
|
|
|
167 |
*/
|
|
|
168 |
protected function attributes($attributes)
|
|
|
169 |
{
|
|
|
170 |
return new MergeValue(
|
|
|
171 |
Arr::only($this->resource->toArray(), $attributes)
|
|
|
172 |
);
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
/**
|
|
|
176 |
* Retrieve an attribute if it exists on the resource.
|
|
|
177 |
*
|
|
|
178 |
* @param string $attribute
|
|
|
179 |
* @param mixed $value
|
|
|
180 |
* @param mixed $default
|
|
|
181 |
* @return \Illuminate\Http\Resources\MissingValue|mixed
|
|
|
182 |
*/
|
|
|
183 |
public function whenHas($attribute, $value = null, $default = null)
|
|
|
184 |
{
|
|
|
185 |
if (func_num_args() < 3) {
|
|
|
186 |
$default = new MissingValue;
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
if (! array_key_exists($attribute, $this->resource->getAttributes())) {
|
|
|
190 |
return value($default);
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
return func_num_args() === 1
|
|
|
194 |
? $this->resource->{$attribute}
|
|
|
195 |
: value($value, $this->resource->{$attribute});
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
/**
|
|
|
199 |
* Retrieve a model attribute if it is null.
|
|
|
200 |
*
|
|
|
201 |
* @param mixed $value
|
|
|
202 |
* @param mixed $default
|
|
|
203 |
* @return \Illuminate\Http\Resources\MissingValue|mixed
|
|
|
204 |
*/
|
|
|
205 |
protected function whenNull($value, $default = null)
|
|
|
206 |
{
|
|
|
207 |
$arguments = func_num_args() == 1 ? [$value] : [$value, $default];
|
|
|
208 |
|
|
|
209 |
return $this->when(is_null($value), ...$arguments);
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
/**
|
|
|
213 |
* Retrieve a model attribute if it is not null.
|
|
|
214 |
*
|
|
|
215 |
* @param mixed $value
|
|
|
216 |
* @param mixed $default
|
|
|
217 |
* @return \Illuminate\Http\Resources\MissingValue|mixed
|
|
|
218 |
*/
|
|
|
219 |
protected function whenNotNull($value, $default = null)
|
|
|
220 |
{
|
|
|
221 |
$arguments = func_num_args() == 1 ? [$value] : [$value, $default];
|
|
|
222 |
|
|
|
223 |
return $this->when(! is_null($value), ...$arguments);
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
/**
|
|
|
227 |
* Retrieve an accessor when it has been appended.
|
|
|
228 |
*
|
|
|
229 |
* @param string $attribute
|
|
|
230 |
* @param mixed $value
|
|
|
231 |
* @param mixed $default
|
|
|
232 |
* @return \Illuminate\Http\Resources\MissingValue|mixed
|
|
|
233 |
*/
|
|
|
234 |
protected function whenAppended($attribute, $value = null, $default = null)
|
|
|
235 |
{
|
|
|
236 |
if ($this->resource->hasAppended($attribute)) {
|
|
|
237 |
return func_num_args() >= 2 ? value($value) : $this->resource->$attribute;
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
return func_num_args() === 3 ? value($default) : new MissingValue;
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
/**
|
|
|
244 |
* Retrieve a relationship if it has been loaded.
|
|
|
245 |
*
|
|
|
246 |
* @param string $relationship
|
|
|
247 |
* @param mixed $value
|
|
|
248 |
* @param mixed $default
|
|
|
249 |
* @return \Illuminate\Http\Resources\MissingValue|mixed
|
|
|
250 |
*/
|
|
|
251 |
protected function whenLoaded($relationship, $value = null, $default = null)
|
|
|
252 |
{
|
|
|
253 |
if (func_num_args() < 3) {
|
|
|
254 |
$default = new MissingValue;
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
if (! $this->resource->relationLoaded($relationship)) {
|
|
|
258 |
return value($default);
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
if (func_num_args() === 1) {
|
|
|
262 |
return $this->resource->{$relationship};
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
if ($this->resource->{$relationship} === null) {
|
|
|
266 |
return;
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
return value($value);
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
/**
|
|
|
273 |
* Retrieve a relationship count if it exists.
|
|
|
274 |
*
|
|
|
275 |
* @param string $relationship
|
|
|
276 |
* @param mixed $value
|
|
|
277 |
* @param mixed $default
|
|
|
278 |
* @return \Illuminate\Http\Resources\MissingValue|mixed
|
|
|
279 |
*/
|
|
|
280 |
public function whenCounted($relationship, $value = null, $default = null)
|
|
|
281 |
{
|
|
|
282 |
if (func_num_args() < 3) {
|
|
|
283 |
$default = new MissingValue;
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
$attribute = (string) Str::of($relationship)->snake()->finish('_count');
|
|
|
287 |
|
|
|
288 |
if (! isset($this->resource->getAttributes()[$attribute])) {
|
|
|
289 |
return value($default);
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
if (func_num_args() === 1) {
|
|
|
293 |
return $this->resource->{$attribute};
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
if ($this->resource->{$attribute} === null) {
|
|
|
297 |
return;
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
return value($value, $this->resource->{$attribute});
|
|
|
301 |
}
|
|
|
302 |
|
|
|
303 |
/**
|
|
|
304 |
* Execute a callback if the given pivot table has been loaded.
|
|
|
305 |
*
|
|
|
306 |
* @param string $table
|
|
|
307 |
* @param mixed $value
|
|
|
308 |
* @param mixed $default
|
|
|
309 |
* @return \Illuminate\Http\Resources\MissingValue|mixed
|
|
|
310 |
*/
|
|
|
311 |
protected function whenPivotLoaded($table, $value, $default = null)
|
|
|
312 |
{
|
|
|
313 |
return $this->whenPivotLoadedAs('pivot', ...func_get_args());
|
|
|
314 |
}
|
|
|
315 |
|
|
|
316 |
/**
|
|
|
317 |
* Execute a callback if the given pivot table with a custom accessor has been loaded.
|
|
|
318 |
*
|
|
|
319 |
* @param string $accessor
|
|
|
320 |
* @param string $table
|
|
|
321 |
* @param mixed $value
|
|
|
322 |
* @param mixed $default
|
|
|
323 |
* @return \Illuminate\Http\Resources\MissingValue|mixed
|
|
|
324 |
*/
|
|
|
325 |
protected function whenPivotLoadedAs($accessor, $table, $value, $default = null)
|
|
|
326 |
{
|
|
|
327 |
if (func_num_args() === 3) {
|
|
|
328 |
$default = new MissingValue;
|
|
|
329 |
}
|
|
|
330 |
|
|
|
331 |
return $this->when(
|
|
|
332 |
isset($this->resource->$accessor) &&
|
|
|
333 |
($this->resource->$accessor instanceof $table ||
|
|
|
334 |
$this->resource->$accessor->getTable() === $table),
|
|
|
335 |
...[$value, $default]
|
|
|
336 |
);
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
/**
|
|
|
340 |
* Transform the given value if it is present.
|
|
|
341 |
*
|
|
|
342 |
* @param mixed $value
|
|
|
343 |
* @param callable $callback
|
|
|
344 |
* @param mixed $default
|
|
|
345 |
* @return mixed
|
|
|
346 |
*/
|
|
|
347 |
protected function transform($value, callable $callback, $default = null)
|
|
|
348 |
{
|
|
|
349 |
return transform(
|
|
|
350 |
$value, $callback, func_num_args() === 3 ? $default : new MissingValue
|
|
|
351 |
);
|
|
|
352 |
}
|
|
|
353 |
}
|