|
- <?php
-
- declare(strict_types=1);
-
- namespace Core;
-
- class Request
- {
- protected static ?self $current = null;
- protected string $method;
- protected string $uri;
- protected array $get;
- protected array $post;
- protected array $server;
-
- public function __construct(array $get, array $post, array $server)
- {
- $this->get = $get;
- $this->post = $post;
- $this->server = $server;
- $this->method = $server['REQUEST_METHOD'] ?? 'GET';
- $this->uri = $server['REQUEST_URI'] ?? '/';
- }
-
- public static function capture(): self
- {
- if (self::$current instanceof self) {
- return self::$current;
- }
-
- return new self($_GET, $_POST, $_SERVER);
- }
-
- public static function setCurrent(self $request): void
- {
- self::$current = $request;
- }
-
- public static function clearCurrent(): void
- {
- self::$current = null;
- }
-
- public function method(): string
- {
- return strtoupper($this->method);
- }
-
- public function path(): string
- {
- $path = parse_url($this->uri, PHP_URL_PATH);
-
- return $path ?: '/';
- }
-
- public function input(string $key, mixed $default = null): mixed
- {
- return $this->post[$key] ?? $this->get[$key] ?? $default;
- }
-
- public function server(string $key, mixed $default = null): mixed
- {
- return $this->server[$key] ?? $default;
- }
-
- public function all(): array
- {
- return array_merge($this->get, $this->post);
- }
- }
|