Passed
Push — master ( ce8fe8...a3b3fc )
by Jasper
09:32
created

Document::getErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Swis\JsonApi\Client;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Swis\JsonApi\Client\Concerns\HasLinks;
7
use Swis\JsonApi\Client\Concerns\HasMeta;
8
use Swis\JsonApi\Client\Interfaces\DataInterface;
9
use Swis\JsonApi\Client\Interfaces\DocumentInterface;
10
11
class Document implements DocumentInterface
12
{
13
    use HasLinks;
14
    use HasMeta;
15
16
    /**
17
     * @var \Psr\Http\Message\ResponseInterface|null
18
     */
19
    protected $response;
20
21
    /**
22
     * @var \Swis\JsonApi\Client\Interfaces\DataInterface
23
     */
24
    protected $data;
25
26
    /**
27
     * @var \Swis\JsonApi\Client\ErrorCollection
28
     */
29
    protected $errors;
30
31
    /**
32
     * @var \Swis\JsonApi\Client\Collection
33
     */
34
    protected $included;
35
36
    /**
37
     * @var \Swis\JsonApi\Client\Jsonapi|null
38
     */
39
    protected $jsonapi;
40
41 432
    public function __construct()
42
    {
43 432
        $this->errors = new ErrorCollection();
44 432
        $this->included = new Collection();
45 432
    }
46
47
    /**
48
     * @return \Psr\Http\Message\ResponseInterface|null
49
     */
50 36
    public function getResponse(): ? ResponseInterface
51
    {
52 36
        return $this->response;
53
    }
54
55
    /**
56
     * @param \Psr\Http\Message\ResponseInterface|null $response
57
     *
58
     * @return $this
59
     */
60 36
    public function setResponse(? ResponseInterface $response)
61
    {
62 36
        $this->response = $response;
63
64 36
        return $this;
65
    }
66
67
    /**
68
     * @return \Swis\JsonApi\Client\ErrorCollection
69
     */
70 27
    public function getErrors(): ErrorCollection
71
    {
72 27
        return $this->errors;
73
    }
74
75
    /**
76
     * @param \Swis\JsonApi\Client\ErrorCollection $errors
77
     *
78
     * @return $this
79
     */
80 45
    public function setErrors(ErrorCollection $errors)
81
    {
82 45
        $this->errors = $errors;
83
84 45
        return $this;
85
    }
86
87
    /**
88
     * @return bool
89
     */
90 45
    public function hasErrors(): bool
91
    {
92 45
        return !$this->errors->isEmpty();
93
    }
94
95
    /**
96
     * @return bool
97
     */
98 9
    public function isSuccess(): bool
99
    {
100 9
        return $this->errors->isEmpty();
101
    }
102
103
    /**
104
     * @return \Swis\JsonApi\Client\Collection
105
     */
106 243
    public function getIncluded(): Collection
107
    {
108 243
        return $this->included;
109
    }
110
111
    /**
112
     * @param \Swis\JsonApi\Client\Collection $included
113
     *
114
     * @return $this
115
     */
116 207
    public function setIncluded(Collection $included)
117
    {
118 207
        $this->included = $included;
119
120 207
        return $this;
121
    }
122
123
    /**
124
     * @return \Swis\JsonApi\Client\Jsonapi|null
125
     */
126 54
    public function getJsonapi(): ? Jsonapi
127
    {
128 54
        return $this->jsonapi;
129
    }
130
131
    /**
132
     * @param \Swis\JsonApi\Client\Jsonapi|null $jsonapi
133
     *
134
     * @return $this
135
     */
136 36
    public function setJsonapi(? Jsonapi $jsonapi)
137
    {
138 36
        $this->jsonapi = $jsonapi;
139
140 36
        return $this;
141
    }
142
143
    /**
144
     * @return \Swis\JsonApi\Client\Interfaces\DataInterface
145
     */
146 180
    public function getData()
147
    {
148 180
        return $this->data;
149
    }
150
151
    /**
152
     * @param \Swis\JsonApi\Client\Interfaces\DataInterface $data
153
     *
154
     * @return $this
155
     */
156 288
    public function setData(DataInterface $data)
157
    {
158 288
        $this->data = $data;
159
160 288
        return $this;
161
    }
162
163
    /**
164
     * @return array
165
     */
166 9
    public function toArray(): array
167
    {
168 9
        $document = [];
169
170 9
        if ($this->getLinks() !== null) {
171 9
            $document['links'] = $this->getLinks()->toArray();
172
        }
173
174 9
        if ($this->getData() !== null) {
175 9
            $document['data'] = $this->data->toJsonApiArray();
176
        }
177
178 9
        if ($this->getIncluded()->isNotEmpty()) {
179 9
            $document['included'] = $this->getIncluded()->toJsonApiArray();
180
        }
181
182 9
        if ($this->getMeta() !== null) {
183 9
            $document['meta'] = $this->getMeta()->toArray();
184
        }
185
186 9
        if ($this->hasErrors()) {
187 9
            $document['errors'] = $this->getErrors()->toArray();
188
        }
189
190 9
        if ($this->getJsonapi() !== null) {
191 9
            $document['jsonapi'] = $this->getJsonapi()->toArray();
192
        }
193
194 9
        return $document;
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     *
200
     * @return object
201
     */
202 27
    public function jsonSerialize()
203
    {
204 27
        $document = [];
205
206 27
        if ($this->getLinks() !== null) {
207 9
            $document['links'] = $this->getLinks();
208
        }
209
210 27
        if ($this->getData() !== null) {
211 27
            $document['data'] = $this->data->toJsonApiArray();
212
        }
213
214 27
        if ($this->getIncluded()->isNotEmpty()) {
215
            $document['included'] = $this->getIncluded()->toJsonApiArray();
216
        }
217
218 27
        if ($this->getMeta() !== null) {
219 9
            $document['meta'] = $this->getMeta();
220
        }
221
222 27
        if ($this->hasErrors()) {
223
            $document['errors'] = $this->getErrors();
224
        }
225
226 27
        if ($this->getJsonapi() !== null) {
227 9
            $document['jsonapi'] = $this->getJsonapi();
228
        }
229
230 27
        return (object) $document;
231
    }
232
}
233