Erebot  latest
A modular IRC bot for PHP 5.3+
Erebot\URI Class Reference

Simple parser/generator for Uniform Resource Identifiers as defined in RFC 3986. More...

+ Inheritance diagram for Erebot\URI:

Public Member Functions

 __construct ($uri=array())
 
 __toString ()
 
 asParsedURL ($component=-1)
 
 getFragment ($raw=false)
 
 getHost ($raw=false)
 
 getPath ($raw=false)
 
 getPort ($raw=false)
 
 getQuery ($raw=false)
 
 getScheme ($raw=false)
 
 getUserInfo ($raw=false)
 
 relative ($reference)
 
 setFragment ($fragment)
 
 setHost ($host)
 
 setPath ($path)
 
 setPort ($port)
 
 setQuery ($query)
 
 setScheme ($scheme)
 
 setUserInfo ($userinfo)
 
 toURI ($raw=false, $credentials=true)
 

Static Public Member Functions

static fromAbsPath ($abspath, $strict=true)
 
static normalizePercentReal ($hexchr)
 

Protected Member Functions

 merge ($path)
 
 parseURI ($uri, $relative)
 
 realSetPath ($path, $relative)
 
 removeDotSegments ($path)
 
 validatePath ($path, $relative)
 

Static Protected Member Functions

static normalizePercent ($data)
 

Protected Attributes

 $fragment
 Fragment component.
 
 $host
 Host component ("authority", even though "authority" is more general).
 
 $path
 Path component.
 
 $port
 Port component.
 
 $query
 Query component.
 
 $scheme
 Scheme component (sometimes also erroneously called a "protocol").
 
 $userinfo
 User information component (such as a "username:password" pair).
 

Detailed Description

Simple parser/generator for Uniform Resource Identifiers as defined in RFC 3986.

This class can be used as both a parser and a generator for Uniform Resource Identifiers (URI) as defined in RFC 3986. It is primarly meant to deal with with absolute URIs but also offers methods to deal with relative URIs. It is mostly compatible with parse_url(), but tends to be stricter when validating data.

This implementation doesn't assume that the "userinfo" part is made up of a "username:password" pair (in contrast to parse_url() which is initially based on RFC 1738), and provides a single field named "userinfo" instead. Such pairs will be merged upon encounter.

This implementation is also compatible with RFC 6874 which adds support for IPv6 zone identifiers.

All components are normalized by default when retrieved using any of the getters except asParsedURL(). You may override this behaviour by passing $raw=true to said getters. Normalization is done using the rules defined in RFC 3986.

Definition at line 49 of file URI.php.

Constructor & Destructor Documentation

Erebot\URI::__construct (   $uri = array())

Constructs an URI.

Parameters
mixed$uri(optional) Either a string representing the URI or an array as returned by PHP's parse_url() function. Defaults to an empty URI (which must be populated afterwards using the setters from this API) if omitted.
Exceptions
::InvalidArgumentExceptionThe given URI is invalid.

Definition at line 78 of file URI.php.

Member Function Documentation

Erebot\URI::__toString ( )

Returns the current URI as a string, in its normalized form.

Note
This method is a shortcut for \Erebot\URI\toURI(false).

Implements Erebot\URIInterface.

Definition at line 296 of file URI.php.

Erebot\URI::asParsedURL (   $component = -1)

Returns information about the current URI, in the same format as parse_url().

Parameters
$component(optional) A specific component to return. Read the documentation about parse_url() for more information.
Return values
mixedEither an array, a string, an integer or null, depending on $component and the actual contents of this URI. Read the documentation about parse_url() for more information.
Note
The behaviour of this method matches that of parse_url() as defined in PHP 5.5.19+, PHP 5.6.3+ and PHP 7.0.0+. In particular, an empty username/password is returned as such, rather than as a null value.

Implements Erebot\URIInterface.

Definition at line 715 of file URI.php.

static Erebot\URI::fromAbsPath (   $abspath,
  $strict = true 
)
static

Given an absolute path to some file or directory, returns an URL belonging to the "file" schema and pointing to that file/directory.

