1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Demv\JSend; |
4
|
|
|
|
5
|
|
|
use function Dgame\Ensurance\ensure; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class AbstractJSendResponse |
9
|
|
|
* @package Demv\JSend |
10
|
|
|
*/ |
11
|
|
|
abstract class AbstractJSendResponse implements JSendResponseInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var StatusInterface |
15
|
|
|
*/ |
16
|
|
|
private $status; |
17
|
|
|
/** |
18
|
|
|
* @var array|null |
19
|
|
|
*/ |
20
|
|
|
private $data; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* AbstractJSendResponse constructor. |
24
|
|
|
* |
25
|
|
|
* @param StatusInterface $status |
26
|
|
|
* @param array|null $data |
27
|
|
|
*/ |
28
|
|
|
public function __construct(StatusInterface $status, array $data = null) |
29
|
|
|
{ |
30
|
|
|
$this->status = $status; |
31
|
|
|
$this->data = $data; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return StatusInterface |
36
|
|
|
*/ |
37
|
|
|
final public function getStatus(): StatusInterface |
38
|
|
|
{ |
39
|
|
|
return $this->status; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
final public function getData(): array |
46
|
|
|
{ |
47
|
|
|
return $this->data ?? []; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public function asArray(): array |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
'status' => (string) $this->status, |
57
|
|
|
'data' => $this->data |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @internal |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public function jsonSerialize(): array |
67
|
|
|
{ |
68
|
|
|
return $this->asArray(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param int|null $code |
73
|
|
|
*/ |
74
|
|
|
public function respond(int $code = null): void |
75
|
|
|
{ |
76
|
|
|
$code = $code ?? JSend::getDefaultHttpStatusCode($this); |
77
|
|
|
ensure($code)->isInt()->isBetween(100, 511); |
78
|
|
|
|
79
|
|
|
header('Content-Type: application/json; charset="UTF-8"', true, $code); |
80
|
|
|
print json_encode($this); |
81
|
|
|
exit; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param array|null $data |
86
|
|
|
* |
87
|
|
|
* @return AbstractJSendResponse |
88
|
|
|
*/ |
89
|
|
|
public static function success(array $data = null): self |
90
|
|
|
{ |
91
|
|
|
return new static(Status::success(), $data); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array|null $data |
96
|
|
|
* |
97
|
|
|
* @return AbstractJSendResponse |
98
|
|
|
*/ |
99
|
|
|
public static function fail(array $data = null): self |
100
|
|
|
{ |
101
|
|
|
return new static(Status::fail(), $data); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $message |
106
|
|
|
* @param int|null $code |
107
|
|
|
* @param array|null $data |
108
|
|
|
* |
109
|
|
|
* @return AbstractJSendResponse |
110
|
|
|
*/ |
111
|
|
|
public static function error(string $message, int $code = null, array $data = null): self |
112
|
|
|
{ |
113
|
|
|
return new JSendErrorResponse( |
114
|
|
|
Status::error(), |
115
|
|
|
[ |
116
|
|
|
'message' => $message, |
117
|
|
|
'code' => $code, |
118
|
|
|
'data' => $data |
119
|
|
|
] |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|