-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequest.php
58 lines (46 loc) · 1.42 KB
/
Request.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace app;
use \app\IRequest;
class Request implements IRequest
{
public function __construct()
{
foreach ($_SERVER as $key => $value) {
$camelCaseKey = $this->toCamelCase($key);
$this->{$camelCaseKey} = $value;
}
}
private function toCamelCase($string) // DOCUMENT_ROOT
{
$result = strtolower($string); // document_root
preg_match_all('/_[a-z]/', $result, $matches);
foreach ($matches[0] as $match) { // _r
$c = str_replace('_', '', strtoupper($match)); // _R -> R
$result = str_replace($match, $c, $result);
}
return $result;
}
public function getBody()
{
$data = [];
if ($this->getMethod() === 'post') {
foreach ($_POST as $key => $value) {
$data[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
}
} else if ($this->getMethod() === 'get') {
foreach ($_GET as $key => $value) {
$data[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_SPECIAL_CHARS);
}
}
return $data;
}
public function getMethod()
{
return strtolower($this->requestMethod);
}
public function getPath()
{
$path = substr($this->requestUri, 0, strpos($this->requestUri, '?') ?: strlen($this->requestUri));
return $path ?? '/';
}
}