Note
On Windows, network shares can be referred to using the UNC or long UNC notation.
Parameters
string$abspathAbsolute path to the file or directory to refer to.
bool$strict(optional) Whether strict parsing rules apply or not. Defaults to true. When set to false, '/' is treated as a path separator even on systems where it is not the native separator (eg. Windows).
Return values
Erebot::URIAn URL poiting to the same file/directory and belonging to the "file" scheme.
Exceptions
::InvalidArgumentExceptionThe given $abspath was invalid.

Implements Erebot\URIInterface.

Definition at line 857 of file URI.php.

Referenced by Erebot\Config\Main\load().

Erebot\URI::getFragment (   $raw = false)

Returns the current URI's fragment.

Parameters
bool$raw(optional) Whether the value should be normalized prior to being returned (false) or not (true). The default is to apply normalization.
Return values
mixedThe current URI's fragment as a string, eventually normalized or null.

Implements Erebot\URIInterface.

Definition at line 687 of file URI.php.

Erebot\URI::getHost (   $raw = false)

Returns the current URI's host.

Parameters
bool$raw(optional) Whether the value should be normalized prior to being returned (false) or not (true). The default is to apply normalization.
Return values
mixedThe current URI's host as a string, eventually normalized or null.

Implements Erebot\URIInterface.

Definition at line 349 of file URI.php.

Erebot\URI::getPath (   $raw = false)

Returns the current URI's path.

Parameters
bool$raw(optional) Whether the value should be normalized prior to being returned (false) or not (true). The default is to apply normalization.
Return values
stringThe current URI's path as a string, eventually normalized.

Implements Erebot\URIInterface.

Definition at line 553 of file URI.php.

Erebot\URI::getPort (   $raw = false)

Returns the current URI's port.

Parameters
bool$raw(optional) Whether the value should be normalized prior to being returned (false) or not (true). The default is to apply normalization.
Return values
mixedWhen normalization is in effect, the port for the current URI will be returned as an integer, or null. When normalization has been disabled, the port will be returned as a string or null.

Implements Erebot\URIInterface.

Definition at line 424 of file URI.php.

Erebot\URI::getQuery (   $raw = false)

Returns the current URI's query.

Parameters
bool$raw(optional) Whether the value should be normalized prior to being returned (false) or not (true). The default is to apply normalization.
Return values
mixedThe current URI's query as a string, eventually normalized or null.

Implements Erebot\URIInterface.

Definition at line 659 of file URI.php.

Erebot\URI::getScheme (   $raw = false)

Returns the current URI's scheme.

Parameters
bool$raw(optional) Whether the value should be normalized prior to being returned (false) or not (true). The default is to apply normalization.
Return values
stringThe current URI's scheme as a string, eventually normalized.

Implements Erebot\URIInterface.

Definition at line 301 of file URI.php.

Erebot\URI::getUserInfo (   $raw = false)

Returns the current URI's user information.

Parameters
bool$raw(optional) Whether the value should be normalized prior to being returned (false) or not (true). The default is to apply normalization.
Return values
mixedThe current URI's user information, eventually normalized or null.

Implements Erebot\URIInterface.

Definition at line 320 of file URI.php.

Erebot\URI::merge (   $path)
protected

Merges the given path with the current URI's path.

Parameters
string$pathPath to merge into the current path.
Return values
stringResult of that merge.
Note
Despite its name, this method does not modify the given $path nor the current object.

Definition at line 539 of file URI.php.

static Erebot\URI::normalizePercent (   $data)
staticprotected

Performs normalization of percent-encoded characters.

Parameters
string$dataSome text containing percent-encoded characters that need to be normalized.
Return values
stringThe same text, after percent-encoding normalization.
Note
This method is just a thin wrapper around Erebot_URI::normalizePercentReal.

Definition at line 223 of file URI.php.

static Erebot\URI::normalizePercentReal (   $hexchr)
static

Performs normalization of a percent-encoded character.

Parameters
string$hexchrOne percent-encoded character that needs to be normalized.
Return values
stringThe same text, after percent-encoding normalization.

Definition at line 241 of file URI.php.

Erebot\URI::parseURI (   $uri,
  $relative 
)
protected

Parses an URI using the grammar defined in RFC 3986.

Parameters
string$uriURI to parse.
bool$relativeWhether $uri must be considered as an absolute URI (false) or a relative reference (true).
Return values
arrayAn associative array containing the different components that could be parsed out of this URI. It uses the same format as parse_url(), except that the "user" and "pass" components are merged into a single "userinfo" component and only string keys are defined.
Exceptions
::InvalidArgumentExceptionThe given $uri is not valid.

