<?php

/**
 * URL Builder Class
 * @author Sam Barrow (www.sambarrow.com)
 *
 * Allows you to create URL objects, to generate URLs on the fly.
 * There is an example at the bottom.
 *
**/

/**
 * Constants
**/

define('sb_protocol_http''http');
define('sb_protocol_https''https');
define('sb_protocol_ftp''ftp');

define('sb_protocol_default'sb_protocol_http);

/**
 * Query string function
**/

function sb_ifsetor(&$var$default) {
    return isset(
$var) ? $var $default;
}
function 
sb_httpq($url, array $vars = array()) {
    
$return $url;
    if (
count($vars)) {
        
$query http_build_query($vars);
        if (
strlen($query)) {
            
$return .= '?' $query;
        }
    }
    return 
$return;
}

/**
 * URL builder class
**/

final class sb_url {

    
/**
     * Constructor
    **/
    
    
public function __construct($domain null$path null, array $vars null$protocol null) {
        if (
$domain !== null) {
            
$this -> setDomain($domain);
        }
        if (
$path !== null) {
            
$this -> setPath($path);
        }
        if (
$vars !== null) {
            
$this -> setVars($vars);
        }
        if (
$protocol !== null) {
            
$this -> setProtocol($protocol);
        }
        return 
true;
    }
    
    
/**
     * Domain
    **/
    
    
private $domain;
    
public function getDomain() {
        return 
$this -> domain;
    }
    
public function setDomain($domain) {
        
$this -> domain $domain;
        return 
true;
    }
    
    
/**
     * Path
    **/
    
    
private $path;
    
public function getPath() {
        return 
sb_ifsetor($this -> path'/');
    }
    
public function setPath($path) {
        
$this -> path $path;
        return 
true;
    }
    
    
/**
     * Port
    **/
    
    
private $port;
    
public function hasPort() {
        return isset(
$this -> port);
    }
    
public function getPort() {
        return 
$this -> port;
    }
    
public function setPort($port) {
        
$this -> port $port;
        return 
true;
    }
    
    
/**
     * Protocol
    **/
    
    
private $protocol;
    
public function getProtocol() {
        return 
sb_ifsetor($this -> protocolsb_protocol_default);
    }
    
public function setProtocol($protocol) {
        
$this -> protocol $protocol;
        return 
true;
    }
    
    
/**
     * Vars
    **/
    
    
private $vars;
    
public function getVars() {
        return 
sb_ifsetor($this -> vars, array());
    }
    
public function setVars(array $vars) {
        
$this -> vars $vars;
        return 
true;
    }
    
public function addVar($name$value) {
        
$this -> vars[$name] = $value;
        return 
true;
    }
    
public function addVars(array $vars) {
        foreach (
$vars as $name => $value) {
            
$this -> addVar($value);
        }
        return 
true;
    }
    
    
/**
     * Generate URL
    **/
    
    
public function generate() {
        
$url $this -> getProtocol() . '://' $this -> getDomain();
        if (
$this -> hasPort()) {
            
$url .= ':' $this -> getPort();
        }
        
$url .= sb_httpq($this -> getPath(), $this -> getVars());
        return 
$url;
    }
    
}

/**
 * Example
**/

// My blog

$url = new sb_url;
$url -> setDomain('www.sambarrow.com');

echo 
$url -> generate() . '<br />';

// My blog archives

$url -> setPath('/archives');

echo 
$url -> generate() . '<br />';

// My blog (using SSL)

$url -> setPath('/');
$url -> setProtocol(sb_protocol_https);

echo 
$url -> generate() . '<br />';