TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
Uri Class Reference
Inheritance diagram for Uri:

Public Member Functions

 __construct ($uri= '')
 
 getScheme ()
 
 getAuthority ()
 
 getUserInfo ()
 
 getHost ()
 
 getPort ()
 
 getPath ()
 
 getQuery ()
 
 getFragment ()
 
 withScheme ($scheme)
 
 withUserInfo ($user, $password=null)
 
 withHost ($host)
 
 withPort ($port)
 
 withPath ($path)
 
 withQuery ($query)
 
 withFragment ($fragment)
 
 __toString ()
 

Public Attributes

const SUBDELIMITER_CHARLIST = '!\$&\'\(\)\*\+,;='
 
const UNRESERVED_CHARLIST = 'a-zA-Z0-9_\-\.~'
 

Protected Member Functions

 parseUri ($uri)
 
 isNonStandardPort ($scheme, $host, $port)
 
 sanitizeScheme ($scheme)
 
 sanitizePath ($path)
 
 sanitizeQuery ($query)
 
 splitQueryValue ($value)
 
 sanitizeFragment ($fragment)
 
 sanitizeQueryOrFragment ($value)
 

Protected Attributes

 $scheme
 
 $supportedSchemes
 
 $authority = ''
 
 $userInfo = ''
 
 $host = ''
 
 $port = null
 
 $path = ''
 
 $query
 
 $fragment
 

Detailed Description

Represents a URI based on the PSR-7 Standard.

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

Definition at line 26 of file Uri.php.

Constructor & Destructor Documentation

__construct (   $uri = '')
Parameters
string | null$uriThe full URI including query string and fragment
Exceptions
InvalidArgumentException when the URI is not a string

Definition at line 102 of file Uri.php.

References Uri\parseUri().

Member Function Documentation

__toString ( )

Return the string representation as a URI reference.

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 "#".
See also
http://tools.ietf.org/html/rfc3986#section-4.1
Returns
string

Definition at line 573 of file Uri.php.

References Uri\$authority, Uri\$fragment, Uri\$path, Uri\$query, Uri\getAuthority(), and Uri\getPath().

getAuthority ( )

Retrieve the authority component of the URI.

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.

See also
https://tools.ietf.org/html/rfc3986#section-3.2
Returns
string The URI authority, in "[user-info@]host[:port]" format.

Definition at line 195 of file Uri.php.

References Uri\$authority, Uri\$host, Uri\$port, and Uri\isNonStandardPort().

Referenced by Uri\__toString().

getFragment ( )

Retrieve the fragment component of the URI.

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.

See also
https://tools.ietf.org/html/rfc3986#section-2
https://tools.ietf.org/html/rfc3986#section-3.5
Returns
string The URI fragment.

Definition at line 340 of file Uri.php.

References Uri\$fragment.

getHost ( )

Retrieve the host component of the URI.

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.

See also
http://tools.ietf.org/html/rfc3986#section-3.2.2
Returns
string The URI host.

Definition at line 244 of file Uri.php.

References Uri\$host.

getPath ( )

Retrieve the path component of the URI.

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.

See also
https://tools.ietf.org/html/rfc3986#section-2
https://tools.ietf.org/html/rfc3986#section-3.3
Returns
string The URI path.

Definition at line 294 of file Uri.php.

References Uri\$path.

Referenced by Uri\__toString().

getPort ( )

Retrieve the port component of the URI.

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.

Returns
null|int The URI port.

Definition at line 264 of file Uri.php.

References Uri\isNonStandardPort().

getQuery ( )

Retrieve the query string of the URI.

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.

See also
https://tools.ietf.org/html/rfc3986#section-2
https://tools.ietf.org/html/rfc3986#section-3.4
Returns
string The URI query string.

Definition at line 319 of file Uri.php.

References Uri\$query.

getScheme ( )

Retrieve the scheme component of the URI.

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.

See also
https://tools.ietf.org/html/rfc3986#section-3.1
Returns
string The URI scheme.

Definition at line 172 of file Uri.php.

References Uri\$scheme.

getUserInfo ( )

Retrieve the user information component of the URI.

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.

Returns
string The URI user information, in "username[:password]" format.

Definition at line 228 of file Uri.php.

References Uri\$userInfo.

isNonStandardPort (   $scheme,
  $host,
  $port 
)
protected

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

Parameters
string$scheme
string$host
int$port
Returns
bool

Definition at line 608 of file Uri.php.

References Uri\$host, Uri\$port, and Uri\$scheme.

Referenced by Uri\getAuthority(), and Uri\getPort().

parseUri (   $uri)
protected

helper function for parsing the full URI string

Parameters
string$uri
Exceptions
InvalidArgumentException if the URI is malformed.

Definition at line 118 of file Uri.php.

References Uri\sanitizeFragment(), Uri\sanitizePath(), Uri\sanitizeQuery(), and Uri\sanitizeScheme().

Referenced by Uri\__construct().

sanitizeFragment (   $fragment)
protected

Filter a fragment value to ensure it is properly encoded.

Parameters
null | string$fragment
Returns
string

Definition at line 710 of file Uri.php.

References Uri\$fragment, and Uri\sanitizeQueryOrFragment().

