17 protected $statusCode = 200;
19 protected $parameters = array();
20 protected $httpHeaders = array();
22 public static $statusTexts = array(
24 101 =>
'Switching Protocols',
28 203 =>
'Non-Authoritative Information',
30 205 =>
'Reset Content',
31 206 =>
'Partial Content',
32 300 =>
'Multiple Choices',
33 301 =>
'Moved Permanently',
36 304 =>
'Not Modified',
38 307 =>
'Temporary Redirect',
40 401 =>
'Unauthorized',
41 402 =>
'Payment Required',
44 405 =>
'Method Not Allowed',
45 406 =>
'Not Acceptable',
46 407 =>
'Proxy Authentication Required',
47 408 =>
'Request Timeout',
50 411 =>
'Length Required',
51 412 =>
'Precondition Failed',
52 413 =>
'Request Entity Too Large',
53 414 =>
'Request-URI Too Long',
54 415 =>
'Unsupported Media Type',
55 416 =>
'Requested Range Not Satisfiable',
56 417 =>
'Expectation Failed',
57 418 =>
'I\'m a teapot',
58 500 =>
'Internal Server Error',
59 501 =>
'Not Implemented',
61 503 =>
'Service Unavailable',
62 504 =>
'Gateway Timeout',
63 505 =>
'HTTP Version Not Supported',
66 public function __construct($parameters = array(), $statusCode = 200, $headers = array())
68 $this->setParameters($parameters);
69 $this->setStatusCode($statusCode);
70 $this->setHttpHeaders($headers);
82 foreach ($this->httpHeaders
as $name => $value) {
83 $headers[$name] = (array) $value;
87 sprintf(
'HTTP/%s %s %s', $this->
version, $this->statusCode, $this->statusText).
"\r\n".
88 $this->getHttpHeadersAsString($headers).
"\r\n".
89 $this->getResponseBody();
102 return sprintf(
"%s: %s\n", $name, $value);
107 return $this->statusCode;
112 $this->statusCode = (int) $statusCode;
113 if ($this->isInvalid()) {
114 throw new \InvalidArgumentException(sprintf(
'The HTTP status code "%s" is not valid.', $statusCode));
117 $this->statusText =
false === $text ?
'' : (
null === $text ? self::$statusTexts[$this->statusCode] : $text);
122 return $this->statusText;
127 return $this->parameters;
152 $this->httpHeaders = $httpHeaders;
157 $this->httpHeaders[$name] = $value;
162 $this->httpHeaders = array_merge($this->httpHeaders, $httpHeaders);
167 return $this->httpHeaders;
172 return isset($this->httpHeaders[$name]) ? $this->httpHeaders[$name] : $default;
182 $xml = new \SimpleXMLElement(
'<response/>');
184 $xml->addChild($key, $param);
187 return $xml->asXML();
190 throw new \InvalidArgumentException(sprintf(
'The format %s is not supported', $format));
194 public function send($format =
'json')
197 if (headers_sent()) {
203 $this->setHttpHeader(
'Content-Type',
'application/json');
206 $this->setHttpHeader(
'Content-Type',
'text/xml');
210 header(sprintf(
'HTTP/%s %s %s', $this->
version, $this->statusCode, $this->statusText));
212 foreach ($this->getHttpHeaders()
as $name => $header) {
213 header(sprintf(
'%s: %s', $name, $header));
215 echo $this->getResponseBody($format);
218 public function setError($statusCode, $error, $errorDescription =
null, $errorUri =
null)
222 'error_description' => $errorDescription,
225 if (!is_null($errorUri)) {
226 if (strlen($errorUri) > 0 && $errorUri[0] ==
'#') {
228 $errorUri =
'http://tools.ietf.org/html/rfc6749' . $errorUri;
230 $parameters[
'error_uri'] = $errorUri;
233 $httpHeaders = array(
234 'Cache-Control' =>
'no-store'
237 $this->setStatusCode($statusCode);
238 $this->addParameters($parameters);
239 $this->addHttpHeaders($httpHeaders);
241 if (!$this->isClientError() && !$this->isServerError()) {
242 throw new \InvalidArgumentException(sprintf(
'The HTTP status code is not an error ("%s" given).', $statusCode));
246 public function setRedirect($statusCode,
$url, $state =
null, $error =
null, $errorDescription =
null, $errorUri =
null)
249 throw new \InvalidArgumentException(
'Cannot redirect to an empty URL.');
252 $parameters = array();
254 if (!is_null($state)) {
255 $parameters[
'state'] = $state;
258 if (!is_null($error)) {
259 $this->setError(400, $error, $errorDescription, $errorUri);
261 $this->setStatusCode($statusCode);
262 $this->addParameters($parameters);
266 $parts = parse_url(
$url);
267 $sep = isset($parts[
'query']) && count($parts[
'query']) > 0 ?
'&' :
'?';
271 $this->addHttpHeaders(array(
'Location' =>
$url));
273 if (!$this->isRedirection()) {
274 throw new \InvalidArgumentException(sprintf(
'The HTTP status code is not a redirect ("%s" given).', $statusCode));
286 return $this->statusCode < 100 || $this->statusCode >= 600;
296 return $this->statusCode >= 100 && $this->statusCode < 200;
306 return $this->statusCode >= 200 && $this->statusCode < 300;
316 return $this->statusCode >= 300 && $this->statusCode < 400;
326 return $this->statusCode >= 400 && $this->statusCode < 500;
336 return $this->statusCode >= 500 && $this->statusCode < 600;
342 private function getHttpHeadersAsString($headers)
344 if (count($headers) == 0) {
348 $max = max(array_map(
'strlen', array_keys($headers))) + 1;
351 foreach ($headers
as $name => $values) {
352 foreach ($values
as $value) {
353 $content .= sprintf(
"%-{$max}s %s\r\n", $this->beautifyHeaderName($name).
':', $value);
360 private function beautifyHeaderName($name)
362 return preg_replace_callback(
'/\-(.)/', array($this,
'beautifyCallback'), ucfirst($name));
365 private function beautifyCallback($match)
367 return '-'.strtoupper($match[1]);