request.class.php 957 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace Juju\Http;
  3. require_once('uri.class.php');
  4. class Request {
  5. private $url;
  6. private $headers;
  7. private $body;
  8. public function __construct($url, array $headers = [], string $body = ''){
  9. if(is_string($url) || is_array($url)){
  10. $this->url = new Uri($url);
  11. }elseif($url instanceof Uri){
  12. $this->url = $url;
  13. }else{
  14. throw new \Exception("Invalid url {$url}");
  15. }
  16. if(is_array($headers)){
  17. $this->headers = $headers;
  18. }else{
  19. $this->headers = [];
  20. }
  21. $this->body = $body;
  22. }
  23. public function __get($name){
  24. switch($name){
  25. case 'headers':
  26. return $this->headers;
  27. break;
  28. case 'url':
  29. return $this->url;
  30. break;
  31. case 'body':
  32. return $this->body;
  33. break;
  34. }
  35. }
  36. public function header($name){
  37. return $this->headers[$name] ?? null;
  38. }
  39. public function json(){
  40. return json_decode($this->body, true);
  41. }
  42. public function text(){
  43. return $this->body;
  44. }
  45. }
  46. ?>