Definition at line 136 of file URI.php.

Erebot\URI::realSetPath (   $path,
  $relative 
)
protected

Sets the current URI's path.

Parameters
string$pathNew path for this URI.
bool$relativeWhether the given $path is relative (true) or not (false).
Exceptions
::InvalidArgumentExceptionThe given $path is not valid.

Definition at line 644 of file URI.php.

Erebot\URI::relative (   $reference)

Given a relative reference, returns a new absolute URI matching that reference.

Parameters
string$referenceSome relative reference (can be an absolute or relative URI). The current absolute URI is used as the base to dereference it.
Return values
Erebot::URIA new absolute URI matching the given $reference.
Exceptions
::InvalidArgumentExceptionThe given $reference is not valid.

Implements Erebot\URIInterface.

Definition at line 784 of file URI.php.

Erebot\URI::removeDotSegments (   $path)
protected

Removes "dot segments" ("." and "..") from a path.

Parameters
string$pathPath on which to operate.
Return values
stringThe same $path, with all its dot segments substituted.

Definition at line 474 of file URI.php.

Erebot\URI::setFragment (   $fragment)

Sets the current URI's fragment.

Parameters
mixed$fragmentNew fragment for this URI (either a string or null).
Exceptions
::InvalidArgumentExceptionThe given $fragment is not valid.

Implements Erebot\URIInterface.

Definition at line 695 of file URI.php.

Erebot\URI::setHost (   $host)

Sets the current URI's host.

Parameters
string$hostNew host for this URI (either a string or null).
Exceptions
::InvalidArgumentExceptionThe given $host is not valid.

Implements Erebot\URIInterface.

Definition at line 361 of file URI.php.

Erebot\URI::setPath (   $path)

Sets the current URI's path.

Parameters
string$pathNew path for this URI.
Exceptions
::InvalidArgumentExceptionThe given $path is not valid.
Note
This is a very thin wrapper around the internal method ::Erebot::URI::_setPath().

Implements Erebot\URIInterface.

Definition at line 654 of file URI.php.

Erebot\URI::setPort (   $port)

Sets the current URI's port.

Parameters
mixed$portNew port for this URI (either a numeric string, an integer or null).
Exceptions
::InvalidArgumentExceptionThe given $port is not valid.

Implements Erebot\URIInterface.

Definition at line 452 of file URI.php.

Erebot\URI::setQuery (   $query)

Sets the current URI's query.

Parameters
mixed$queryNew query for this URI (either a string or null).
Exceptions
::InvalidArgumentExceptionThe given $query is not valid.

Implements Erebot\URIInterface.

Definition at line 667 of file URI.php.

Erebot\URI::setScheme (   $scheme)

Sets the current URI's scheme.

Parameters
string$schemeNew scheme for this URI, as a string.
Exceptions
::InvalidArgumentExceptionThe given $scheme is not valid.

Implements Erebot\URIInterface.

Definition at line 311 of file URI.php.

Erebot\URI::setUserInfo (   $userinfo)

Sets the current URI's user information.

Parameters
mixed$userinfoNew user information for this URI (either a string or null).
Exceptions
::InvalidArgumentExceptionThe given user information is not valid.

Implements Erebot\URIInterface.

Definition at line 330 of file URI.php.

Erebot\URI::toURI (   $raw = false,
  $credentials = true 
)

Returns the current URI as a string.

Parameters
bool$raw(optional) Whether the raw contents of the components should be used (true) or a normalized alternative (false). The default is to apply normalization.
bool$credentials(optional) Whether the content of the "user information" component should be part of the returned string (true) or not (false). The default is for such credentials to appear in the result.
Return values
stringThe current URI as a string, eventually normalized.

Implements Erebot\URIInterface.

Definition at line 257 of file URI.php.

Erebot\URI::validatePath (   $path,
  $relative 
)
protected

Validates the given path.

Parameters
string$pathPath to validate.
bool$relativeWhether the given $path is relative (true) or not (false).
Return values
booltrue if the given $path is valid, false otherwise.

Definition at line 579 of file URI.php.


The documentation for this class was generated from the following file: