|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Huntie\JsonApi\Serializers; |
|
4
|
|
|
|
|
5
|
|
|
class JsonApiSerializer |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* The model instance to transform. |
|
9
|
|
|
* |
|
10
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $record; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The subset of record attributes to return. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $fields; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The record relations to include. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $include; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Meta information to include. |
|
30
|
|
|
* |
|
31
|
|
|
* @var \Illuminate\Support\Collection |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $meta; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Resource links to include. |
|
37
|
|
|
* |
|
38
|
|
|
* @var \Illuminate\Support\Collection |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $links; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The loaded records to include. |
|
44
|
|
|
* |
|
45
|
|
|
* @var \Illuminate\Support\Collection |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $included; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Create a new JSON API resource serializer. |
|
51
|
|
|
* |
|
52
|
|
|
* @param Model $record The model instance to serialise |
|
53
|
|
|
* @param array|null $fields Subset of fields to return |
|
54
|
|
|
* @param array|null $include Relations to include |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct($record, array $fields = [], array $include = []) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->record = $record; |
|
|
|
|
|
|
59
|
|
|
$this->fields = array_unique($fields); |
|
60
|
|
|
$this->include = array_unique(array_merge($record->getRelations(), $include)); |
|
61
|
|
|
$this->meta = collect([]); |
|
62
|
|
|
$this->links = collect([]); |
|
63
|
|
|
$this->included = collect([]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Limit which relations can be included. |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $include |
|
70
|
|
|
*/ |
|
71
|
|
|
public function scopeIncludes($include) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->include->intersect($include); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Add meta information to the returned object. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string|array $key |
|
80
|
|
|
* @param string|int|null $value |
|
81
|
|
|
*/ |
|
82
|
|
|
public function addMeta($key, $value = null) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->meta->merge(is_array($key) ? $key : [$key => $value]); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Add one or more links to the returned object. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string|array $key |
|
91
|
|
|
* @param string|int|null $value |
|
92
|
|
|
*/ |
|
93
|
|
|
public function addLinks($key, $value = null) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->links->merge(is_array($key) ? $key : [$key => $value]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Return a JSON API resource identifier object for the primary record. |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
public function toResourceIdentifier() |
|
104
|
|
|
{ |
|
105
|
|
|
return [ |
|
106
|
|
|
'type' => $this->getRecordType(), |
|
107
|
|
|
'id' => $this->record->id, |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Return a JSON API resource object for the primary record. |
|
113
|
|
|
* |
|
114
|
|
|
* @return array |
|
115
|
|
|
*/ |
|
116
|
|
|
public function toResourceObject() |
|
117
|
|
|
{ |
|
118
|
|
|
return array_merge($this->toResourceIdentifier(), [ |
|
119
|
|
|
'attributes' => $this->transformRecordAttributes(), |
|
120
|
|
|
]); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Serialise complete JSON API document to an array. |
|
125
|
|
|
* |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
|
|
public function serialiseToObject() |
|
129
|
|
|
{ |
|
130
|
|
|
return array_filter([ |
|
131
|
|
|
'data' => $this->toResourceObject(), |
|
132
|
|
|
'included' => $this->included->isEmpty() ? null : $this->included->toArray(), |
|
133
|
|
|
'links' => $this->links->isEmpty() ? null : $this->links->toArray(), |
|
134
|
|
|
'meta' => $this->meta->isEmpty() ? null : $this->meta->toArray(), |
|
135
|
|
|
]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Serialise complete JSON API document to a JSON string. |
|
140
|
|
|
* |
|
141
|
|
|
* @return array |
|
142
|
|
|
*/ |
|
143
|
|
|
public function serializeToJson() |
|
144
|
|
|
{ |
|
145
|
|
|
return json_encode($this->serialiseToObject()); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Return the primary record type name. |
|
150
|
|
|
* |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function getRecordType() |
|
154
|
|
|
{ |
|
155
|
|
|
return str_slug(str_plural(get_class($this->record))); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Return the attribute object data for the primary record. |
|
160
|
|
|
* |
|
161
|
|
|
* @return array |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function transformRecordAttributes() |
|
164
|
|
|
{ |
|
165
|
|
|
$attributes = array_diff_key($this->record->toArray(), $this->record->getRelations()); |
|
166
|
|
|
$attributes = array_except($attributes, ['id']); |
|
167
|
|
|
|
|
168
|
|
|
if (!empty($this->fields)) { |
|
169
|
|
|
$attributes = array_only($attributes, $this->fields); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $attributes; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..