Passed
Push — master ( e9bd9f...92cbc5 )
by Michael
03:05
created

ErrorObject::toArray()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 17
cp 0
rs 4.6666
c 0
b 0
f 0
cc 8
eloc 17
nc 128
nop 0
crap 72
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Document;
5
6
use Mikemirten\Component\JsonApi\Document\Behaviour\LinksAwareInterface;
7
use Mikemirten\Component\JsonApi\Document\Behaviour\LinksContainer;
8
use Mikemirten\Component\JsonApi\Document\Behaviour\MetadataAwareInterface;
9
use Mikemirten\Component\JsonApi\Document\Behaviour\MetadataContainer;
10
11
/**
12
 * Error object
13
 *
14
 * @see http://jsonapi.org/format/#errors
15
 *
16
 * @package Mikemirten\Component\JsonApi\Document
17
 */
18
class ErrorObject implements LinksAwareInterface, MetadataAwareInterface
19
{
20
    use LinksContainer;
21
    use MetadataContainer;
22
23
    /**
24
     * Identifier of occurrence
25
     *
26
     * @var string
27
     */
28
    protected $id;
29
30
    /**
31
     * HTTP status code
32
     *
33
     * @var string
34
     */
35
    protected $status;
36
37
    /**
38
     * Application specific code
39
     *
40
     * @var string
41
     */
42
    protected $code;
43
44
    /**
45
     * Human-readable summary
46
     *
47
     * @var string
48
     */
49
    protected $title;
50
51
    /**
52
     * Human-readable explanation
53
     *
54
     * @var string
55
     */
56
    protected $detail;
57
58
    /**
59
     * ErrorObject constructor.
60
     *
61
     * @param string $id
62
     * @param string $title
63
     */
64
    public function __construct(string $id = null, string $title = null)
65
    {
66
        $this->id    = $id;
67
        $this->title = $title;
68
    }
69
70
    /**
71
     * Set ID of occurrence
72
     *
73
     * @param string $id
74
     */
75
    public function setId(string $id)
76
    {
77
        $this->id = $id;
78
    }
79
80
    /**
81
     * Has ID of occurrence ?
82
     *
83
     * @return bool
84
     */
85
    public function hasId(): bool
86
    {
87
        return $this->id !== null;
88
    }
89
90
    /**
91
     * Get ID of occurrence
92
     *
93
     * @return string
94
     */
95
    public function getId(): string
96
    {
97
        return $this->id;
98
    }
99
100
    /**
101
     * Set HTTP status code
102
     *
103
     * @param string $status
104
     */
105
    public function setStatus(string $status)
106
    {
107
        $this->status = $status;
108
    }
109
110
    /**
111
     * Has HTTP status code ?
112
     *
113
     * @return bool
114
     */
115
    public function hasStatus(): bool
116
    {
117
        return $this->status !== null;
118
    }
119
120
    /**
121
     * Get HTTP status code
122
     *
123
     * @return string
124
     */
125
    public function getStatus(): string
126
    {
127
        return $this->status;
128
    }
129
130
    /**
131
     * Set application specific code
132
     *
133
     * @param string $code
134
     */
135
    public function setCode(string $code)
136
    {
137
        $this->code = $code;
138
    }
139
140
    /**
141
     * Has application specific code
142
     *
143
     * @return bool
144
     */
145
    public function hasCode(): bool
146
    {
147
        return $this->code !== null;
148
    }
149
150
    /**
151
     * Get application specific code
152
     *
153
     * @return string
154
     */
155
    public function getCode(): string
156
    {
157
        return $this->code;
158
    }
159
160
    /**
161
     * Set title
162
     *
163
     * @param string $title
164
     */
165
    public function setTitle(string $title)
166
    {
167
        $this->title = $title;
168
    }
169
170
    /**
171
     * Has title
172
     *
173
     * @return bool
174
     */
175
    public function hasTitle(): bool
176
    {
177
        return $this->title !== null;
178
    }
179
180
    /**
181
     * Get title
182
     *
183
     * @return string
184
     */
185
    public function getTitle(): string
186
    {
187
        return $this->title;
188
    }
189
190
    /**
191
     * Set detailed explanation
192
     *
193
     * @param string $detail
194
     */
195
    public function setDetail(string $detail)
196
    {
197
        $this->detail = $detail;
198
    }
199
200
    /**
201
     * Has detailed explanation ?
202
     *
203
     * @return bool
204
     */
205
    public function hasDetail(): bool
206
    {
207
        return $this->detail !== null;
208
    }
209
210
    /**
211
     * Get detailed explanation
212
     *
213
     * @return string
214
     */
215
    public function getDetail(): string
216
    {
217
        return $this->detail;
218
    }
219
220
    /**
221
     * Cast to an array
222
     *
223
     * @return array
224
     */
225
    public function toArray(): array
226
    {
227
        $data = [];
228
229
        if ($this->hasId()) {
230
            $data['id'] = $this->getId();
231
        }
232
233
        if ($this->hasStatus()) {
234
            $data['status'] = $this->getStatus();
235
        }
236
237
        if ($this->hasCode()) {
238
            $data['code'] = $this->getCode();
239
        }
240
241
        if ($this->hasTitle()) {
242
            $data['title'] = $this->getTitle();
243
        }
244
245
        if ($this->hasDetail()) {
246
            $data['detail'] = $this->getDetail();
247
        }
248
249
        if ($this->hasLinks()) {
250
            $data['links'] = $this->linksToArray();
251
        }
252
253
        if ($this->hasMetadata()) {
254
            $data['meta'] = $this->getMetadata();
255
        }
256
257
        return $data;
258
    }
259
}