Skip to content

Commit c87652a

Browse files
committed
Match method added to Router.php class
1 parent 20176c3 commit c87652a

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/Request.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,15 @@ public function post(string $key)
105105
* @param string $key
106106
* @return string
107107
*/
108-
public function input(string $key): string
108+
public function input(string $key): string|null
109109
{
110-
return json_decode(file_get_contents("php://input"), false)->{$key} ?? '';
110+
if (array_key_exists($key, $_POST)) {
111+
return $_POST[$key] ?? null;
112+
}
113+
if (array_key_exists($key, $_GET)) {
114+
return $_GET[$key] ?? null;
115+
}
116+
return json_decode(file_get_contents("php://input"), false)?->{$key};
111117
}
112118

113119
/**
@@ -150,7 +156,7 @@ public function getProtocol(): string
150156
*/
151157
public function getHost()
152158
{
153-
return parse_url($_SERVER['HTTP_HOST'],PHP_URL_HOST);
159+
return parse_url($_SERVER['HTTP_HOST'], PHP_URL_HOST);
154160
}
155161

156162
/**
@@ -175,7 +181,7 @@ public function ip()
175181
*/
176182
public function session(string $key = null): mixed
177183
{
178-
if(!isset($_SESSION[$key])){
184+
if (!isset($_SESSION[$key])) {
179185
return null;
180186
}
181187
return $_SESSION[$key];
@@ -187,7 +193,7 @@ public function session(string $key = null): mixed
187193
*/
188194
public function cookie(string $key = null): mixed
189195
{
190-
if(!isset($_COOKIE[$key])){
196+
if (!isset($_COOKIE[$key])) {
191197
return null;
192198
}
193199
return $_COOKIE[$key];

src/Router.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ final class Router implements IRoute
3131
private array $middlewares = [];
3232

3333

34+
const GET = 'GET';
35+
const POST = 'POST';
36+
const PUT = 'PUT';
37+
const PATCH = 'PATCH';
38+
const DELETE = 'DELETE';
39+
40+
3441
/**
3542
*
3643
* @throws ExceptionAlias
@@ -138,6 +145,23 @@ public static function delete(string $path, callable|array $callback): IRoute
138145
}
139146

140147

148+
/**
149+
* @param array $methods
150+
* @param string $path
151+
* @param callable|array $callback
152+
* @return IRoute
153+
* @throws ExceptionAlias
154+
*/
155+
public static function match(array $methods, string $path, callable|array $callback): IRoute
156+
{
157+
foreach ($methods as $method) {
158+
static::getInstance()->addRoute($method, $path, $callback);
159+
}
160+
161+
return static::getInstance();
162+
}
163+
164+
141165
/**
142166
* @return void
143167
* @throws ExceptionAlias

0 commit comments

Comments
 (0)