|
1
|
|
|
<?php |
|
2
|
|
|
namespace Javis\JsonApi\Schema; |
|
3
|
|
|
|
|
4
|
|
|
class Document |
|
5
|
|
|
{ |
|
6
|
|
|
/** |
|
7
|
|
|
* @var \Javis\JsonApi\Schema\JsonApi |
|
8
|
|
|
*/ |
|
9
|
|
|
protected $jsonApi; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $meta; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var \Javis\JsonApi\Schema\Links |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $links; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \Javis\JsonApi\Schema\Resources |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $resources; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \Javis\JsonApi\Schema\Error[] |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $errors; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param array $document |
|
33
|
|
|
* @return $this |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function createFromArray(array $document) |
|
36
|
|
|
{ |
|
37
|
|
|
if (isset($document["jsonApi"]) && is_array($document["jsonApi"])) { |
|
38
|
|
|
$jsonApi = $document["jsonApi"]; |
|
39
|
|
|
} else { |
|
40
|
|
|
$jsonApi = []; |
|
41
|
|
|
} |
|
42
|
|
|
$jsonApiObject = JsonApi::createFromArray($jsonApi); |
|
43
|
|
|
|
|
44
|
|
|
if (isset($document["meta"]) && is_array($document["meta"])) { |
|
45
|
|
|
$meta = $document["meta"]; |
|
46
|
|
|
} else { |
|
47
|
|
|
$meta = []; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (isset($document["links"]) && is_array($document["links"])) { |
|
51
|
|
|
$links = $document["links"]; |
|
52
|
|
|
} else { |
|
53
|
|
|
$links = []; |
|
54
|
|
|
} |
|
55
|
|
|
$linksObject = Links::createFromArray($links); |
|
56
|
|
|
|
|
57
|
|
|
if (isset($document["data"]) && is_array($document["data"])) { |
|
58
|
|
|
$data = $document["data"]; |
|
59
|
|
|
} else { |
|
60
|
|
|
$data = []; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (isset($document["included"]) && is_array($document["included"])) { |
|
64
|
|
|
$included = $document["included"]; |
|
65
|
|
|
} else { |
|
66
|
|
|
$included = []; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$resources = new Resources($data, $included); |
|
70
|
|
|
|
|
71
|
|
|
$errors = []; |
|
72
|
|
|
if (isset($document["errors"]) && is_array($document["errors"])) { |
|
73
|
|
|
foreach ($document["errors"] as $error) { |
|
74
|
|
|
if (is_array($error)) { |
|
75
|
|
|
$errors[] = Error::createFromArray($error); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return new self($jsonApiObject, $meta, $linksObject, $resources, $errors); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param \Javis\JsonApi\Schema\JsonApi $jsonApi |
|
85
|
|
|
* @param array $meta |
|
86
|
|
|
* @param \Javis\JsonApi\Schema\Links $links |
|
87
|
|
|
* @param \Javis\JsonApi\Schema\Resources $resources |
|
88
|
|
|
* @param \Javis\JsonApi\Schema\Error[] $errors |
|
89
|
|
|
*/ |
|
90
|
|
|
public function __construct(JsonApi $jsonApi, array $meta, Links $links, Resources $resources, array $errors) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->jsonApi = $jsonApi; |
|
93
|
|
|
$this->meta = $meta; |
|
94
|
|
|
$this->links = $links; |
|
95
|
|
|
$this->resources = $resources; |
|
96
|
|
|
$this->errors = $errors; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
|
|
public function toArray() |
|
103
|
|
|
{ |
|
104
|
|
|
$content = []; |
|
105
|
|
|
|
|
106
|
|
|
if ($this->hasJsonApi()) { |
|
107
|
|
|
$content["jsonApi"] = $this->jsonApi->toArray(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if ($this->hasMeta()) { |
|
111
|
|
|
$content["meta"] = $this->meta; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if ($this->hasLinks()) { |
|
115
|
|
|
$content["links"] = $this->links->toArray(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if ($this->hasPrimaryResources()) { |
|
119
|
|
|
$content["data"] = $this->resources->primaryDataToArray(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if ($this->hasErrors()) { |
|
123
|
|
|
$errors = []; |
|
124
|
|
|
foreach ($this->errors as $error) { |
|
125
|
|
|
$errors = $error->toArray(); |
|
126
|
|
|
} |
|
127
|
|
|
$content["errors"] = $errors; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if ($this->resources->hasIncludedResources()) { |
|
131
|
|
|
$content["included"] = $this->resources->includedToArray(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $content; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return bool |
|
139
|
|
|
*/ |
|
140
|
|
|
public function hasJsonApi() |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->jsonApi->hasJsonApi(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return \Javis\JsonApi\Schema\JsonApi |
|
147
|
|
|
*/ |
|
148
|
|
|
public function jsonApi() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->jsonApi; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return bool |
|
155
|
|
|
*/ |
|
156
|
|
|
public function hasMeta() |
|
157
|
|
|
{ |
|
158
|
|
|
return empty($this->meta) === false; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return array |
|
163
|
|
|
*/ |
|
164
|
|
|
public function meta() |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->meta; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return bool |
|
171
|
|
|
*/ |
|
172
|
|
|
public function hasLinks() |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->links->hasLinks(); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return \Javis\JsonApi\Schema\Links |
|
179
|
|
|
*/ |
|
180
|
|
|
public function links() |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->links; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @return bool |
|
187
|
|
|
*/ |
|
188
|
|
|
public function isSingleResourceDocument() |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->resources->isSinglePrimaryResource() === true; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return bool |
|
195
|
|
|
*/ |
|
196
|
|
|
public function isResourceCollectionDocument() |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->resources->isPrimaryResourceCollection() === true; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @return bool |
|
203
|
|
|
*/ |
|
204
|
|
|
public function hasPrimaryResources() |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->resources->hasPrimaryResources(); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @return \Javis\JsonApi\Schema\Resource|null |
|
211
|
|
|
*/ |
|
212
|
|
|
public function primaryResource() |
|
213
|
|
|
{ |
|
214
|
|
|
return $this->resources->getPrimaryResource(); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @return \Javis\JsonApi\Schema\Resource[] |
|
219
|
|
|
*/ |
|
220
|
|
|
public function primaryResources() |
|
221
|
|
|
{ |
|
222
|
|
|
return $this->resources->getPrimaryResources(); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @param string $type |
|
227
|
|
|
* @param string $id |
|
228
|
|
|
* @return \Javis\JsonApi\Schema\Resource|null |
|
229
|
|
|
*/ |
|
230
|
|
|
public function resource($type, $id) |
|
231
|
|
|
{ |
|
232
|
|
|
return $this->resources->getResource($type, $id); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @return bool |
|
237
|
|
|
*/ |
|
238
|
|
|
public function hasIncludedResources() |
|
239
|
|
|
{ |
|
240
|
|
|
return $this->resources->hasIncludedResources(); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @param string $type |
|
245
|
|
|
* @param string $id |
|
246
|
|
|
* @return bool |
|
247
|
|
|
*/ |
|
248
|
|
|
public function hasIncludedResource($type, $id) |
|
249
|
|
|
{ |
|
250
|
|
|
return $this->resources->hasIncludedResource($type, $id); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @return \Javis\JsonApi\Schema\Resource[] |
|
255
|
|
|
*/ |
|
256
|
|
|
public function includedResources() |
|
257
|
|
|
{ |
|
258
|
|
|
return $this->resources->getIncludedResources(); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @return bool |
|
263
|
|
|
*/ |
|
264
|
|
|
public function hasErrors() |
|
265
|
|
|
{ |
|
266
|
|
|
return empty($this->errors) === false; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* @return \Javis\JsonApi\Schema\Error[] |
|
271
|
|
|
*/ |
|
272
|
|
|
public function errors() |
|
273
|
|
|
{ |
|
274
|
|
|
return $this->errors; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* @param int $number |
|
279
|
|
|
* @return \Javis\JsonApi\Schema\Error|null |
|
280
|
|
|
*/ |
|
281
|
|
|
public function error($number) |
|
282
|
|
|
{ |
|
283
|
|
|
return isset($this->errors[$number]) ? $this->errors[$number] : null; |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
|