Request::isMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the alphaz Framework.
5
 *
6
 * @author Muhammad Umer Farooq (Malik) <[email protected]>
7
 *
8
 * @link https://github.com/alphazframework/framework
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 * @since 1.0.0
13
 *
14
 * @license MIT
15
 */
16
17
namespace alphaz\http;
18
19
class Request extends Uri
20
{
21
    /**
22
     * Constructor.
23
     *
24
     * Instantiate the request object
25
     *
26
     * @since 1.0.0
27
     */
28
    public function __construct()
29
    {
30
        parent::__construct();
31
        $this->get = $_GET ?? [];
32
        $this->post = $_POST ?? [];
33
        $this->files = $_FILES ?? [];
34
        $this->session = $_SESSION ?? [];
35
        $this->cookie = $_COOKIE ?? [];
36
        $this->server = $_SERVER ?? [];
37
        $this->env = $_ENV ?? [];
38
39
        if ($this->getRequestMethod()) {
40
            $this->parseData();
41
        }
42
    }
43
44
    /**
45
     * Does this request use a given method?
46
     *
47
     * @param (string) $method HTTP method
48
     *
49
     * @since 1.0.0
50
     *
51
     * @return bool
52
     */
53
    public function isMethod($method)
54
    {
55
        return $this->getRequestMethod() === $method;
56
    }
57
58
    /**
59
     * Determine whether or not the request has FILES.
60
     *
61
     * @since 1.0.0
62
     *
63
     * @return bool
64
     */
65
    public function hasFiles()
66
    {
67
        return count($this->files) > 0;
68
    }
69
70
    /**
71
     * Is this an GET request.
72
     *
73
     * @since 1.0.0
74
     *
75
     * @return bool
76
     */
77
    public function isGet()
78
    {
79
        return $this->isMethod('GET');
80
    }
81
82
    /**
83
     * Is this an HEAD request.
84
     *
85
     * @since 1.0.0
86
     *
87
     * @return bool
88
     */
89
    public function isHead()
90
    {
91
        return $this->isMethod('HEAD');
92
    }
93
94
    /**
95
     * Is this an POST request.
96
     *
97
     * @since 1.0.0
98
     *
99
     * @return bool
100
     */
101
    public function isPost()
102
    {
103
        return $this->isMethod('POST');
104
    }
105
106
    /**
107
     * Is this an PUT request.
108
     *
109
     * @since 1.0.0
110
     *
111
     * @return bool
112
     */
113
    public function isPut()
114
    {
115
        return $this->isMethod('PUT');
116
    }
117
118
    /**
119
     * Is this an DELETE request.
120
     *
121
     * @since 1.0.0
122
     *
123
     * @return bool
124
     */
125
    public function isDelete()
126
    {
127
        return $this->isMethod('DELETE');
128
    }
129
130
    /**
131
     * Is this an TRACE request.
132
     *
133
     * @since 1.0.0
134
     *
135
     * @return bool
136
     */
137
    public function isTrace()
138
    {
139
        return $this->isMethod('TRACE');
140
    }
141
142
    /**
143
     * Is this an OPTIONS request.
144
     *
145
     * @since 1.0.0
146
     *
147
     * @return bool
148
     */
149
    public function isOptions()
150
    {
151
        return $this->isMethod('OPTIONS');
152
    }
153
154
    /**
155
     * Is this an CONNEXT request.
156
     *
157
     * @since 1.0.0
158
     *
159
     * @return bool
160
     */
161
    public function isConnect()
162
    {
163
        return $this->isMethod('CONNECT');
164
    }
165
166
    /**
167
     * Is this an PATH request.
168
     *
169
     * @since 1.0.0
170
     *
171
     * @return bool
172
     */
173
    public function isPatch()
174
    {
175
        return $this->isMethod('PATCH');
176
    }
177
178
    /**
179
     * Return whether or not the request is secure.
180
     *
181
     * @since 1.0.0
182
     *
183
     * @return bool
184
     */
185
    public function isSecure()
186
    {
187
        return $this->gethttps() || $this->getServerPort() && $this->getServerPort() === '443';
188
    }
189
190
    /**
191
     * Is this an XHR request?
192
     *
193
     * @since 1.0.0
194
     *
195
     * @return bool
196
     */
197
    public function isXhr()
198
    {
199
        return strtolower($this->getHeader('x-requested-with')) === 'xmlhttprequest';
200
    }
201
202
    /**
203
     * Get a value from $_GET, or the whole array.
204
     *
205
     * @param (string) $key
206
     *
207
     * @since 1.0.0
208
     *
209
     * @return mixed
210
     */
211
    public function getQuery($key = null)
212
    {
213
        if ($key === null) {
214
            return $this->get;
215
        } else {
216
            return (isset($this->get[$key])) ? $this->get[$key] : null;
217
        }
218
    }
219
220
    /**
221
     * Get a value from $_POST, or the whole array.
222
     *
223
     * @param (string) $key
224
     *
225
     * @since 1.0.0
226
     *
227
     * @return mixed
228
     */
229
    public function getPost($key = null)
230
    {
231
        if ($key === null) {
232
            return $this->post;
233
        } else {
234
            return (isset($this->post[$key])) ? $this->post[$key] : null;
235
        }
236
    }
237
238
    /**
239
     * Get a value from $_FILES, or the whole array.
240
     *
241
     * @param (string) $key
242
     *
243
     * @since 1.0.0
244
     *
245
     * @return mixed
246
     */
247
    public function getFiles($key = null)
248
    {
249
        if ($key === null) {
250
            return $this->files;
251
        } else {
252
            return (isset($this->files[$key])) ? $this->files[$key] : null;
253
        }
254
    }
255
256
    /**
257
     * Get a value from PUT query data, or the whole array.
258
     *
259
     * @param (string) $key
260
     *
261
     * @since 1.0.0
262
     *
263
     * @return mixed
264
     */
265
    public function getPut($key = null)
266
    {
267
        if ($key === null) {
268
            return $this->put;
269
        } else {
270
            return (isset($this->put[$key])) ? $this->put[$key] : null;
271
        }
272
    }
273
274
    /**
275
     * Get a value from PATCH query data, or the whole array.
276
     *
277
     * @param (string) $key
278
     *
279
     * @since 1.0.0
280
     *
281
     * @return mixed
282
     */
283
    public function getPatch($key = null)
284
    {
285
        if ($key === null) {
286
            return $this->patch;
287
        } else {
288
            return (isset($this->patch[$key])) ? $this->patch[$key] : null;
289
        }
290
    }
291
292
    /**
293
     * Get a value from DELETE query data, or the whole array.
294
     *
295
     * @param (string) $key
296
     *
297
     * @since 1.0.0
298
     *
299
     * @return mixed
300
     */
301
    public function getDelete($key = null)
302
    {
303
        if ($key === null) {
304
            return $this->delete;
305
        } else {
306
            return (isset($this->delete[$key])) ? $this->delete[$key] : null;
307
        }
308
    }
309
310
    /**
311
     * Get a value from $_SESSION, or the whole array.
312
     *
313
     * @param (string) $key
314
     *
315
     * @since 1.0.0
316
     *
317
     * @return mixed
318
     */
319
    public function getSession($key = null)
320
    {
321
        if ($key === null) {
322
            return $this->session;
323
        } else {
324
            return (isset($this->session[$key])) ? $this->session[$key] : null;
325
        }
326
    }
327
328
    /**
329
     * Get a value from $_COOKIE, or the whole array.
330
     *
331
     * @param (string) $key
332
     *
333
     * @since 1.0.0
334
     *
335
     * @return mixed
336
     */
337
    public function getCookie($key = null)
338
    {
339
        if ($key === null) {
340
            return $this->cookie;
341
        } else {
342
            return (isset($this->cookie[$key])) ? $this->cookie[$key] : null;
343
        }
344
    }
345
346
    /**
347
     * Get a value from $_SERVER, or the whole array.
348
     *
349
     * @param (string) $key
350
     *
351
     * @since 1.0.0
352
     *
353
     * @return mixed
354
     */
355
    public function getServer($key = null)
356
    {
357
        if ($key === null) {
358
            return $this->server;
359
        } else {
360
            return (isset($this->server[$key])) ? $this->server[$key] : null;
361
        }
362
    }
363
364
    /**
365
     * Get a value from $_ENV, or the whole array.
366
     *
367
     * @param (string) $key
368
     *
369
     * @since 1.0.0
370
     *
371
     * @return mixed
372
     */
373
    public function getEnv($key = null)
374
    {
375
        if ($key === null) {
376
            return $this->env;
377
        } else {
378
            return (isset($this->env[$key])) ? $this->env[$key] : null;
379
        }
380
    }
381
382
    /**
383
     * Get a value from parsed data, or the whole array.
384
     *
385
     * @param (string) $key
386
     *
387
     * @since 1.0.0
388
     *
389
     * @return mixed
390
     */
391
    public function getParsedData($key = null)
392
    {
393
        $result = null;
394
395
        if ($this->parsedData !== null && is_array($this->parsedData)) {
396
            if (null === $key) {
397
                $result = $this->parsedData;
398
            } else {
399
                $result = (isset($this->parsedData[$key])) ? $this->parsedData[$key] : null;
400
            }
401
        }
402
403
        return $result;
404
    }
405
406
    /**
407
     * Get the raw data.
408
     *
409
     * @since 1.0.0
410
     *
411
     * @return string
412
     */
413
    public function getRawData()
414
    {
415
        return $this->rawData;
416
    }
417
418
    /**
419
     * Parse any data that came with the request.
420
     *
421
     * @since 1.0.0
422
     *
423
     * @return void
424
     */
425
    protected function parseData()
426
    {
427
        if (strtoupper($this->getRequestMethod()) == 'GET') {
0 ignored issues
show
Bug introduced by
$this->getRequestMethod() of type array|boolean is incompatible with the type string expected by parameter $string of strtoupper(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

427
        if (strtoupper(/** @scrutinizer ignore-type */ $this->getRequestMethod()) == 'GET') {
Loading history...
428
            $this->rawData = ($this->getQueryString()) ? rawurldecode($this->getQueryString()) : null;
0 ignored issues
show
Bug introduced by
$this->getQueryString() of type array|true is incompatible with the type string expected by parameter $string of rawurldecode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

428
            $this->rawData = ($this->getQueryString()) ? rawurldecode(/** @scrutinizer ignore-type */ $this->getQueryString()) : null;
Loading history...
429
        } else {
430
            $input = fopen('php://input', 'r');
431
            while ($data = fread($input, 1024)) {
432
                $this->rawData .= $data;
433
            }
434
        }
435
436
        // If the content-type is JSON
437
        if ($this->getQueryString() && stripos($this->getQueryString(), 'json') !== false) {
0 ignored issues
show
Bug introduced by
$this->getQueryString() of type array|true is incompatible with the type string expected by parameter $haystack of stripos(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

437
        if ($this->getQueryString() && stripos(/** @scrutinizer ignore-type */ $this->getQueryString(), 'json') !== false) {
Loading history...
438
            $this->parsedData = json_decode($this->rawData, true);
439
            // Else, if the content-type is XML
440
        } elseif ($this->getContentType() && stripos($this->getContentType(), 'xml') !== false) {
441
            $matches = [];
442
            preg_match_all('/<!\[cdata\[(.*?)\]\]>/is', $this->rawData, $matches);
443
444
            foreach ($matches[0] as $match) {
445
                $strip = str_replace(
446
                    ['<![CDATA[', ']]>', '<', '>'],
447
                    ['', '', '&lt;', '&gt;'],
448
                    $match
449
                );
450
                $this->rawData = str_replace($match, $strip, $this->rawData);
451
            }
452
453
            $this->parsedData = json_decode(json_encode((array) simplexml_load_string($this->rawData)), true);
454
            // Else, default to a regular URL-encoded string
455
        } else {
456
            switch (strtoupper($this->getRequestMethod())) {
457
                case 'GET':
458
                    $this->parsedData = $this->get;
459
                    break;
460
461
                case 'POST':
462
                    $this->parsedData = $this->post;
463
                    break;
464
                default:
465
                    if ($this->getContentType() && strtolower($this->getContentType()) == 'application/x-www-form-urlencoded') {
0 ignored issues
show
Bug introduced by
$this->getContentType() of type array|true is incompatible with the type string expected by parameter $string of strtolower(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

465
                    if ($this->getContentType() && strtolower(/** @scrutinizer ignore-type */ $this->getContentType()) == 'application/x-www-form-urlencoded') {
Loading history...
466
                        parse_str($this->rawData, $this->parsedData);
467
                    }
468
            }
469
        }
470
        switch (strtoupper($this->getRequestMethod())) {
471
            case 'PUT':
472
                $this->put = $this->parsedData;
473
                break;
474
475
            case 'PATCH':
476
                $this->patch = $this->parsedData;
477
                break;
478
479
            case 'DELETE':
480
                $this->delete = $this->parsedData;
481
                break;
482
        }
483
    }
484
}
485