WRestResponse.php
2.1 KB
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
<?php
/**
* @author Weavora Team <hello@weavora.com>
* @link http://weavora.com
* @copyright Copyright (c) 2011 Weavora LLC
*/
abstract class WRestResponse
{
protected $_body = '';
protected $status = 200;
public abstract function getContentType();
public abstract function setParams($params = array());
public function getBody()
{
return $this->_body;
}
/**
*
* @param string $type
* @return WRestResponse
*/
public static function factory($type)
{
$className = ucfirst($type) . "Response";
return new $className();
}
/**
*
* @param string $status
* @return WRestResponse
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
public function getStatusCodeMessage($status, $isCode = true)
{
$codes = Array(
200 => array('OK' => 'OK'),
400 => array('Bad Request' => 'Bad Request'),
401 => array('Unauthorized' => 'You must be authorized to view this page.'),
402 => array('Payment Required' => 'Payment Required'),
403 => array('Forbidden' => 'Forbidden'),
404 => array('Not Found' => 'The requested URL was not found.'),
500 => array('Internal Server Error' => 'The server encountered an error processing your request.'),
501 => array('Not Implemented' => 'The requested method is not implemented.'),
);
$result = "";
if (isset($codes[$status])) {
$code = ($isCode) ? array_keys($codes[$status]) : array_values($codes[$status]);
$result = array_pop($code);
}
return $result;
}
public function getErrorMessage($status){
return array(
'error' => array(
'code' => $status,
'title' => $this->getStatusCodeMessage($status),
'message' => $this->getStatusCodeMessage($status, false)
)
);
}
public function getHeaders(){
$headers = array();
$status = $this->status;
// set the status
$statusHeader = 'HTTP/1.1 ' . $status . ' ' . $this->getStatusCodeMessage($status);
$headers[] = $statusHeader;
// and the content type
$headers[] = 'Content-type: ' . $this->getContentType();
return $headers;
}
public function getStatus(){
return $this->status;
}
}