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