request.class.php 901 B

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