| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Http\Resources;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
6 |
use Illuminate\Pagination\AbstractCursorPaginator;
|
|
|
7 |
use Illuminate\Pagination\AbstractPaginator;
|
|
|
8 |
use Illuminate\Support\Collection;
|
|
|
9 |
use Illuminate\Support\Str;
|
|
|
10 |
use LogicException;
|
|
|
11 |
use ReflectionClass;
|
|
|
12 |
use Traversable;
|
|
|
13 |
|
|
|
14 |
trait CollectsResources
|
|
|
15 |
{
|
|
|
16 |
/**
|
|
|
17 |
* Map the given collection resource into its individual resources.
|
|
|
18 |
*
|
|
|
19 |
* @param mixed $resource
|
|
|
20 |
* @return mixed
|
|
|
21 |
*/
|
|
|
22 |
protected function collectResource($resource)
|
|
|
23 |
{
|
|
|
24 |
if ($resource instanceof MissingValue) {
|
|
|
25 |
return $resource;
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
if (is_array($resource)) {
|
|
|
29 |
$resource = new Collection($resource);
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
$collects = $this->collects();
|
|
|
33 |
|
|
|
34 |
$this->collection = $collects && ! $resource->first() instanceof $collects
|
|
|
35 |
? $resource->mapInto($collects)
|
|
|
36 |
: $resource->toBase();
|
|
|
37 |
|
|
|
38 |
return ($resource instanceof AbstractPaginator || $resource instanceof AbstractCursorPaginator)
|
|
|
39 |
? $resource->setCollection($this->collection)
|
|
|
40 |
: $this->collection;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Get the resource that this resource collects.
|
|
|
45 |
*
|
|
|
46 |
* @return string|null
|
|
|
47 |
*/
|
|
|
48 |
protected function collects()
|
|
|
49 |
{
|
|
|
50 |
$collects = null;
|
|
|
51 |
|
|
|
52 |
if ($this->collects) {
|
|
|
53 |
$collects = $this->collects;
|
|
|
54 |
} elseif (str_ends_with(class_basename($this), 'Collection') &&
|
|
|
55 |
(class_exists($class = Str::replaceLast('Collection', '', get_class($this))) ||
|
|
|
56 |
class_exists($class = Str::replaceLast('Collection', 'Resource', get_class($this))))) {
|
|
|
57 |
$collects = $class;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
if (! $collects || is_a($collects, JsonResource::class, true)) {
|
|
|
61 |
return $collects;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
throw new LogicException('Resource collections must collect instances of '.JsonResource::class.'.');
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Get the JSON serialization options that should be applied to the resource response.
|
|
|
69 |
*
|
|
|
70 |
* @return int
|
|
|
71 |
*/
|
|
|
72 |
public function jsonOptions()
|
|
|
73 |
{
|
|
|
74 |
$collects = $this->collects();
|
|
|
75 |
|
|
|
76 |
if (! $collects) {
|
|
|
77 |
return 0;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
return (new ReflectionClass($collects))
|
|
|
81 |
->newInstanceWithoutConstructor()
|
|
|
82 |
->jsonOptions();
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Get an iterator for the resource collection.
|
|
|
87 |
*
|
|
|
88 |
* @return \ArrayIterator
|
|
|
89 |
*/
|
|
|
90 |
public function getIterator(): Traversable
|
|
|
91 |
{
|
|
|
92 |
return $this->collection->getIterator();
|
|
|
93 |
}
|
|
|
94 |
}
|