Completed
Pull Request — master (#17)
by
unknown
01:59
created

HttpRequest::getRelativePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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
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;
0 ignored issues
show
Bug introduced by
The property inputStream does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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()
149
    {
150
        return $this->inputStream;
151
    }
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()
169
    {
170
        return $this->files;
171
    }
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()
180
    {
181
        return $this->getServerVariable('REQUEST_URI');
182
    }
183
184
    /**
185
     * Return just the path
186
     *
187
     * @return string
188
     */
189
    public function getPath()
190
    {
191
        return strtok($this->getServerVariable('REQUEST_URI'), '?');
192
    }
193
194
    /**
195
     * Get path relative to executed script
196
     *
197
     * @return string
198
     */
199
    public function getRelativePath()
200
    {
201
        return substr($this->getPath(), strlen(dirname($this->getServerVariable('PHP_SELF'))));
202
    }
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()
212
    {
213
        return $this->getServerVariable('REQUEST_METHOD');
214
    }
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()
223
    {
224
        return $this->getServerVariable('HTTP_ACCEPT');
225
    }
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()
235
    {
236
        return $this->getServerVariable('HTTP_REFERER');
237
    }
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()
246
    {
247
        return $this->getServerVariable('HTTP_USER_AGENT');
248
    }
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()
257
    {
258
        return $this->getServerVariable('REMOTE_ADDR');
259
    }
260
261
    /**
262
     * Checks to see whether the current request is using HTTPS.
263
     *
264
     * @return boolean
265
     */
266
    public function isSecure()
267
    {
268
        return (array_key_exists('HTTPS', $this->server)
269
            && $this->server['HTTPS'] !== 'off'
270
        );
271
    }
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()
280
    {
281
        return $this->getServerVariable('QUERY_STRING');
282
    }
283
284
    private function getServerVariable($key)
285
    {
286
        if (!array_key_exists($key, $this->server)) {
287
            throw new MissingRequestMetaVariableException($key);
288
        }
289
290
        return $this->server[$key];
291
    }
292
}
293