Completed
Pull Request — master (#14)
by
unknown
05:25
created

HttpRequest::parseInputParameters()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 6
nc 4
nop 0
1
<?php
2
3
namespace Http;
4
5
class HttpRequest implements Request
6
{
7
    protected $getParameters;
8
    protected $postParameters;
9
    protected $server;
10
    protected $files;
11
    protected $cookies;
12
    protected $inputStream;
13
14
    public function __construct(
15
        array $get,
16
        array $post,
17
        array $cookies,
18
        array $files,
19
        array $server,
20
        $inputStream = ''
21
    ) {
22
        $this->getParameters = $get;
23
        $this->postParameters = $post;
24
        $this->cookies = $cookies;
25
        $this->files = $files;
26
        $this->server = $server;
27
        $this->inputStream = $inputStream;
28
29
        $this->parseInputParameters();
30
    }
31
32
    /**
33
     * Returns a parameter value or a default value if none is set.
34
     *
35
     * @param  string $key
36
     * @param  string $defaultValue (optional)
37
     * @return string
38
     */
39
    public function getParameter($key, $defaultValue = null)
40
    {
41
        if (array_key_exists($key, $this->postParameters)) {
42
            return $this->postParameters[$key];
43
        }
44
45
        if (array_key_exists($key, $this->getParameters)) {
46
            return $this->getParameters[$key];
47
        }
48
49
        return $defaultValue;
50
    }
51
52
    /**
53
     * Returns a query parameter value or a default value if none is set.
54
     *
55
     * @param  string $key
56
     * @param  string $defaultValue (optional)
57
     * @return string
58
     */
59
    public function getQueryParameter($key, $defaultValue = null)
60
    {
61
        if (array_key_exists($key, $this->getParameters)) {
62
            return $this->getParameters[$key];
63
        }
64
65
        return $defaultValue;
66
    }
67
68
    /**
69
     * Returns a body parameter value or a default value if none is set.
70
     *
71
     * @param  string $key
72
     * @param  string $defaultValue (optional)
73
     * @return string
74
     */
75
    public function getBodyParameter($key, $defaultValue = null)
76
    {
77
        if (array_key_exists($key, $this->postParameters)) {
78
            return $this->postParameters[$key];
79
        }
80
81
        return $defaultValue;
82
    }
83
84
    /**
85
     * Returns a file value or a default value if none is set.
86
     *
87
     * @param  string $key
88
     * @param  string $defaultValue (optional)
89
     * @return string
90
     */
91
    public function getFile($key, $defaultValue = null)
92
    {
93
        if (array_key_exists($key, $this->files)) {
94
            return $this->files[$key];
95
        }
96
97
        return $defaultValue;
98
    }
99
100
    /**
101
     * Returns a cookie value or a default value if none is set.
102
     *
103
     * @param  string $key
104
     * @param  string $defaultValue (optional)
105
     * @return string
106
     */
107
    public function getCookie($key, $defaultValue = null)
108
    {
109
        if (array_key_exists($key, $this->cookies)) {
110
            return $this->cookies[$key];
111
        }
112
113
        return $defaultValue;
114
    }
115
116
    /**
117
     * Returns all parameters.
118
     *
119
     * @return array
120
     */
121
    public function getParameters()
122
    {
123
        return array_merge($this->getParameters, $this->postParameters);
124
    }
125
126
    /**
127
     * Returns all query parameters.
128
     *
129
     * @return array
130
     */
131
    public function getQueryParameters()
132
    {
133
        return $this->getParameters;
134
    }
135
136
    /**
137
     * Returns all body parameters.
138
     *
139
     * @return array
140
     */
141
    public function getBodyParameters()
142
    {
143
        return $this->postParameters;
144
    }
145
146
    /**
147
     * Returns raw values from the read-only stream that allows you to read raw data from the request body.
148
     *
149
     * @return string
150
     */
151
    public function getRawBody()
152
    {
153
        return $this->inputStream;
154
    }
155
156
    /**
157
     * Returns a Cookie Iterator.
158
     *
159
     * @return array
160
     */
161
    public function getCookies()
162
    {
163
        return $this->cookies;
164
    }
165
166
    /**
167
     * Returns a File Iterator.
168
     *
169
     * @return array
170
     */
171
    public function getFiles()
172
    {
173
        return $this->files;
174
    }
175
176
    /**
177
     * The URI which was given in order to access this page
178
     *
179
     * @return string
180
     * @throws MissingRequestMetaVariableException
181
     */
182
    public function getUri()
183
    {
184
        return $this->getServerVariable('REQUEST_URI');
185
    }
186
187
    /**
188
     * Return just the path
189
     *
190
     * @return string
191
     */
192
    public function getPath()
193
    {
194
        return strtok($this->getServerVariable('REQUEST_URI'), '?');
195
    }
196
197
    /**
198
     * Which request method was used to access the page;
199
     * i.e. 'GET', 'HEAD', 'POST', 'PUT'.
200
     *
201
     * @return string
202
     * @throws MissingRequestMetaVariableException
203
     */
204
    public function getMethod()
205
    {
206
        return $this->getServerVariable('REQUEST_METHOD');
207
    }
208
209
    /**
210
     * Contents of the Accept: header from the current request, if there is one.
211
     *
212
     * @return string
213
     * @throws MissingRequestMetaVariableException
214
     */
215
    public function getHttpAccept()
216
    {
217
        return $this->getServerVariable('HTTP_ACCEPT');
218
    }
219
220
    /**
221
     * The address of the page (if any) which referred the user agent to the
222
     * current page.
223
     *
224
     * @return string
225
     * @throws MissingRequestMetaVariableException
226
     */
227
    public function getReferer()
228
    {
229
        return $this->getServerVariable('HTTP_REFERER');
230
    }
231
232
    /**
233
     * Content of the User-Agent header from the request, if there is one.
234
     *
235
     * @return string
236
     * @throws MissingRequestMetaVariableException
237
     */
238
    public function getUserAgent()
239
    {
240
        return $this->getServerVariable('HTTP_USER_AGENT');
241
    }
242
243
    /**
244
     * The IP address from which the user is viewing the current page.
245
     *
246
     * @return string
247
     * @throws MissingRequestMetaVariableException
248
     */
249
    public function getIpAddress()
250
    {
251
        return $this->getServerVariable('REMOTE_ADDR');
252
    }
253
254
    /**
255
     * Checks to see whether the current request is using HTTPS.
256
     *
257
     * @return boolean
258
     */
259
    public function isSecure()
260
    {
261
        return (array_key_exists('HTTPS', $this->server)
262
            && $this->server['HTTPS'] !== 'off'
263
        );
264
    }
265
266
    /**
267
     * The query string, if any, via which the page was accessed.
268
     *
269
     * @return string
270
     * @throws MissingRequestMetaVariableException
271
     */
272
    public function getQueryString()
273
    {
274
        return $this->getServerVariable('QUERY_STRING');
275
    }
276
277
    private function getServerVariable($key)
278
    {
279
        if (!array_key_exists($key, $this->server)) {
280
            throw new MissingRequestMetaVariableException($key);
281
        }
282
283
        return $this->server[$key];
284
    }
285
286
    private function parseInputParameters()
287
    {
288
        if (($this->getMethod() == 'PUT' || $this->getMethod() == 'PATCH') && isset($this->server['HTTP_CONTENT_TYPE'])) {
289
            if ($this->server['HTTP_CONTENT_TYPE'] == 'application/x-www-form-urlencoded') {
290
                parse_str($this->inputStream, $this->postParameters);
291
            }
292
            else if ($this->server['HTTP_CONTENT_TYPE'] == 'application/json') {
293
                $this->postParameters = json_decode($this->inputStream, true);
294
            }
295
        }
296
    }
297
}
298