Completed
Pull Request — master (#40)
by
unknown
03:49
created

Document::getLinks()   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
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 50
    public function __construct()
42
    {
43 50
        $this->errors = new ErrorCollection();
44 50
        $this->included = new Collection();
45 50
    }
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 10
    public function setErrors(ErrorCollection $errors)
91
    {
92 10
        $this->errors = $errors;
93 10
    }
94
95
    /**
96
     * @return bool
97
     */
98 10
    public function hasErrors(): bool
99
    {
100 10
        return !$this->errors->isEmpty();
101
    }
102
103
    /**
104
     * @return bool
105
     */
106 5
    public function isSuccess(): bool
107
    {
108 5
        return $this->errors->isEmpty();
109
    }
110
111
    /**
112
     * @return \Swis\JsonApi\Client\Collection
113
     */
114 5
    public function getIncluded(): Collection
115
    {
116 5
        return $this->included;
117
    }
118
119
    /**
120
     * @param \Swis\JsonApi\Client\Collection $included
121
     *
122
     * @return static
123
     */
124 40
    public function setIncluded(Collection $included)
125
    {
126 40
        $this->included = $included;
127
128 40
        return $this;
129
    }
130
131
    /**
132
     * @return array
133
     */
134 5
    public function getJsonapi(): array
135
    {
136 5
        return $this->jsonapi;
137
    }
138
139
    /**
140
     * @param array $jsonapi
141
     */
142 5
    public function setJsonapi(array $jsonapi)
143
    {
144 5
        $this->jsonapi = $jsonapi;
145 5
    }
146
147
    /**
148
     * @return \Swis\JsonApi\Client\Interfaces\DataInterface
149
     */
150 45
    public function getData()
151
    {
152 45
        return $this->data;
153
    }
154
155
    /**
156
     * @param \Swis\JsonApi\Client\Interfaces\DataInterface $data
157
     *
158
     * @return static
159
     */
160 45
    public function setData(DataInterface $data)
161
    {
162 45
        $this->data = $data;
163
164 45
        return $this;
165
    }
166
167
    /**
168
     * Specify data which should be serialized to JSON.
169
     *
170
     * @see  http://php.net/manual/en/jsonserializable.jsonserialize.php
171
     *
172
     * @return mixed data which can be serialized by <b>json_encode</b>,
173
     *               which is a value of any type other than a resource
174
     *
175
     * @since 5.4.0
176
     */
177
    public function jsonSerialize()
178
    {
179
        return $this->toArray();
180
    }
181
182
    /**
183
     * @return array
184
     */
185 5
    public function toArray(): array
186
    {
187 5
        $document = [];
188
189 5
        if (!empty($this->getLinks())) {
190 5
            $document['links'] = $this->links;
191
        }
192
193 5
        if (!empty($this->getData())) {
194 5
            $document['data'] = $this->data->toJsonApiArray();
195
        }
196
197 5
        if ($this->getIncluded()->isNotEmpty()) {
198 5
            $document['included'] = $this->getIncluded()->toJsonApiArray();
199
        }
200
201 5
        if (!empty($this->getMeta())) {
202 5
            $document['meta'] = $this->meta;
203
        }
204
205 5
        if ($this->hasErrors()) {
206 5
            $document['errors'] = $this->errors->toArray();
207
        }
208
209 5
        if (!empty($this->getJsonapi())) {
210 5
            $document['jsonapi'] = $this->jsonapi;
211
        }
212
213 5
        return $document;
214
    }
215
}
216