Open Journal Systems  3.3.0
UriNormalizer.php
1 <?php
2 namespace GuzzleHttp\Psr7;
3 
5 
13 final class UriNormalizer
14 {
22 
29 
40 
46  const CONVERT_EMPTY_PATH = 4;
47 
60 
66  const REMOVE_DEFAULT_PORT = 16;
67 
76  const REMOVE_DOT_SEGMENTS = 32;
77 
88 
101 
119  public static function normalize(UriInterface $uri, $flags = self::PRESERVING_NORMALIZATIONS)
120  {
121  if ($flags & self::CAPITALIZE_PERCENT_ENCODING) {
122  $uri = self::capitalizePercentEncoding($uri);
123  }
124 
125  if ($flags & self::DECODE_UNRESERVED_CHARACTERS) {
126  $uri = self::decodeUnreservedCharacters($uri);
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 }
Psr\Http\Message\UriInterface\getHost
getHost()
GuzzleHttp\Psr7\UriNormalizer\CAPITALIZE_PERCENT_ENCODING
const CAPITALIZE_PERCENT_ENCODING
Definition: UriNormalizer.php:28
Psr\Http\Message\UriInterface\withQuery
withQuery($query)
Psr\Http\Message\UriInterface\getPath
getPath()
GuzzleHttp\Psr7\UriNormalizer\REMOVE_DEFAULT_PORT
const REMOVE_DEFAULT_PORT
Definition: UriNormalizer.php:66
GuzzleHttp\Psr7\UriResolver\removeDotSegments
static removeDotSegments($path)
Definition: UriResolver.php:23
GuzzleHttp\Psr7\UriNormalizer\REMOVE_DEFAULT_HOST
const REMOVE_DEFAULT_HOST
Definition: UriNormalizer.php:59
GuzzleHttp\Psr7\UriNormalizer\REMOVE_DUPLICATE_SLASHES
const REMOVE_DUPLICATE_SLASHES
Definition: UriNormalizer.php:87
GuzzleHttp\Psr7\UriNormalizer\CONVERT_EMPTY_PATH
const CONVERT_EMPTY_PATH
Definition: UriNormalizer.php:46
Psr\Http\Message\UriInterface\withPort
withPort($port)
GuzzleHttp\Psr7\UriNormalizer\PRESERVING_NORMALIZATIONS
const PRESERVING_NORMALIZATIONS
Definition: UriNormalizer.php:21
GuzzleHttp\Psr7\UriNormalizer\SORT_QUERY_PARAMETERS
const SORT_QUERY_PARAMETERS
Definition: UriNormalizer.php:100
GuzzleHttp\Psr7
Definition: AppendStream.php:2
Psr\Http\Message\UriInterface
Definition: UriInterface.php:24
Psr\Http\Message\UriInterface\withPath
withPath($path)
GuzzleHttp\Psr7\UriNormalizer
Definition: UriNormalizer.php:13
GuzzleHttp\Psr7\Uri\isDefaultPort
static isDefaultPort(UriInterface $uri)
Definition: Uri.php:171
GuzzleHttp\Psr7\Uri\isRelativePathReference
static isRelativePathReference(UriInterface $uri)
Definition: Uri.php:243
Psr\Http\Message\UriInterface\getScheme
getScheme()
Psr\Http\Message\UriInterface\withHost
withHost($host)
GuzzleHttp\Psr7\UriNormalizer\REMOVE_DOT_SEGMENTS
const REMOVE_DOT_SEGMENTS
Definition: UriNormalizer.php:76
Psr\Http\Message\UriInterface\getQuery
getQuery()
GuzzleHttp\Psr7\UriNormalizer\DECODE_UNRESERVED_CHARACTERS
const DECODE_UNRESERVED_CHARACTERS
Definition: UriNormalizer.php:39
GuzzleHttp\Psr7\UriNormalizer\isEquivalent
static isEquivalent(UriInterface $uri1, UriInterface $uri2, $normalizations=self::PRESERVING_NORMALIZATIONS)
Definition: UriNormalizer.php:175
GuzzleHttp\Psr7\UriNormalizer\normalize
static normalize(UriInterface $uri, $flags=self::PRESERVING_NORMALIZATIONS)
Definition: UriNormalizer.php:119
Psr\Http\Message\UriInterface\getPort
getPort()