1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Huntie\JsonApi\Support; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Transform Eloquent models and collections into JSON API objects. |
7
|
|
|
*/ |
8
|
|
|
trait JsonApiTransforms |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Transform a model instance into a JSON API object. |
12
|
|
|
* |
13
|
|
|
* @param \Illuminate\Database\Eloquent\Model $record |
14
|
|
|
* @param array|null $fields Field subset to return |
15
|
|
|
* @param array|null $include Relations to include |
16
|
|
|
* |
17
|
|
|
* @return array |
18
|
|
|
*/ |
19
|
|
|
protected function transformRecord($record, array $fields = [], array $include = []) |
20
|
|
|
{ |
21
|
|
|
$relations = array_unique(array_merge($record->getRelations(), $include)); |
22
|
|
|
$record = $record->load($relations); |
23
|
|
|
|
24
|
|
|
$attributes = $record->toArray(); |
25
|
|
|
$relationships = []; |
26
|
|
|
$included = []; |
27
|
|
|
|
28
|
|
|
foreach ($relations as $relation) { |
29
|
|
|
$relatedRecords = $record->{$relation}; |
30
|
|
|
$relationships[$relation] = $this->transformCollectionIds($relatedRecords); |
31
|
|
|
|
32
|
|
|
if (in_array($relation, $include)) { |
33
|
|
|
$included = array_merge($included, $this->transformCollectionSimple($relatedRecords)['data']); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
array_forget($attributes, $relations); |
38
|
|
|
$included = array_filter($included); |
39
|
|
|
|
40
|
|
|
if (!empty($fields)) { |
41
|
|
|
$attributes = array_only($attributes, $fields); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$data = array_filter([ |
45
|
|
|
'type' => $record->getTable(), |
46
|
|
|
'id' => $record->id, |
47
|
|
|
'attributes' => array_except($attributes, ['id']), |
48
|
|
|
'relationships' => $relationships, |
49
|
|
|
]); |
50
|
|
|
|
51
|
|
|
return array_filter(compact('data', 'included')); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Transform a model instance into a JSON API object without additonal data. |
56
|
|
|
* |
57
|
|
|
* @param \Illuminate\Database\Eloquent\Model $record |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
protected function transformRecordSimple($record) |
62
|
|
|
{ |
63
|
|
|
$attributes = array_diff_key($record->toArray(), $record->getRelations()); |
64
|
|
|
$attributes = array_except($attributes, ['id']); |
65
|
|
|
|
66
|
|
|
return [ |
67
|
|
|
'data' => [ |
68
|
|
|
'type' => $record->getTable(), |
69
|
|
|
'id' => $record->id, |
70
|
|
|
'attributes' => $attributes, |
71
|
|
|
] |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Transform a set of models into a JSON API collection. |
77
|
|
|
* |
78
|
|
|
* @param \Illuminate\Support\Collection $records |
79
|
|
|
* @param array $fields |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
protected function transformCollection($records, array $fields = []) |
84
|
|
|
{ |
85
|
|
|
$data = $records->map(function ($record) use ($fields) { |
86
|
|
|
return $this->transformRecord($record, $fields)['data']; |
87
|
|
|
})->toArray(); |
88
|
|
|
|
89
|
|
|
return compact('data'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Transform a set of models into a JSON API colleciton without additional data. |
94
|
|
|
* |
95
|
|
|
* @param \Illuminate\Support\Collection $records |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
protected function transformCollectionSimple($records) |
100
|
|
|
{ |
101
|
|
|
$data = $records->map(function ($record) { |
102
|
|
|
return $this->transformRecordSimple($record)['data']; |
103
|
|
|
})->toArray(); |
104
|
|
|
|
105
|
|
|
return compact('data'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Transform a set of models into a collection of JSON API resource |
110
|
|
|
* identifier objects. |
111
|
|
|
* |
112
|
|
|
* @param \Illuminate\Support\Collection $records |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
protected function transformCollectionIds($records) |
117
|
|
|
{ |
118
|
|
|
$data = $records->map(function ($record) { |
119
|
|
|
return [ |
120
|
|
|
'type' => $record->getTable(), |
121
|
|
|
'id' => $record->id, |
122
|
|
|
]; |
123
|
|
|
})->toArray(); |
124
|
|
|
|
125
|
|
|
return compact('data'); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|