Documentation

UriString
in package

FinalYes

A class to parse a URI string according to RFC3986.

Tags
link
https://tools.ietf.org/html/rfc3986
author

Ignace Nyamagana Butera nyamsprod@gmail.com

since
6.0.0
phpstan-type

AuthorityMap array{user: ?string, pass: ?string, host: ?string, port: ?int}

phpstan-type

ComponentMap array{scheme: ?string, user: ?string, pass: ?string, host: ?string, port: ?int, path: string, query: ?string, fragment: ?string}

phpstan-type

InputComponentMap array{scheme? : ?string, user? : ?string, pass? : ?string, host? : ?string, port? : ?int, path? : ?string, query? : ?string, fragment? : ?string}

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_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
link
https://tools.ietf.org/html/rfc3986#appendix-B

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
link
https://tools.ietf.org/html/rfc3986#section-5.3
link
https://tools.ietf.org/html/rfc3986#section-7.5
Return values
string

buildAuthority()

Generate a URI authority representation from its parsed representation.

public static buildAuthority(InputComponentMap $components) : string|null
Parameters
$components : InputComponentMap
Return values
string|null

buildUri()

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
link
https://tools.ietf.org/html/rfc3986#section-5.3
link
https://tools.ietf.org/html/rfc3986#section-7.5§
Return values
string

containsRfc3986Chars()

public static containsRfc3986Chars(Stringable|string $uri) : bool
Parameters
$uri : Stringable|string
Return values
bool

containsRfc3987Chars()

public static containsRfc3987Chars(Stringable|string $uri) : bool
Parameters
$uri : Stringable|string
Return values
bool

isValidHost()

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
codeCoverageIgnore
see
HostRecoord::validate()

Create a new instance from the environment.

Return values
bool

isValidScheme()

Tells whether the scheme component is valid.

public static isValidScheme(Stringable|string|null $scheme) : bool
Parameters
$scheme : Stringable|string|null
Return values
bool

normalize()

Parses and normalizes the URI following RFC3986 destructive and non-destructive constraints.

public static normalize(Stringable|string $uri) : string
Parameters
$uri : Stringable|string
Tags
throws
SyntaxError

if the URI is not parsable

Return values
string

normalizeAuthority()

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
throws
SyntaxError

if the URI is not parsable

Return values
string|null

parse()

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
link
https://tools.ietf.org/html/rfc3986
throws
SyntaxError

if the URI contains invalid characters

throws
SyntaxError

if the URI contains an invalid scheme

throws
SyntaxError

if the URI contains an invalid path

Return values
ComponentMap

parseNormalized()

Parses and normalizes the URI following RFC3986 destructive and non-destructive constraints.

public static parseNormalized(Stringable|string $uri) : ComponentMap
Parameters
$uri : Stringable|string
Tags
throws
SyntaxError

if the URI is not parsable

Return values
ComponentMap

resolve()

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
see
https://www.rfc-editor.org/rfc/rfc3986.html#section-5
throws
SyntaxError

if the BaseUri is not absolute or in absence of a BaseUri if the uri is not absolute

Return values
string

toIriString()

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
link
https://tools.ietf.org/html/rfc3986#section-5.3
link
https://tools.ietf.org/html/rfc3986#section-7.5
Return values
string

normalizeHost()

private static normalizeHost(string|null $host) : string|null
Parameters
$host : string|null
Return values
string|null

resolvePathAndQuery()

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}

        
On this page

Search results