Completed
Pull Request — master (#32)
by Jasper
05:19 queued 02:40
created

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