aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/sabre/uri/lib/functions.php
diff options
context:
space:
mode:
authorMario <mario@mariovavti.com>2019-11-10 12:49:51 +0000
committerMario <mario@mariovavti.com>2019-11-10 14:10:03 +0100
commit580c3f4ffe9608d2beb56d418c68b3b112420e76 (patch)
tree82335d01179ac361d3f547a4b8e8c598d302e9f3 /vendor/sabre/uri/lib/functions.php
parentd22766f458a8539a40a57f3946459a9be1f21cd6 (diff)
downloadvolse-hubzilla-580c3f4ffe9608d2beb56d418c68b3b112420e76.tar.gz
volse-hubzilla-580c3f4ffe9608d2beb56d418c68b3b112420e76.tar.bz2
volse-hubzilla-580c3f4ffe9608d2beb56d418c68b3b112420e76.zip
another bulk of composer updates
(cherry picked from commit 6685381fd8db507493c3d7c1793f8c05c681bbce)
Diffstat (limited to 'vendor/sabre/uri/lib/functions.php')
-rw-r--r--vendor/sabre/uri/lib/functions.php180
1 files changed, 100 insertions, 80 deletions
diff --git a/vendor/sabre/uri/lib/functions.php b/vendor/sabre/uri/lib/functions.php
index 39b4a6f08..161e684d7 100644
--- a/vendor/sabre/uri/lib/functions.php
+++ b/vendor/sabre/uri/lib/functions.php
@@ -1,5 +1,7 @@
<?php
+declare(strict_types=1);
+
namespace Sabre\Uri;
/**
@@ -18,30 +20,32 @@ namespace Sabre\Uri;
*
* @param string $basePath
* @param string $newPath
+ *
* @return string
+ *
+ * @throws InvalidUriException
*/
-function resolve($basePath, $newPath) {
-
- $base = parse($basePath);
+function resolve(string $basePath, string $newPath): string
+{
$delta = parse($newPath);
- $pick = function($part) use ($base, $delta) {
+ // If the new path defines a scheme, it's absolute and we can just return
+ // that.
+ if ($delta['scheme']) {
+ return build($delta);
+ }
+ $base = parse($basePath);
+ $pick = function ($part) use ($base, $delta) {
if ($delta[$part]) {
return $delta[$part];
} elseif ($base[$part]) {
return $base[$part];
}
- return null;
+ return null;
};
- // If the new path defines a scheme, it's absolute and we can just return
- // that.
- if ($delta['scheme']) {
- return build($delta);
- }
-
$newParts = [];
$newParts['scheme'] = $pick('scheme');
@@ -49,17 +53,18 @@ function resolve($basePath, $newPath) {
$newParts['port'] = $pick('port');
$path = '';
- if ($delta['path']) {
+ if (is_string($delta['path']) and strlen($delta['path']) > 0) {
// If the path starts with a slash
- if ($delta['path'][0] === '/') {
+ if ('/' === $delta['path'][0]) {
$path = $delta['path'];
} else {
// Removing last component from base path.
$path = $base['path'];
- if (strpos($path, '/') !== false) {
- $path = substr($path, 0, strrpos($path, '/'));
+ $length = strrpos((string) $path, '/');
+ if (false !== $length) {
+ $path = substr($path, 0, $length);
}
- $path .= '/' . $delta['path'];
+ $path .= '/'.$delta['path'];
}
} else {
$path = $base['path'] ?: '/';
@@ -68,15 +73,14 @@ function resolve($basePath, $newPath) {
$pathParts = explode('/', $path);
$newPathParts = [];
foreach ($pathParts as $pathPart) {
-
switch ($pathPart) {
//case '' :
- case '.' :
+ case '.':
break;
- case '..' :
+ case '..':
array_pop($newPathParts);
break;
- default :
+ default:
$newPathParts[] = $pathPart;
break;
}
@@ -95,8 +99,8 @@ function resolve($basePath, $newPath) {
if ($delta['fragment']) {
$newParts['fragment'] = $delta['fragment'];
}
- return build($newParts);
+ return build($newParts);
}
/**
@@ -109,10 +113,13 @@ function resolve($basePath, $newPath) {
* It will also change a %3a into a %3A.
*
* @param string $uri
+ *
* @return string
+ *
+ * @throws InvalidUriException
*/
-function normalize($uri) {
-
+function normalize(string $uri): string
+{
$parts = parse($uri);
if (!empty($parts['path'])) {
@@ -123,23 +130,23 @@ function normalize($uri) {
case '.':
// skip
break;
- case '..' :
+ case '..':
// One level up in the hierarchy
array_pop($newPathParts);
break;
- default :
+ default:
// Ensuring that everything is correctly percent-encoded.
$newPathParts[] = rawurlencode(rawurldecode($pathPart));
break;
}
}
- $parts['path'] = '/' . implode('/', $newPathParts);
+ $parts['path'] = '/'.implode('/', $newPathParts);
}
if ($parts['scheme']) {
$parts['scheme'] = strtolower($parts['scheme']);
$defaultPorts = [
- 'http' => '80',
+ 'http' => '80',
'https' => '443',
];
@@ -149,8 +156,8 @@ function normalize($uri) {
}
// A few HTTP specific rules.
switch ($parts['scheme']) {
- case 'http' :
- case 'https' :
+ case 'http':
+ case 'https':
if (empty($parts['path'])) {
// An empty path is equivalent to / in http.
$parts['path'] = '/';
@@ -159,10 +166,11 @@ function normalize($uri) {
}
}
- if ($parts['host']) $parts['host'] = strtolower($parts['host']);
+ if ($parts['host']) {
+ $parts['host'] = strtolower($parts['host']);
+ }
return build($parts);
-
}
/**
@@ -176,10 +184,13 @@ function normalize($uri) {
* percent-encoded strings. PHP's parse_url corrupts these characters on OS X.
*
* @param string $uri
+ *
* @return array
+ *
+ * @throws InvalidUriException
*/
-function parse($uri) {
-
+function parse(string $uri): array
+{
// Normally a URI must be ASCII, however. However, often it's not and
// parse_url might corrupt these strings.
//
@@ -187,7 +198,7 @@ function parse($uri) {
// uriencode them first.
$uri = preg_replace_callback(
'/[^[:ascii:]]/u',
- function($matches) {
+ function ($matches) {
return rawurlencode($matches[0]);
},
$uri
@@ -200,15 +211,14 @@ function parse($uri) {
return
$result + [
- 'scheme' => null,
- 'host' => null,
- 'path' => null,
- 'port' => null,
- 'user' => null,
- 'query' => null,
+ 'scheme' => null,
+ 'host' => null,
+ 'path' => null,
+ 'port' => null,
+ 'user' => null,
+ 'query' => null,
'fragment' => null,
];
-
}
/**
@@ -216,46 +226,44 @@ function parse($uri) {
* it to generate a new uri.
*
* @param array $parts
+ *
* @return string
*/
-function build(array $parts) {
-
+function build(array $parts): string
+{
$uri = '';
$authority = '';
if (!empty($parts['host'])) {
$authority = $parts['host'];
if (!empty($parts['user'])) {
- $authority = $parts['user'] . '@' . $authority;
+ $authority = $parts['user'].'@'.$authority;
}
if (!empty($parts['port'])) {
- $authority = $authority . ':' . $parts['port'];
+ $authority = $authority.':'.$parts['port'];
}
}
if (!empty($parts['scheme'])) {
// If there's a scheme, there's also a host.
- $uri = $parts['scheme'] . ':';
-
+ $uri = $parts['scheme'].':';
}
- if ($authority || (!empty($parts['scheme']) && $parts['scheme'] === 'file')) {
+ if ($authority || (!empty($parts['scheme']) && 'file' === $parts['scheme'])) {
// No scheme, but there is a host.
- $uri .= '//' . $authority;
-
+ $uri .= '//'.$authority;
}
if (!empty($parts['path'])) {
$uri .= $parts['path'];
}
if (!empty($parts['query'])) {
- $uri .= '?' . $parts['query'];
+ $uri .= '?'.$parts['query'];
}
if (!empty($parts['fragment'])) {
- $uri .= '#' . $parts['fragment'];
+ $uri .= '#'.$parts['fragment'];
}
return $uri;
-
}
/**
@@ -274,16 +282,17 @@ function build(array $parts) {
* the end of the string is stripped off.
*
* @param string $path
+ *
* @return array
*/
-function split($path) {
-
+function split(string $path): array
+{
$matches = [];
if (preg_match('/^(?:(?:(.*)(?:\/+))?([^\/]+))(?:\/?)$/u', $path, $matches)) {
return [$matches[1], $matches[2]];
}
- return [null,null];
+ return [null, null];
}
/**
@@ -297,10 +306,13 @@ function split($path) {
* crude and probably slow, so the original parse_url is usually preferred.
*
* @param string $uri
+ *
* @return array
+ *
+ * @throws InvalidUriException
*/
-function _parse_fallback($uri) {
-
+function _parse_fallback(string $uri): array
+{
// Normally a URI must be ASCII, however. However, often it's not and
// parse_url might corrupt these strings.
//
@@ -308,45 +320,43 @@ function _parse_fallback($uri) {
// uriencode them first.
$uri = preg_replace_callback(
'/[^[:ascii:]]/u',
- function($matches) {
+ function ($matches) {
return rawurlencode($matches[0]);
},
$uri
);
$result = [
- 'scheme' => null,
- 'host' => null,
- 'port' => null,
- 'user' => null,
- 'path' => null,
+ 'scheme' => null,
+ 'host' => null,
+ 'port' => null,
+ 'user' => null,
+ 'path' => null,
'fragment' => null,
- 'query' => null,
+ 'query' => null,
];
if (preg_match('% ^([A-Za-z][A-Za-z0-9+-\.]+): %x', $uri, $matches)) {
-
$result['scheme'] = $matches[1];
// Take what's left.
$uri = substr($uri, strlen($result['scheme']) + 1);
-
}
// Taking off a fragment part
- if (strpos($uri, '#') !== false) {
+ if (false !== strpos($uri, '#')) {
list($uri, $result['fragment']) = explode('#', $uri, 2);
}
// Taking off the query part
- if (strpos($uri, '?') !== false) {
+ if (false !== strpos($uri, '?')) {
list($uri, $result['query']) = explode('?', $uri, 2);
}
- if (substr($uri, 0, 3) === '///') {
- // The triple slash uris are a bit unusual, but we have special handling
- // for them.
- $result['path'] = substr($uri, 2);
- $result['host'] = '';
- } elseif (substr($uri, 0, 2) === '//') {
+ if ('///' === substr($uri, 0, 3)) {
+ // The triple slash uris are a bit unusual, but we have special handling
+ // for them.
+ $result['path'] = substr($uri, 2);
+ $result['host'] = '';
+ } elseif ('//' === substr($uri, 0, 2)) {
// Uris that have an authority part.
$regex = '
%^
@@ -360,11 +370,21 @@ function _parse_fallback($uri) {
if (!preg_match($regex, $uri, $matches)) {
throw new InvalidUriException('Invalid, or could not parse URI');
}
- if ($matches['host']) $result['host'] = $matches['host'];
- if ($matches['port']) $result['port'] = (int)$matches['port'];
- if (isset($matches['path'])) $result['path'] = $matches['path'];
- if ($matches['user']) $result['user'] = $matches['user'];
- if ($matches['pass']) $result['pass'] = $matches['pass'];
+ if ($matches['host']) {
+ $result['host'] = $matches['host'];
+ }
+ if (isset($matches['port'])) {
+ $result['port'] = (int) $matches['port'];
+ }
+ if (isset($matches['path'])) {
+ $result['path'] = $matches['path'];
+ }
+ if ($matches['user']) {
+ $result['user'] = $matches['user'];
+ }
+ if ($matches['pass']) {
+ $result['pass'] = $matches['pass'];
+ }
} else {
$result['path'] = $uri;
}