Referenced by Uri\parseUri(), and Uri\withFragment().

sanitizePath (   $path)
protected

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

Parameters
string$path
Returns
string

Definition at line 651 of file Uri.php.

References Uri\$path.

Referenced by Uri\parseUri(), and Uri\withPath().

sanitizeQuery (   $query)
protected

Filter a query string to ensure it is propertly encoded.

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

Parameters
string$query
Returns
string

Definition at line 670 of file Uri.php.

References Uri\$query, Uri\sanitizeQueryOrFragment(), and Uri\splitQueryValue().

Referenced by Uri\parseUri(), and Uri\withQuery().

sanitizeQueryOrFragment (   $value)
protected

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

Parameters
string$value
Returns
string

Definition at line 729 of file Uri.php.

Referenced by Uri\sanitizeFragment(), and Uri\sanitizeQuery().

sanitizeScheme (   $scheme)
protected

Filters the scheme to ensure it is a valid scheme.

Parameters
string$schemeScheme name.
Returns
string Filtered scheme.
Exceptions
InvalidArgumentException when a scheme is given which is not supported

Definition at line 629 of file Uri.php.

References Uri\$scheme.

Referenced by Uri\parseUri(), and Uri\withScheme().

splitQueryValue (   $value)
protected

Split a query value into a key/value tuple.

Parameters
string$value
Returns
array A value with exactly two elements, key and value

Definition at line 695 of file Uri.php.

Referenced by Uri\sanitizeQuery().

withFragment (   $fragment)

Return an instance with the specified URI fragment.

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
string$fragmentThe fragment to use with the new instance.
Returns
self A new instance with the specified fragment.

Definition at line 542 of file Uri.php.

References Uri\$fragment, and Uri\sanitizeFragment().

withHost (   $host)

Return an instance with the specified host.

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
string$hostThe hostname to use with the new instance.
Returns
self A new instance with the specified host.
Exceptions
InvalidArgumentException for invalid hostnames.

Definition at line 410 of file Uri.php.

References Uri\$host.

withPath (   $path)

Return an instance with the specified path.

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
string$pathThe path to use with the new instance.
Returns
self A new instance with the specified path.
Exceptions
InvalidArgumentException for invalid paths.

Definition at line 475 of file Uri.php.

References Uri\$path, and Uri\sanitizePath().

withPort (   $port)

Return an instance with the specified port.

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
null | int$portThe port to use with the new instance; a null value removes the port information.
Returns
self A new instance with the specified port.
Exceptions
InvalidArgumentException for invalid ports.

Definition at line 435 of file Uri.php.

References Uri\$port.

withQuery (   $query)

Return an instance with the specified query string.

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
string$queryThe query string to use with the new instance.
Returns
self A new instance with the specified query string.
Exceptions
InvalidArgumentException for invalid query strings.

Definition at line 511 of file Uri.php.

References Uri\$query, and Uri\sanitizeQuery().

withScheme (   $scheme)

Return an instance with the specified scheme.

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
string$schemeThe scheme to use with the new instance.
Returns
self A new instance with the specified scheme.
Exceptions
InvalidArgumentException for invalid or unsupported schemes.

Definition at line 361 of file Uri.php.

References Uri\$scheme, and Uri\sanitizeScheme().

withUserInfo (   $user,
  $password = null 
)

Return an instance with the specified user information.

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
string$userThe user name to use for authority.
null | string$passwordThe password associated with $user.
Returns
self A new instance with the specified user information.

Definition at line 385 of file Uri.php.

References Uri\$userInfo.

Member Data Documentation

$authority = ''
protected

Definition at line 60 of file Uri.php.

Referenced by Uri\__toString(), and Uri\getAuthority().

$fragment
protected

Definition at line 96 of file Uri.php.

Referenced by Uri\__toString(), Uri\getFragment(), Uri\sanitizeFragment(), and Uri\withFragment().

$host = ''
protected

Definition at line 72 of file Uri.php.

Referenced by Uri\getAuthority(), Uri\getHost(), Uri\isNonStandardPort(), and Uri\withHost().

$path = ''
protected

Definition at line 84 of file Uri.php.

Referenced by Uri\__toString(), Uri\getPath(), Uri\sanitizePath(), and Uri\withPath().

$port = null
protected

Definition at line 78 of file Uri.php.

Referenced by Uri\getAuthority(), Uri\isNonStandardPort(), and Uri\withPort().

$query
protected

Definition at line 90 of file Uri.php.

Referenced by Uri\__toString(), Uri\getQuery(), Uri\sanitizeQuery(), and Uri\withQuery().

$scheme
protected

Definition at line 46 of file Uri.php.

Referenced by Uri\getScheme(), Uri\isNonStandardPort(), Uri\sanitizeScheme(), and Uri\withScheme().

$supportedSchemes
protected
Initial value:
= [
'http' => 80

Definition at line 51 of file Uri.php.

$userInfo = ''
protected

Definition at line 66 of file Uri.php.

Referenced by Uri\getUserInfo(), and Uri\withUserInfo().

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

Sub-delimiters used in query strings and fragments.

string

Definition at line 33 of file Uri.php.

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

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

string

Definition at line 40 of file Uri.php.