UriString
in package
A class to parse a URI string according to RFC3986.
Tags
Table of Contents
Constants
- DOT_SEGMENTS = ['.' => 1, '..' => 1]
- REGEXP_HOST_PORT = ',^(?<host>\[.*\]|[^:]*)(:(?<port>.*))?$,'
- Host and Port splitter regular expression.
- REGEXP_INVALID_PATH = ',^(([^/]*):)(.*)?/,'
- Invalid path for URI without scheme and authority regular expression.
- REGEXP_INVALID_URI_RFC3987_CHARS = '/[\x00-\x1f\x7f\s]/'
- Range of invalid characters in URI 3987 string.
- REGEXP_URI_PARTS = ',^ (?<scheme>(?<scontent>[^:/?\#]+):)? # URI scheme component (?<authority>//(?<acontent>[^/?\#]*))? # URI authority part (?<path>[^?\#]*) # URI path component (?<query>\?(?<qcontent>[^\#]*))? # URI query component (?<fragment>\#(?<fcontent>.*))? # URI fragment component ,x'
- RFC3986 regular expression URI splitter.
- REGEXP_URI_SCHEME = '/^([a-z][a-z\d+.-]*)?$/i'
- URI scheme regular expression.
- REGEXP_VALID_URI_RFC3986_CHARS = '/^(?:[A-Za-z0-9\-._~:\/?#[\]@!$&\'()*+,;=%]|%[0-9A-Fa-f]{2})*$/'
- Range of invalid characters in URI 3986 string.
- URI_COMPONENTS = ['scheme' => null, 'user' => null, 'pass' => null, 'host' => null, 'port' => null, 'path' => '', 'query' => null, 'fragment' => null]
- Default URI component values.
- URI_SHORTCUTS = ['' => ['path' => ''], '#' => ['fragment' => ''], '?' => ['query' => ''], '?#' => ['query' => '', 'fragment' => ''], '/' => ['path' => '/'], '//' => ['host' => ''], '///' => ['host' => '', 'path' => '/']]
- Simple URI which do not need any parsing.
Methods
- build() : string
- Generate a URI string representation from its parsed representation returned by League\UriString::parse() or PHP's parse_url.
- buildAuthority() : string|null
- Generate a URI authority representation from its parsed representation.
- buildUri() : string
- Generates a URI string representation based on RFC3986 algorithm.
- containsRfc3986Chars() : bool
- containsRfc3987Chars() : bool
- isValidHost() : bool
- DEPRECATION WARNING! This method will be removed in the next major point release.
- isValidScheme() : bool
- Tells whether the scheme component is valid.
- normalize() : string
- Parses and normalizes the URI following RFC3986 destructive and non-destructive constraints.
- normalizeAuthority() : string|null
- Parses and normalizes the URI following RFC3986 destructive and non-destructive constraints.
- parse() : ComponentMap
- Parse a URI string into its components.
- parseAuthority() : AuthorityMap
- Parses the URI authority part.
- parseNormalized() : ComponentMap
- Parses and normalizes the URI following RFC3986 destructive and non-destructive constraints.
- removeDotSegments() : string
- Filter Dot segment according to RFC3986.
- resolve() : string
- Resolves a URI against a base URI using RFC3986 rules.
- toIriString() : string
- Generate an IRI string representation (RFC3987) from its parsed representation returned by League\UriString::parse() or PHP's parse_url.
- filterHost() : string|null
- Returns whether a hostname is valid.
- filterPort() : int|null
- Filter and format the port component.
- normalizeHost() : string|null
- resolvePathAndQuery() : array{0: string, 1: string|null}
- Resolves an URI path and query component.
- validateComponents() : void
- Assert the URI internal state is valid.
Constants
DOT_SEGMENTS
private
array<string, int>
DOT_SEGMENTS
= ['.' => 1, '..' => 1]
REGEXP_HOST_PORT
Host and Port splitter regular expression.
private
string
REGEXP_HOST_PORT
= ',^(?<host>\[.*\]|[^:]*)(:(?<port>.*))?$,'
REGEXP_INVALID_PATH
Invalid path for URI without scheme and authority regular expression.
private
string
REGEXP_INVALID_PATH
= ',^(([^/]*):)(.*)?/,'
Tags
REGEXP_INVALID_URI_RFC3987_CHARS
Range of invalid characters in URI 3987 string.
private
string
REGEXP_INVALID_URI_RFC3987_CHARS
= '/[\x00-\x1f\x7f\s]/'
REGEXP_URI_PARTS
RFC3986 regular expression URI splitter.
private
string
REGEXP_URI_PARTS
= ',^
(?<scheme>(?<scontent>[^:/?\#]+):)? # URI scheme component
(?<authority>//(?<acontent>[^/?\#]*))? # URI authority part
(?<path>[^?\#]*) # URI path component
(?<query>\?(?<qcontent>[^\#]*))? # URI query component
(?<fragment>\#(?<fcontent>.*))? # URI fragment component
,x'
Tags
REGEXP_URI_SCHEME
URI scheme regular expression.
private
string
REGEXP_URI_SCHEME
= '/^([a-z][a-z\d+.-]*)?$/i'
Tags
REGEXP_VALID_URI_RFC3986_CHARS
Range of invalid characters in URI 3986 string.
private
string
REGEXP_VALID_URI_RFC3986_CHARS
= '/^(?:[A-Za-z0-9\-._~:\/?#[\]@!$&\'()*+,;=%]|%[0-9A-Fa-f]{2})*$/'
URI_COMPONENTS
Default URI component values.
private
ComponentMap
URI_COMPONENTS
= ['scheme' => null, 'user' => null, 'pass' => null, 'host' => null, 'port' => null, 'path' => '', 'query' => null, 'fragment' => null]
URI_SHORTCUTS
Simple URI which do not need any parsing.
private
array<string, array<string|int, string>>
URI_SHORTCUTS
= ['' => ['path' => ''], '#' => ['fragment' => ''], '?' => ['query' => ''], '?#' => ['query' => '', 'fragment' => ''], '/' => ['path' => '/'], '//' => ['host' => ''], '///' => ['host' => '', 'path' => '/']]
Methods
build()
Generate a URI string representation from its parsed representation returned by League\UriString::parse() or PHP's parse_url.
public
static build(InputComponentMap $components) : string
If you supply your own array, you are responsible for providing valid components without their URI delimiters.
Parameters
- $components : InputComponentMap
Tags
Return values
stringbuildAuthority()
Generate a URI authority representation from its parsed representation.
public
static buildAuthority(InputComponentMap $components) : string|null
Parameters
- $components : InputComponentMap
Return values
string|nullbuildUri()
Generates a URI string representation based on RFC3986 algorithm.
public
static buildUri([string|null $scheme = null ][, string|null $authority = null ][, string|null $path = null ][, string|null $query = null ][, string|null $fragment = null ]) : string
Valid URI component MUST be provided without their URI delimiters but properly encoded.
Parameters
- $scheme : string|null = null
- $authority : string|null = null
- $path : string|null = null
- $query : string|null = null
- $fragment : string|null = null
Tags
Return values
stringcontainsRfc3986Chars()
public
static containsRfc3986Chars(Stringable|string $uri) : bool
Parameters
- $uri : Stringable|string
Return values
boolcontainsRfc3987Chars()
public
static containsRfc3987Chars(Stringable|string $uri) : bool
Parameters
- $uri : Stringable|string
Return values
boolisValidHost()
DEPRECATION WARNING! This method will be removed in the next major point release.
public
static isValidHost(Stringable|string|null $host) : bool
Since version 7.6.0
use League\Uri\HostRecord::validate() instead
Parameters
- $host : Stringable|string|null
Tags
Return values
boolisValidScheme()
Tells whether the scheme component is valid.
public
static isValidScheme(Stringable|string|null $scheme) : bool
Parameters
- $scheme : Stringable|string|null
Return values
boolnormalize()
Parses and normalizes the URI following RFC3986 destructive and non-destructive constraints.
public
static normalize(Stringable|string $uri) : string
Parameters
- $uri : Stringable|string
Tags
Return values
stringnormalizeAuthority()
Parses and normalizes the URI following RFC3986 destructive and non-destructive constraints.
public
static normalizeAuthority(Stringable|string|null $authority) : string|null
Parameters
- $authority : Stringable|string|null
Tags
Return values
string|nullparse()
Parse a URI string into its components.
public
static parse(Stringable|string|int $uri) : ComponentMap
This method parses a URI and returns an associative array containing any of the various components of the URI that are present.
$components = UriString::parse('http://foo@test.example.com:42?query#');
var_export($components);
//will display
array(
'scheme' => 'http', // the URI scheme component
'user' => 'foo', // the URI user component
'pass' => null, // the URI pass component
'host' => 'test.example.com', // the URI host component
'port' => 42, // the URI port component
'path' => '', // the URI path component
'query' => 'query', // the URI query component
'fragment' => '', // the URI fragment component
);
The returned array is similar to PHP's parse_url return value with the following differences:
- All components are always present in the returned array
- Empty and undefined component are treated differently. And empty component is set to the empty string while an undefined component is set to the `null` value.
- The path component is never undefined
- The method parses the URI following the RFC3986 rules, but you are still required to validate the returned components against its related scheme specific rules.
Parameters
- $uri : Stringable|string|int
Tags
Return values
ComponentMapparseAuthority()
Parses the URI authority part.
public
static parseAuthority(Stringable|string|null $authority) : AuthorityMap
Parameters
- $authority : Stringable|string|null
Tags
Return values
AuthorityMapparseNormalized()
Parses and normalizes the URI following RFC3986 destructive and non-destructive constraints.
public
static parseNormalized(Stringable|string $uri) : ComponentMap
Parameters
- $uri : Stringable|string
Tags
Return values
ComponentMapremoveDotSegments()
Filter Dot segment according to RFC3986.
public
static removeDotSegments(Stringable|string $path) : string
Parameters
- $path : Stringable|string
Tags
Return values
stringresolve()
Resolves a URI against a base URI using RFC3986 rules.
public
static resolve(Stringable|string $uri[, Stringable|string|null $baseUri = null ]) : string
This method MUST retain the state of the submitted URI instance, and return a URI instance of the same type that contains the applied modifications.
This method MUST be transparent when dealing with error and exceptions. It MUST not alter or silence them apart from validating its own parameters.
Parameters
- $uri : Stringable|string
- $baseUri : Stringable|string|null = null
Tags
Return values
stringtoIriString()
Generate an IRI string representation (RFC3987) from its parsed representation returned by League\UriString::parse() or PHP's parse_url.
public
static toIriString(Stringable|string $uri) : string
If you supply your own array, you are responsible for providing valid components without their URI delimiters.
Parameters
- $uri : Stringable|string
Tags
Return values
stringfilterHost()
Returns whether a hostname is valid.
private
static filterHost(Stringable|string|null $host) : string|null
Parameters
- $host : Stringable|string|null
Tags
Return values
string|nullfilterPort()
Filter and format the port component.
private
static filterPort(string $port) : int|null
Parameters
- $port : string
Tags
Return values
int|nullnormalizeHost()
private
static normalizeHost(string|null $host) : string|null
Parameters
- $host : string|null
Return values
string|nullresolvePathAndQuery()
Resolves an URI path and query component.
private
static resolvePathAndQuery(ComponentMap $uri, ComponentMap $baseUri) : array{0: string, 1: string|null}
Parameters
- $uri : ComponentMap
- $baseUri : ComponentMap
Return values
array{0: string, 1: string|null}validateComponents()
Assert the URI internal state is valid.
private
static validateComponents(string|null $scheme, string|null $authority, string|null $path) : void
Parameters
- $scheme : string|null
- $authority : string|null
- $path : string|null