30 $segments = explode(
'/',
$path);
31 foreach ($segments as $segment) {
32 if ($segment ===
'..') {
34 } elseif ($segment !==
'.') {
41 if (
$path[0] ===
'/' && (!isset($newPath[0]) || $newPath[0] !==
'/')) {
43 $newPath =
'/' . $newPath;
44 } elseif ($newPath !==
'' && ($segment ===
'.' || $segment ===
'..')) {
64 if ((
string) $rel ===
'') {
78 $targetAuthority =
$base->getAuthority();
80 $targetPath =
$base->getPath();
83 if ($rel->
getPath()[0] ===
'/') {
86 if ($targetAuthority !=
'' &&
$base->getPath() ===
'') {
87 $targetPath =
'/' . $rel->
getPath();
89 $lastSlashPos = strrpos(
$base->getPath(),
'/');
90 if ($lastSlashPos ===
false) {
93 $targetPath = substr(
$base->getPath(), 0, $lastSlashPos + 1) . $rel->
getPath();
139 if (
$target->getScheme() !==
'' &&
153 return $target->withScheme(
'');
159 $emptyPathUri =
$target->withScheme(
'')->withPath(
'')->withUserInfo(
'')->withPort(
null)->withHost(
'');
162 return $emptyPathUri->withPath(self::getRelativePath(
$base,
$target));
167 return $emptyPathUri->withQuery(
'');
172 if (
$target->getQuery() ===
'') {
173 $segments = explode(
'/',
$target->getPath());
174 $lastSegment = end($segments);
176 return $emptyPathUri->withPath($lastSegment ===
'' ?
'./' : $lastSegment);
179 return $emptyPathUri;
184 $sourceSegments = explode(
'/',
$base->getPath());
185 $targetSegments = explode(
'/',
$target->getPath());
186 array_pop($sourceSegments);
187 $targetLastSegment = array_pop($targetSegments);
188 foreach ($sourceSegments as
$i => $segment) {
189 if (isset($targetSegments[
$i]) && $segment === $targetSegments[
$i]) {
190 unset($sourceSegments[
$i], $targetSegments[
$i]);
195 $targetSegments[] = $targetLastSegment;
196 $relativePath = str_repeat(
'../', count($sourceSegments)) . implode(
'/', $targetSegments);
201 if (
'' === $relativePath ||
false !== strpos(explode(
'/', $relativePath, 2)[0],
':')) {
202 $relativePath =
"./$relativePath";
203 } elseif (
'/' === $relativePath[0]) {
204 if (
$base->getAuthority() !=
'' &&
$base->getPath() ===
'') {
206 $relativePath =
".$relativePath";
208 $relativePath =
"./$relativePath";
212 return $relativePath;
An exception for terminatinating execution or to throw for unit testing.
Resolves a URI reference in the context of a base URI and the opposite way.
static getRelativePath(UriInterface $base, UriInterface $target)
static relativize(UriInterface $base, UriInterface $target)
Returns the target URI as a relative reference from the base URI.
static removeDotSegments($path)
Removes dot segments from a path and returns the new path.
static resolve(UriInterface $base, UriInterface $rel)
Converts the relative URI into a new URI that is resolved against the base URI.
PSR-7 URI implementation.
static isRelativePathReference(UriInterface $uri)
Whether the URI is a relative-path reference.
static composeComponents($scheme, $authority, $path, $query, $fragment)
Composes a URI reference string from its various components.
Value object representing a URI.
getScheme()
Retrieve the scheme component of the URI.
getAuthority()
Retrieve the authority component of the URI.
getQuery()
Retrieve the query string of the URI.
getFragment()
Retrieve the fragment component of the URI.
withPath($path)
Return an instance with the specified path.
getPath()
Retrieve the path component of the URI.