-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
21 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
|
||
|
||
/** | ||
* URI Syntax (RFC 3986). | ||
* Mutable representation of a URL. | ||
* | ||
* <pre> | ||
* scheme user password host port basePath relativeUrl | ||
|
@@ -25,12 +25,6 @@ | |
* authority path query fragment | ||
* </pre> | ||
* | ||
* - authority: [user[:password]@]host[:port] | ||
* - hostUrl: http://user:[email protected]:8042 | ||
* - basePath: /en/ (everything before relative URI not including the script name) | ||
* - baseUrl: http://user:[email protected]:8042/en/ | ||
* - relativeUrl: manual.php | ||
* | ||
* @property string $scheme | ||
* @property string $user | ||
* @property string $password | ||
|
@@ -56,8 +50,6 @@ class Url implements \JsonSerializable | |
'http' => 80, | ||
'https' => 443, | ||
'ftp' => 21, | ||
'news' => 119, | ||
'nntp' => 119, | ||
]; | ||
|
||
/** @var string */ | ||
|
@@ -116,80 +108,64 @@ public function __construct($url = null) | |
|
||
|
||
/** | ||
* Sets the scheme part of URI. | ||
* @return static | ||
*/ | ||
public function setScheme(string $value) | ||
public function setScheme(string $scheme) | ||
{ | ||
$this->scheme = $value; | ||
$this->scheme = $scheme; | ||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* Returns the scheme part of URI. | ||
*/ | ||
public function getScheme(): string | ||
{ | ||
return $this->scheme; | ||
} | ||
|
||
|
||
/** | ||
* Sets the user name part of URI. | ||
* @return static | ||
*/ | ||
public function setUser(string $value) | ||
public function setUser(string $user) | ||
{ | ||
$this->user = $value; | ||
$this->user = $user; | ||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* Returns the user name part of URI. | ||
*/ | ||
public function getUser(): string | ||
{ | ||
return $this->user; | ||
} | ||
|
||
|
||
/** | ||
* Sets the password part of URI. | ||
* @return static | ||
*/ | ||
public function setPassword(string $value) | ||
public function setPassword(string $password) | ||
{ | ||
$this->password = $value; | ||
$this->password = $password; | ||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* Returns the password part of URI. | ||
*/ | ||
public function getPassword(): string | ||
{ | ||
return $this->password; | ||
} | ||
|
||
|
||
/** | ||
* Sets the host part of URI. | ||
* @return static | ||
*/ | ||
public function setHost(string $value) | ||
public function setHost(string $host) | ||
{ | ||
$this->host = $value; | ||
$this->host = $host; | ||
$this->setPath($this->path); | ||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* Returns the host part of URI. | ||
*/ | ||
public function getHost(): string | ||
{ | ||
return $this->host; | ||
|
@@ -208,77 +184,64 @@ public function getDomain(int $level = 2): string | |
|
||
|
||
/** | ||
* Sets the port part of URI. | ||
* @return static | ||
*/ | ||
public function setPort(int $value) | ||
public function setPort(int $port) | ||
{ | ||
$this->port = $value; | ||
$this->port = $port; | ||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* Returns the port part of URI. | ||
*/ | ||
public function getPort(): ?int | ||
{ | ||
return $this->port ?: (self::$defaultPorts[$this->scheme] ?? null); | ||
} | ||
|
||
|
||
/** | ||
* Sets the path part of URI. | ||
* @return static | ||
*/ | ||
public function setPath(string $value) | ||
public function setPath(string $path) | ||
{ | ||
$this->path = $value; | ||
$this->path = $path; | ||
if ($this->host && substr($this->path, 0, 1) !== '/') { | ||
$this->path = '/' . $this->path; | ||
} | ||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* Returns the path part of URI. | ||
*/ | ||
public function getPath(): string | ||
{ | ||
return $this->path; | ||
} | ||
|
||
|
||
/** | ||
* Sets the query part of URI. | ||
* @param string|array $value | ||
* @return static | ||
*/ | ||
public function setQuery($value) | ||
public function setQuery($query) | ||
{ | ||
$this->query = is_array($value) ? $value : self::parseQuery($value); | ||
$this->query = is_array($query) ? $query : self::parseQuery($query); | ||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* Appends the query part of URI. | ||
* @param string|array $value | ||
* @return static | ||
*/ | ||
public function appendQuery($value) | ||
public function appendQuery($query) | ||
{ | ||
$this->query = is_array($value) | ||
? $value + $this->query | ||
: self::parseQuery($this->getQuery() . '&' . $value); | ||
$this->query = is_array($query) | ||
? $query + $this->query | ||
: self::parseQuery($this->getQuery() . '&' . $query); | ||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* Returns the query part of URI. | ||
*/ | ||
public function getQuery(): string | ||
{ | ||
return http_build_query($this->query, '', '&', PHP_QUERY_RFC3986); | ||
|
@@ -315,28 +278,21 @@ public function setQueryParameter(string $name, $value) | |
|
||
|
||
/** | ||
* Sets the fragment part of URI. | ||
* @return static | ||
*/ | ||
public function setFragment(string $value) | ||
public function setFragment(string $fragment) | ||
{ | ||
$this->fragment = $value; | ||
$this->fragment = $fragment; | ||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* Returns the fragment part of URI. | ||
*/ | ||
public function getFragment(): string | ||
{ | ||
return $this->fragment; | ||
} | ||
|
||
|
||
/** | ||
* Returns the entire URI including query string and fragment. | ||
*/ | ||
public function getAbsoluteUrl(): string | ||
{ | ||
return $this->getHostUrl() . $this->path | ||
|
@@ -372,28 +328,19 @@ public function getHostUrl(): string | |
} | ||
|
||
|
||
/** | ||
* Returns the base-path. | ||
*/ | ||
public function getBasePath(): string | ||
{ | ||
$pos = strrpos($this->path, '/'); | ||
return $pos === false ? '' : substr($this->path, 0, $pos + 1); | ||
} | ||
|
||
|
||
/** | ||
* Returns the base-URI. | ||
*/ | ||
public function getBaseUrl(): string | ||
{ | ||
return $this->getHostUrl() . $this->getBasePath(); | ||
} | ||
|
||
|
||
/** | ||
* Returns the relative-URI. | ||
*/ | ||
public function getRelativeUrl(): string | ||
{ | ||
return substr($this->getAbsoluteUrl(), strlen($this->getBaseUrl())); | ||
|