1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swis\JsonApi\Client; |
4
|
|
|
|
5
|
|
|
use Swis\JsonApi\Client\Errors\ErrorCollection; |
6
|
|
|
use Swis\JsonApi\Client\Interfaces\DataInterface; |
7
|
|
|
use Swis\JsonApi\Client\Interfaces\DocumentInterface; |
8
|
|
|
|
9
|
|
|
class Document implements DocumentInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \Swis\JsonApi\Client\Interfaces\DataInterface |
13
|
|
|
*/ |
14
|
|
|
protected $data; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $meta = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $links = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Swis\JsonApi\Client\Errors\ErrorCollection |
28
|
|
|
*/ |
29
|
|
|
protected $errors; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \Swis\JsonApi\Client\Collection |
33
|
|
|
*/ |
34
|
|
|
protected $included; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $jsonapi = []; |
40
|
|
|
|
41
|
50 |
|
public function __construct() |
42
|
|
|
{ |
43
|
50 |
|
$this->errors = new ErrorCollection(); |
44
|
50 |
|
$this->included = new Collection(); |
45
|
50 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
5 |
|
public function getMeta(): array |
51
|
|
|
{ |
52
|
5 |
|
return $this->meta; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $meta |
57
|
|
|
*/ |
58
|
5 |
|
public function setMeta(array $meta) |
59
|
|
|
{ |
60
|
5 |
|
$this->meta = $meta; |
61
|
5 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
5 |
|
public function getLinks(): array |
67
|
|
|
{ |
68
|
5 |
|
return $this->links; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array $links |
73
|
|
|
*/ |
74
|
5 |
|
public function setLinks(array $links) |
75
|
|
|
{ |
76
|
5 |
|
$this->links = $links; |
77
|
5 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return \Swis\JsonApi\Client\Errors\ErrorCollection |
81
|
|
|
*/ |
82
|
|
|
public function getErrors(): ErrorCollection |
83
|
|
|
{ |
84
|
|
|
return $this->errors; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param \Swis\JsonApi\Client\Errors\ErrorCollection $errors |
89
|
|
|
*/ |
90
|
10 |
|
public function setErrors(ErrorCollection $errors) |
91
|
|
|
{ |
92
|
10 |
|
$this->errors = $errors; |
93
|
10 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
10 |
|
public function hasErrors(): bool |
99
|
|
|
{ |
100
|
10 |
|
return !$this->errors->isEmpty(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
5 |
|
public function isSuccess(): bool |
108
|
|
|
{ |
109
|
5 |
|
return $this->errors->isEmpty(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return \Swis\JsonApi\Client\Collection |
114
|
|
|
*/ |
115
|
5 |
|
public function getIncluded(): Collection |
116
|
|
|
{ |
117
|
5 |
|
return $this->included; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param \Swis\JsonApi\Client\Collection $included |
122
|
|
|
* |
123
|
|
|
* @return static |
124
|
|
|
*/ |
125
|
40 |
|
public function setIncluded(Collection $included) |
126
|
|
|
{ |
127
|
40 |
|
$this->included = $included; |
128
|
|
|
|
129
|
40 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
5 |
|
public function getJsonapi(): array |
136
|
|
|
{ |
137
|
5 |
|
return $this->jsonapi; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param array $jsonapi |
142
|
|
|
*/ |
143
|
5 |
|
public function setJsonapi(array $jsonapi) |
144
|
|
|
{ |
145
|
5 |
|
$this->jsonapi = $jsonapi; |
146
|
5 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return \Swis\JsonApi\Client\Interfaces\DataInterface |
150
|
|
|
*/ |
151
|
45 |
|
public function getData() |
152
|
|
|
{ |
153
|
45 |
|
return $this->data; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param \Swis\JsonApi\Client\Interfaces\DataInterface $data |
158
|
|
|
* |
159
|
|
|
* @return static |
160
|
|
|
*/ |
161
|
45 |
|
public function setData(DataInterface $data) |
162
|
|
|
{ |
163
|
45 |
|
$this->data = $data; |
164
|
|
|
|
165
|
45 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Specify data which should be serialized to JSON. |
170
|
|
|
* |
171
|
|
|
* @see http://php.net/manual/en/jsonserializable.jsonserialize.php |
172
|
|
|
* |
173
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
174
|
|
|
* which is a value of any type other than a resource |
175
|
|
|
* |
176
|
|
|
* @since 5.4.0 |
177
|
|
|
*/ |
178
|
|
|
public function jsonSerialize() |
179
|
|
|
{ |
180
|
|
|
return $this->toArray(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
5 |
|
public function toArray(): array |
187
|
|
|
{ |
188
|
5 |
|
$document = []; |
189
|
|
|
|
190
|
5 |
|
if (!empty($this->getLinks())) { |
191
|
5 |
|
$document['links'] = $this->links; |
192
|
|
|
} |
193
|
|
|
|
194
|
5 |
|
if (!empty($this->getData())) { |
195
|
5 |
|
$document['data'] = $this->data->toJsonApiArray(); |
196
|
|
|
} |
197
|
|
|
|
198
|
5 |
|
if ($this->getIncluded()->isNotEmpty()) { |
199
|
5 |
|
$document['included'] = $this->getIncluded()->toJsonApiArray(); |
200
|
|
|
} |
201
|
|
|
|
202
|
5 |
|
if (!empty($this->getMeta())) { |
203
|
5 |
|
$document['meta'] = $this->meta; |
204
|
|
|
} |
205
|
|
|
|
206
|
5 |
|
if ($this->hasErrors()) { |
207
|
5 |
|
$document['errors'] = $this->errors->toArray(); |
208
|
|
|
} |
209
|
|
|
|
210
|
5 |
|
if (!empty($this->getJsonapi())) { |
211
|
5 |
|
$document['jsonapi'] = $this->jsonapi; |
212
|
|
|
} |
213
|
|
|
|
214
|
5 |
|
return $document; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|