選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

71 行
1.5KB

  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. return strtoupper($this->method);
  38. }
  39. public function path(): string
  40. {
  41. $path = parse_url($this->uri, PHP_URL_PATH);
  42. return $path ?: '/';
  43. }
  44. public function input(string $key, mixed $default = null): mixed
  45. {
  46. return $this->post[$key] ?? $this->get[$key] ?? $default;
  47. }
  48. public function server(string $key, mixed $default = null): mixed
  49. {
  50. return $this->server[$key] ?? $default;
  51. }
  52. public function all(): array
  53. {
  54. return array_merge($this->get, $this->post);
  55. }
  56. }

Powered by TurnKey Linux.