1 | <?php |
||
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 | 22 | public function __construct() |
|
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 |
||
63 | |||
64 | /** |
||
65 | * Return any secondary included resource objects. |
||
66 | * |
||
67 | * @return \Illuminate\Support\Collection |
||
68 | */ |
||
69 | public function getIncluded() |
||
73 | |||
74 | /** |
||
75 | * Set the base URL for document links. |
||
76 | * |
||
77 | * @param string $url |
||
78 | */ |
||
79 | 2 | public function setBaseUrl(string $url) |
|
83 | |||
84 | /** |
||
85 | * Add included meta information. |
||
86 | * |
||
87 | * @param string|array $key |
||
88 | * @param string|int|null $value |
||
89 | */ |
||
90 | 2 | public function addMeta($key, $value = null) |
|
94 | |||
95 | /** |
||
96 | * Add one or more included links. |
||
97 | * |
||
98 | * @param string|array $key |
||
99 | * @param string|int|null $value |
||
100 | */ |
||
101 | 22 | public function addLinks($key, $value = null) |
|
105 | |||
106 | /** |
||
107 | * Serialise JSON API document to an array. |
||
108 | */ |
||
109 | 9 | public function serializeToObject(): array |
|
121 | |||
122 | /** |
||
123 | * Convert the object into something JSON serializable. |
||
124 | */ |
||
125 | 2 | public function jsonSerialize(): array |
|
129 | |||
130 | /** |
||
131 | * Serialise JSON API document to a JSON string. |
||
132 | */ |
||
133 | 2 | public function serializeToJson(): string |
|
137 | |||
138 | /** |
||
139 | * Reduce a collection of records to unique entries, by comparing their |
||
140 | * resource identifier values. |
||
141 | * |
||
142 | * @param \Illuminate\Support\Collection $records |
||
143 | * |
||
144 | * @return \Illuminate\Support\Collection |
||
145 | */ |
||
146 | protected function filterUnique($records) |
||
152 | |||
153 | /** |
||
154 | * Return JSON API implementation information. |
||
155 | */ |
||
156 | 9 | private function getDocumentMeta(): array |
|
162 | } |
||
163 |