1
0

uri.class.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. }
  39. $auth = $this->user;
  40. if($auth){
  41. $auth = $this->pass ? "{$auth}:{$this->pass}@" : "{$auth}@";
  42. }
  43. $query = $this->query;
  44. if($query){
  45. $query = "?{$query}";
  46. }
  47. $fragmanet = $this->fragmanet;
  48. if($fragmanet){
  49. $fragmanet = "#{$fragmanet}";
  50. }
  51. return "{$this->scheme}://{$auth}{$this->host}{$port}{$this->path}{$query}{$fragmanet}";
  52. }
  53. }
  54. ?>