HttpRequest::getPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
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
     * Which request method was used to access the page;
196
     * i.e. 'GET', 'HEAD', 'POST', 'PUT'.
197
     *
198
     * @return string
199
     * @throws MissingRequestMetaVariableException
200
     */
201
    public function getMethod()
202
    {
203
        return $this->getServerVariable('REQUEST_METHOD');
204
    }
205
206
    /**
207
     * Contents of the Accept: header from the current request, if there is one.
208
     *
209
     * @return string
210
     * @throws MissingRequestMetaVariableException
211
     */
212
    public function getHttpAccept()
213
    {
214
        return $this->getServerVariable('HTTP_ACCEPT');
215
    }
216
217
    /**
218
     * The address of the page (if any) which referred the user agent to the
219
     * current page.
220
     *
221
     * @return string
222
     * @throws MissingRequestMetaVariableException
223
     */
224
    public function getReferer()
225
    {
226
        return $this->getServerVariable('HTTP_REFERER');
227
    }
228
229
    /**
230
     * Content of the User-Agent header from the request, if there is one.
231
     *
232
     * @return string
233
     * @throws MissingRequestMetaVariableException
234
     */
235
    public function getUserAgent()
236
    {
237
        return $this->getServerVariable('HTTP_USER_AGENT');
238
    }
239
240
    /**
241
     * The IP address from which the user is viewing the current page.
242
     *
243
     * @return string
244
     * @throws MissingRequestMetaVariableException
245
     */
246
    public function getIpAddress()
247
    {
248
        return $this->getServerVariable('REMOTE_ADDR');
249
    }
250
251
    /**
252
     * Checks to see whether the current request is using HTTPS.
253
     *
254
     * @return boolean
255
     */
256
    public function isSecure()
257
    {
258
        return (array_key_exists('HTTPS', $this->server)
259
            && $this->server['HTTPS'] !== 'off'
260
        );
261
    }
262
263
    /**
264
     * The query string, if any, via which the page was accessed.
265
     *
266
     * @return string
267
     * @throws MissingRequestMetaVariableException
268
     */
269
    public function getQueryString()
270
    {
271
        return $this->getServerVariable('QUERY_STRING');
272
    }
273
274
    private function getServerVariable($key)
275
    {
276
        if (!array_key_exists($key, $this->server)) {
277
            throw new MissingRequestMetaVariableException($key);
278
        }
279
280
        return $this->server[$key];
281
    }
282
}
283