1 | <?php |
||
8 | class ResourceSerializer extends JsonApiSerializer |
||
9 | { |
||
10 | /** |
||
11 | * The model instance to transform. |
||
12 | * |
||
13 | * @var \Illuminate\Database\Eloquent\Model |
||
14 | */ |
||
15 | protected $record; |
||
16 | |||
17 | /** |
||
18 | * The record relationships to return. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $relationships; |
||
23 | |||
24 | /** |
||
25 | * The subset of record attributes to return. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $fields; |
||
30 | |||
31 | /** |
||
32 | * The relationships to load and include. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $include; |
||
37 | |||
38 | /** |
||
39 | * Create a new JSON API resource serializer. |
||
40 | * |
||
41 | * @param Model $record The model instance to serialise |
||
42 | * @param array|null $fields Subset of fields to return |
||
43 | * @param array|null $include Relations to include |
||
44 | */ |
||
45 | public function __construct($record, array $fields = [], array $include = []) |
||
54 | |||
55 | /** |
||
56 | * Limit which relations can be included. |
||
57 | * |
||
58 | * @param array $include |
||
59 | */ |
||
60 | public function scopeIncludes($include) |
||
64 | |||
65 | /** |
||
66 | * Return a JSON API resource identifier object for the primary record. |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | public function toResourceIdentifier() |
||
77 | |||
78 | /** |
||
79 | * Return a base JSON API resource object for the primary record containing |
||
80 | * only immediate attributes. |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | public function toBaseResourceObject() |
||
90 | |||
91 | /** |
||
92 | * Return a full JSON API resource object for the primary record. |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | public function toResourceObject() |
||
104 | |||
105 | /** |
||
106 | * Serialise JSON API document to an array. |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | public function serializeToObject() |
||
116 | |||
117 | /** |
||
118 | * Return primary data for the JSON API document. |
||
119 | * |
||
120 | * @return array |
||
121 | */ |
||
122 | protected function getPrimaryData() |
||
126 | |||
127 | /** |
||
128 | * Return the primary record type name. |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | protected function getRecordType() |
||
138 | |||
139 | /** |
||
140 | * Return the attribute object data for the primary record. |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | protected function transformRecordAttributes() |
||
155 | |||
156 | /** |
||
157 | * Return a collection of JSON API resource identifier objects by each |
||
158 | * relation on the primary record. |
||
159 | * |
||
160 | * @return \Illuminate\Support\Collection |
||
161 | */ |
||
162 | protected function transformRecordRelations() |
||
176 | |||
177 | /** |
||
178 | * Return a collection of JSON API resource objects for each included |
||
179 | * relationship. |
||
180 | * |
||
181 | * @return \Illuminate\Support\Collection |
||
182 | */ |
||
183 | protected function transformIncludedRelations() |
||
197 | |||
198 | /** |
||
199 | * Run a map over each item in a loaded relation on the primary record. |
||
200 | * |
||
201 | * @param string $relation |
||
202 | * @param \Closure $callback |
||
203 | * |
||
204 | * @return Collection|Model|null |
||
205 | */ |
||
206 | protected function mapRelation($relation, $callback) |
||
218 | } |
||
219 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.