Chris@0: getPath() === '' && Chris@0: ($uri->getScheme() === 'http' || $uri->getScheme() === 'https') Chris@0: ) { Chris@0: $uri = $uri->withPath('/'); Chris@0: } Chris@0: Chris@0: if ($flags & self::REMOVE_DEFAULT_HOST && $uri->getScheme() === 'file' && $uri->getHost() === 'localhost') { Chris@0: $uri = $uri->withHost(''); Chris@0: } Chris@0: Chris@0: if ($flags & self::REMOVE_DEFAULT_PORT && $uri->getPort() !== null && Uri::isDefaultPort($uri)) { Chris@0: $uri = $uri->withPort(null); Chris@0: } Chris@0: Chris@0: if ($flags & self::REMOVE_DOT_SEGMENTS && !Uri::isRelativePathReference($uri)) { Chris@0: $uri = $uri->withPath(UriResolver::removeDotSegments($uri->getPath())); Chris@0: } Chris@0: Chris@0: if ($flags & self::REMOVE_DUPLICATE_SLASHES) { Chris@0: $uri = $uri->withPath(preg_replace('#//++#', '/', $uri->getPath())); Chris@0: } Chris@0: Chris@0: if ($flags & self::SORT_QUERY_PARAMETERS && $uri->getQuery() !== '') { Chris@0: $queryKeyValues = explode('&', $uri->getQuery()); Chris@0: sort($queryKeyValues); Chris@0: $uri = $uri->withQuery(implode('&', $queryKeyValues)); Chris@0: } Chris@0: Chris@0: return $uri; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Whether two URIs can be considered equivalent. Chris@0: * Chris@0: * Both URIs are normalized automatically before comparison with the given $normalizations bitmask. The method also Chris@0: * accepts relative URI references and returns true when they are equivalent. This of course assumes they will be Chris@0: * resolved against the same base URI. If this is not the case, determination of equivalence or difference of Chris@0: * relative references does not mean anything. Chris@0: * Chris@0: * @param UriInterface $uri1 An URI to compare Chris@0: * @param UriInterface $uri2 An URI to compare Chris@0: * @param int $normalizations A bitmask of normalizations to apply, see constants Chris@0: * Chris@0: * @return bool Chris@0: * @link https://tools.ietf.org/html/rfc3986#section-6.1 Chris@0: */ Chris@0: public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, $normalizations = self::PRESERVING_NORMALIZATIONS) Chris@0: { Chris@0: return (string) self::normalize($uri1, $normalizations) === (string) self::normalize($uri2, $normalizations); Chris@0: } Chris@0: Chris@0: private static function capitalizePercentEncoding(UriInterface $uri) Chris@0: { Chris@0: $regex = '/(?:%[A-Fa-f0-9]{2})++/'; Chris@0: Chris@0: $callback = function (array $match) { Chris@0: return strtoupper($match[0]); Chris@0: }; Chris@0: Chris@0: return Chris@0: $uri->withPath( Chris@0: preg_replace_callback($regex, $callback, $uri->getPath()) Chris@0: )->withQuery( Chris@0: preg_replace_callback($regex, $callback, $uri->getQuery()) Chris@0: ); Chris@0: } Chris@0: Chris@0: private static function decodeUnreservedCharacters(UriInterface $uri) Chris@0: { Chris@0: $regex = '/%(?:2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i'; Chris@0: Chris@0: $callback = function (array $match) { Chris@0: return rawurldecode($match[0]); Chris@0: }; Chris@0: Chris@0: return Chris@0: $uri->withPath( Chris@0: preg_replace_callback($regex, $callback, $uri->getPath()) Chris@0: )->withQuery( Chris@0: preg_replace_callback($regex, $callback, $uri->getQuery()) Chris@0: ); Chris@0: } Chris@0: Chris@0: private function __construct() Chris@0: { Chris@0: // cannot be instantiated Chris@0: } Chris@0: }