# Class HttpSoft\Uri\UriNormalizer
Validates and normalizes components of the URI in accordance with the RFC 3986 specification.
Implements HttpSoft\Uri\UriNormalizerInterface and used within HttpSoft\Uri\Uri.
use HttpSoft\Uri\UriNormalizer;
$normalizer = new UriNormalizer();
// Parses the URI into components and normalizes it.
$uriData = $normalizer->normalize('https://example.com/pa<>th?qu$ery=str:ing#fag^ment');
$uriData->getScheme(); // 'https'
$uriData->getHost(); // 'example.com'
$uriData->getPath(); // '/pa%3C%3Eth'
$uriData->getQuery(); // 'qu%24ery=str%3Aing'
$uriData->getFragment(); // 'fag%5Ement'
(string) $uriData; // 'https://example.com/pa%3C%3Eth?qu%24ery=str%3Aing#fag%5Ement'
// Only parses the components, but does not normalize the URI.
$uriRawData = $normalizer->normalize('https://example.com/pa<>th', true);
$uriData->getScheme(); // 'https'
$uriData->getHost(); // 'example.com'
$uriData->getPath(); // '/pa<>th'
$uriData->getQuery(); // 'qu$ery=str:ing'
$uriData->getFragment(); // 'fag^ment'
(string) $uriData; // 'https://example.com/pa<>th?qu$ery=str:ing#fag^ment'
// You can normalize the components of the URI separately, see below description of the methods. # Public methods
# normalize
Normalize URI according to RFC 3986 Syntax Components.
public function normalize(string $uri, bool $raw = false): UriData; Parses and normalizes all URI components by calling methods inside: normalizeScheme(), normalizeUserInfo(), normalizeHost(), normalizePort(), normalizePath(), normalizeQuery(), normalizeFragment().
If
$raw === truereturns HttpSoft\Uri\UriData object with raw non normalized URI components. This is useful if you need to parse a non-HTTP link (mailto,ftp).
# normalizeScheme
Normalize the scheme component of the URI according to RFC 3986 Scheme.
public function normalizeScheme(string $scheme): string; If $scheme invalid or unsupported, the \InvalidArgumentException exception will be thrown. Supported schemes: 'http' and 'https'.
$normalizer->normalizeScheme('https'); // 'https'
$normalizer->normalizeScheme('https:'); // 'https'
$normalizer->normalizeScheme('https://'); // 'https'
$normalizer->normalizeScheme('https:/'); // throws InvalidArgumentException
$normalizer->normalizeScheme('invalid'); // throws InvalidArgumentException
$normalizer->normalizeScheme('mailto'); // throws InvalidArgumentException
$normalizer->normalizeScheme('file'); // throws InvalidArgumentException
$normalizer->normalizeScheme('ftp'); // throws InvalidArgumentException
$normalizer->normalizeScheme('ssh'); // throws InvalidArgumentException # normalizeUserInfo
Normalize the user information component of the URI according to RFC 3986 User Information.
public function normalizeUserInfo(string $user, ?string $pass = null): string; If $user is empty, an empty string will be returned. If $user and $pass are not empty, the string 'user:pass' will be returned. If $user is not empty, but $pass is empty, the string 'user' will be returned.
$normalizer->normalizeUserInfo('', 'password'); // ''
$normalizer->normalizeUserInfo('us<>er'); // 'us%3C%3Eer'
$normalizer->normalizeUserInfo('us%3C%3Eer'); // 'us%3C%3Eer'
$normalizer->normalizeUserInfo('user', 'password'); // 'user:password' # normalizeHost
Normalize the host component of the URI according to RFC 3986 Host.
public function normalizeHost(string $host): string; All uppercase Latin letters will be reduced to lowercase and all slashes at the edges will be removed.
$normalizer->normalizeHost('127.0.0.1'); // '127.0.0.1'
$normalizer->normalizeHost('example.com'); // 'example.com'
$normalizer->normalizeHost('Example.com'); // 'example.com'
$normalizer->normalizeHost('//example.com/'); // 'example.com' # normalizePort
Normalize the port component of the URI according to RFC 3986 Port.
public function normalizePort($port): ?int; If $port is not an integer (8080) or not a string representation number ('8080') or less than 1 or more than 65535 or not equal to null, the \InvalidArgumentException exception will be thrown.
$normalizer->normalizePort(8080); // 8080
$normalizer->normalizePort('8080'); // 8080
$normalizer->normalizePort('80.80'); // 80
$normalizer->normalizePort(null); // null
$normalizer->normalizePort(80.80); // throws InvalidArgumentException
$normalizer->normalizePort('string'); // throws InvalidArgumentException
$normalizer->normalizePort(0); // throws InvalidArgumentException
$normalizer->normalizePort('0'); // throws InvalidArgumentException
$normalizer->normalizePort(65536); // throws InvalidArgumentException
$normalizer->normalizePort('65536'); // throws InvalidArgumentException # normalizePath
Normalize the path component of the URI according to RFC 3986 Path.
public function normalizePath(string $path): string; $path can either be empty or absolute (starting with a slash) or rootless (not starting with a slash). If $path contains character ? or #, the \InvalidArgumentException exception will be thrown.
$normalizer->normalizePath(''); // ''
$normalizer->normalizePath('/'); // '/'
$normalizer->normalizePath('path/to/act<>ion'); // 'path/to/act%3C%3Eion'
$normalizer->normalizePath('path/to/act%3C%3Eion'); // 'path/to/act%3C%3Eion'
$normalizer->normalizePath('/path/to/action'); // '/path/to/action'
$normalizer->normalizePath('/path/to/action/'); // '/path/to/action/'
$normalizer->normalizePath('///path/to/action///'); // '/path/to/action/'
$normalizer->normalizePath('path/to/action?query=string') // throws InvalidArgumentException
$normalizer->normalizePath('path/to/action#fragment') // throws InvalidArgumentException # normalizeQuery
Normalize the query string component of the URI according to RFC 3986 Query.
public function normalizeQuery(string $query): string; $query can either be empty or starting with a question mark or not starting with a question mark. If $query contains character #, the \InvalidArgumentException exception will be thrown.
$normalizer->normalizeQuery('?qu$ery'); // 'qu%24ery'
$normalizer->normalizeQuery('?qu%24ery'); // 'qu%24ery'
$normalizer->normalizeQuery('query=str<>ing'); // 'query=str%3C%3Eing'
$normalizer->normalizeQuery('query=str%3C%3Eing'); // 'query=str%3C%3Eing'
$normalizer->normalizeQuery('?key1=value1&key2=value2'); // 'key1=value1&key2=value2'
$normalizer->normalizeQuery('query=string#fragment') // throws InvalidArgumentException # normalizeFragment
Normalize the fragment (hash) component of the URI according to RFC 3986 Fragment.
public function normalizeFragment(string $fragment): string; $fragment can contain any string, it can start with the # character, or it cannot start with the # character.
$normalizer->normalizeFragment('frag<>ment'); // 'frag%3C%3Ement'
$normalizer->normalizeFragment('#frag<>ment'); // 'frag%3C%3Ement'
$normalizer->normalizeFragment('#frag%3C%3Ement'); // 'frag%3C%3Ement' 