Request.php 860 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 function __construct()
  10. {
  11. $this->body = $_REQUEST;
  12. $this->method = $_SERVER['REQUEST_METHOD'];
  13. $this->headers = getallheaders() ?? [];
  14. }
  15. public function all(): array
  16. {
  17. return $this->body + $this->files();
  18. }
  19. public function set($field, $value): void
  20. {
  21. $this->body[$field] = $value;
  22. }
  23. public function get($field)
  24. {
  25. return $this->body[$field];
  26. }
  27. public function files(): array
  28. {
  29. return $_FILES;
  30. }
  31. public function __get($key)
  32. {
  33. if (array_key_exists($key, $this->body)) {
  34. return $this->body[$key];
  35. }
  36. throw new Error('Accessing a non-existent property');
  37. }
  38. }