Uri implements UriInterface

Represents a URI based on the PSR-7 Standard.

Highly inspired by https://github.com/phly/http/

Internal

Note that this is not public API yet.

Table of Contents

Interfaces

UriInterface

Constants

SUBDELIMITER_CHARLIST  = '!\$&\'\(\)\*\+,;='
Sub-delimiters used in query strings and fragments.
UNRESERVED_CHARLIST  = 'a-zA-Z0-9_\-\.~'
Unreserved characters used in paths, query strings, and fragments.

Properties

$authority  : string
The authority part of the URI
$fragment  : string
The fragment part of the URI without the # before
$host  : string
The host part of the URI
$path  : string
The path part of the URI (can be empty or /)
$port  : int|null
The port of the URI (empty if it is the standard port for the scheme)
$query  : string
The query part of the URI without the ?
$scheme  : string
The default scheme for the URI
$supportedSchemes  : array<string|int, int>
$userInfo  : string
The userInfo part of the URI

Methods

__construct()  : mixed
__toString()  : string
Return the string representation as a URI reference.
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()  : int|null
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.
withFragment()  : static
Return an instance with the specified URI fragment.
withHost()  : static
Return an instance with the specified host.
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.
withScheme()  : static
Return an instance with the specified scheme.
withUserInfo()  : static
Return an instance with the specified user information.
isNonStandardPort()  : bool
Is a given port non-standard for the current scheme?
parseUri()  : void
Helper to parse the full URI string
sanitizeFragment()  : string
Filter a fragment value to ensure it is properly encoded.
sanitizePath()  : string
Filters the path of a URI to ensure it is properly encoded.
sanitizeQuery()  : string
Filter a query string to ensure it is properly encoded.
sanitizeQueryOrFragment()  : string
Filter a query string key or value, or a fragment.
sanitizeScheme()  : string
Filters the scheme to ensure it is a valid scheme.
splitQueryValue()  : array<string|int, mixed>
Split a query value into a key/value tuple.

Constants

SUBDELIMITER_CHARLIST

Sub-delimiters used in query strings and fragments.

public string SUBDELIMITER_CHARLIST = '!\$&\'\(\)\*\+,;='

UNRESERVED_CHARLIST

Unreserved characters used in paths, query strings, and fragments.

public string UNRESERVED_CHARLIST = 'a-zA-Z0-9_\-\.~'

Properties

$authority

The authority part of the URI

protected string $authority = ''

$fragment

The fragment part of the URI without the # before

protected string $fragment = ''

$host

The host part of the URI

protected string $host = ''

$path

The path part of the URI (can be empty or /)

protected string $path = ''

$port

The port of the URI (empty if it is the standard port for the scheme)

protected int|null $port = null

$query

The query part of the URI without the ?

protected string $query = ''

$scheme

The default scheme for the URI

protected string $scheme = ''

$supportedSchemes

protected array<string|int, int> $supportedSchemes = ['http' => 80, 'https' => 443, 'ws' => 80, 'wss' => 443]

Associative array containing schemes and their default ports.

$userInfo

The userInfo part of the URI

protected string $userInfo = ''

Methods

__construct()

public __construct([string $uri = '' ]) : mixed
Parameters
$uri : string = ''

The full URI including query string and fragment

Tags
throws
InvalidArgumentException

when the URI is not a string

__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 "#".
Tags
see
https://tools.ietf.org/html/rfc3986#section-4.1
Return values
string

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.

Tags
see
https://tools.ietf.org/html/rfc3986#section-3.2
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.

Tags
see
https://tools.ietf.org/html/rfc3986#section-2
see
https://tools.ietf.org/html/rfc3986#section-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.

Tags
see
https://tools.ietf.org/html/rfc3986#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.

Tags
see
https://tools.ietf.org/html/rfc3986#section-2
see
https://tools.ietf.org/html/rfc3986#section-3.3
Return values
string

The URI path.

getPort()

Retrieve the port component of the URI.

public getPort() : int|null

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
int|null

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.

Tags
see
https://tools.ietf.org/html/rfc3986#section-2
see
https://tools.ietf.org/html/rfc3986#section-3.4
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.

Tags
see
https://tools.ietf.org/html/rfc3986#section-3.1
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.

withFragment()

Return an instance with the specified URI fragment.

public withFragment(string $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 : string

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(string $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 : string

The hostname to use with the new instance.

Tags
throws
InvalidArgumentException

for invalid hostnames.

Return values
static

A new instance with the specified host.

withPath()

Return an instance with the specified path.

public withPath(string $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 : string

The path to use with the new instance.

Tags
throws
InvalidArgumentException

for invalid paths.

Return values
static

A new instance with the specified path.

withPort()

Return an instance with the specified port.

public withPort(int|null $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 : int|null

The port to use with the new instance; a null value removes the port information.

Tags
throws
InvalidArgumentException

for invalid ports.

Return values
static

A new instance with the specified port.

withQuery()

Return an instance with the specified query string.

public withQuery(string $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 : string

The query string to use with the new instance.

Tags
throws
InvalidArgumentException

for invalid query strings.

Return values
static

A new instance with the specified query string.

withScheme()

Return an instance with the specified scheme.

public withScheme(string $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 : string

The scheme to use with the new instance.

Tags
throws
InvalidArgumentException

for invalid or unsupported schemes.

Return values
static

A new instance with the specified scheme.

withUserInfo()

Return an instance with the specified user information.

public withUserInfo(string $user[, string|null $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 : string

The username to use for authority.

$password : string|null = null

The password associated with $user.

Return values
static

A new instance with the specified user information.

isNonStandardPort()

Is a given port non-standard for the current scheme?

protected isNonStandardPort(string $scheme, string $host, int|null $port) : bool
Parameters
$scheme : string
$host : string
$port : int|null
Return values
bool

parseUri()

Helper to parse the full URI string

protected parseUri(string $uri) : void
Parameters
$uri : string
Tags
throws
InvalidArgumentException

if the URI is malformed.

sanitizeFragment()

Filter a fragment value to ensure it is properly encoded.

protected sanitizeFragment(string $fragment) : string
Parameters
$fragment : string
Return values
string

sanitizePath()

Filters the path of a URI to ensure it is properly encoded.

protected sanitizePath(string $path) : string
Parameters
$path : string
Return values
string

sanitizeQuery()

Filter a query string to ensure it is properly encoded.

protected sanitizeQuery(string $query) : string

Ensures that the values in the query string are properly urlencoded.

Parameters
$query : string
Return values
string

sanitizeQueryOrFragment()

Filter a query string key or value, or a fragment.

protected sanitizeQueryOrFragment(string $value) : string
Parameters
$value : string
Return values
string

sanitizeScheme()

Filters the scheme to ensure it is a valid scheme.

protected sanitizeScheme(string $scheme) : string
Parameters
$scheme : string

Scheme name.

Tags
throws
InvalidArgumentException

when a scheme is given which is not supported

Return values
string

Filtered scheme.

splitQueryValue()

Split a query value into a key/value tuple.

protected splitQueryValue(string $value) : array<string|int, mixed>
Parameters
$value : string
Return values
array<string|int, mixed>

A value with exactly two elements, key and value


        
On this page

Search results