<?php
class Url {
private $user;
private $pass;
private $scheme;
private $host;
private $port;
private $path;
private $query = [];
private $fragment;
public function __construct($url){
$parts = parse_url($url);
$parts = array_merge(array("user"=>"","pass"=>"", "scheme"=>"", "host"=>"", "port"=>"", "path"=>"", "query"=>"", "fragment"=>""), $parts);
$this->user = $parts["user"];
$this->pass = $parts["pass"];
$this->scheme = $parts["scheme"];
$this->host = $parts["host"];
$this->port = $parts["port"];
$this->path = $parts["path"];
parse_str($parts["query"], $this->query);
$this->fragment = $parts["fragment"];
}
public function __get($property){
if (!property_exists($this, $property)){
throw new ErrorException("Property $property does not exist");
}
return $this->$property;
}
public function __set($property, $value){
if (!property_exists($this, $property)){
throw new ErrorException("Property $property does not exist");
}
$this->$property = $value;
}
public function setQueryParam($key, $value){
$this->query[$key] = $value;
}
public function getQueryParam($key, $default = false){
if (array_key_exists($key, $this->query)){
return $this->query["key"];
} else {
return $default;
}
}
public function __toString(){
return ($this->scheme?$this->scheme."://":"").
($this->user && $this->pass?$this->user.":".$this->pass."@":($this->user?$this->user."@":"")).
($this->host?$this->host:"").
($this->path?$this->path:"").
($this->query?"?".http_build_query($this->query):"").
($this->fragment?"#".$this->fragment:"");
}
}
$url = new Url("http://test.com/test-one,two three?a=b#bla");
$url->setQueryParam("a", "blah");
$url->setQueryParam("foo", "bar");
$url->scheme = "https";
$url->user = "admin";
$url->pass = "topsecret";
echo (string)$url;
?>
preferences:
40.41 ms | 402 KiB | 5 Q