/
Rest.php
119 lines (105 loc) · 3.06 KB
/
Rest.php
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
<?php
class Zend_Controller_Request_Rest extends Zend_Controller_Request_Http
{
protected $_paramSources = array('_GET', '_POST', '_PUT');
protected $_PUT = array();
public function getPut(){
return $this->_PUT;
}
public function __construct(){
if( isset( $_SERVER['REQUEST_METHOD'] ) ){
switch( $_SERVER['REQUEST_METHOD'] ){
case 'PUT':
parse_str(file_get_contents("php://input"), $this->_PUT);
break;
}
}
}
/**
* Retrieve a parameter
*
* Retrieves a parameter from the instance. Priority is in the order of
* userland parameters (see {@link setParam()}), $_GET, $_POST. If a
* parameter matching the $key is not found, null is returned.
*
* If the $key is an alias, the actual key aliased will be used.
*
* @param mixed $key
* @param mixed $default Default value to use if key not found
* @return mixed
*/
public function getParam($key, $default = null)
{
$keyName = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;
$paramSources = $this->getParamSources();
if (isset($this->_params[$keyName])) {
return $this->_params[$keyName];
} elseif (in_array('_GET', $paramSources) && (isset($_GET[$keyName]))) {
return $_GET[$keyName];
} elseif (in_array('_POST', $paramSources) && (isset($_POST[$keyName]))) {
return $_POST[$keyName];
} elseif (in_array('_PUT', $paramSources) && (isset($this->_PUT[$keyName]))) {
return $this->_PUT[$keyName];
}
return $default;
}
/**
* Retrieve an array of parameters
*
* Retrieves a merged array of parameters, with precedence of userland
* params (see {@link setParam()}), $_GET, $_POST (i.e., values in the
* userland params will take precedence over all others).
*
* @return array
*/
public function getParams()
{
$return = $this->_params;
$paramSources = $this->getParamSources();
if (in_array('_GET', $paramSources)
&& isset($_GET)
&& is_array($_GET)
) {
$return += $_GET;
}
if (in_array('_POST', $paramSources)
&& isset($_POST)
&& is_array($_POST)
) {
$return += $_POST;
}
if (in_array('_PUT', $paramSources)
&& isset($this->_PUT)
&& is_array($this->_PUT)
) {
$return += $this->_PUT;
}
return $return;
}
public function __get($key)
{
switch (true) {
case isset($this->_params[$key]):
return $this->_params[$key];
case isset($_GET[$key]):
return $_GET[$key];
case isset($_POST[$key]):
return $_POST[$key];
case isset($this->_PUT[$key]):
return $this->_PUT[$key];
case isset($_COOKIE[$key]):
return $_COOKIE[$key];
case ($key == 'REQUEST_URI'):
return $this->getRequestUri();
case ($key == 'PATH_INFO'):
return $this->getPathInfo();
case isset($_SERVER[$key]):
return $_SERVER[$key];
case isset($_ENV[$key]):
return $_ENV[$key];
default:
return null;
}
}
}
?>