Completed
Pull Request — master (#32)
by Jasper
08:16
created

Document::getMeta()   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
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
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 40
    public function __construct()
42
    {
43 40
        $this->errors = new ErrorCollection();
44 40
        $this->included = new Collection();
45 40
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function getMeta(): array
51
    {
52
        return $this->meta;
53
    }
54
55
    /**
56
     * @param array $meta
57
     */
58
    public function setMeta(array $meta)
59
    {
60
        $this->meta = $meta;
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    public function getLinks(): array
67
    {
68
        return $this->links;
69
    }
70
71
    /**
72
     * @param array $links
73
     */
74
    public function setLinks(array $links)
75
    {
76
        $this->links = $links;
77
    }
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
    public function setErrors(ErrorCollection $errors)
91
    {
92
        $this->errors = $errors;
93
    }
94
95
    /**
96
     * @return bool
97
     */
98
    public function hasErrors(): bool
99
    {
100
        return !$this->errors->isEmpty();
101
    }
102
103
    /**
104
     * @return \Swis\JsonApi\Client\Collection
105
     */
106
    public function getIncluded(): Collection
107
    {
108
        return $this->included;
109
    }
110
111
    /**
112
     * @param \Swis\JsonApi\Client\Collection $included
113
     *
114
     * @return static
115
     */
116 35
    public function setIncluded(Collection $included)
117
    {
118 35
        $this->included = $included;
119
120 35
        return $this;
121
    }
122
123
    /**
124
     * @return array
125
     */
126
    public function getJsonapi(): array
127
    {
128
        return $this->jsonapi;
129
    }
130
131
    /**
132
     * @param array $jsonapi
133
     */
134
    public function setJsonapi(array $jsonapi)
135
    {
136
        $this->jsonapi = $jsonapi;
137
    }
138
139
    /**
140
     * @return \Swis\JsonApi\Client\Interfaces\DataInterface
141
     */
142 40
    public function getData()
143
    {
144 40
        return $this->data;
145
    }
146
147
    /**
148
     * @param \Swis\JsonApi\Client\Interfaces\DataInterface $data
149
     *
150
     * @return static
151
     */
152 40
    public function setData(DataInterface $data)
153
    {
154 40
        $this->data = $data;
155
156 40
        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
    public function toArray(): array
178
    {
179
        $document = [];
180
181
        if (!empty($this->getLinks())) {
182
            $document['links'] = $this->links;
183
        }
184
185
        if (!empty($this->getData())) {
186
            $document['data'] = $this->data->toJsonApiArray();
187
        }
188
189
        if ($this->getIncluded()->isNotEmpty()) {
190
            $document['included'] = $this->getIncluded()->toJsonApiArray();
191
        }
192
193
        if (!empty($this->getMeta())) {
194
            $document['meta'] = $this->meta;
195
        }
196
197
        if ($this->hasErrors()) {
198
            $document['errors'] = $this->errors->toArray();
199
        }
200
201
        if (!empty($this->getJsonapi())) {
202
            $document['jsonapi'] = $this->jsonapi;
203
        }
204
205
        return $document;
206
    }
207
}
208