Request.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Core;
  3. /*
  4. * Proporciona herramientas para
  5. * las peticiones de la aplicación.
  6. */
  7. class Request
  8. {
  9. /*
  10. * Obtiene la ruta de la petición.
  11. */
  12. public function getPath()
  13. {
  14. $path = $_SERVER['REQUEST_URI'] ?? '/';
  15. $position = strpos($path, '?');
  16. // Elimina los query params de la petición.
  17. if ($position !== false) {
  18. $path = substr($path, 0, $position);
  19. }
  20. return $path;
  21. }
  22. /*
  23. * Obtiene el método HTTP de la petición.
  24. */
  25. public function getMethod()
  26. {
  27. return strtolower($_SERVER['REQUEST_METHOD']);
  28. }
  29. /*
  30. * Valida si el método HTTP de la petición es GET.
  31. */
  32. public function isGet()
  33. {
  34. return $this->getMethod() === 'get';
  35. }
  36. /*
  37. * Valida si el método HTTP de la petición es POST.
  38. */
  39. public function isPost()
  40. {
  41. return $this->getMethod() === 'post';
  42. }
  43. /*
  44. * Obtiene el cuerpo de la petición.
  45. */
  46. public function getBody()
  47. {
  48. $body = [];
  49. if ($this->isGet()) {
  50. foreach ($_GET as $key => $value) {
  51. $body[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_SPECIAL_CHARS);
  52. }
  53. }
  54. if ($this->isPost()) {
  55. foreach ($_POST as $key => $value) {
  56. $body[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
  57. }
  58. }
  59. return $body;
  60. }
  61. }