1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Borobudur-Http package. |
4
|
|
|
* |
5
|
|
|
* (c) Hexacodelabs <http://hexacodelabs.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Borobudur\Http; |
12
|
|
|
|
13
|
|
|
use Borobudur\Http\Exception\InvalidArgumentException; |
14
|
|
|
use Borobudur\Http\Header\Content\ContentEncodingHeader; |
15
|
|
|
use Borobudur\Http\Header\Content\ContentTypeHeader; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Iqbal Maulana <[email protected]> |
19
|
|
|
* @created 8/5/15 |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractResponse |
22
|
|
|
{ |
23
|
|
|
use HttpStatusTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var HeaderBag |
27
|
|
|
*/ |
28
|
|
|
public $headers; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $content; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $version; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
protected $statusCode; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $statusText; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $charset = 'UTF-8'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $encoding; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $contentType; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set protocol version. |
67
|
|
|
* |
68
|
|
|
* @param string $version |
69
|
|
|
* |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function setProtocolVersion($version) |
73
|
|
|
{ |
74
|
|
|
$this->version = $version; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get content. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getContent() |
85
|
|
|
{ |
86
|
|
|
return $this->content; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set content. |
91
|
|
|
* |
92
|
|
|
* @param string $content |
93
|
|
|
* |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function setContent($content) |
97
|
|
|
{ |
98
|
|
|
if (null !== $content && (is_object($content) && !is_callable(array($content, '__toString')))) { |
99
|
|
|
throw new InvalidArgumentException(sprintf( |
100
|
|
|
'Content should be string or object that implementing __toString, "%s" given', |
101
|
|
|
gettype($content) |
102
|
|
|
)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->content = (string) $content; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get return protocol version. |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getProtocolVersion() |
116
|
|
|
{ |
117
|
|
|
return $this->version; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get charset. |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getCharset() |
126
|
|
|
{ |
127
|
|
|
return $this->charset; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set charset. |
132
|
|
|
* |
133
|
|
|
* @param string $charset |
134
|
|
|
* |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
|
|
public function setCharset($charset) |
138
|
|
|
{ |
139
|
|
|
$this->charset = $charset; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set content encoding. |
146
|
|
|
* |
147
|
|
|
* @param string $encoding |
148
|
|
|
* |
149
|
|
|
* @return $this |
150
|
|
|
*/ |
151
|
|
|
public function setContentEncoding($encoding) |
152
|
|
|
{ |
153
|
|
|
$this->headers->set(new ContentEncodingHeader($encoding), true); |
154
|
|
|
$this->encoding = $encoding; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get content encoding. |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
public function getContentEncoding() |
165
|
|
|
{ |
166
|
|
|
return $this->encoding; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get content type. |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function getContentType() |
175
|
|
|
{ |
176
|
|
|
return $this->contentType; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Set content type. |
181
|
|
|
* |
182
|
|
|
* @param string $contentType |
183
|
|
|
* |
184
|
|
|
* @return $this |
185
|
|
|
*/ |
186
|
|
|
public function setContentType($contentType) |
187
|
|
|
{ |
188
|
|
|
$this->headers->set(new ContentTypeHeader($contentType)); |
189
|
|
|
$this->contentType = $contentType; |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Cast Response as string representation. |
196
|
|
|
* |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
abstract public function __toString(); |
200
|
|
|
|
201
|
|
|
} |
202
|
|
|
|