Uri
in package
implements
UriInterface
PSR-7 URI implementation.
Tags
Interfaces, Classes and Traits
- UriInterface
- Value object representing a URI.
Table of Contents
- HTTP_DEFAULT_HOST = 'localhost'
- Absolute http and https URIs require a host per RFC 7230 Section 2.7 but in generic URIs the host can be empty. So for http(s) URIs we apply this default host when no host is given yet to form a valid URI.
- $charSubDelims : mixed
- $charUnreserved : mixed
- $defaultPorts : mixed
- $fragment : string
- $host : string
- $path : string
- $port : int|null
- $query : string
- $replaceQuery : mixed
- $scheme : string
- $userInfo : string
- __construct() : mixed
- __toString() : string
- Return the string representation as a URI reference.
- composeComponents() : string
- Composes a URI reference string from its various components.
- fromParts() : UriInterface
- Creates a URI from a hash of `parse_url` components.
- getAuthority() : string
- Retrieve the authority component of the URI.
- getFragment() : string
- Retrieve the fragment component of the URI.
- getHost() : string
- Retrieve the host component of the URI.
- getPath() : string
- Retrieve the path component of the URI.
- getPort() : null|int
- Retrieve the port component of the URI.
- getQuery() : string
- Retrieve the query string of the URI.
- getScheme() : string
- Retrieve the scheme component of the URI.
- getUserInfo() : string
- Retrieve the user information component of the URI.
- isAbsolute() : bool
- Whether the URI is absolute, i.e. it has a scheme.
- isAbsolutePathReference() : bool
- Whether the URI is a absolute-path reference.
- isDefaultPort() : bool
- Whether the URI has the default port of the current scheme.
- isNetworkPathReference() : bool
- Whether the URI is a network-path reference.
- isRelativePathReference() : bool
- Whether the URI is a relative-path reference.
- isSameDocumentReference() : bool
- Whether the URI is a same-document reference.
- removeDotSegments() : string
- Removes dot segments from a path and returns the new path.
- resolve() : UriInterface
- Converts the relative URI into a new URI that is resolved against the base URI.
- withFragment() : static
- Return an instance with the specified URI fragment.
- withHost() : static
- Return an instance with the specified host.
- withoutQueryValue() : UriInterface
- Creates a new URI with a specific query string value removed.
- withPath() : static
- Return an instance with the specified path.
- withPort() : static
- Return an instance with the specified port.
- withQuery() : static
- Return an instance with the specified query string.
- withQueryValue() : UriInterface
- Creates a new URI with a specific query string value.
- withQueryValues() : UriInterface
- Creates a new URI with multiple specific query string values.
- withScheme() : static
- Return an instance with the specified scheme.
- withUserInfo() : static
- Return an instance with the specified user information.
- applyParts() : mixed
- Apply parse_url parts to a URI.
- filterHost() : string
- filterPath() : string
- Filters the path of a URI
- filterPort() : int|null
- filterQueryAndFragment() : string
- Filters the query string or fragment of a URI.
- filterScheme() : string
- filterUserInfoComponent() : string
- generateQueryString() : string
- getFilteredQueryString() : array<string|int, mixed>
- parse() : array<string|int, mixed>|false
- UTF-8 aware \parse_url() replacement.
- rawurlencodeMatchZero() : mixed
- removeDefaultPort() : mixed
- validateState() : mixed
Constants
HTTP_DEFAULT_HOST
Absolute http and https URIs require a host per RFC 7230 Section 2.7 but in generic URIs the host can be empty. So for http(s) URIs we apply this default host when no host is given yet to form a valid URI.
public
mixed
HTTP_DEFAULT_HOST
= 'localhost'
Properties
$charSubDelims
private
static mixed
$charSubDelims
= '!\$&'\(\)\*\+,;='
$charUnreserved
private
static mixed
$charUnreserved
= 'a-zA-Z0-9_\-\.~'
$defaultPorts
private
static mixed
$defaultPorts
= ['http' => 80, 'https' => 443, 'ftp' => 21, 'gopher' => 70, 'nntp' => 119, 'news' => 119, 'telnet' => 23, 'tn3270' => 23, 'imap' => 143, 'pop' => 110, 'ldap' => 389]
$fragment
private
string
$fragment
= ''
$host
private
string
$host
= ''
$path
private
string
$path
= ''
$port
private
int|null
$port
$query
private
string
$query
= ''
$replaceQuery
private
static mixed
$replaceQuery
= ['=' => '%3D', '&' => '%26']
$scheme
private
string
$scheme
= ''
$userInfo
private
string
$userInfo
= ''
Methods
__construct()
public
__construct([string $uri = '' ]) : mixed
Parameters
- $uri : string = ''
-
URI to parse
Return values
mixed —__toString()
Return the string representation as a URI reference.
public
__toString() : string
Depending on which components of the URI are present, the resulting string is either a full URI or relative reference according to RFC 3986, Section 4.1. The method concatenates the various components of the URI, using the appropriate delimiters:
- If a scheme is present, it MUST be suffixed by ":".
- If an authority is present, it MUST be prefixed by "//".
- The path can be concatenated without delimiters. But there are two
cases where the path has to be adjusted to make the URI reference
valid as PHP does not allow to throw an exception in __toString():
- If the path is rootless and an authority is present, the path MUST be prefixed by "/".
- If the path is starting with more than one "/" and no authority is present, the starting slashes MUST be reduced to one.
- If a query is present, it MUST be prefixed by "?".
- If a fragment is present, it MUST be prefixed by "#".
Return values
string —composeComponents()
Composes a URI reference string from its various components.
public
static composeComponents(string $scheme, string $authority, string $path, string $query, string $fragment) : string
Usually this method does not need to be called manually but instead is used indirectly via
Psr\Http\Message\UriInterface::__toString
.
PSR-7 UriInterface treats an empty component the same as a missing component as getQuery(), getFragment() etc. always return a string. This explains the slight difference to RFC 3986 Section 5.3.
Another adjustment is that the authority separator is added even when the authority is missing/empty
for the "file" scheme. This is because PHP stream functions like file_get_contents
only work with
file:///myfile
but not with file:/myfile
although they are equivalent according to RFC 3986. But
file:///
is the more common syntax for the file scheme anyway (Chrome for example redirects to
that format).
Parameters
- $scheme : string
- $authority : string
- $path : string
- $query : string
- $fragment : string
Tags
Return values
string —fromParts()
Creates a URI from a hash of `parse_url` components.
public
static fromParts(array<string|int, mixed> $parts) : UriInterface
Parameters
- $parts : array<string|int, mixed>
Tags
Return values
UriInterface —getAuthority()
Retrieve the authority component of the URI.
public
getAuthority() : string
If no authority information is present, this method MUST return an empty string.
The authority syntax of the URI is:
[user-info@]host[:port]
If the port component is not set or is the standard port for the current scheme, it SHOULD NOT be included.
Return values
string —The URI authority, in "[user-info@]host[:port]" format.
getFragment()
Retrieve the fragment component of the URI.
public
getFragment() : string
If no fragment is present, this method MUST return an empty string.
The leading "#" character is not part of the fragment and MUST NOT be added.
The value returned MUST be percent-encoded, but MUST NOT double-encode any characters. To determine what characters to encode, please refer to RFC 3986, Sections 2 and 3.5.
Return values
string —The URI fragment.
getHost()
Retrieve the host component of the URI.
public
getHost() : string
If no host is present, this method MUST return an empty string.
The value returned MUST be normalized to lowercase, per RFC 3986 Section 3.2.2.
Return values
string —The URI host.
getPath()
Retrieve the path component of the URI.
public
getPath() : string
The path can either be empty or absolute (starting with a slash) or rootless (not starting with a slash). Implementations MUST support all three syntaxes.
Normally, the empty path "" and absolute path "/" are considered equal as defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically do this normalization because in contexts with a trimmed base path, e.g. the front controller, this difference becomes significant. It's the task of the user to handle both "" and "/".
The value returned MUST be percent-encoded, but MUST NOT double-encode any characters. To determine what characters to encode, please refer to RFC 3986, Sections 2 and 3.3.
As an example, if the value should include a slash ("/") not intended as delimiter between path segments, that value MUST be passed in encoded form (e.g., "%2F") to the instance.
Return values
string —The URI path.
getPort()
Retrieve the port component of the URI.
public
getPort() : null|int
If a port is present, and it is non-standard for the current scheme, this method MUST return it as an integer. If the port is the standard port used with the current scheme, this method SHOULD return null.
If no port is present, and no scheme is present, this method MUST return a null value.
If no port is present, but a scheme is present, this method MAY return the standard port for that scheme, but SHOULD return null.
Return values
null|int —The URI port.
getQuery()
Retrieve the query string of the URI.
public
getQuery() : string
If no query string is present, this method MUST return an empty string.
The leading "?" character is not part of the query and MUST NOT be added.
The value returned MUST be percent-encoded, but MUST NOT double-encode any characters. To determine what characters to encode, please refer to RFC 3986, Sections 2 and 3.4.
As an example, if a value in a key/value pair of the query string should include an ampersand ("&") not intended as a delimiter between values, that value MUST be passed in encoded form (e.g., "%26") to the instance.
Return values
string —The URI query string.
getScheme()
Retrieve the scheme component of the URI.
public
getScheme() : string
If no scheme is present, this method MUST return an empty string.
The value returned MUST be normalized to lowercase, per RFC 3986 Section 3.1.
The trailing ":" character is not part of the scheme and MUST NOT be added.
Return values
string —The URI scheme.
getUserInfo()
Retrieve the user information component of the URI.
public
getUserInfo() : string
If no user information is present, this method MUST return an empty string.
If a user is present in the URI, this will return that value; additionally, if the password is also present, it will be appended to the user value, with a colon (":") separating the values.
The trailing "@" character is not part of the user information and MUST NOT be added.
Return values
string —The URI user information, in "username[:password]" format.
isAbsolute()
Whether the URI is absolute, i.e. it has a scheme.
public
static isAbsolute(UriInterface $uri) : bool
An instance of UriInterface can either be an absolute URI or a relative reference. This method returns true if it is the former. An absolute URI has a scheme. A relative reference is used to express a URI relative to another URI, the base URI. Relative references can be divided into several forms:
- network-path references, e.g. '//example.com/path'
- absolute-path references, e.g. '/path'
- relative-path references, e.g. 'subpath'
Parameters
- $uri : UriInterface
Tags
Return values
bool —isAbsolutePathReference()
Whether the URI is a absolute-path reference.
public
static isAbsolutePathReference(UriInterface $uri) : bool
A relative reference that begins with a single slash character is termed an absolute-path reference.
Parameters
- $uri : UriInterface
Tags
Return values
bool —isDefaultPort()
Whether the URI has the default port of the current scheme.
public
static isDefaultPort(UriInterface $uri) : bool
Psr\Http\Message\UriInterface::getPort
may return null or the standard port. This method can be used
independently of the implementation.
Parameters
- $uri : UriInterface
Return values
bool —isNetworkPathReference()
Whether the URI is a network-path reference.
public
static isNetworkPathReference(UriInterface $uri) : bool
A relative reference that begins with two slash characters is termed an network-path reference.
Parameters
- $uri : UriInterface
Tags
Return values
bool —isRelativePathReference()
Whether the URI is a relative-path reference.
public
static isRelativePathReference(UriInterface $uri) : bool
A relative reference that does not begin with a slash character is termed a relative-path reference.
Parameters
- $uri : UriInterface
Tags
Return values
bool —isSameDocumentReference()
Whether the URI is a same-document reference.
public
static isSameDocumentReference(UriInterface $uri[, UriInterface|null $base = null ]) : bool
A same-document reference refers to a URI that is, aside from its fragment component, identical to the base URI. When no base URI is given, only an empty URI reference (apart from its fragment) is considered a same-document reference.
Parameters
- $uri : UriInterface
-
The URI to check
- $base : UriInterface|null = null
-
An optional base URI to compare against
Tags
Return values
bool —removeDotSegments()
Removes dot segments from a path and returns the new path.
public
static removeDotSegments(string $path) : string
Parameters
- $path : string
Tags
Return values
string —resolve()
Converts the relative URI into a new URI that is resolved against the base URI.
public
static resolve(UriInterface $base, string|UriInterface $rel) : UriInterface
Parameters
- $base : UriInterface
-
Base URI
- $rel : string|UriInterface
-
Relative URI
Tags
Return values
UriInterface —withFragment()
Return an instance with the specified URI fragment.
public
withFragment(mixed $fragment) : static
This method MUST retain the state of the current instance, and return an instance that contains the specified URI fragment.
Users can provide both encoded and decoded fragment characters. Implementations ensure the correct encoding as outlined in getFragment().
An empty fragment value is equivalent to removing the fragment.
Parameters
- $fragment : mixed
-
The fragment to use with the new instance.
Return values
static —A new instance with the specified fragment.
withHost()
Return an instance with the specified host.
public
withHost(mixed $host) : static
This method MUST retain the state of the current instance, and return an instance that contains the specified host.
An empty host value is equivalent to removing the host.
Parameters
- $host : mixed
-
The hostname to use with the new instance.
Return values
static —A new instance with the specified host.
withoutQueryValue()
Creates a new URI with a specific query string value removed.
public
static withoutQueryValue(UriInterface $uri, string $key) : UriInterface
Any existing query string values that exactly match the provided key are removed.
Parameters
- $uri : UriInterface
-
URI to use as a base.
- $key : string
-
Query string key to remove.
Return values
UriInterface —withPath()
Return an instance with the specified path.
public
withPath(mixed $path) : static
This method MUST retain the state of the current instance, and return an instance that contains the specified path.
The path can either be empty or absolute (starting with a slash) or rootless (not starting with a slash). Implementations MUST support all three syntaxes.
If the path is intended to be domain-relative rather than path relative then it must begin with a slash ("/"). Paths not starting with a slash ("/") are assumed to be relative to some base path known to the application or consumer.
Users can provide both encoded and decoded path characters. Implementations ensure the correct encoding as outlined in getPath().
Parameters
- $path : mixed
-
The path to use with the new instance.
Return values
static —A new instance with the specified path.
withPort()
Return an instance with the specified port.
public
withPort(mixed $port) : static
This method MUST retain the state of the current instance, and return an instance that contains the specified port.
Implementations MUST raise an exception for ports outside the established TCP and UDP port ranges.
A null value provided for the port is equivalent to removing the port information.
Parameters
- $port : mixed
-
The port to use with the new instance; a null value removes the port information.
Return values
static —A new instance with the specified port.
withQuery()
Return an instance with the specified query string.
public
withQuery(mixed $query) : static
This method MUST retain the state of the current instance, and return an instance that contains the specified query string.
Users can provide both encoded and decoded query characters. Implementations ensure the correct encoding as outlined in getQuery().
An empty query string value is equivalent to removing the query string.
Parameters
- $query : mixed
-
The query string to use with the new instance.
Return values
static —A new instance with the specified query string.
withQueryValue()
Creates a new URI with a specific query string value.
public
static withQueryValue(UriInterface $uri, string $key, string|null $value) : UriInterface
Any existing query string values that exactly match the provided key are removed and replaced with the given key value pair.
A value of null will set the query string key without a value, e.g. "key" instead of "key=value".
Parameters
- $uri : UriInterface
-
URI to use as a base.
- $key : string
-
Key to set.
- $value : string|null
-
Value to set
Return values
UriInterface —withQueryValues()
Creates a new URI with multiple specific query string values.
public
static withQueryValues(UriInterface $uri, array<string|int, mixed> $keyValueArray) : UriInterface
It has the same behavior as withQueryValue() but for an associative array of key => value.
Parameters
- $uri : UriInterface
-
URI to use as a base.
- $keyValueArray : array<string|int, mixed>
-
Associative array of key and values
Return values
UriInterface —withScheme()
Return an instance with the specified scheme.
public
withScheme(mixed $scheme) : static
This method MUST retain the state of the current instance, and return an instance that contains the specified scheme.
Implementations MUST support the schemes "http" and "https" case insensitively, and MAY accommodate other schemes if required.
An empty scheme is equivalent to removing the scheme.
Parameters
- $scheme : mixed
-
The scheme to use with the new instance.
Return values
static —A new instance with the specified scheme.
withUserInfo()
Return an instance with the specified user information.
public
withUserInfo(mixed $user[, mixed $password = null ]) : static
This method MUST retain the state of the current instance, and return an instance that contains the specified user information.
Password is optional, but the user information MUST include the user; an empty string for the user is equivalent to removing user information.
Parameters
- $user : mixed
-
The user name to use for authority.
- $password : mixed = null
-
The password associated with $user.
Return values
static —A new instance with the specified user information.
applyParts()
Apply parse_url parts to a URI.
private
applyParts(array<string|int, mixed> $parts) : mixed
Parameters
- $parts : array<string|int, mixed>
-
Array of parse_url parts to apply.
Return values
mixed —filterHost()
private
filterHost(string $host) : string
Parameters
- $host : string
Tags
Return values
string —filterPath()
Filters the path of a URI
private
filterPath(string $path) : string
Parameters
- $path : string
Tags
Return values
string —filterPort()
private
filterPort(int|null $port) : int|null
Parameters
- $port : int|null
Tags
Return values
int|null —filterQueryAndFragment()
Filters the query string or fragment of a URI.
private
filterQueryAndFragment(string $str) : string
Parameters
- $str : string
Tags
Return values
string —filterScheme()
private
filterScheme(string $scheme) : string
Parameters
- $scheme : string
Tags
Return values
string —filterUserInfoComponent()
private
filterUserInfoComponent(string $component) : string
Parameters
- $component : string
Tags
Return values
string —generateQueryString()
private
static generateQueryString(string $key, string|null $value) : string
Parameters
- $key : string
- $value : string|null
Return values
string —getFilteredQueryString()
private
static getFilteredQueryString(UriInterface $uri, array<string|int, mixed> $keys) : array<string|int, mixed>
Parameters
- $uri : UriInterface
- $keys : array<string|int, mixed>
Return values
array<string|int, mixed> —parse()
UTF-8 aware \parse_url() replacement.
private
static parse(string $url) : array<string|int, mixed>|false
The internal function produces broken output for non ASCII domain names (IDN) when used with locales other than "C".
On the other hand, cURL understands IDN correctly only when UTF-8 locale is configured ("C.UTF-8", "en_US.UTF-8", etc.).
Parameters
- $url : string
Tags
Return values
array<string|int, mixed>|false —rawurlencodeMatchZero()
private
rawurlencodeMatchZero(array<string|int, mixed> $match) : mixed
Parameters
- $match : array<string|int, mixed>
Return values
mixed —removeDefaultPort()
private
removeDefaultPort() : mixed
Return values
mixed —validateState()
private
validateState() : mixed