1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- class Uri{
- private $url;
- public function __construct($url){
- if(is_array($url)){
- $this->url = $url;
- }else if(is_string($url)){
- $this->url = parse_url($url);
- }else{
- throw new Exception("Invalid Url");
- }
- }
- public function __get($name){
- if(isset($this->url[$name])){
- return $this->url[$name];
- }else{
- switch($name){
- case 'variables':
- parse_str((string)$this, $output);
- return $output;
- break;
- }
- }
- }
- public function __set($name, $value){
- if(isset($this->url[$name])){
- $this->url[$name] = $value;
- }
- }
- public function __toString(){
- $port = $this->port;
- if($port){
- if($this->scheme == 'http'){
- $port = $port == 80 ? "" : ":{$port}";
- }elseif($this->scheme = 'https'){
- $port = $port == 443 ? "" : ":{$port}";
- }
- // @todo - add other default port types
- }
- $auth = $this->user;
- if($auth){
- $auth = $this->pass ? "{$auth}:{$this->pass}@" : "{$auth}@";
- }
- $query = $this->query;
- if($query){
- $query = "?{$query}";
- }
- $fragmanet = $this->fragmanet;
- if($fragmanet){
- $fragmanet = "#{$fragmanet}";
- }
- // @todo - handle when scheme requires other formats
- return "{$this->scheme}://{$auth}{$this->host}{$port}{$this->path}{$query}{$fragmanet}";
- }
- }
- ?>
|