31 public static function factory($url)
33 static $defaults = array(
'scheme' =>
null,
'host' =>
null,
'path' =>
null,
'port' =>
null,
'query' =>
null,
34 'user' =>
null,
'pass' =>
null,
'fragment' =>
null);
36 if (
false === ($parts = parse_url($url))) {
43 if ($parts[
'query'] || 0 !== strlen($parts[
'query'])) {
47 return new static($parts[
'scheme'], $parts[
'host'], $parts[
'user'],
48 $parts[
'pass'], $parts[
'port'], $parts[
'path'], $parts[
'query'],
59 public static function buildUrl(array $parts)
63 if (isset($parts[
'scheme'])) {
68 if (isset($parts[
'host'])) {
70 if (isset($parts[
'user'])) {
71 $url .= $parts[
'user'];
72 if (isset($parts[
'pass'])) {
73 $url .=
':' . $parts[
'pass'];
78 $url .= $parts[
'host'];
81 if (isset($parts[
'port'])
82 && !((
$scheme ==
'http' && $parts[
'port'] == 80) || (
$scheme ==
'https' && $parts[
'port'] == 443))
84 $url .=
':' . $parts[
'port'];
89 if (isset($parts[
'path']) && 0 !== strlen($parts[
'path'])) {
91 if ($url && $parts[
'path'][0] !=
'/' && substr($url, -1) !=
'/') {
94 $url .= $parts[
'path'];
98 if (isset($parts[
'query'])) {
99 $url .=
'?' . $parts[
'query'];
103 if (isset($parts[
'fragment'])) {
104 $url .=
'#' . $parts[
'fragment'];
163 $query = (string) $this->query;
166 'scheme' => $this->scheme,
167 'user' => $this->username,
168 'pass' => $this->password,
169 'host' => $this->host,
170 'port' => $this->port,
173 'fragment' => $this->fragment,
186 if (strpos(
$host,
':') ===
false) {
216 if ($this->scheme ==
'http' && $this->port == 80) {
218 } elseif ($this->scheme ==
'https' && $this->port == 443) {
260 } elseif ($this->scheme ==
'http') {
262 } elseif ($this->scheme ==
'https') {
278 static $pathReplace = array(
' ' =>
'%20',
'?' =>
'%3F');
283 $this->path = strtr(
$path, $pathReplace);
295 if (!$this->path || $this->path ==
'/' || $this->path ==
'*') {
301 foreach ($segments as $segment) {
302 if ($segment ==
'..') {
304 } elseif ($segment !=
'.' && $segment !=
'') {
305 $results[] = $segment;
310 $this->path = ($this->path[0] ==
'/' ?
'/' :
'') . implode(
'/', $results);
313 if ($this->path !=
'/' && end($segments) ==
'') {
327 public function addPath($relativePath)
329 if ($relativePath !=
'/' && is_string($relativePath) && strlen($relativePath) > 0) {
331 if ($relativePath[0] !=
'/') {
332 $relativePath =
'/' . $relativePath;
334 $this->
setPath(str_replace(
'//',
'/', $this->path . $relativePath));
357 return array_slice(explode(
'/', $this->
getPath()), 1);
429 parse_str(
$query, $output);
431 } elseif (is_array(
$query)) {
433 } elseif (
$query instanceof QueryString) {
488 public function combine($url, $strictRfc3986 =
false)
493 if (!$this->
isAbsolute() && $url->isAbsolute()) {
494 $url = $url->combine($this);
498 if ($buffer = $url->getScheme()) {
499 $this->scheme = $buffer;
500 $this->host = $url->getHost();
501 $this->port = $url->getPort();
502 $this->username = $url->getUsername();
503 $this->password = $url->getPassword();
504 $this->path = $url->getPath();
505 $this->query = $url->getQuery();
506 $this->fragment = $url->getFragment();
511 if ($buffer = $url->getHost()) {
512 $this->host = $buffer;
513 $this->port = $url->getPort();
514 $this->username = $url->getUsername();
515 $this->password = $url->getPassword();
516 $this->path = $url->getPath();
517 $this->query = $url->getQuery();
518 $this->fragment = $url->getFragment();
522 $path = $url->getPath();
523 $query = $url->getQuery();
527 $this->addQuery(
$query, $strictRfc3986);
530 if (
$path[0] ==
'/') {
532 } elseif ($strictRfc3986) {
533 $this->path .=
'/../' .
$path;
535 $this->path .=
'/' .
$path;
538 $this->addQuery(
$query, $strictRfc3986);
541 $this->fragment = $url->getFragment();
546 private function addQuery(QueryString $new, $strictRfc386)
548 if (!$strictRfc386) {
549 $new->merge($this->query);