1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Huntie\JsonApi\Serializers; |
4
|
|
|
|
5
|
|
|
use JsonSerializable; |
6
|
|
|
|
7
|
|
|
abstract class JsonApiSerializer implements JsonSerializable |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The JSON API version being implemented. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
const JSON_API_VERSION = '1.0'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Meta information to include. |
18
|
|
|
* |
19
|
|
|
* @var \Illuminate\Support\Collection |
20
|
|
|
*/ |
21
|
|
|
protected $meta; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Resource links to include. |
25
|
|
|
* |
26
|
|
|
* @var \Illuminate\Support\Collection |
27
|
|
|
*/ |
28
|
|
|
protected $links; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create a new JSON API document serializer. |
32
|
|
|
*/ |
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
$this->meta = collect([]); |
36
|
|
|
$this->links = collect([]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Return primary data for the JSON API document. |
41
|
|
|
* |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
|
|
abstract protected function getPrimaryData(); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Add included meta information. |
48
|
|
|
* |
49
|
|
|
* @param string|array $key |
50
|
|
|
* @param string|int|null $value |
51
|
|
|
*/ |
52
|
|
|
public function addMeta($key, $value = null) |
53
|
|
|
{ |
54
|
|
|
$this->meta = $this->meta->merge(is_array($key) ? $key : [$key => $value]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Add one or more included links. |
59
|
|
|
* |
60
|
|
|
* @param string|array $key |
61
|
|
|
* @param string|int|null $value |
62
|
|
|
*/ |
63
|
|
|
public function addLinks($key, $value = null) |
64
|
|
|
{ |
65
|
|
|
$this->links = $this->links->merge(is_array($key) ? $key : [$key => $value]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Serialise JSON API document to an array. |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function serializeToObject() |
74
|
|
|
{ |
75
|
|
|
return array_filter([ |
76
|
|
|
'data' => $this->getPrimaryData(), |
77
|
|
|
'links' => $this->links->toArray(), |
78
|
|
|
'meta' => $this->meta->toArray(), |
79
|
|
|
'included' => $this->getIncludedData(), |
80
|
|
|
'jsonapi' => $this->getDocumentMeta(), |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Convert the object into something JSON serializable. |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function jsonSerialize() |
90
|
|
|
{ |
91
|
|
|
return $this->serializeToObject(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Serialise JSON API document to a JSON string. |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public function serializeToJson() |
100
|
|
|
{ |
101
|
|
|
return json_encode($this->jsonSerialize()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Return any secondary included resource data. |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
protected function getIncludedData() |
110
|
|
|
{ |
111
|
|
|
return []; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Return JSON API implementation information. |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
private function getDocumentMeta() |
120
|
|
|
{ |
121
|
|
|
return array_filter([ |
122
|
|
|
'version' => config('jsonapi.include_version') ? self::JSON_API_VERSION : null, |
123
|
|
|
]); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|