// We must remove the path before removing the authority because if the path starts with two slashes, the URI
// would turn invalid. And we also cannot set a relative path before removing the authority, as that is also
// invalid.
$emptyPathUri =
$target->
withScheme(''
)->
withPath(''
)->
withUserInfo(''
)->
withPort(null
)->
withHost(''
);
if ($base->
getPath() !==
$target->
getPath()) { return $emptyPathUri->
withPath(self::
getRelativePath($base,
$target));
} if ($base->
getQuery() ===
$target->
getQuery()) { // Only the target fragment is left. And it must be returned even if base and target fragment are the same.
return $emptyPathUri->
withQuery(''
);
} // If the base URI has a query but the target has none, we cannot return an empty path reference as it would
// inherit the base query component when resolving.
if ($target->
getQuery() === ''
) { $segments =
explode('/',
$target->
getPath());
/** @var string $lastSegment */
$lastSegment =
end($segments);
return $emptyPathUri->
withPath($lastSegment === '' ? './' :
$lastSegment);
}