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