Generic URI handler.
More...
|
| __construct ($uri=null) |
| Create a new URI object.
|
|
| isValid () |
| Check if the URI is valid.
|
|
| isValidRelative () |
| Check if the URI is a valid relative URI.
|
|
| isAbsolute () |
| Check if the URI is an absolute or relative URI.
|
|
| parse ($uri) |
| Parse a URI string.
|
|
| toString () |
| Compose the URI into a string.
|
|
| normalize () |
| Normalize the URI.
|
|
| resolve ($baseUri) |
| Convert a relative URI into an absolute URI using a base absolute URI as a reference.
|
|
| makeRelative ($baseUri) |
| Convert the link to a relative link by substracting a base URI.
|
|
| getScheme () |
| Get the scheme part of the URI.
|
|
| getUserInfo () |
| Get the User-info (usually user:password) part.
|
|
| getHost () |
| Get the URI host.
|
|
| getPort () |
| Get the URI port.
|
|
| getPath () |
| Get the URI path.
|
|
| getQuery () |
| Get the URI query.
|
|
| getQueryAsArray () |
| Return the query string as an associative array of key => value pairs.
|
|
| getFragment () |
| Get the URI fragment.
|
|
| setScheme ($scheme) |
| Set the URI scheme.
|
|
| setUserInfo ($userInfo) |
| Set the URI User-info part (usually user:password)
|
|
| setHost ($host) |
| Set the URI host.
|
|
| setPort ($port) |
| Set the port part of the URI.
|
|
| setPath ($path) |
| Set the path.
|
|
| setQuery ($query) |
| Set the query string.
|
|
| setFragment ($fragment) |
| Set the URI fragment part.
|
|
| __toString () |
| Magic method to convert the URI to a string.
|
|
|
| reset () |
| Reset URI parts.
|
|
|
static | isValidIpAddress ($host, $allowed) |
| Check if a host name is a valid IP address, depending on allowed IP address types.
|
|
static | isValidDnsHostname ($host) |
| Check if an address is a valid DNS hostname.
|
|
static | isValidRegName ($host) |
| Check if an address is a valid registered name (as defined by RFC-3986) address.
|
|
static | normalizeScheme ($scheme) |
| Part normalization methods.
|
|
static | normalizeHost ($host) |
| Normalize the host part.
|
|
static | normalizePort ($port, $scheme=null) |
| Normalize the port.
|
|
static | normalizePath ($path) |
| Normalize the path.
|
|
static | normalizeQuery ($query) |
| Normalize the query part.
|
|
static | normalizeFragment ($fragment) |
| Normalize the fragment part.
|
|
static | decodeUrlEncodedChars ($input, $allowed= '') |
| Decode all percent encoded characters which are allowed to be represented literally.
|
|
__construct |
( |
|
$uri = null | ) |
|
Create a new URI object.
- Parameters
-
- Exceptions
-
Exception\InvalidArgumentException | |
Implements UriInterface.
Magic method to convert the URI to a string.
- Returns
- string
Implements UriInterface.
static decodeUrlEncodedChars |
( |
|
$input, |
|
|
|
$allowed = '' |
|
) |
| |
|
staticprotected |
Decode all percent encoded characters which are allowed to be represented literally.
Will not decode any characters which are not listed in the 'allowed' list
- Parameters
-
string | $input | |
string | $allowed | Pattern of allowed characters |
- Returns
- mixed
static encodePath |
( |
|
$path | ) |
|
|
static |
Encode the path.
Will replace all characters which are not strictly allowed in the path part with percent-encoded representation
- Parameters
-
- Exceptions
-
Exception\InvalidArgumentException | |
- Returns
- string
static encodeQueryFragment |
( |
|
$input | ) |
|
|
static |
URL-encode a query string or fragment based on RFC-3986 guidelines.
Note that query and fragment encoding allows more unencoded characters than the usual rawurlencode() function would usually return - for example '/' and ':' are allowed as literals.
- Parameters
-
- Returns
- string
- Exceptions
-
Exception\InvalidArgumentException | |
static encodeUserInfo |
( |
|
$userInfo | ) |
|
|
static |
URL-encode the user info part of a URI.
- Parameters
-
- Returns
- string
- Exceptions
-
Exception\InvalidArgumentException | |
Retrieve Escaper instance.
Lazy-loads one if none provided
- Returns
- Escaper
Get the URI fragment.
- Returns
- string|null
Implements UriInterface.
Get the URI host.
- Returns
- string|null
Implements UriInterface.
Get the URI path.
- Returns
- string|null
Implements UriInterface.
Get the URI query.
- Returns
- string|null
Implements UriInterface.
Return the query string as an associative array of key => value pairs.
This is an extension to RFC-3986 but is quite useful when working with most common URI types
- Returns
- array
Implements UriInterface.
Get the scheme part of the URI.
- Returns
- string|null
Implements UriInterface.
Get the User-info (usually user:password) part.
- Returns
- string|null
Implements UriInterface.
Check if the URI is an absolute or relative URI.
- Returns
- bool
Implements UriInterface.
Check if the URI is valid.
Note that a relative URI may still be valid
- Returns
- bool
Implements UriInterface.
static isValidDnsHostname |
( |
|
$host | ) |
|
|
staticprotected |
Check if an address is a valid DNS hostname.
- Parameters
-
- Returns
- bool
static isValidIpAddress |
( |
|
$host, |
|
|
|
$allowed |
|
) |
| |
|
staticprotected |
Check if a host name is a valid IP address, depending on allowed IP address types.
- Parameters
-
string | $host | |
int | $allowed | allowed address types |
- Returns
- bool
static isValidRegName |
( |
|
$host | ) |
|
|
staticprotected |
Check if an address is a valid registered name (as defined by RFC-3986) address.
- Parameters
-
- Returns
- bool
Check if the URI is a valid relative URI.
- Returns
- bool
Implements UriInterface.
Convert the link to a relative link by substracting a base URI.
This is the opposite of resolving a relative link - i.e. creating a relative reference link from an original URI and a base URI.
If the two URIs do not intersect (e.g. the original URI is not in any way related to the base URI) the URI will not be modified.
- Parameters
-
- Returns
- Uri
Implements UriInterface.
static merge |
( |
|
$baseUri, |
|
|
|
$relativeUri |
|
) |
| |
|
static |
Merge a base URI and a relative URI into a new URI object.
This convenience method wraps ::resolve() to allow users to quickly create new absolute URLs without the need to instantiate and clone URI objects.
If objects are passed in, none of the passed objects will be modified.
- Parameters
-
Uri | string | $baseUri | |
Uri | string | $relativeUri | |
- Returns
- Uri
Normalize the URI.
Normalizing a URI includes removing any redundant parent directory or current directory references from the path (e.g. foo/bar/../baz becomes foo/baz), normalizing the scheme case, decoding any over-encoded characters etc.
Eventually, two normalized URLs pointing to the same resource should be equal even if they were originally represented by two different strings
- Returns
- Uri
Implements UriInterface.
static normalizeFragment |
( |
|
$fragment | ) |
|
|
staticprotected |
Normalize the fragment part.
Currently this is exactly the same as normalizeQuery().
- Parameters
-
- Returns
- string
static normalizeHost |
( |
|
$host | ) |
|
|
staticprotected |
Normalize the host part.
By default this converts host names to lower case
- Parameters
-
- Returns
- string
static normalizePath |
( |
|
$path | ) |
|
|
staticprotected |
Normalize the path.
This involves removing redundant dot segments, decoding any over-encoded characters and encoding everything that needs to be encoded and is not
- Parameters
-
- Returns
- string
static normalizePort |
( |
|
$port, |
|
|
|
$scheme = null |
|
) |
| |
|
staticprotected |
Normalize the port.
If the class defines a default port for the current scheme, and the current port is default, it will be unset.
- Parameters
-
- Returns
- int|null
static normalizeQuery |
( |
|
$query | ) |
|
|
staticprotected |
Normalize the query part.
This involves decoding everything that doesn't need to be encoded, and encoding everything else
- Parameters
-
- Returns
- string
static normalizeScheme |
( |
|
$scheme | ) |
|
|
staticprotected |
Part normalization methods.
These are called by normalize() using static::_normalize*() so they may be extended or overridden by extending classes to implement additional scheme specific normalization rules Normalize the scheme
Usually this means simply converting the scheme to lower case
- Parameters
-
- Returns
- string
static parseScheme |
( |
|
$uriString | ) |
|
|
static |
Extract only the scheme part out of a URI string.
This is used by the parse() method, but is useful as a standalone public method if one wants to test a URI string for it's scheme before doing anything with it.
Will return the scheme if found, or NULL if no scheme found (URI may still be valid, but not full)
- Parameters
-
- Exceptions
-
Exception\InvalidArgumentException | |
- Returns
- string|null
static removePathDotSegments |
( |
|
$path | ) |
|
|
static |
Convert a relative URI into an absolute URI using a base absolute URI as a reference.
This is similar to merge() - only it uses the supplied URI as the base reference instead of using the current URI as the base reference.
Merging algorithm is adapted from RFC-3986 section 5.2 (Uri|string $baseUri Exception Uri
static setEscaper |
( |
Escaper |
$escaper | ) |
|
|
static |
Set the URI fragment part.
- Parameters
-
- Returns
- Uri
- Exceptions
-
Exception\InvalidUriPartException | If the schema definition does not have this part |
Implements UriInterface.
Set the URI host.
Note that the generic syntax for URIs allows using host names which are not necessarily IPv4 addresses or valid DNS host names. For example, IPv6 addresses are allowed as well, and also an abstract "registered name" which may be any name composed of a valid set of characters, including, for example, tilda (~) and underscore (_) which are not allowed in DNS names.
Subclasses of Uri may impose more strict validation of host names - for example the HTTP RFC clearly states that only IPv4 and valid DNS names are allowed in HTTP URIs.
- Parameters
-
- Exceptions
-
Exception\InvalidUriPartException | |
- Returns
- Uri
Implements UriInterface.
Set the port part of the URI.
- Parameters
-
- Returns
- Uri
Implements UriInterface.
Set the query string.
If an array is provided, will encode this array of parameters into a query string. Array values will be represented in the query string using PHP's common square bracket notation.
- Parameters
-
- Returns
- Uri
Implements UriInterface.
Set the URI scheme.
If the scheme is not valid according to the generic scheme syntax or is not acceptable by the specific URI class (e.g. 'http' or 'https' are the only acceptable schemes for the Zend class) an exception will be thrown.
You can check if a scheme is valid before setting it using the validateScheme() method.
- Parameters
-
- Exceptions
-
Exception\InvalidUriPartException | |
- Returns
- Uri
Implements UriInterface.
Set the URI User-info part (usually user:password)
- Parameters
-
- Returns
- Uri
- Exceptions
-
Exception\InvalidUriPartException | If the schema definition does not have this part |
Implements UriInterface.
Compose the URI into a string.
- Returns
- string
- Exceptions
-
Exception\InvalidUriException | |
Implements UriInterface.
static validateHost |
( |
|
$host, |
|
|
|
$allowed = self::HOST_ALL |
|
) |
| |
|
static |
Validate the host part.
Users may control which host types to allow by passing a second parameter with a bitmask of HOST_* constants which are allowed. If not specified, all address types will be allowed.
Note that the generic URI syntax allows different host representations, including IPv4 addresses, IPv6 addresses and future IP address formats enclosed in square brackets, and registered names which may be DNS names or even more complex names. This is different (and is much more loose) from what is commonly accepted as valid HTTP URLs for example.
- Parameters
-
string | $host | |
int | $allowed | bitmask of allowed host types |
- Returns
- bool
static validatePath |
( |
|
$path | ) |
|
|
static |
Validate the path.
- Parameters
-
- Returns
- bool
static validatePort |
( |
|
$port | ) |
|
|
static |
Validate the port.
Valid values include numbers between 1 and 65535, and empty values
- Parameters
-
- Returns
- bool
static validateQueryFragment |
( |
|
$input | ) |
|
|
static |
Check if a URI query or fragment part is valid or not.
Query and Fragment parts are both restricted by the same syntax rules, so the same validation method can be used for both.
You can encode a query or fragment part to ensure it is valid by passing it through the encodeQueryFragment() method.
- Parameters
-
- Returns
- bool
static validateScheme |
( |
|
$scheme | ) |
|
|
static |
Encoding and Validation Methods.
Check if a scheme is valid or not
Will check $scheme to be valid against the generic scheme syntax defined in RFC-3986. If the class also defines specific acceptable schemes, will also check that $scheme is one of them.
- Parameters
-
- Returns
- bool
static validateUserInfo |
( |
|
$userInfo | ) |
|
|
static |
Check that the userInfo part of a URI is valid.
- Parameters
-
- Returns
- bool
$validHostTypes = self::HOST_ALL |
|
protected |
const CHAR_GEN_DELIMS = ':\/\?#\[\]@' |
const CHAR_QUERY_DELIMS = '!\$\'\(\)\*\,' |
Not in the spec - those characters have special meaning in urlencoded query parameters.
const CHAR_RESERVED = ':\/\?#\[\]@!\$&\'\(\)\*\+,;=' |
const CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;=' |
const CHAR_UNRESERVED = 'a-zA-Z0-9_\-\.~' |
Character classes defined in RFC-3986.
const HOST_DNS_OR_IPV4 = 0x09 |
const HOST_DNS_OR_IPV4_OR_IPV6 = 0x0B |
const HOST_DNS_OR_IPV4_OR_IPV6_OR_REGNAME = 0x1B |
const HOST_DNS_OR_IPV6 = 0x0A |
const HOST_DNS_OR_IPVANY = 0x0F |
Host part types represented as binary masks The binary mask consists of 5 bits in the following order: <RegName> | <DNS> | <IPvFuture> | <IPv6> | <IPv4> Place 1 or 0 in the different positions for enable or disable the part.
Finally use a hexadecimal representation.
const HOST_IPVFUTURE = 0x04 |
const HOST_REGNAME = 0x10 |