1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GinoPane\NanoRest\Response; |
4
|
|
|
|
5
|
|
|
use GinoPane\NanoHttpStatus\NanoHttpStatus; |
6
|
|
|
|
7
|
|
|
use GinoPane\NanoRest\{ |
8
|
|
|
Exceptions\ResponseContextException, Request\RequestContext, Supplemental\HeadersProperty |
9
|
|
|
}; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ResponseContext |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
abstract class ResponseContext |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Constant for JSON response type |
19
|
|
|
*/ |
20
|
|
|
const RESPONSE_TYPE_JSON = "JSON"; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Stored raw content |
24
|
|
|
* |
25
|
|
|
* @var string|null |
26
|
|
|
*/ |
27
|
|
|
protected $content = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Response status code |
31
|
|
|
* |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $httpStatusCode; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Request context that was used for request |
38
|
|
|
* |
39
|
|
|
* @var RequestContext |
40
|
|
|
*/ |
41
|
|
|
protected $requestContext = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get raw result data |
45
|
|
|
* |
46
|
|
|
* @param array $options |
47
|
|
|
* |
48
|
|
|
* @return string|null |
49
|
|
|
*/ |
50
|
|
|
abstract public function getRaw(array $options = array()): ?string; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get result data as array |
54
|
|
|
* |
55
|
|
|
* @param array $options |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
abstract public function getArray(array $options = array()): array; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get result data as object |
63
|
|
|
* |
64
|
|
|
* @param array $options |
65
|
|
|
* |
66
|
|
|
* @return object|null |
67
|
|
|
*/ |
68
|
|
|
abstract public function getObject(array $options = array()); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Makes sure that $content is valid for this AbstractResponseContext instance |
72
|
|
|
* |
73
|
|
|
* @param string $content |
74
|
|
|
* |
75
|
|
|
* @throws ResponseContextException |
76
|
|
|
* |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
abstract protected function assertIsValid(string $content): bool; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* String representation of response for debug purposes |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
abstract public function __toString(); |
87
|
|
|
|
88
|
|
|
use HeadersProperty; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* ResponseContext constructor |
92
|
|
|
* |
93
|
|
|
* @param string|null $content |
94
|
|
|
*/ |
95
|
|
|
public function __construct(string $content = null) |
96
|
|
|
{ |
97
|
|
|
if (!is_null($content)) { |
98
|
|
|
$this->setContent($content); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set result content. |
104
|
|
|
* |
105
|
|
|
* @param string $content |
106
|
|
|
* |
107
|
|
|
* @return ResponseContext |
108
|
|
|
*/ |
109
|
|
|
public function setContent(string $content): ResponseContext |
110
|
|
|
{ |
111
|
|
|
$this->assertIsValid($content); |
112
|
|
|
|
113
|
|
|
$this->content = $content; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get response content |
120
|
|
|
* |
121
|
|
|
* @return null|string |
122
|
|
|
*/ |
123
|
|
|
public function getContent(): ?string |
124
|
|
|
{ |
125
|
|
|
return $this->content; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get HTTP status code from response |
130
|
|
|
* |
131
|
|
|
* @return int |
132
|
|
|
*/ |
133
|
|
|
public function getHttpStatusCode(): int |
134
|
|
|
{ |
135
|
|
|
return $this->httpStatusCode; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get HTTP status message from response |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getHttpStatusMessage(): string |
144
|
|
|
{ |
145
|
|
|
return (new NanoHttpStatus())->getMessage($this->httpStatusCode); |
146
|
|
|
} |
147
|
|
|
/** |
148
|
|
|
* Set HTTP status code for response |
149
|
|
|
* |
150
|
|
|
* @param int|string $httpStatusCode |
151
|
|
|
* |
152
|
|
|
* @return ResponseContext |
153
|
|
|
*/ |
154
|
|
|
public function setHttpStatusCode(int $httpStatusCode): ResponseContext |
155
|
|
|
{ |
156
|
|
|
$this->httpStatusCode = $httpStatusCode; |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set request context that is being used for request |
163
|
|
|
* |
164
|
|
|
* @param RequestContext $request |
165
|
|
|
* |
166
|
|
|
* @return ResponseContext |
167
|
|
|
*/ |
168
|
|
|
public function setRequestContext(RequestContext $request): ResponseContext |
169
|
|
|
{ |
170
|
|
|
$this->requestContext = $request; |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get request context for this request |
177
|
|
|
* |
178
|
|
|
* @return RequestContext |
179
|
|
|
*/ |
180
|
|
|
public function getRequestContext() |
181
|
|
|
{ |
182
|
|
|
return $this->requestContext ?: new RequestContext(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Returns if current response context has HTTP error status code set |
187
|
|
|
* |
188
|
|
|
* @return bool |
189
|
|
|
*/ |
190
|
|
|
public function hasHttpError(): bool |
191
|
|
|
{ |
192
|
|
|
$httpStatus = new NanoHttpStatus(); |
193
|
|
|
|
194
|
|
|
return $httpStatus->isClientError($this->httpStatusCode) || $httpStatus->isServerError($this->httpStatusCode); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Create response context instance |
199
|
|
|
* |
200
|
|
|
* @param string $type |
201
|
|
|
* @param string|null $content |
202
|
|
|
* |
203
|
|
|
* @return ResponseContext |
204
|
|
|
*/ |
205
|
|
|
public static function getByType(string $type, string $content = null): ResponseContext |
206
|
|
|
{ |
207
|
|
|
switch ($type) { |
208
|
|
|
case self::RESPONSE_TYPE_JSON: |
209
|
|
|
return new JsonResponseContext($content); |
210
|
|
|
default: |
211
|
|
|
return new DummyResponseContext($content); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|