<?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}";
				}
			}
			$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}";
			}
			return "{$this->scheme}://{$auth}{$this->host}{$port}{$this->path}{$query}{$fragmanet}";
		}
	}
?>