← All changes
|
includes/sdk/s3/GuzzleHttp/Psr7/UriNormalizer.php
+19
-25
1.2.11
→
1.4.2
View file →
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7; |
| 4 | 5 | |
| 5 | 6 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\UriInterface; |
| 6 | 7 | /** |
| @@ -7,25 +8,22 @@ | ||
| 7 | 8 | * Provides methods to normalize and compare URIs. |
| 8 | 9 | * |
| 9 | 10 | * @author Tobias Schultze |
| 10 | 11 | * |
| 11 | - * @link https://tools.ietf.org/html/rfc3986#section-6 | |
| 12 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6 | |
| 12 | 13 | */ |
| 13 | 14 | final class UriNormalizer |
| 14 | 15 | { |
| 15 | 16 | /** |
| 16 | 17 | * Default normalizations which only include the ones that preserve semantics. |
| 17 | - * | |
| 18 | - * self::CAPITALIZE_PERCENT_ENCODING | self::DECODE_UNRESERVED_CHARACTERS | self::CONVERT_EMPTY_PATH | | |
| 19 | - * self::REMOVE_DEFAULT_HOST | self::REMOVE_DEFAULT_PORT | self::REMOVE_DOT_SEGMENTS | |
| 20 | 18 | */ |
| 21 | - const PRESERVING_NORMALIZATIONS = 63; | |
| 19 | + public const PRESERVING_NORMALIZATIONS = self::CAPITALIZE_PERCENT_ENCODING | self::DECODE_UNRESERVED_CHARACTERS | self::CONVERT_EMPTY_PATH | self::REMOVE_DEFAULT_HOST | self::REMOVE_DEFAULT_PORT | self::REMOVE_DOT_SEGMENTS; | |
| 22 | 20 | /** |
| 23 | 21 | * All letters within a percent-encoding triplet (e.g., "%3A") are case-insensitive, and should be capitalized. |
| 24 | 22 | * |
| 25 | 23 | * Example: http://example.org/a%c2%b1b → http://example.org/a%C2%B1b |
| 26 | 24 | */ |
| 27 | - const CAPITALIZE_PERCENT_ENCODING = 1; | |
| 25 | + public const CAPITALIZE_PERCENT_ENCODING = 1; | |
| 28 | 26 | /** |
| 29 | 27 | * Decodes percent-encoded octets of unreserved characters. |
| 30 | 28 | * |
| 31 | 29 | * For consistency, percent-encoded octets in the ranges of ALPHA (%41–%5A and %61–%7A), DIGIT (%30–%39), |
| @@ -33,15 +31,15 @@ | ||
| 33 | 31 | * when found in a URI, should be decoded to their corresponding unreserved characters by URI normalizers. |
| 34 | 32 | * |
| 35 | 33 | * Example: http://example.org/%7Eusern%61me/ → http://example.org/~username/ |
| 36 | 34 | */ |
| 37 | - const DECODE_UNRESERVED_CHARACTERS = 2; | |
| 35 | + public const DECODE_UNRESERVED_CHARACTERS = 2; | |
| 38 | 36 | /** |
| 39 | 37 | * Converts the empty path to "/" for http and https URIs. |
| 40 | 38 | * |
| 41 | 39 | * Example: http://example.org → http://example.org/ |
| 42 | 40 | */ |
| 43 | - const CONVERT_EMPTY_PATH = 4; | |
| 41 | + public const CONVERT_EMPTY_PATH = 4; | |
| 44 | 42 | /** |
| 45 | 43 | * Removes the default host of the given URI scheme from the URI. |
| 46 | 44 | * |
| 47 | 45 | * Only the "file" scheme defines the default host "localhost". |
| @@ -51,15 +49,15 @@ | ||
| 51 | 49 | * second format in the Uri class. See `GuzzleHttp\Psr7\Uri::composeComponents`. |
| 52 | 50 | * |
| 53 | 51 | * Example: file://localhost/myfile → file:///myfile |
| 54 | 52 | */ |
| 55 | - const REMOVE_DEFAULT_HOST = 8; | |
| 53 | + public const REMOVE_DEFAULT_HOST = 8; | |
| 56 | 54 | /** |
| 57 | 55 | * Removes the default port of the given URI scheme from the URI. |
| 58 | 56 | * |
| 59 | 57 | * Example: http://example.org:80/ → http://example.org/ |
| 60 | 58 | */ |
| 61 | - const REMOVE_DEFAULT_PORT = 16; | |
| 59 | + public const REMOVE_DEFAULT_PORT = 16; | |
| 62 | 60 | /** |
| 63 | 61 | * Removes unnecessary dot-segments. |
| 64 | 62 | * |
| 65 | 63 | * Dot-segments in relative-path references are not removed as it would |
| @@ -66,9 +64,9 @@ | ||
| 66 | 64 | * change the semantics of the URI reference. |
| 67 | 65 | * |
| 68 | 66 | * Example: http://example.org/../a/b/../c/./d.html → http://example.org/a/c/d.html |
| 69 | 67 | */ |
| 70 | - const REMOVE_DOT_SEGMENTS = 32; | |
| 68 | + public const REMOVE_DOT_SEGMENTS = 32; | |
| 71 | 69 | /** |
| 72 | 70 | * Paths which include two or more adjacent slashes are converted to one. |
| 73 | 71 | * |
| 74 | 72 | * Webservers usually ignore duplicate slashes and treat those URIs equivalent. |
| @@ -76,9 +74,9 @@ | ||
| 76 | 74 | * may change the semantics. Encoded slashes (%2F) are not removed. |
| 77 | 75 | * |
| 78 | 76 | * Example: http://example.org//foo///bar.html → http://example.org/foo/bar.html |
| 79 | 77 | */ |
| 80 | - const REMOVE_DUPLICATE_SLASHES = 64; | |
| 78 | + public const REMOVE_DUPLICATE_SLASHES = 64; | |
| 81 | 79 | /** |
| 82 | 80 | * Sort query parameters with their values in alphabetical order. |
| 83 | 81 | * |
| 84 | 82 | * However, the order of parameters in a URI may be significant (this is not defined by the standard). |
| @@ -88,9 +86,9 @@ | ||
| 88 | 86 | * |
| 89 | 87 | * Note: The sorting is neither locale nor Unicode aware (the URI query does not get decoded at all) as the |
| 90 | 88 | * purpose is to be able to compare URIs in a reproducible way, not to have the params sorted perfectly. |
| 91 | 89 | */ |
| 92 | - const SORT_QUERY_PARAMETERS = 128; | |
| 90 | + public const SORT_QUERY_PARAMETERS = 128; | |
| 93 | 91 | /** |
| 94 | 92 | * Returns a normalized URI. |
| 95 | 93 | * |
| 96 | 94 | * The scheme and host component are already normalized to lowercase per PSR-7 UriInterface. |
| @@ -103,13 +101,11 @@ | ||
| 103 | 101 | * |
| 104 | 102 | * @param UriInterface $uri The URI to normalize |
| 105 | 103 | * @param int $flags A bitmask of normalizations to apply, see constants |
| 106 | 104 | * |
| 107 | - * @return UriInterface The normalized URI | |
| 108 | - * | |
| 109 | - * @link https://tools.ietf.org/html/rfc3986#section-6.2 | |
| 105 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6.2 | |
| 110 | 106 | */ |
| 111 | - public static function normalize(UriInterface $uri, $flags = self::PRESERVING_NORMALIZATIONS) | |
| 107 | + public static function normalize(UriInterface $uri, int $flags = self::PRESERVING_NORMALIZATIONS) : UriInterface | |
| 112 | 108 | { |
| 113 | 109 | if ($flags & self::CAPITALIZE_PERCENT_ENCODING) { |
| 114 | 110 | $uri = self::capitalizePercentEncoding($uri); |
| 115 | 111 | } |
| @@ -149,28 +145,26 @@ | ||
| 149 | 145 | * @param UriInterface $uri1 An URI to compare |
| 150 | 146 | * @param UriInterface $uri2 An URI to compare |
| 151 | 147 | * @param int $normalizations A bitmask of normalizations to apply, see constants |
| 152 | 148 | * |
| 153 | - * @return bool | |
| 154 | - * | |
| 155 | - * @link https://tools.ietf.org/html/rfc3986#section-6.1 | |
| 149 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6.1 | |
| 156 | 150 | */ |
| 157 | - public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, $normalizations = self::PRESERVING_NORMALIZATIONS) | |
| 151 | + public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, int $normalizations = self::PRESERVING_NORMALIZATIONS) : bool | |
| 158 | 152 | { |
| 159 | 153 | return (string) self::normalize($uri1, $normalizations) === (string) self::normalize($uri2, $normalizations); |
| 160 | 154 | } |
| 161 | - private static function capitalizePercentEncoding(UriInterface $uri) | |
| 155 | + private static function capitalizePercentEncoding(UriInterface $uri) : UriInterface | |
| 162 | 156 | { |
| 163 | 157 | $regex = '/(?:%[A-Fa-f0-9]{2})++/'; |
| 164 | - $callback = function (array $match) { | |
| 158 | + $callback = function (array $match) : string { | |
| 165 | 159 | return \strtoupper($match[0]); |
| 166 | 160 | }; |
| 167 | 161 | return $uri->withPath(\preg_replace_callback($regex, $callback, $uri->getPath()))->withQuery(\preg_replace_callback($regex, $callback, $uri->getQuery())); |
| 168 | 162 | } |
| 169 | - private static function decodeUnreservedCharacters(UriInterface $uri) | |
| 163 | + private static function decodeUnreservedCharacters(UriInterface $uri) : UriInterface | |
| 170 | 164 | { |
| 171 | 165 | $regex = '/%(?:2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i'; |
| 172 | - $callback = function (array $match) { | |
| 166 | + $callback = function (array $match) : string { | |
| 173 | 167 | return \rawurldecode($match[0]); |
| 174 | 168 | }; |
| 175 | 169 | return $uri->withPath(\preg_replace_callback($regex, $callback, $uri->getPath()))->withQuery(\preg_replace_callback($regex, $callback, $uri->getQuery())); |
| 176 | 170 | } |