Completed
Pull Request — master (#48)
by Jasper
12:30
created

Document::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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