1 | <?php |
||
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) |
||
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) |
||
67 | |||
68 | /** |
||
69 | * Serialise JSON API document to an array. |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | public function serializeToObject() |
||
83 | |||
84 | /** |
||
85 | * Convert the object into something JSON serializable. |
||
86 | * |
||
87 | * @return array |
||
88 | */ |
||
89 | public function jsonSerialize() |
||
93 | |||
94 | /** |
||
95 | * Serialise JSON API document to a JSON string. |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | public function serializeToJson() |
||
103 | |||
104 | /** |
||
105 | * Return any secondary included resource data. |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | protected function getIncludedData() |
||
113 | |||
114 | /** |
||
115 | * Return JSON API implementation information. |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | private function getDocumentMeta() |
||
125 | } |
||
126 |