uri.class.php 1.3 KB

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