1 | <?php |
||
5 | class HttpRequest implements Request |
||
6 | { |
||
7 | protected $getParameters; |
||
8 | protected $postParameters; |
||
9 | protected $server; |
||
10 | protected $files; |
||
11 | protected $cookies; |
||
12 | |||
13 | public function __construct( |
||
14 | array $get, |
||
15 | array $post, |
||
16 | array $cookies, |
||
17 | array $files, |
||
18 | array $server, |
||
19 | $inputStream = '' |
||
20 | ) { |
||
21 | $this->getParameters = $get; |
||
22 | $this->postParameters = $post; |
||
23 | $this->cookies = $cookies; |
||
24 | $this->files = $files; |
||
25 | $this->server = $server; |
||
26 | $this->inputStream = $inputStream; |
||
|
|||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Returns a parameter value or a default value if none is set. |
||
31 | * |
||
32 | * @param string $key |
||
33 | * @param string $defaultValue (optional) |
||
34 | * @return string |
||
35 | */ |
||
36 | public function getParameter($key, $defaultValue = null) |
||
37 | { |
||
38 | if (array_key_exists($key, $this->postParameters)) { |
||
39 | return $this->postParameters[$key]; |
||
40 | } |
||
41 | |||
42 | if (array_key_exists($key, $this->getParameters)) { |
||
43 | return $this->getParameters[$key]; |
||
44 | } |
||
45 | |||
46 | return $defaultValue; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Returns a query parameter value or a default value if none is set. |
||
51 | * |
||
52 | * @param string $key |
||
53 | * @param string $defaultValue (optional) |
||
54 | * @return string |
||
55 | */ |
||
56 | public function getQueryParameter($key, $defaultValue = null) |
||
57 | { |
||
58 | if (array_key_exists($key, $this->getParameters)) { |
||
59 | return $this->getParameters[$key]; |
||
60 | } |
||
61 | |||
62 | return $defaultValue; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Returns a body parameter value or a default value if none is set. |
||
67 | * |
||
68 | * @param string $key |
||
69 | * @param string $defaultValue (optional) |
||
70 | * @return string |
||
71 | */ |
||
72 | public function getBodyParameter($key, $defaultValue = null) |
||
73 | { |
||
74 | if (array_key_exists($key, $this->postParameters)) { |
||
75 | return $this->postParameters[$key]; |
||
76 | } |
||
77 | |||
78 | return $defaultValue; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Returns a file value or a default value if none is set. |
||
83 | * |
||
84 | * @param string $key |
||
85 | * @param string $defaultValue (optional) |
||
86 | * @return string |
||
87 | */ |
||
88 | public function getFile($key, $defaultValue = null) |
||
89 | { |
||
90 | if (array_key_exists($key, $this->files)) { |
||
91 | return $this->files[$key]; |
||
92 | } |
||
93 | |||
94 | return $defaultValue; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Returns a cookie value or a default value if none is set. |
||
99 | * |
||
100 | * @param string $key |
||
101 | * @param string $defaultValue (optional) |
||
102 | * @return string |
||
103 | */ |
||
104 | public function getCookie($key, $defaultValue = null) |
||
105 | { |
||
106 | if (array_key_exists($key, $this->cookies)) { |
||
107 | return $this->cookies[$key]; |
||
108 | } |
||
109 | |||
110 | return $defaultValue; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Returns all parameters. |
||
115 | * |
||
116 | * @return array |
||
117 | */ |
||
118 | public function getParameters() |
||
119 | { |
||
120 | return array_merge($this->getParameters, $this->postParameters); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Returns all query parameters. |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | public function getQueryParameters() |
||
129 | { |
||
130 | return $this->getParameters; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Returns all body parameters. |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | public function getBodyParameters() |
||
139 | { |
||
140 | return $this->postParameters; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Returns raw values from the read-only stream that allows you to read raw data from the request body. |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getRawBody() |
||
152 | |||
153 | /** |
||
154 | * Returns a Cookie Iterator. |
||
155 | * |
||
156 | * @return array |
||
157 | */ |
||
158 | public function getCookies() |
||
159 | { |
||
160 | return $this->cookies; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Returns a File Iterator. |
||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | public function getFiles() |
||
172 | |||
173 | /** |
||
174 | * The URI which was given in order to access this page |
||
175 | * |
||
176 | * @return string |
||
177 | * @throws MissingRequestMetaVariableException |
||
178 | */ |
||
179 | public function getUri() |
||
183 | |||
184 | /** |
||
185 | * Return just the path |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getPath() |
||
193 | |||
194 | /** |
||
195 | * Get path relative to executed script |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | public function getRelativePath() |
||
203 | |||
204 | /** |
||
205 | * Which request method was used to access the page; |
||
206 | * i.e. 'GET', 'HEAD', 'POST', 'PUT'. |
||
207 | * |
||
208 | * @return string |
||
209 | * @throws MissingRequestMetaVariableException |
||
210 | */ |
||
211 | public function getMethod() |
||
215 | |||
216 | /** |
||
217 | * Contents of the Accept: header from the current request, if there is one. |
||
218 | * |
||
219 | * @return string |
||
220 | * @throws MissingRequestMetaVariableException |
||
221 | */ |
||
222 | public function getHttpAccept() |
||
226 | |||
227 | /** |
||
228 | * The address of the page (if any) which referred the user agent to the |
||
229 | * current page. |
||
230 | * |
||
231 | * @return string |
||
232 | * @throws MissingRequestMetaVariableException |
||
233 | */ |
||
234 | public function getReferer() |
||
238 | |||
239 | /** |
||
240 | * Content of the User-Agent header from the request, if there is one. |
||
241 | * |
||
242 | * @return string |
||
243 | * @throws MissingRequestMetaVariableException |
||
244 | */ |
||
245 | public function getUserAgent() |
||
249 | |||
250 | /** |
||
251 | * The IP address from which the user is viewing the current page. |
||
252 | * |
||
253 | * @return string |
||
254 | * @throws MissingRequestMetaVariableException |
||
255 | */ |
||
256 | public function getIpAddress() |
||
260 | |||
261 | /** |
||
262 | * Checks to see whether the current request is using HTTPS. |
||
263 | * |
||
264 | * @return boolean |
||
265 | */ |
||
266 | public function isSecure() |
||
272 | |||
273 | /** |
||
274 | * The query string, if any, via which the page was accessed. |
||
275 | * |
||
276 | * @return string |
||
277 | * @throws MissingRequestMetaVariableException |
||
278 | */ |
||
279 | public function getQueryString() |
||
283 | |||
284 | private function getServerVariable($key) |
||
292 | } |
||
293 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: