Passed
Push — master ( 92cbc5...dc473e )
by Michael
02:54
created

ErrorObject::setCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 10
    public function __construct(string $id = null, string $title = null)
65
    {
66 10
        $this->id    = $id;
67 10
        $this->title = $title;
68 10
    }
69
70
    /**
71
     * Set ID of occurrence
72
     *
73
     * @param string $id
74
     */
75 2
    public function setId(string $id)
76
    {
77 2
        $this->id = $id;
78 2
    }
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 1
    public function getId(): string
96
    {
97 1
        return $this->id;
98
    }
99
100
    /**
101
     * Set HTTP status code
102
     *
103
     * @param string $status
104
     */
105 2
    public function setStatus(string $status)
106
    {
107 2
        $this->status = $status;
108 2
    }
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 1
    public function getStatus(): string
126
    {
127 1
        return $this->status;
128
    }
129
130
    /**
131
     * Set application specific code
132
     *
133
     * @param string $code
134
     */
135 2
    public function setCode(string $code)
136
    {
137 2
        $this->code = $code;
138 2
    }
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 1
    public function getCode(): string
156
    {
157 1
        return $this->code;
158
    }
159
160
    /**
161
     * Set title
162
     *
163
     * @param string $title
164
     */
165 2
    public function setTitle(string $title)
166
    {
167 2
        $this->title = $title;
168 2
    }
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 1
    public function getTitle(): string
186
    {
187 1
        return $this->title;
188
    }
189
190
    /**
191
     * Set detailed explanation
192
     *
193
     * @param string $detail
194
     */
195 2
    public function setDetail(string $detail)
196
    {
197 2
        $this->detail = $detail;
198 2
    }
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 1
    public function getDetail(): string
216
    {
217 1
        return $this->detail;
218
    }
219
220
    /**
221
     * Cast to an array
222
     *
223
     * @return array
224
     */
225 3
    public function toArray(): array
226
    {
227 3
        $data = array_filter([
228 3
            'id'     => $this->id,
229 3
            'status' => $this->status,
230 3
            'code'   => $this->code,
231 3
            'title'  => $this->title,
232 3
            'detail' => $this->detail
233
        ]);
234
235 3
        if ($this->hasLinks()) {
236 1
            $data['links'] = $this->linksToArray();
237
        }
238
239 3
        if ($this->hasMetadata()) {
240 1
            $data['meta'] = $this->getMetadata();
241
        }
242
243 3
        return $data;
244
    }
245
}