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. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $links = []; |
||
37 | |||
38 | /** |
||
39 | * Create a new JSON API document serializer. |
||
40 | */ |
||
41 | 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 secondary included resource objects. |
||
56 | * |
||
57 | * @return \Illuminate\Support\Collection |
||
58 | */ |
||
59 | public function getIncluded() |
||
63 | |||
64 | /** |
||
65 | * Set the base URL for document links. |
||
66 | * |
||
67 | * @param string $url |
||
68 | */ |
||
69 | public function setBaseUrl(string $url) |
||
74 | |||
75 | /** |
||
76 | * Add included meta information. |
||
77 | * |
||
78 | * @param string|array $key |
||
79 | * @param string|int|null $value |
||
80 | */ |
||
81 | public function addMeta($key, $value = null) |
||
85 | |||
86 | /** |
||
87 | * Add one or more included links. |
||
88 | * |
||
89 | * @param string|array $key |
||
90 | * @param string|int|null $value |
||
91 | */ |
||
92 | public function addLinks($key, $value = null) |
||
96 | |||
97 | /** |
||
98 | * Serialise JSON API document to an array. |
||
99 | */ |
||
100 | public function serializeToObject(): array |
||
110 | |||
111 | /** |
||
112 | * Convert the object into something JSON serializable. |
||
113 | */ |
||
114 | public function jsonSerialize(): array |
||
118 | |||
119 | /** |
||
120 | * Serialise JSON API document to a JSON string. |
||
121 | */ |
||
122 | public function serializeToJson(): string |
||
126 | |||
127 | /** |
||
128 | * Return JSON API implementation information. |
||
129 | */ |
||
130 | private function getDocumentMeta(): array |
||
136 | } |
||
137 |