12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- namespace Juju\Http;
- class Uri implements \JsonSerializable {
- 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}";
- }
- public function jsonSerialize(){
- return "$this";
- }
- }
- ?>
|