1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace System\Http; |
4
|
|
|
|
5
|
|
|
use System\Application; |
6
|
|
|
|
7
|
|
|
class Request |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Application Object |
11
|
|
|
* |
12
|
|
|
* @var \System\Application |
13
|
|
|
*/ |
14
|
|
|
private $app; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Constructor |
18
|
|
|
* |
19
|
|
|
* @param \System\Application $app |
20
|
|
|
*/ |
21
|
|
|
public function __construct(Application $app) |
22
|
|
|
{ |
23
|
|
|
$this->app = $app; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get value from spcefic request type |
28
|
|
|
* |
29
|
|
|
* @param array $requestType |
30
|
|
|
* @param string $key |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
|
|
private function getValueOfRequest($requestType, $key) |
34
|
|
|
{ |
35
|
|
|
$value = array_get($requestType, $key); |
36
|
|
|
|
37
|
|
|
if (is_array($value)) { |
38
|
|
|
$value = array_filter($value); |
39
|
|
|
} else { |
40
|
|
|
$value = trim($value); |
41
|
|
|
} |
42
|
|
|
return $value; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get value from $_GET by the given key |
47
|
|
|
* |
48
|
|
|
* @param string $key |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
|
|
public function get(string $key = null) |
52
|
|
|
{ |
53
|
|
|
if ($key !== null) { |
54
|
|
|
return $this->getValueOfRequest($_GET, $key); |
55
|
|
|
} |
56
|
|
|
return $_GET; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get value from $_POST by the given key |
61
|
|
|
* |
62
|
|
|
* @param string $key |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function post(string $key = null) |
66
|
|
|
{ |
67
|
|
|
if ($key !== null) { |
68
|
|
|
return $this->getValueOfRequest($_POST, $key); |
69
|
|
|
} |
70
|
|
|
return $_POST; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get value from $_FILES by the given key |
75
|
|
|
* |
76
|
|
|
* @param string $key |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
public function file(string $key = null) |
80
|
|
|
{ |
81
|
|
|
if ($key !== null) { |
82
|
|
|
return $this->getValueOfRequest($_FILES, $key); |
83
|
|
|
} |
84
|
|
|
return $_FILES; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get value from $_SERVER by the given key |
89
|
|
|
* |
90
|
|
|
* @param string $key |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function server(string $key) |
94
|
|
|
{ |
95
|
|
|
if ($key !== null) { |
96
|
|
|
return $this->getValueOfRequest($_SERVER, $key); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
return $_SERVER; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set value To $_POST For the given key |
103
|
|
|
* |
104
|
|
|
* @param string $key |
105
|
|
|
* @param mixed $value |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
public function setPost(string $key, $value) |
109
|
|
|
{ |
110
|
|
|
$_POST[$key] = $value; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set value To $_GET For the given key |
115
|
|
|
* |
116
|
|
|
* @param string $key |
117
|
|
|
* @param mixed $value |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
public function setGet(string $key, $value) |
121
|
|
|
{ |
122
|
|
|
$_GET[$key] = $value; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Set value To $_FILES For the given key |
127
|
|
|
* |
128
|
|
|
* @param string $key |
129
|
|
|
* @param mixed $value |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
public function setFile(string $key, $value) |
133
|
|
|
{ |
134
|
|
|
$_FILES[$key] = $value; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set value To $_SERVER For the given key |
139
|
|
|
* |
140
|
|
|
* @param string $key |
141
|
|
|
* @param mixed $value |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function setServer(string $key, $value) |
145
|
|
|
{ |
146
|
|
|
$_SERVER[$key] = $value; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get current request method |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public function method() |
155
|
|
|
{ |
156
|
|
|
return $this->server('REQUEST_METHOD'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Check if the request to the admin panel |
161
|
|
|
* |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
|
|
public function isRequestToAdminManagement() |
165
|
|
|
{ |
166
|
|
|
$url = $this->app->url->parameters(); |
|
|
|
|
167
|
|
|
; |
168
|
|
|
$url = ltrim($url, '/'); |
169
|
|
|
$url = explode('/', $url)[0]; |
170
|
|
|
|
171
|
|
|
return $url == 'admin'; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Check the request method |
176
|
|
|
* |
177
|
|
|
* @param string|array $methods |
|
|
|
|
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
public function isMatchingRequestMethod($method) |
181
|
|
|
{ |
182
|
|
|
if ($this->method() == strtoupper($method)) { |
183
|
|
|
return true; |
184
|
|
|
} |
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Check if the request can be Continued |
190
|
|
|
* |
191
|
|
|
* @property object $load |
192
|
|
|
* @param array $middlewares |
193
|
|
|
* @return bool |
194
|
|
|
*/ |
195
|
|
|
public function canRequestContinue(array $middlewares) |
196
|
|
|
{ |
197
|
|
|
if (!empty($middlewares)) { |
198
|
|
|
foreach ($middlewares as $middleware) { |
199
|
|
|
$output = $this->app->load->middleware($middleware)->handle(); |
|
|
|
|
200
|
|
|
|
201
|
|
|
if (!$output) { |
202
|
|
|
return false; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
return true; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|