Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: ae2f0dfd8b
Fetching contributors…

Cannot retrieve contributors at this time

178 lines (157 sloc) 3.738 kB
<?php
/**
* URI constructor.
*
* @package Uri
* @author Anton Lindqvist <anton@qvister.se>
* @copyright 2011 Anton Lindqvist <anton@qvister.se>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://github.com/mptre/php-uri
*/
class Uri {
/**
* URI protocol.
*
* @access public
*
* @var string
*/
public $protocol;
/**
* URI username.
*
* @access public
*
* @var string
*/
public $username;
/**
* URI password.
*
* @access public
*
* @var string
*/
public $password;
/**
* URI hostname.
*
* @access public
*
* @var string
*/
public $host;
/**
* URI path.
*
* @access public
*
* @var array
*/
public $path;
/**
* URI parameters.
*
* @access public
*
* @var array
*/
public $parameters;
/**
* URI hash.
*
* @access public
*
* @var array
*/
public $hash;
/**
* Default options.
*
* @access private
* @static
*
* @var array
*/
private static $_defaultOptions = array(
'encode' => false,
'hash' => null,
'parameters' => null,
'password' => null,
'port' => 80,
'protocol' => 'http',
'separator' => '&',
'ssl' => false,
'username' => null
);
/**
* Options.
*
* @access private
*
* @var array
*/
private $_options;
/**
* Class constructor.
*
* @access public
*
* @param string $host Hostname
* @param string|array $path Optional path to request either as a string or array
* @param array $options Optional options
*
* @return void
*/
public function __construct($host, $path = null, $options = array()) {
$this->host = $host;
$this->path = $path;
$this->_options = array_merge(self::$_defaultOptions, $options);
$this->initialize($host, $path);
}
/**
* Construct public variables.
*
* @access public
*
* @return void
*/
public function initialize() {
$this->protocol = $this->_options['protocol'];
$this->protocol .= ($this->_options['ssl']) ? 's' : '';
$this->username = $this->_options['username'];
$this->password = $this->_options['password'];
$this->host = $this->host;
$this->host .= ($this->_options['port'] != 80)
? ':' . $this->_options['port']
: '';
$this->path = (is_array($this->path)) ? $this->path : array($this->path);
$this->path = '/' . implode('/' , array_filter($this->path));
$this->parameters = $this->_options['parameters'];
$this->hash = $this->_options['hash'];
}
/**
* Magic method used for constructing the URI.
*
* @access public
*
* @return string $uri
*/
public function __toString() {
$uri = $this->protocol;
$uri .= '://';
$uri .= ($this->username) ?: '';
$uri .= ($this->username && $this->password) ? ':' . $this->password : '';
$uri .= ($this->username || $this->password) ? '@' : '';
$uri .= $this->host;
$uri .= $this->path;
$uri .= ($this->parameters)
? '?' . http_build_query($this->parameters, '', $this->_options['separator'])
: '';
$uri .= ($this->hash)
? '#' . http_build_query($this->hash, '', $this->_options['separator'])
: '';
$uri = ($this->_options['encode']) ? urlencode($uri) : $uri;
return $uri;
}
}
Jump to Line
Something went wrong with that request. Please try again.