1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the JSend package. |
5
|
|
|
* |
6
|
|
|
* @author Michael Webbers <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2015, Michael Webbers |
8
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache v2 License |
9
|
|
|
* @version 1.0.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace miWebb\JSend; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The JSend class. |
16
|
|
|
* |
17
|
|
|
* @since 1.0.0 |
18
|
|
|
* @see http://labs.omniti.com/labs/jsend |
19
|
|
|
*/ |
20
|
|
|
class JSend |
21
|
|
|
{ |
22
|
|
|
const SUCCESS = 'success'; |
23
|
|
|
const FAIL = 'fail'; |
24
|
|
|
const ERROR = 'error'; |
25
|
|
|
|
26
|
|
|
/** @var string The status. */ |
27
|
|
|
private $status; |
28
|
|
|
|
29
|
|
|
/** @var mixed The data. */ |
30
|
|
|
private $data; |
31
|
|
|
|
32
|
|
|
/** @var string|null The message. */ |
33
|
|
|
private $message; |
34
|
|
|
|
35
|
|
|
/** @var int|null The code. */ |
36
|
|
|
private $code; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Construct a JSend object with the given status, data, message and code. |
40
|
|
|
* |
41
|
|
|
* @param string $status |
42
|
|
|
* @param mixed $data = null |
43
|
|
|
* @param string|null $message = null |
44
|
|
|
* @param int|null $code = null |
45
|
|
|
* @throws \UnexpectedValueException |
46
|
|
|
*/ |
47
|
32 |
|
public function __construct($status, $data = null, $message = null, $code = null) |
48
|
|
|
{ |
49
|
32 |
|
$this->setStatus($status); |
50
|
32 |
|
$this->setData($data); |
51
|
32 |
|
$this->setMessage($message); |
52
|
32 |
|
$this->setCode($code); |
53
|
32 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns the string representation of the object. |
57
|
|
|
* |
58
|
|
|
* @return string the string representation of the object. |
59
|
|
|
*/ |
60
|
7 |
|
public function __toString() |
61
|
|
|
{ |
62
|
7 |
|
return $this->encode(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns the array representation of the object. |
67
|
|
|
* |
68
|
|
|
* @return array the array representation of the object. |
69
|
|
|
*/ |
70
|
9 |
|
public function toArray() |
71
|
|
|
{ |
72
|
9 |
|
$result = ['status' => $this->status]; |
73
|
|
|
|
74
|
9 |
|
switch ($this->status) { |
75
|
9 |
|
case self::ERROR: |
76
|
5 |
|
$result['message'] = $this->message; |
77
|
|
|
|
78
|
5 |
|
if ($this->code !== null) { |
79
|
5 |
|
$result['code'] = $this->code; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
//if (!empty($this->data)) { |
83
|
5 |
|
$result['data'] = $this->data; |
84
|
|
|
//} |
85
|
|
|
|
86
|
5 |
|
break; |
87
|
|
|
|
88
|
|
|
default: |
89
|
7 |
|
$result['data'] = $this->data; |
90
|
|
|
|
91
|
7 |
|
break; |
92
|
|
|
} |
93
|
|
|
|
94
|
9 |
|
return $result; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns a JSend succes object with the given data. |
99
|
|
|
* |
100
|
|
|
* @param mixed $data = null |
101
|
|
|
* @return JSend a JSend succes object with the given data. |
102
|
|
|
*/ |
103
|
2 |
|
public static function success($data = null) |
104
|
|
|
{ |
105
|
2 |
|
return new self(self::SUCCESS, $data); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns a JSend fail object with the given data. |
110
|
|
|
* |
111
|
|
|
* @param mixed $data = null |
112
|
|
|
* @return JSend a JSend fail object with the given data. |
113
|
|
|
*/ |
114
|
2 |
|
public static function fail($data = null) |
115
|
|
|
{ |
116
|
2 |
|
return new self(self::FAIL, $data); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns a JSend error object with the given message, code and data. |
121
|
|
|
* |
122
|
|
|
* @param string $message |
123
|
|
|
* @param int|null $code = null |
124
|
|
|
* @param mixed $data = null |
125
|
|
|
* @return JSend a JSend error object with the given message, code and data. |
126
|
|
|
*/ |
127
|
2 |
|
public static function error($message, $code = null, $data = null) |
128
|
|
|
{ |
129
|
2 |
|
return new self(self::ERROR, $data, $message, $code); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns the decoded JSend object. |
134
|
|
|
* |
135
|
|
|
* @param string $input |
136
|
|
|
* @return JSend the decoded JSend object. |
137
|
|
|
* @throws \UnexpectedValueException |
138
|
|
|
*/ |
139
|
7 |
|
public static function decode($input) |
140
|
|
|
{ |
141
|
7 |
|
$json = json_decode($input, true); |
142
|
|
|
|
143
|
7 |
|
if ($json === null) { |
144
|
1 |
|
throw new \UnexpectedValueException('JSend JSON can not be decoded.'); |
145
|
|
|
} |
146
|
|
|
|
147
|
6 |
|
return self::decodeJson($json); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns the decoded JSend input. |
152
|
|
|
* |
153
|
|
|
* @param array $json |
154
|
|
|
* @return JSend the decoded JSend input. |
155
|
|
|
* @throws \UnexpectedValueException |
156
|
|
|
*/ |
157
|
6 |
|
public static function decodeJson(array $json) |
158
|
|
|
{ |
159
|
6 |
|
if (!array_key_exists('status', $json)) { |
160
|
1 |
|
throw new \UnexpectedValueException('JSend objects require a status.'); |
161
|
|
|
} |
162
|
|
|
|
163
|
5 |
|
switch ($json['status']) { |
164
|
5 |
|
case self::SUCCESS: |
165
|
2 |
|
return self::decodeSucces($json); |
166
|
|
|
|
167
|
4 |
|
case self::FAIL: |
168
|
2 |
|
return self::decodeFail($json); |
169
|
|
|
|
170
|
3 |
|
case self::ERROR: |
171
|
2 |
|
return self::decodeError($json); |
172
|
|
|
} |
173
|
|
|
|
174
|
1 |
|
throw new \UnexpectedValueException($json['status'] . ' is not a valid JSend status.'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Returns the decoded jSend succes object. |
179
|
|
|
* |
180
|
|
|
* @param array $json |
181
|
|
|
* @return JSend the decoded jSend succes object. |
182
|
|
|
* @throws \UnexpectedValueException |
183
|
|
|
*/ |
184
|
2 |
|
public static function decodeSucces(array $json) |
185
|
|
|
{ |
186
|
2 |
|
if (!array_key_exists('data', $json)) { |
187
|
1 |
|
throw new \UnexpectedValueException('JSend success objects require data.'); |
188
|
|
|
} |
189
|
|
|
|
190
|
1 |
|
return self::success($json['data']); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Returns the decoded jSend fail object. |
195
|
|
|
* |
196
|
|
|
* @param array $json |
197
|
|
|
* @return JSend the decoded jSend fail object. |
198
|
|
|
* @throws \UnexpectedValueException |
199
|
|
|
*/ |
200
|
2 |
|
public static function decodeFail(array $json) |
201
|
|
|
{ |
202
|
2 |
|
if (!array_key_exists('data', $json)) { |
203
|
1 |
|
throw new \UnexpectedValueException('JSend fail objects require data.'); |
204
|
|
|
} |
205
|
|
|
|
206
|
1 |
|
return self::fail($json['data']); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Returns the decoded jSend error object. |
211
|
|
|
* |
212
|
|
|
* @param array $json |
213
|
|
|
* @return JSend the decoded jSend error object. |
214
|
|
|
* @throws \UnexpectedValueException |
215
|
|
|
*/ |
216
|
2 |
|
public static function decodeError(array $json) |
217
|
|
|
{ |
218
|
2 |
|
if (!array_key_exists('message', $json)) { |
219
|
1 |
|
throw new \UnexpectedValueException('JSend error objects require a message.'); |
220
|
|
|
} |
221
|
|
|
|
222
|
1 |
|
$code = array_key_exists('code', $json) ? $json['code'] : null; |
223
|
1 |
|
$data = array_key_exists('data', $json) ? $json['data'] : null; |
224
|
|
|
|
225
|
1 |
|
return self::error($json['message'], $code, $data); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Returns the encoded JSend object. |
230
|
|
|
* |
231
|
|
|
* @return string the encoded JSend object. |
232
|
|
|
*/ |
233
|
8 |
|
public function encode() |
234
|
|
|
{ |
235
|
8 |
|
return json_encode($this->toArray()); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Returns true if the status is success. |
240
|
|
|
* |
241
|
|
|
* @return bool true if the status is success. |
242
|
|
|
*/ |
243
|
1 |
|
public function isSuccess() |
244
|
|
|
{ |
245
|
1 |
|
return self::SUCCESS === $this->status; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Returns true if the status is fail. |
250
|
|
|
* |
251
|
|
|
* @return bool true if the status is fail. |
252
|
|
|
*/ |
253
|
1 |
|
public function isFail() |
254
|
|
|
{ |
255
|
1 |
|
return self::FAIL === $this->status; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Returns true if the status is error. |
260
|
|
|
* |
261
|
|
|
* @return bool true if the status is error. |
262
|
|
|
*/ |
263
|
1 |
|
public function isError() |
264
|
|
|
{ |
265
|
1 |
|
return self::ERROR === $this->status; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Returns the status. |
270
|
|
|
* |
271
|
|
|
* @return string the status. |
272
|
|
|
*/ |
273
|
2 |
|
public function getStatus() |
274
|
|
|
{ |
275
|
2 |
|
return $this->status; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Set the status. |
280
|
|
|
* |
281
|
|
|
* @param string $status |
282
|
|
|
* @return JSend $this |
283
|
|
|
* @throws \UnexpectedValueException |
284
|
|
|
*/ |
285
|
32 |
|
public function setStatus($status) |
286
|
|
|
{ |
287
|
32 |
|
if ($status !== self::SUCCESS && $status !== self::FAIL && $status !== self::ERROR) { |
288
|
1 |
|
throw new \UnexpectedValueException($status . ' is not a valid JSend status.'); |
289
|
|
|
} |
290
|
|
|
|
291
|
32 |
|
$this->status = $status; |
292
|
|
|
|
293
|
32 |
|
return $this; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Returns the data. |
298
|
|
|
* |
299
|
|
|
* @return mixed the data. |
300
|
|
|
*/ |
301
|
3 |
|
public function getData() |
302
|
|
|
{ |
303
|
3 |
|
return $this->data; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Set the data. |
308
|
|
|
* |
309
|
|
|
* @param mixed $data = null |
310
|
|
|
* @return JSend $this |
311
|
|
|
*/ |
312
|
32 |
|
public function setData($data = null) |
313
|
|
|
{ |
314
|
32 |
|
$this->data = $data; |
315
|
|
|
|
316
|
32 |
|
return $this; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Returns the message. |
321
|
|
|
* |
322
|
|
|
* @return string|null the message. |
323
|
|
|
*/ |
324
|
2 |
|
public function getMessage() |
325
|
|
|
{ |
326
|
2 |
|
return $this->message; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Set the message. |
331
|
|
|
* |
332
|
|
|
* @param string|null $message = null |
333
|
|
|
* @return JSend $this |
334
|
|
|
*/ |
335
|
32 |
|
public function setMessage($message = null) |
336
|
|
|
{ |
337
|
32 |
|
$this->message = $message; |
338
|
|
|
|
339
|
32 |
|
return $this; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Returns the code. |
344
|
|
|
* |
345
|
|
|
* @return int|null the code. |
346
|
|
|
*/ |
347
|
2 |
|
public function getCode() |
348
|
|
|
{ |
349
|
2 |
|
return $this->code; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Set the code. |
354
|
|
|
* |
355
|
|
|
* @param int|null $code = null |
356
|
|
|
* @return JSend $this |
357
|
|
|
*/ |
358
|
32 |
|
public function setCode($code = null) |
359
|
|
|
{ |
360
|
32 |
|
$this->code = $code; |
361
|
|
|
|
362
|
32 |
|
return $this; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Sends the JSend object. |
367
|
|
|
* |
368
|
|
|
* @return void |
369
|
|
|
*/ |
370
|
6 |
|
public function send() |
371
|
|
|
{ |
372
|
6 |
|
if ($this->status === self::FAIL) { |
373
|
2 |
|
http_response_code(400); |
374
|
4 |
|
} elseif ($this->status === self::ERROR) { |
375
|
2 |
|
http_response_code(500); |
376
|
|
|
} |
377
|
|
|
|
378
|
6 |
|
header('Content-Type: application/json'); |
379
|
6 |
|
echo (string) $this; |
380
|
6 |
|
} |
381
|
|
|
} |
382
|
|
|
|