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