Completed
Push — develop ( be74e2...4aecc6 )
by Michael
03:06
created

JSend::setData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
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 MWebbers\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 array 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 array $data = []
43
	 * @param string|null $message = null
44
	 * @param int|null $code = null
45
	 * @throws \UnexpectedValueException
46
	 */
47
	public function __construct($status, array $data = [], $message = null, $code = null)
48
	{
49
		$this->setStatus($status);
50
		$this->setData($data);
51
		$this->setMessage($message);
52
		$this->setCode($code);
53
	}
54
55
	/**
56
	 * Returns the string representation of the object.
57
	 *
58
	 * @return string the string representation of the object.
59
	 */
60
	public function __toString()
61
	{
62
		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
	public function toArray()
71
	{
72
		$result = ['status' => $this->status];
73
74
		switch ($this->status) {
75
			case self::ERROR:
76
				$result['message'] = $this->message;
77
78
				if ($this->code !== null) {
79
					$result['code'] = $this->code;
80
				}
81
82
				if (!empty($this->data)) {
83
					$result['data'] = $this->data;
84
				}
85
86
				break;
87
88
			default:
89
				$result['data'] = $this->data;
90
91
				break;
92
		}
93
94
		return $result;
95
	}
96
97
	/**
98
	 * Returns a JSend succes object with the given data.
99
	 *
100
	 * @param array $data = []
101
	 * @return JSend a JSend succes object with the given data.
102
	 */
103
	public static function success(array $data = [])
104
	{
105
		return new self(self::SUCCESS, $data);
106
	}
107
108
	/**
109
	 * Returns a JSend fail object with the given data.
110
	 *
111
	 * @param array $data = []
112
	 * @return JSend a JSend fail object with the given data.
113
	 */
114
	public static function fail(array $data = [])
115
	{
116
		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 array $data = []
125
	 * @return JSend a JSend error object with the given message, code and data.
126
	 */
127
	public static function error($message, $code = null, array $data = [])
128
	{
129
		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
	public static function decode($input)
140
	{
141
		$json = json_decode($input, true);
142
143
		if ($json === null) {
144
			throw new \UnexpectedValueException('JSend JSON can not be decoded.');
145
		}
146
147
		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
	public static function decodeJson(array $json)
158
	{
159
		if (!array_key_exists('status', $json)) {
160
			throw new \UnexpectedValueException('JSend objects require a status.');
161
		}
162
163
		switch ($json['status']) {
164
			case self::SUCCESS:
165
				return self::decodeSucces($json);
166
167
			case self::FAIL:
168
				return self::decodeFail($json);
169
170
			case self::ERROR:
171
				return self::decodeError($json);
172
		}
173
174
		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 View Code Duplication
	public static function decodeSucces(array $json)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
	{
186
		if (!array_key_exists('data', $json)) {
187
			throw new \UnexpectedValueException('JSend success objects require data.');
188
		}
189
190
		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 View Code Duplication
	public static function decodeFail(array $json)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201
	{
202
		if (!array_key_exists('data', $json)) {
203
			throw new \UnexpectedValueException('JSend fail objects require data.');
204
		}
205
206
		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
	public static function decodeError(array $json)
217
	{
218
		if (!array_key_exists('message', $json)) {
219
			throw new \UnexpectedValueException('JSend error objects require a message.');
220
		}
221
222
		$code = array_key_exists('code', $json) ? $json['code'] : null;
223
		$data = array_key_exists('data', $json) ? $json['data'] : null;
224
225
		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
	public function encode()
234
	{
235
		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
	public function isSuccess()
244
	{
245
		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
	public function isFail()
254
	{
255
		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
	public function isError()
264
	{
265
		return self::ERROR === $this->status;
266
	}
267
268
	/**
269
	 * Returns the status.
270
	 *
271
	 * @return string the status.
272
	 */
273
	public function getStatus()
274
	{
275
		return $this->status;
276
	}
277
278
	/**
279
	 * Set the status.
280
	 *
281
	 * @param string $status
282
	 * @return JSend $this
283
	 * @throws \UnexpectedValueException
284
	 */
285
	public function setStatus($status)
286
	{
287
		if ($status !== self::SUCCESS && $status !== self::FAIL && $status !== self::ERROR) {
288
			throw new \UnexpectedValueException($status . ' is not a valid JSend status.');
289
		}
290
291
		$this->status = $status;
292
293
		return $this;
294
	}
295
296
	/**
297
	 * Returns the data.
298
	 *
299
	 * @return array the data.
300
	 */
301
	public function getData()
302
	{
303
		return $this->data;
304
	}
305
306
	/**
307
	 * Set the data.
308
	 *
309
	 * @param array $data = []
310
	 * @return JSend $this
311
	 */
312
	public function setData(array $data = [])
313
	{
314
		$this->data = $data;
315
316
		return $this;
317
	}
318
319
	/**
320
	 * Returns the message.
321
	 *
322
	 * @return string|null the message.
323
	 */
324
	public function getMessage()
325
	{
326
		return $this->message;
327
	}
328
329
	/**
330
	 * Set the message.
331
	 *
332
	 * @param string|null $message = null
333
	 * @return JSend $this
334
	 */
335
	public function setMessage($message = null)
336
	{
337
		$this->message = $message;
338
339
		return $this;
340
	}
341
342
	/**
343
	 * Returns the code.
344
	 *
345
	 * @return int|null the code.
346
	 */
347
	public function getCode()
348
	{
349
		return $this->code;
350
	}
351
352
	/**
353
	 * Set the code.
354
	 *
355
	 * @param int|null $code = null
356
	 * @return JSend $this
357
	 */
358
	public function setCode($code = null)
359
	{
360
		$this->code = $code;
361
362
		return $this;
363
	}
364
365
	/**
366
	 * Sends the JSend object.
367
	 *
368
	 * @return void
369
	 */
370
	public function send()
371
	{
372
		if ($this->status === self::FAIL) {
373
			http_response_code(400);
374
		} elseif ($this->status === self::ERROR) {
375
			http_response_code(500);
376
		}
377
378
		header('Content-Type: application/json');
379
		echo (string) $this;
380
	}
381
}
382