1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Huntie\JsonApi\Serializers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
6
|
|
|
|
7
|
|
|
class CollectionSerializer extends JsonApiSerializer |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The collection of records to transform. |
11
|
|
|
* |
12
|
|
|
* @var \Illuminate\Support\Collection |
13
|
|
|
*/ |
14
|
|
|
protected $records; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The subset of attributes to return on each record type. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $fields; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The relationships to load and include. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $include; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create a new JSON API collection serializer. |
32
|
|
|
* |
33
|
|
|
* @param \Illuminate\Support\Collection|LengthAwarePaginator $records The collection of records to serialize |
34
|
|
|
* @param array|null $fields Subset of fields to return by record type |
35
|
|
|
* @param array|null $include Relations to include |
36
|
|
|
*/ |
37
|
|
|
public function __construct($records, array $fields = [], array $include = []) |
38
|
|
|
{ |
39
|
|
|
parent::__construct(); |
40
|
|
|
|
41
|
|
|
if ($records instanceof LengthAwarePaginator) { |
42
|
|
|
$this->addPaginationLinks($records); |
43
|
|
|
|
44
|
|
|
$this->records = $records->getCollection(); |
45
|
|
|
} else { |
46
|
|
|
$this->records = $records; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->fields = array_unique($fields); |
50
|
|
|
$this->include = array_unique($include); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Return a collection of JSON API resource objects for the record set. |
55
|
|
|
* |
56
|
|
|
* @return \Illuminate\Support\Collection |
57
|
|
|
*/ |
58
|
|
|
public function toResourceCollection() |
59
|
|
|
{ |
60
|
|
|
return $this->records->map(function ($record) { |
61
|
|
|
return (new ResourceSerializer($record, $this->fields, $this->include))->toResourceObject(); |
62
|
|
|
}); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return primary data for the JSON API document. |
67
|
|
|
* |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
|
|
protected function getPrimaryData() |
71
|
|
|
{ |
72
|
|
|
return $this->toResourceCollection()->toArray(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return any secondary included resource objects. |
77
|
|
|
* |
78
|
|
|
* @throws \Huntie\JsonApi\Exceptions\InvalidRelationPathException |
79
|
|
|
* |
80
|
|
|
* @return \Illuminate\Support\Collection |
81
|
|
|
*/ |
82
|
|
|
public function getIncluded() |
83
|
|
|
{ |
84
|
|
|
$included = collect(); |
85
|
|
|
|
86
|
|
|
foreach ($this->records as $record) { |
87
|
|
|
$included = $included->merge( |
88
|
|
|
(new ResourceSerializer($record, $this->fields, $this->include)) |
89
|
|
|
->getIncluded() |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $included->unique(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Add pagination links and meta information to the main document. |
98
|
|
|
* |
99
|
|
|
* @param LengthAwarePaginator $paginator |
100
|
|
|
*/ |
101
|
|
|
protected function addPaginationLinks($paginator) |
102
|
|
|
{ |
103
|
|
|
$this->addLinks([ |
104
|
|
|
'first' => $paginator->url(1), |
105
|
|
|
'last' => $paginator->url($paginator->lastPage()), |
106
|
|
|
'prev' => $paginator->previousPageUrl(), |
107
|
|
|
'next' => $paginator->nextPageUrl(), |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
$this->addMeta('total', $paginator->total()); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|