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