Passed
Push — master ( 983e5f...18069b )
by Jasper
03:15
created

Document   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Test Coverage

Coverage 96.97%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 221
ccs 64
cts 66
cp 0.9697
rs 10
wmc 27

15 Methods

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