ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
UriNormalizer.php
Go to the documentation of this file.
1<?php
2namespace GuzzleHttp\Psr7;
3
5
13final class UriNormalizer
14{
22
29
40
47
60
67
77
88
101
119 public static function normalize(UriInterface $uri, $flags = self::PRESERVING_NORMALIZATIONS)
120 {
121 if ($flags & self::CAPITALIZE_PERCENT_ENCODING) {
123 }
124
125 if ($flags & self::DECODE_UNRESERVED_CHARACTERS) {
127 }
128
129 if ($flags & self::CONVERT_EMPTY_PATH && $uri->getPath() === '' &&
130 ($uri->getScheme() === 'http' || $uri->getScheme() === 'https')
131 ) {
132 $uri = $uri->withPath('/');
133 }
134
135 if ($flags & self::REMOVE_DEFAULT_HOST && $uri->getScheme() === 'file' && $uri->getHost() === 'localhost') {
136 $uri = $uri->withHost('');
137 }
138
139 if ($flags & self::REMOVE_DEFAULT_PORT && $uri->getPort() !== null && Uri::isDefaultPort($uri)) {
140 $uri = $uri->withPort(null);
141 }
142
143 if ($flags & self::REMOVE_DOT_SEGMENTS && !Uri::isRelativePathReference($uri)) {
144 $uri = $uri->withPath(UriResolver::removeDotSegments($uri->getPath()));
145 }
146
147 if ($flags & self::REMOVE_DUPLICATE_SLASHES) {
148 $uri = $uri->withPath(preg_replace('#//++#', '/', $uri->getPath()));
149 }
150
151 if ($flags & self::SORT_QUERY_PARAMETERS && $uri->getQuery() !== '') {
152 $queryKeyValues = explode('&', $uri->getQuery());
153 sort($queryKeyValues);
154 $uri = $uri->withQuery(implode('&', $queryKeyValues));
155 }
156
157 return $uri;
158 }
159
175 public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, $normalizations = self::PRESERVING_NORMALIZATIONS)
176 {
177 return (string) self::normalize($uri1, $normalizations) === (string) self::normalize($uri2, $normalizations);
178 }
179
180 private static function capitalizePercentEncoding(UriInterface $uri)
181 {
182 $regex = '/(?:%[A-Fa-f0-9]{2})++/';
183
184 $callback = function (array $match) {
185 return strtoupper($match[0]);
186 };
187
188 return
189 $uri->withPath(
190 preg_replace_callback($regex, $callback, $uri->getPath())
191 )->withQuery(
192 preg_replace_callback($regex, $callback, $uri->getQuery())
193 );
194 }
195
196 private static function decodeUnreservedCharacters(UriInterface $uri)
197 {
198 $regex = '/%(?:2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i';
199
200 $callback = function (array $match) {
201 return rawurldecode($match[0]);
202 };
203
204 return
205 $uri->withPath(
206 preg_replace_callback($regex, $callback, $uri->getPath())
207 )->withQuery(
208 preg_replace_callback($regex, $callback, $uri->getQuery())
209 );
210 }
211
212 private function __construct()
213 {
214 // cannot be instantiated
215 }
216}
An exception for terminatinating execution or to throw for unit testing.
Provides methods to normalize and compare URIs.
const REMOVE_DOT_SEGMENTS
Removes unnecessary dot-segments.
const REMOVE_DUPLICATE_SLASHES
Paths which include two or more adjacent slashes are converted to one.
const REMOVE_DEFAULT_HOST
Removes the default host of the given URI scheme from the URI.
static decodeUnreservedCharacters(UriInterface $uri)
const PRESERVING_NORMALIZATIONS
Default normalizations which only include the ones that preserve semantics.
static normalize(UriInterface $uri, $flags=self::PRESERVING_NORMALIZATIONS)
Returns a normalized URI.
const REMOVE_DEFAULT_PORT
Removes the default port of the given URI scheme from the URI.
static isEquivalent(UriInterface $uri1, UriInterface $uri2, $normalizations=self::PRESERVING_NORMALIZATIONS)
Whether two URIs can be considered equivalent.
const CONVERT_EMPTY_PATH
Converts the empty path to "/" for http and https URIs.
const SORT_QUERY_PARAMETERS
Sort query parameters with their values in alphabetical order.
const DECODE_UNRESERVED_CHARACTERS
Decodes percent-encoded octets of unreserved characters.
static capitalizePercentEncoding(UriInterface $uri)
const CAPITALIZE_PERCENT_ENCODING
All letters within a percent-encoding triplet (e.g., "%3A") are case-insensitive, and should be capit...
static removeDotSegments($path)
Removes dot segments from a path and returns the new path.
Definition: UriResolver.php:23
static isRelativePathReference(UriInterface $uri)
Whether the URI is a relative-path reference.
Definition: Uri.php:222
static isDefaultPort(UriInterface $uri)
Whether the URI has the default port of the current scheme.
Definition: Uri.php:150
Value object representing a URI.
getScheme()
Retrieve the scheme component of the URI.
withHost($host)
Return an instance with the specified host.
getHost()
Retrieve the host component of the URI.
withQuery($query)
Return an instance with the specified query string.
getQuery()
Retrieve the query string of the URI.
withPath($path)
Return an instance with the specified path.
getPath()
Retrieve the path component of the URI.
getPort()
Retrieve the port component of the URI.
withPort($port)
Return an instance with the specified port.