1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Huntie\JsonApi\Serializers; |
4
|
|
|
|
5
|
|
|
use Illuminate\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 a collection of JSON API resource objects for each included |
67
|
|
|
* relationship. |
68
|
|
|
* |
69
|
|
|
* @return \Illuminate\Support\Collection |
70
|
|
|
*/ |
71
|
|
|
public function getIncludedRecords() |
72
|
|
|
{ |
73
|
|
|
return $this->records->map(function ($record) { |
74
|
|
|
return (new ResourceSerializer($record, $this->fields, $this->include))->getIncludedRecords(); |
75
|
|
|
})->flatten(1)->unique()->values(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return primary data for the JSON API document. |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
protected function getPrimaryData() |
84
|
|
|
{ |
85
|
|
|
return $this->toResourceCollection()->toArray(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Return any secondary included resource data. |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
protected function getIncludedData() |
94
|
|
|
{ |
95
|
|
|
return $this->getIncludedRecords()->toArray(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Add pagination links and meta information to the main document. |
100
|
|
|
* |
101
|
|
|
* @param LengthAwarePaginator $paginator |
102
|
|
|
*/ |
103
|
|
|
protected function addPaginationLinks($paginator) |
104
|
|
|
{ |
105
|
|
|
$this->addLinks([ |
106
|
|
|
'first' => $paginator->url(1), |
107
|
|
|
'last' => $paginator->url($paginator->lastPage()), |
108
|
|
|
'prev' => $paginator->previousPageUrl(), |
109
|
|
|
'next' => $paginator->nextPageUrl(), |
110
|
|
|
]); |
111
|
|
|
|
112
|
|
|
$this->addMeta('total', $paginator->total()); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|