-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
199 lines (168 loc) · 4.57 KB
/
Request.php
File metadata and controls
199 lines (168 loc) · 4.57 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
191
192
193
194
195
196
197
198
199
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Core;
use Core\RequestMethods\RequestMethod;
/**
* Used to parse http requests.
* Do not include validation.
* @author azcraft
*/
class Request implements \Serializable
{
private array $full_path = [];
/** $path has no empty spaces entries. */
private array $path = [];
private array $vars = [];
private int $method = RequestMethod::GET;
public function __construct(?string $uri = null, ?string $method = null)
{
if (isset($uri, $method)){
$this->defineFromString($uri);
$this->setMethod($method);
} else {
$this->defineFromServerGlobals();
}
}
/**
* Returns the Request's method
* @return int The Request's method
*/
public function method(): int
{
return $this->method;
}
/**
* Overwrites the request method
* @param int|string $method
* @return void
* @throws Exception
*/
public function setMethod($method): void
{
if (is_string($method)){
$method = strtolower($method);
}
switch ($method)
{
default:
case "get":
case RequestMethod::GET:
$this->method = RequestMethod::GET;
return;
case "put":
case RequestMethod::PUT:
$this->method = RequestMethod::PUT;
return;
case "post":
case RequestMethod::POST:
$this->method = RequestMethod::POST;
return;
case "delete":
case RequestMethod::DELETE:
$this->method = RequestMethod::DELETE;
return;
}
}
/**
* Returns the Request's arguments
* They are the target path without the module
*/
public function path(): array
{
return $this->path;
}
public function setPath(array $path): void
{
$this->path = $path;
}
public function fullPath(): array
{
return $this->full_path;
}
public function setFullPath(array $path): void
{
$this->full_path = $path;
}
public function var($name): mixed
{
if (!isset($this->vars[$name])){
return null;
}
return $this->vars[$name];
}
public function setVar(string $name, mixed $value)
{
$this->vars[$name] = urldecode($value);
}
public function vars(): array
{
return $this->vars;
}
public function clearVars(): void
{
$this->vars = [];
}
public function __get($name)
{
if (!isset($this->vars[$name])){
throw new Exception("Undefined \$Request->$name");
}
return $this->vars[$name];
}
/**
* Parses a string to target module and arguments
* @param string $uri
* @return void
*/
private function defineFromString(string $uri): void
{
$pure_uri = self::stripVariablesFromPath($uri);
$this->path = preg_split("/\//", $pure_uri, -1, PREG_SPLIT_NO_EMPTY);
$this->full_path = preg_split("/\//", $pure_uri, -1);
if($this->full_path[0] == "")
array_splice($this->full_path, 0, 1);
}
/**
* Removes uri elements other than the resource path
* @param string $input
* @return string
*/
private static function stripVariablesFromPath(string $input): string
{
$without_get_variables = explode('?', $input, 2)[0];
$without_focus_element = explode('#', $without_get_variables, 2)[0];
return $without_focus_element;
}
/**
* Gathers information about the request from $_SERVER
* @return void
*/
private function defineFromServerGlobals(): void
{
$this->defineFromString($_SERVER["REQUEST_URI"]);
$this->setMethod($_SERVER["REQUEST_METHOD"] ?? RequestMethod::GET);
}
/**
* String representation of object
* @return string
*/
public function serialize(): string
{
return $this->method() . '*' . implode('/', $this->path);
}
/**
* Constructs the object from its string representation
* @param string $serialized
* @return void
*/
public function unserialize(string $serialized): void
{
$data = explode('*', $serialized, 2);
$this->setMethod($data[0]);
$this->defineFromString($data[1]);
}
}