Request.php 756 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace Src;
  3. use Error;
  4. class Request {
  5. protected array $body;
  6. public string $method;
  7. public array $headers;
  8. public function __construct()
  9. {
  10. $this->body = $_REQUEST;
  11. $this->method = $_SERVER['REQUEST_METHOD'];
  12. $this->headers = getallheaders() ?? [];
  13. }
  14. public function set($field, $value): void {
  15. $this->body[$field] = $value;
  16. }
  17. public function get($field) {
  18. return $this->body[$field];
  19. }
  20. public function files(): array {
  21. return $_FILES;
  22. }
  23. public function __get($key)
  24. {
  25. if (array_key_exists($key, $this->body)) {
  26. return $this->body[$key];
  27. }
  28. throw new Error('Acessing a non-existent property');
  29. }
  30. }