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|LengthAwarePaginator |
13
|
|
|
*/ |
14
|
|
|
protected $records; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The subset of attributes to return for each record. |
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 Collection|LengthAwarePaginator $records The collection of records to serialize |
34
|
|
|
* @param array|null $fields Subset of fields to return |
35
|
|
|
* @param array|null $include Relations to include |
36
|
|
|
*/ |
37
|
|
|
public function __construct($records, array $fields = [], array $include = []) |
38
|
|
|
{ |
39
|
|
|
parent::__construct(); |
40
|
|
|
|
41
|
|
|
$this->records = $records; |
|
|
|
|
42
|
|
|
$this->fields = array_unique($fields); |
43
|
|
|
$this->include = array_unique($include); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Return a collection of JSON API resource objects for the record set. |
48
|
|
|
* |
49
|
|
|
* @return \Illuminate\Support\Collection |
50
|
|
|
*/ |
51
|
|
|
public function toResourceCollection() |
52
|
|
|
{ |
53
|
|
|
if ($records instanceof LengthAwarePaginator) { |
|
|
|
|
54
|
|
|
$this->addPaginationLinks(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->records->map(function ($record) { |
|
|
|
|
58
|
|
|
return (new ResourceSerializer($record, $this->fields, $this->include))->toResourceObject(); |
59
|
|
|
}); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return a collection of JSON API resource objects for each included |
64
|
|
|
* relationship. |
65
|
|
|
* |
66
|
|
|
* @return \Illuminate\Support\Collection |
67
|
|
|
*/ |
68
|
|
|
public function getIncludedRecords() |
69
|
|
|
{ |
70
|
|
|
return $this->records->map(function ($record) { |
|
|
|
|
71
|
|
|
return (new ResourceSerializer($record, $this->fields, $this->include))->getIncludedRecords(); |
72
|
|
|
})->flatten(1); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return primary data for the JSON API document. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
protected function getPrimaryData() |
81
|
|
|
{ |
82
|
|
|
return $this->toResourceCollection()->toArray(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return any secondary included resource data. |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
protected function getIncludedData() |
91
|
|
|
{ |
92
|
|
|
return $this->getIncludedRecords()->toArray(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Add pagination links and meta information to the main document. |
97
|
|
|
*/ |
98
|
|
|
protected function addPaginationLinks() |
99
|
|
|
{ |
100
|
|
|
$this->addLinks([ |
101
|
|
|
'first' => $this->records->url(1), |
|
|
|
|
102
|
|
|
'last' => $this->records->url($this->records->lastPage()), |
|
|
|
|
103
|
|
|
'prev' => $this->records->previousPageUrl(), |
|
|
|
|
104
|
|
|
'next' => $this->records->nextPageUrl(), |
|
|
|
|
105
|
|
|
]); |
106
|
|
|
|
107
|
|
|
$this->addMeta('total', $this->records->total()); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.