1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
<?php
/**
* Phergie
*
* PHP version 5
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.
* It is also available through the world-wide-web at this URL:
* http://phergie.org/license
*
* @category Phergie
* @package Phergie_Plugin_Http
* @author Phergie Development Team <team@phergie.org>
* @copyright 2008-2010 Phergie Development Team (http://phergie.org)
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Plugin_Http
*/
/**
* Data structure for HTTP response information.
*
* @category Phergie
* @package Phergie_Plugin_Http
* @author Phergie Development Team <team@phergie.org>
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Plugin_Http
*/
class Phergie_Plugin_Http_Response
{
/**
* HTTP response code or 0 if no HTTP response was received
*
* @var string
*/
protected $code;
/**
* HTTP response strings
*
* @var array
*/
protected $codeStrings = array(
0 => 'No Response',
100 => 'Continue',
200 => 'OK',
201 => 'Created',
204 => 'No Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
408 => 'Request Timeout',
410 => 'Gone',
413 => 'Request Entity Too Large',
414 => 'Request URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Method Not Implemented',
503 => 'Service Unavailable',
506 => 'Variant Also Negotiates'
);
/**
* Description of the HTTP response code or the error message if no HTTP
* response was received
*
* @var string
*/
protected $message;
/**
* Content of the response body, decoded for supported content types
*
* @var mixed
*/
protected $content;
/**
* Associative array mapping response header names to their values
*
* @var array
*/
protected $headers;
/**
* Associative array containing other metadata about the response
*
* @var array
*/
protected $meta;
/**
* Sets the HTTP response code.
*
* @param string $code Response code
*
* @return Phergie_Plugin_Http_Response Provides a fluent interface
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Returns the HTTP response code.
*
* @return string Response code
*/
public function getCode()
{
return $this->code;
}
/**
* Returns the HTTP response code text.
*
* @return string Response code text
*/
public function getCodeAsString()
{
$code = $this->code;
if (!isset($this->codeStrings[$code])) {
return 'Unkown HTTP Status';
}
return $this->codeStrings[$code];
}
/**
* Returns whether the response indicates a client- or server-side error.
*
* @return bool TRUE if the response indicates an error, FALSE otherwise
*/
public function isError()
{
switch (substr($this->code, 0, 1)) {
case '0':
case '4':
case '5':
return true;
default:
return false;
}
}
/**
* Sets the HTTP response description.
*
* @param string $message Response description
*
* @return Phergie_Plugin_Http_Response Provides a fluent interface
*/
public function setMessage($message)
{
$this->message = $message;
return $this;
}
/**
* Returns the HTTP response description.
*
* @return string
*/
public function getMessage()
{
return $this->message;
}
/**
* Sets the content of the response body.
*
* @param mixed $content Response body content
*
* @return Phergie_Plugin_Http_Response Provides a fluent interface
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Returns the content of the response body.
*
* @return mixed Response body content, decoded for supported content
* types
*/
public function getContent()
{
return $this->content;
}
/**
* Sets the response headers.
*
* @param array $headers Associative array of response headers indexed
* by header name
*
* @return Phergie_Plugin_Http_Response Provides a fluent interface
*/
public function setHeaders(array $headers)
{
$names = array_map('strtolower', array_keys($headers));
$values = array_values($headers);
$this->headers = array_combine($names, $values);
return $this;
}
/**
* Returns all response headers or the value of a single specified
* response header.
*
* @param string $name Optional name of a single header for which the
* associated value should be returned
*
* @return array|string Associative array of all header values, a string
* containing the value of the header indicated by $name if one
* is set, or null if one is not
*/
public function getHeaders($name = null)
{
if ($name) {
$name = strtolower($name);
if (empty($this->headers[$name])) {
return null;
}
return $this->headers[$name];
}
return $this->headers;
}
/**
* Sets the response metadata.
*
* @param array $meta Associative array of response metadata
*
* @return Phergie_Plugin_Http_Response Provides a fluent interface
*/
public function setMeta(array $meta)
{
$this->meta = $meta;
return $this;
}
/**
* Returns all metadata or the value of a single specified metadatum.
*
* @param string $name Optional name of a single metadatum for which the
* associated value should be returned
*
* @return array|string|null Associative array of all metadata values, a
* string containing the value of the metadatum indicated by
* $name if one is set, or null if one is not
*/
public function getMeta($name = null)
{
if ($name) {
if (empty($this->meta[$name])) {
return null;
}
return $this->meta[$name];
}
return $this->meta;
}
}
|