1 | <?php |
||
12 | class HttpRequest extends AbstractRequest implements HttpRequestInterface |
||
13 | { |
||
14 | /** Holds the HTTP verb used in the request */ |
||
15 | private $verb; |
||
16 | |||
17 | /** Holds the contents of the various inputs ($_GET, $_POST, etc) */ |
||
18 | private $input; |
||
19 | |||
20 | /** The input stream (stream or location string) */ |
||
21 | private $stream; |
||
22 | |||
23 | /** Array key for query parameters */ |
||
24 | const INPUT_METHOD_QUERY = 'QUERY'; |
||
25 | /** Array key for post parameters */ |
||
26 | const INPUT_METHOD_POST = 'POST'; |
||
27 | /** Array key for input stream body */ |
||
28 | const INPUT_METHOD_BODY = 'BODY'; |
||
29 | |||
30 | // mappings between magic filter strings and the filter functions |
||
31 | private static $filterCallbacks = array( |
||
32 | 'int' => 'intval', |
||
33 | 'float' => 'floatval', |
||
34 | 'trim' => 'trim', |
||
35 | 'lower' => 'strtolower', |
||
36 | 'upper' => 'strtoupper', |
||
37 | 'squeeze' => array(__CLASS__, 'squeeze') |
||
38 | ); |
||
39 | |||
40 | /** |
||
41 | * Constructor for a request. |
||
42 | * @param string $controller The controller being requested. |
||
43 | * @param string $action The action being invoked. |
||
44 | * @param string $verb The HTTP verb used in the request. |
||
45 | * @param mixed $stream A stream or a string describing a stream location. |
||
46 | */ |
||
47 | 42 | public function __construct($controller, $action, $verb, $stream = 'php://input') |
|
59 | |||
60 | /** |
||
61 | * Returns the HTTP verb used in the request. |
||
62 | * @return string The HTTP verb used in the request. |
||
63 | */ |
||
64 | 6 | public function getVerb() |
|
68 | |||
69 | /** |
||
70 | * Sets the HTTP verb used in the request. |
||
71 | * @param string $verb The HTTP verb used in the request. |
||
72 | * @return RequestInterface Returns $this. |
||
73 | */ |
||
74 | 42 | public function setVerb($verb) |
|
79 | |||
80 | /** |
||
81 | * Sets the stream used in the request. |
||
82 | * @param mixed $stream The stream used in the request. |
||
83 | * @return RequestInterface Returns $this. |
||
84 | */ |
||
85 | 42 | public function setStream($stream) |
|
90 | |||
91 | /** |
||
92 | * Returns true if the request is a GET request and false otherwise. |
||
93 | * @return bool Returns true if the request is a GET request and false |
||
94 | * otherwise. |
||
95 | */ |
||
96 | 1 | public function isGet() |
|
100 | |||
101 | /** |
||
102 | * Returns true if the request is a POST request and false otherwise. |
||
103 | * @return bool Returns true if the request is a POST request and false |
||
104 | * otherwise. |
||
105 | */ |
||
106 | 1 | public function isPost() |
|
110 | |||
111 | /** |
||
112 | * Returns the GET data parameter associated with the specified key. |
||
113 | * @param string $param The GET data parameter. |
||
114 | * @param mixed $defaultValue The default value to use when the key is not present. |
||
115 | * @param mixed $filters The array of filters (or single filter) to apply to the data. |
||
116 | * @return mixed Returns the data from the GET parameter after being filtered (or |
||
117 | * the default value if the parameter is not present) |
||
118 | */ |
||
119 | 7 | public function getQuery($param, $defaultValue = null, $filters = array()) |
|
128 | |||
129 | /** |
||
130 | * Sets all the QUERY data for the current request. |
||
131 | * @param array $queryData The query data for the current request. |
||
132 | * @return HttpRequestInterface Returns $this. |
||
133 | */ |
||
134 | 33 | public function setQuery($queryData) |
|
139 | |||
140 | /** |
||
141 | * Returns the POST data parameter associated with the specified key. |
||
142 | * @param string $param The POST data parameter. |
||
143 | * @param mixed $defaultValue The default value to use when the key is not present. |
||
144 | * @param mixed $filters The array of filters (or single filter) to apply to the data. |
||
145 | * @return mixed Returns the data from the POST parameter after being filtered (or |
||
146 | * the default value if the parameter is not present) |
||
147 | */ |
||
148 | 1 | public function getPost($param, $defaultValue = null, $filters = array()) |
|
157 | |||
158 | /** |
||
159 | * Sets all the POST data for the current request. |
||
160 | * @param array $postData The post data for the current request. |
||
161 | * @return HttpRequestInterface Returns $this. |
||
162 | */ |
||
163 | 27 | public function setPost($postData) |
|
168 | |||
169 | /** |
||
170 | * Returns the input stream data for the current request |
||
171 | * @return string The input stream data |
||
172 | */ |
||
173 | 3 | public function getBody() |
|
204 | |||
205 | /** |
||
206 | * Returns an array of all the input parameters from the query and post data. |
||
207 | * @return array An array of all input. |
||
208 | */ |
||
209 | 1 | public function getAllInput() |
|
216 | |||
217 | /** |
||
218 | * Fetches the input value from the given array, or the default value. Also |
||
219 | * applies any requested filters. |
||
220 | * @param array $array The array ($_POST, $_GET, $params, etc). |
||
221 | * @param string $param The array key to lookup. |
||
222 | * @param mixed $defaultValue The default value to use if the key is not |
||
223 | * found in the array. |
||
224 | * @param mixed $filters The array of input filters to apply (or single filter). |
||
225 | * @return mixed Returns the value filtered (or the default value filtered). |
||
226 | */ |
||
227 | 7 | private function fetchInputValue($array, $param, $defaultValue, $filters) |
|
232 | |||
233 | /** |
||
234 | * Applies the array of filters against the input value. |
||
235 | * @param string $value The input value. |
||
236 | * @param array $filters The array of filters. |
||
237 | * @return string Returns the value after the filters have been applied. |
||
238 | */ |
||
239 | 7 | private function applyInputFilters($value, $filters) |
|
248 | |||
249 | /** |
||
250 | * Takes a string and removes empty lines. |
||
251 | * @param string $string The input string. |
||
252 | * @return string Returns the string with empty lines removed. |
||
253 | */ |
||
254 | 1 | private static function squeeze($string) |
|
267 | } |
||
268 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.