Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

81 строка
1.8KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace Core;
  4. class Request
  5. {
  6. protected static ?self $current = null;
  7. protected string $method;
  8. protected string $uri;
  9. protected array $get;
  10. protected array $post;
  11. protected array $server;
  12. public function __construct(array $get, array $post, array $server)
  13. {
  14. $this->get = $get;
  15. $this->post = $post;
  16. $this->server = $server;
  17. $this->method = $server['REQUEST_METHOD'] ?? 'GET';
  18. $this->uri = $server['REQUEST_URI'] ?? '/';
  19. }
  20. public static function capture(): self
  21. {
  22. if (self::$current instanceof self) {
  23. return self::$current;
  24. }
  25. return new self($_GET, $_POST, $_SERVER);
  26. }
  27. public static function setCurrent(self $request): void
  28. {
  29. self::$current = $request;
  30. }
  31. public static function clearCurrent(): void
  32. {
  33. self::$current = null;
  34. }
  35. public function method(): string
  36. {
  37. $method = strtoupper($this->method);
  38. if ($method === 'POST') {
  39. $override = strtoupper((string) ($this->post['_method'] ?? $this->server['HTTP_X_HTTP_METHOD_OVERRIDE'] ?? ''));
  40. if (in_array($override, ['PUT', 'PATCH', 'DELETE'], true)) {
  41. return $override;
  42. }
  43. }
  44. return $method;
  45. }
  46. public function path(): string
  47. {
  48. $path = parse_url($this->uri, PHP_URL_PATH);
  49. return $path ?: '/';
  50. }
  51. public function input(string $key, mixed $default = null): mixed
  52. {
  53. return $this->post[$key] ?? $this->get[$key] ?? $default;
  54. }
  55. public function server(string $key, mixed $default = null): mixed
  56. {
  57. return $this->server[$key] ?? $default;
  58. }
  59. public function all(): array
  60. {
  61. return array_merge($this->get, $this->post);
  62. }
  63. }

Powered by TurnKey Linux.