Request.php 984 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace Src;
  3. use Error;
  4. class Request
  5. {
  6. protected array $body;
  7. public string $method;
  8. public array $headers;
  9. public array $post;
  10. public string $url;
  11. public function __construct()
  12. {
  13. $this->body = $_REQUEST;
  14. $this->method = $_SERVER['REQUEST_METHOD'];
  15. $this->url = $_SERVER['REQUEST_URI'];
  16. $this->headers = getallheaders() ?? [];
  17. $this->post = $_POST;
  18. }
  19. public function all(): array
  20. {
  21. return $this->body + $this->files();
  22. }
  23. public function set($field, $value): void
  24. {
  25. $this->body[$field] = $value;
  26. }
  27. public function get($field)
  28. {
  29. return $this->body[$field];
  30. }
  31. public function files(): array
  32. {
  33. return $_FILES;
  34. }
  35. public function __get($key)
  36. {
  37. if (array_key_exists($key, $this->body)) {
  38. return $this->body[$key];
  39. }
  40. throw new Error('Accessing a non-existent property');
  41. }
  42. }