-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequest.php
166 lines (139 loc) · 3.46 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
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
<?php
namespace Rodri\SimpleRouter;
use JetBrains\PhpStorm\Pure;
/**
* Class Request
* @package Rodri\SimpleRouter
* @author Rodrigo Andrade
* @version 1.3.0
*/
class Request
{
# Attributes
private array $params;
private array $values;
# Constants
public const PARAM_SEPARATOR = ':';
public function __construct()
{
$this->params = [];
$this->values = [];
}
/**
* @return string|bool
*/
public function body(): string|bool
{
return file_get_contents('php://input');
}
/**
* Add a new value to be carried between controllers and middlewares.
* @param string $name
* @param mixed $value
*/
public function addValue(string $name, mixed $value): void
{
$this->values[$name] = $value;
}
/**
* Get a value set before to be carried
* @param string $name
* @return mixed
*/
public function getValue(string $name): mixed
{
return $this->values[$name] ?? null;
}
/**
* Get a value from body request
* @param string $in
* @return mixed
*/
public function input(string $in): mixed
{
$associativeBody = json_decode($this->body(), true);
if (!$associativeBody || !$associativeBody[$in])
return '';
return $associativeBody[$in];
}
/**
* Get a router param by named param with separator ':'
* @param String $param
* @return mixed
*/
public function param(string $param): mixed
{
return !empty($this->params[$param]) ? $this->params[$param] : null;
}
/**
* Get all params
* @return array
*/
public function allParams(): array
{
return $this->params;
}
/**
* Get the authorization value or return false if not exists.
* @return bool|array
*/
public function authorization(): bool|string
{
return $_SERVER['HTTP_AUTHORIZATION'] ?? false;
}
#[Pure] public function method(string $method): bool
{
return $method == $this->methodRequest();
}
/**
* Check if a uri from a handle is equals a uri from server
* request, and split the expected params.
* @param string $uri
* @return bool
*/
public function uri(string $uri): bool
{
# Split by the bar
$uriSplit = explode('/', $uri);
$requestSplit = explode('/', $this->uriRequest());
if (count($uriSplit) != count($requestSplit))
return false;
for ($i = 0; $i < count($uriSplit); $i++) {
# Search the separator to get a param
if (str_contains($uriSplit[$i], Request::PARAM_SEPARATOR)) {
$this->params[$uriSplit[$i]] = $requestSplit[$i];
continue;
}
# If the uri and serve uir is different
if ($uriSplit[$i] != $requestSplit[$i])
return false;
}
return true;
}
/**
* Server URI Request
* @return mixed
*/
private function uriRequest(): mixed
{
return $_SERVER['REQUEST_URI'];
}
/**
* Server method
* @return mixed
*/
private function methodRequest(): mixed
{
return $_SERVER['REQUEST_METHOD'];
}
private function __clone(): void
{
}
public function __set(string $name, $value): void
{
}
public function __get(string $name): mixed
{
return $this->getValue($name);
}
}