-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
190 lines (168 loc) · 3.5 KB
/
Request.php
File metadata and controls
190 lines (168 loc) · 3.5 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace alvin\phpmvc;
/**
* http Request class
*/
class Request {
/**
* Current http route.
* @var string
*/
public $route;
/**
* Query parameters.
* @var string
*/
public $params;
/**
* All html form inputs.
* @var object
*/
public $inputs;
/**
* html form body.
* @var object
*/
public $body;
/**
* current http verb.
* @var string
*/
public $method;
/**
* Create a new Request object.
* @return void
*/
public function __construct() {
$this->setParams();
$this->setBody();
$this->setRoute();
$this->method = $_SERVER['REQUEST_METHOD'];
}
/**
* Loops over QUERY_STRING global and sets params array.
* @return void
*/
public function setParams() {
$params = [];
array_key_exists('QUERY_STRING', $_SERVER, ) && parse_str($_SERVER['QUERY_STRING'], $params);
foreach ($params as $key => $value) {
$params[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_SPECIAL_CHARS);
}
$this->params = $params;
}
/**
* Loops over $_GET,$_POST,$_FILES and sets body and inputs array.
* @return void
*/
public function setBody() {
$data = [];
if ($this->isGet()) {
foreach ($_GET as $key => $value) {
$data[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_SPECIAL_CHARS);
}
}
if ($this->isPost()) {
foreach ($_POST as $key => $value) {
$data[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
}
}
foreach ($_FILES as $key => $value) {
$data[$key] = $value;
$fileName = $_FILES[$key]['name'];
$fileNameArray = explode(".", $fileName);
$fileExt = strtolower(end($fileNameArray));
$data[$key]['ext'] = $fileExt;
}
unset($data['submit']);
$this->inputs = $data;
unset($data['_csrf']);
$this->body = $data;
}
/**
* Sets current route using $_SERVER['REQUEST_URI'].
* @return void
*/
public function setRoute() {
$route = $_SERVER['REQUEST_URI'];
$queryPos = strpos($route, '?');
if ($queryPos !== false) {
$route = substr($route, 0, $queryPos);
}
$this->route = $route;
}
/**
* Returns true if current http method is get.
* @return boolean
*/
public function isGet() {
return $this->getMethod() === 'get';
}
/**
* Returns true if current http method is post.
* @return boolean
*/
public function isPost() {
return $this->getMethod() === 'post';
}
/**
* Returns current http method.
* @return string
*/
public function getMethod() {
return strtolower($_SERVER['REQUEST_METHOD']);
}
/**
* Returns html form body.
* @return object
*/
public function getBody() {
return $this->body;
}
/**
* Returns route.
* @return string
*/
public function getRoute() {
return $this->route;
}
/**
* Returns an input value for a key.
* @param string $key
* @return mixed
*/
public function input($key) {
return $this->inputs[$key] ?? false;
}
/**
* Returns a query parameter.
*
* @param string $key
* @return mixed
*/
public function query(string $key) {
return $this->params[$key] ?? null;
}
/**
* Returns a cookie value from $_COOKIE.
*
* @param string $key
* @return string
*/
public function getCookie(string $key) {
return $_COOKIE[$key] ?? null;
}
/**
* Return json body
*
* @param boolean $associative
* @param integer $flags
* @param integer $depth
* @return object
*/
public function body($associative = true, int $flags = 0, int $depth = 512) {
$body = file_get_contents("php://input");
$object = json_decode($body, $associative, $depth, $flags);
return $object;
}
}