Document::setResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
    use HasLinks;
16
    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 196
    public function __construct()
44
    {
45 196
        $this->errors = new ErrorCollection;
46 196
        $this->included = new Collection;
47 98
    }
48
49 16
    public function getResponse(): ?ResponseInterface
50
    {
51 16
        return $this->response;
52
    }
53
54
    /**
55
     * @return $this
56
     */
57 16
    public function setResponse(?ResponseInterface $response)
58
    {
59 16
        $this->response = $response;
60
61 16
        return $this;
62
    }
63
64 12
    public function getErrors(): ErrorCollection
65
    {
66 12
        return $this->errors;
67
    }
68
69
    /**
70
     * @return $this
71
     */
72 20
    public function setErrors(ErrorCollection $errors)
73
    {
74 20
        $this->errors = $errors;
75
76 20
        return $this;
77
    }
78
79 20
    public function hasErrors(): bool
80
    {
81 20
        return ! $this->errors->isEmpty();
82
    }
83
84 4
    public function isSuccess(): bool
85
    {
86 4
        return $this->errors->isEmpty();
87
    }
88
89 112
    public function getIncluded(): Collection
90
    {
91 112
        return $this->included;
92
    }
93
94
    /**
95
     * @return $this
96
     */
97 96
    public function setIncluded(Collection $included)
98
    {
99 96
        $this->included = $included;
100
101 96
        return $this;
102
    }
103
104 24
    public function getJsonapi(): ?Jsonapi
105
    {
106 24
        return $this->jsonapi;
107
    }
108
109
    /**
110
     * @return $this
111
     */
112 16
    public function setJsonapi(?Jsonapi $jsonapi)
113
    {
114 16
        $this->jsonapi = $jsonapi;
115
116 16
        return $this;
117
    }
118
119
    /**
120
     * @return \Swis\JsonApi\Client\Interfaces\DataInterface
121
     */
122 84
    public function getData()
123
    {
124 84
        return $this->data;
125
    }
126
127
    /**
128
     * @return $this
129
     */
130 132
    public function setData(DataInterface $data)
131
    {
132 132
        $this->data = $data;
133
134 132
        return $this;
135
    }
136
137 4
    public function toArray(): array
138
    {
139 4
        $document = [];
140
141 4
        if ($this->getLinks() !== null) {
142 4
            $document['links'] = $this->getLinks()->toArray();
143
        }
144
145 4
        if ($this->getData() !== null) {
146 4
            $document['data'] = $this->data->toJsonApiArray();
147
        }
148
149 4
        if ($this->getIncluded()->isNotEmpty()) {
150 4
            $document['included'] = $this->getIncluded()->toJsonApiArray();
151
        }
152
153 4
        if ($this->getMeta() !== null) {
154 4
            $document['meta'] = $this->getMeta()->toArray();
155
        }
156
157 4
        if ($this->hasErrors()) {
158 4
            $document['errors'] = $this->getErrors()->toArray();
159
        }
160
161 4
        if ($this->getJsonapi() !== null) {
162 4
            $document['jsonapi'] = $this->getJsonapi()->toArray();
163
        }
164
165 4
        return $document;
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     *
171
     * @return object
172
     */
173 12
    #[\ReturnTypeWillChange]
174
    public function jsonSerialize()
175
    {
176 12
        $document = [];
177
178 12
        if ($this->getLinks() !== null) {
179 4
            $document['links'] = $this->getLinks();
180
        }
181
182 12
        if ($this->getData() !== null) {
183 12
            $document['data'] = $this->data->toJsonApiArray();
184
        }
185
186 12
        if ($this->getIncluded()->isNotEmpty()) {
187
            $document['included'] = $this->getIncluded()->toJsonApiArray();
188
        }
189
190 12
        if ($this->getMeta() !== null) {
191 4
            $document['meta'] = $this->getMeta();
192
        }
193
194 12
        if ($this->hasErrors()) {
195
            $document['errors'] = $this->getErrors();
196
        }
197
198 12
        if ($this->getJsonapi() !== null) {
199 4
            $document['jsonapi'] = $this->getJsonapi();
200
        }
201
202 12
        return (object) $document;
203
    }
204
}
205