← All changes
|
vendor_prefixed/guzzlehttp/psr7/src/UriResolver.php
+25
-41
18.9
→
trunk
View file →
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace YoastSEO_Vendor\GuzzleHttp\Psr7; |
| 4 | 5 | |
| 5 | 6 | use YoastSEO_Vendor\Psr\Http\Message\UriInterface; |
| 6 | 7 | /** |
| @@ -7,9 +8,9 @@ | ||
| 7 | 8 | * Resolves a URI reference in the context of a base URI and the opposite way. |
| 8 | 9 | * |
| 9 | 10 | * @author Tobias Schultze |
| 10 | 11 | * |
| 11 | - * @link https://tools.ietf.org/html/rfc3986#section-5 | |
| 12 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-5 | |
| 12 | 13 | */ |
| 13 | 14 | final class UriResolver |
| 14 | 15 | { |
| 15 | 16 | /** |
| @@ -14,15 +15,11 @@ | ||
| 14 | 15 | { |
| 15 | 16 | /** |
| 16 | 17 | * Removes dot segments from a path and returns the new path. |
| 17 | 18 | * |
| 18 | - * @param string $path | |
| 19 | - * | |
| 20 | - * @return string | |
| 21 | - * | |
| 22 | - * @link http://tools.ietf.org/html/rfc3986#section-5.2.4 | |
| 19 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4 | |
| 23 | 20 | */ |
| 24 | - public static function removeDotSegments($path) | |
| 21 | + public static function removeDotSegments(string $path) : string | |
| 25 | 22 | { |
| 26 | 23 | if ($path === '' || $path === '/') { |
| 27 | 24 | return $path; |
| 28 | 25 | } |
| @@ -48,16 +45,11 @@ | ||
| 48 | 45 | } |
| 49 | 46 | /** |
| 50 | 47 | * Converts the relative URI into a new URI that is resolved against the base URI. |
| 51 | 48 | * |
| 52 | - * @param UriInterface $base Base URI | |
| 53 | - * @param UriInterface $rel Relative URI | |
| 54 | - * | |
| 55 | - * @return UriInterface | |
| 56 | - * | |
| 57 | - * @link http://tools.ietf.org/html/rfc3986#section-5.2 | |
| 49 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-5.2 | |
| 58 | 50 | */ |
| 59 | - public static function resolve(\YoastSEO_Vendor\Psr\Http\Message\UriInterface $base, \YoastSEO_Vendor\Psr\Http\Message\UriInterface $rel) | |
| 51 | + public static function resolve(\YoastSEO_Vendor\Psr\Http\Message\UriInterface $base, \YoastSEO_Vendor\Psr\Http\Message\UriInterface $rel) : \YoastSEO_Vendor\Psr\Http\Message\UriInterface | |
| 60 | 52 | { |
| 61 | 53 | if ((string) $rel === '') { |
| 62 | 54 | // we can simply return the same base URI instance for this same-document reference |
| 63 | 55 | return $base; |
| @@ -65,36 +57,32 @@ | ||
| 65 | 57 | if ($rel->getScheme() != '') { |
| 66 | 58 | return $rel->withPath(self::removeDotSegments($rel->getPath())); |
| 67 | 59 | } |
| 68 | 60 | if ($rel->getAuthority() != '') { |
| 69 | - $targetAuthority = $rel->getAuthority(); | |
| 70 | - $targetPath = self::removeDotSegments($rel->getPath()); | |
| 71 | - $targetQuery = $rel->getQuery(); | |
| 61 | + return $rel->withScheme($base->getScheme())->withPath(self::removeDotSegments($rel->getPath())); | |
| 62 | + } | |
| 63 | + if ($rel->getPath() === '') { | |
| 64 | + $targetPath = $base->getPath(); | |
| 65 | + $targetQuery = $rel->getQuery() != '' ? $rel->getQuery() : $base->getQuery(); | |
| 72 | 66 | } else { |
| 73 | - $targetAuthority = $base->getAuthority(); | |
| 74 | - if ($rel->getPath() === '') { | |
| 75 | - $targetPath = $base->getPath(); | |
| 76 | - $targetQuery = $rel->getQuery() != '' ? $rel->getQuery() : $base->getQuery(); | |
| 67 | + if ($rel->getPath()[0] === '/') { | |
| 68 | + $targetPath = $rel->getPath(); | |
| 77 | 69 | } else { |
| 78 | - if ($rel->getPath()[0] === '/') { | |
| 79 | - $targetPath = $rel->getPath(); | |
| 70 | + if ($base->getAuthority() != '' && $base->getPath() === '') { | |
| 71 | + $targetPath = '/' . $rel->getPath(); | |
| 80 | 72 | } else { |
| 81 | - if ($targetAuthority != '' && $base->getPath() === '') { | |
| 82 | - $targetPath = '/' . $rel->getPath(); | |
| 73 | + $lastSlashPos = \strrpos($base->getPath(), '/'); | |
| 74 | + if ($lastSlashPos === \false) { | |
| 75 | + $targetPath = $rel->getPath(); | |
| 83 | 76 | } else { |
| 84 | - $lastSlashPos = \strrpos($base->getPath(), '/'); | |
| 85 | - if ($lastSlashPos === \false) { | |
| 86 | - $targetPath = $rel->getPath(); | |
| 87 | - } else { | |
| 88 | - $targetPath = \substr($base->getPath(), 0, $lastSlashPos + 1) . $rel->getPath(); | |
| 89 | - } | |
| 77 | + $targetPath = \substr($base->getPath(), 0, $lastSlashPos + 1) . $rel->getPath(); | |
| 90 | 78 | } |
| 91 | 79 | } |
| 92 | - $targetPath = self::removeDotSegments($targetPath); | |
| 93 | - $targetQuery = $rel->getQuery(); | |
| 94 | 80 | } |
| 81 | + $targetPath = self::removeDotSegments($targetPath); | |
| 82 | + $targetQuery = $rel->getQuery(); | |
| 95 | 83 | } |
| 96 | - return new \YoastSEO_Vendor\GuzzleHttp\Psr7\Uri(\YoastSEO_Vendor\GuzzleHttp\Psr7\Uri::composeComponents($base->getScheme(), $targetAuthority, $targetPath, $targetQuery, $rel->getFragment())); | |
| 84 | + return $base->withPath($targetPath)->withQuery($targetQuery)->withFragment($rel->getFragment()); | |
| 97 | 85 | } |
| 98 | 86 | /** |
| 99 | 87 | * Returns the target URI as a relative reference from the base URI. |
| 100 | 88 | * |
| @@ -114,15 +102,10 @@ | ||
| 114 | 102 | * This method also accepts a target that is already relative and will try to relativize it further. Only a |
| 115 | 103 | * relative-path reference will be returned as-is. |
| 116 | 104 | * |
| 117 | 105 | * echo UriResolver::relativize($base, new Uri('/a/b/c')); // prints 'c' as well |
| 118 | - * | |
| 119 | - * @param UriInterface $base Base URI | |
| 120 | - * @param UriInterface $target Target URI | |
| 121 | - * | |
| 122 | - * @return UriInterface The relative URI reference | |
| 123 | 106 | */ |
| 124 | - public static function relativize(\YoastSEO_Vendor\Psr\Http\Message\UriInterface $base, \YoastSEO_Vendor\Psr\Http\Message\UriInterface $target) | |
| 107 | + public static function relativize(\YoastSEO_Vendor\Psr\Http\Message\UriInterface $base, \YoastSEO_Vendor\Psr\Http\Message\UriInterface $target) : \YoastSEO_Vendor\Psr\Http\Message\UriInterface | |
| 125 | 108 | { |
| 126 | 109 | if ($target->getScheme() !== '' && ($base->getScheme() !== $target->getScheme() || $target->getAuthority() === '' && $base->getAuthority() !== '')) { |
| 127 | 110 | return $target; |
| 128 | 111 | } |
| @@ -149,14 +132,15 @@ | ||
| 149 | 132 | // If the base URI has a query but the target has none, we cannot return an empty path reference as it would |
| 150 | 133 | // inherit the base query component when resolving. |
| 151 | 134 | if ($target->getQuery() === '') { |
| 152 | 135 | $segments = \explode('/', $target->getPath()); |
| 136 | + /** @var string $lastSegment */ | |
| 153 | 137 | $lastSegment = \end($segments); |
| 154 | 138 | return $emptyPathUri->withPath($lastSegment === '' ? './' : $lastSegment); |
| 155 | 139 | } |
| 156 | 140 | return $emptyPathUri; |
| 157 | 141 | } |
| 158 | - private static function getRelativePath(\YoastSEO_Vendor\Psr\Http\Message\UriInterface $base, \YoastSEO_Vendor\Psr\Http\Message\UriInterface $target) | |
| 142 | + private static function getRelativePath(\YoastSEO_Vendor\Psr\Http\Message\UriInterface $base, \YoastSEO_Vendor\Psr\Http\Message\UriInterface $target) : string | |
| 159 | 143 | { |
| 160 | 144 | $sourceSegments = \explode('/', $base->getPath()); |
| 161 | 145 | $targetSegments = \explode('/', $target->getPath()); |
| 162 | 146 | \array_pop($sourceSegments); |