uri.class.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Juju\Http;
  3. class Uri implements \JsonSerializable {
  4. private $url;
  5. public function __construct($url){
  6. if(is_array($url)){
  7. $this->url = $url;
  8. }else if(is_string($url)){
  9. $this->url = parse_url($url);
  10. }else{
  11. throw new \Exception("Invalid Url");
  12. }
  13. }
  14. public function __get($name){
  15. if(isset($this->url[$name])){
  16. return $this->url[$name];
  17. }else{
  18. switch($name){
  19. case 'variables':
  20. parse_str((string)$this, $output);
  21. return $output;
  22. break;
  23. }
  24. }
  25. }
  26. public function __set($name, $value){
  27. if(isset($this->url[$name])){
  28. $this->url[$name] = $value;
  29. }
  30. }
  31. public function __toString(){
  32. $port = $this->port;
  33. if($port){
  34. if($this->scheme == 'http'){
  35. $port = $port == 80 ? "" : ":{$port}";
  36. }elseif($this->scheme = 'https'){
  37. $port = $port == 443 ? "" : ":{$port}";
  38. }
  39. // @todo - add other default port types
  40. }
  41. $auth = $this->user;
  42. if($auth){
  43. $auth = $this->pass ? "{$auth}:{$this->pass}@" : "{$auth}@";
  44. }
  45. $query = $this->query;
  46. if($query){
  47. $query = "?{$query}";
  48. }
  49. $fragmanet = $this->fragmanet;
  50. if($fragmanet){
  51. $fragmanet = "#{$fragmanet}";
  52. }
  53. // @todo - handle when scheme requires other formats
  54. return "{$this->scheme}://{$auth}{$this->host}{$port}{$this->path}{$query}{$fragmanet}";
  55. }
  56. public function jsonSerialize(){
  57. return "$this";
  58. }
  59. }
  60. ?>