<?php
namespace Guzzle\Http;
use Guzzle\Common\Exception\InvalidArgumentException;
use Guzzle\Parser\ParserRegistry;
class Url {
protected $scheme;
protected $host;
protected $port;
protected $username;
protected $password;
protected $path = '/';
protected $fragment;
protected $query;
public static function factory($url) {
$parts = ParserRegistry::getInstance()
->getParser('url')
->parseUrl($url);
if ($parts['query']) {
$parts['query'] = QueryString::fromString($parts['query']);
}
return new self($parts['scheme'], $parts['host'], $parts['user'], $parts['pass'], $parts['port'], $parts['path'], $parts['query'], $parts['fragment']);
}
public static function buildUrl(array $parts) {
$url = $scheme = '';
if (isset($parts['scheme'])) {
$scheme = $parts['scheme'];
$url .= $scheme . '://';
}
if (isset($parts['host'])) {
if (isset($parts['user'])) {
$url .= $parts['user'];
if (isset($parts['pass'])) {
$url .= ':' . $parts['pass'];
}
$url .= '@';
}
$url .= $parts['host'];
if (isset($parts['port']) && !($scheme == 'http' && $parts['port'] == 80 || $scheme == 'https' && $parts['port'] == 443)) {
$url .= ':' . $parts['port'];
}
}
if (empty($parts['path'])) {
$url .= '/';
}
else {
if ($parts['path'][0] != '/') {
$url .= '/';
}
$url .= $parts['path'];
}
if (!empty($parts['query'])) {
if ($parts['query'][0] != '?') {
$url .= array_key_exists('query_prefix', $parts) ? $parts['query_prefix'] : '?';
}
$url .= $parts['query'];
}
if (isset($parts['fragment']) && !empty($parts['fragment'])) {
$url .= '#' . $parts['fragment'];
}
return $url;
}
public function __construct($scheme, $host, $username = null, $password = null, $port = null, $path = null, QueryString $query = null, $fragment = null) {
$this->scheme = $scheme;
$this->host = $host;
$this->port = $port;
$this->username = $username;
$this->password = $password;
$this->fragment = $fragment;
$this
->setQuery($query ?: new QueryString());
if ($path) {
$this
->setPath($path);
}
}
public function __clone() {
$this->query = clone $this->query;
}
public function __toString() {
return self::buildUrl($this
->getParts());
}
public function getParts() {
return array(
'scheme' => $this->scheme,
'user' => $this->username,
'pass' => $this->password,
'host' => $this->host,
'port' => $this->port,
'path' => $this
->getPath(),
'query' => (string) $this->query,
'fragment' => $this->fragment,
'query_prefix' => $this->query
->getPrefix(),
);
}
public function setHost($host) {
if (strpos($host, ':') === false) {
$this->host = $host;
}
else {
list($host, $port) = explode(':', $host);
$this->host = $host;
$this
->setPort($port);
}
return $this;
}
public function getHost() {
return $this->host;
}
public function setScheme($scheme) {
$this->scheme = $scheme;
return $this;
}
public function getScheme() {
return $this->scheme;
}
public function setPort($port) {
$this->port = $port;
return $this;
}
public function getPort() {
if ($this->port) {
return $this->port;
}
elseif ($this->scheme == 'http') {
return 80;
}
elseif ($this->scheme == 'https') {
return 443;
}
return null;
}
public function setPath($path) {
if (is_array($path)) {
$this->path = '/' . implode('/', $path);
}
else {
if (substr($path, 0, 1) != '/' && $path != '*') {
$path = '/' . $path;
}
$this->path = $path;
}
return $this;
}
public function normalizePath() {
if ($this->path == '*') {
return $this;
}
if ($this->path && $this->path != '/') {
$this->path = str_replace(array(
'/./',
'//',
), '/', $this->path);
$segments = $this
->getPathSegments();
$last = end($segments);
$trailingSlash = false;
if ($last === '') {
array_pop($segments);
$trailingSlash = true;
}
while ($last == '..' || $last == '.') {
if ($last == '..') {
array_pop($segments);
$last = array_pop($segments);
}
if ($last == '.' || $last == '') {
$last = array_pop($segments);
}
}
$this->path = implode('/', $segments);
if ($trailingSlash) {
$this->path .= '/';
}
}
if (substr($this->path, 0, 1) != '/') {
$this->path = '/' . $this->path;
}
return $this;
}
public function addPath($relativePath) {
if (!$relativePath || $relativePath == '/') {
return $this;
}
if ($relativePath[0] != '/') {
$relativePath = '/' . $relativePath;
}
return $this
->setPath(str_replace('//', '/', $this
->getPath() . $relativePath));
}
public function getPath() {
return $this->path ?: '/';
}
public function getPathSegments() {
return array_slice(explode('/', $this
->getPath()), 1);
}
public function setPassword($password) {
$this->password = $password;
return $this;
}
public function getPassword() {
return $this->password;
}
public function setUsername($username) {
$this->username = $username;
return $this;
}
public function getUsername() {
return $this->username;
}
public function getQuery() {
return $this->query;
}
public function setQuery($query) {
if (is_string($query)) {
$output = null;
parse_str($query, $output);
$this->query = new QueryString($output);
}
elseif (is_array($query)) {
$this->query = new QueryString($query);
}
elseif ($query instanceof QueryString) {
$this->query = $query;
}
return $this;
}
public function getFragment() {
return $this->fragment;
}
public function setFragment($fragment) {
$this->fragment = $fragment;
return $this;
}
public function isAbsolute() {
return $this->scheme && $this->host;
}
public function combine($url) {
$absolutePath = $url[0] == '/';
$url = self::factory($url);
if ($buffer = $url
->getScheme()) {
$this->scheme = $buffer;
}
if ($buffer = $url
->getHost()) {
$this->host = $buffer;
}
if ($buffer = $url
->getPort()) {
$this->port = $buffer;
}
if ($buffer = $url
->getUsername()) {
$this->username = $buffer;
}
if ($buffer = $url
->getPassword()) {
$this->password = $buffer;
}
if ($buffer = $url
->getFragment()) {
$this->fragment = $buffer;
}
if ($absolutePath) {
if ($buffer = $url
->getPath()) {
$this->path = $buffer;
}
if (count($url
->getQuery())) {
$this->query = clone $url
->getQuery();
}
}
else {
if ($buffer = $url
->getPath()) {
$this
->addPath($buffer);
}
if ($buffer = $url
->getQuery()) {
$this->query
->merge($buffer);
}
}
return $this;
}
}