Passed
Push — master ( 8cd1cf...370b72 )
by Jasper
03:04
created

Document::setResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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