You are here

public static function UriResolver::removeDotSegments in Lockr 7.3

Removes dot segments from a path and returns the new path.

@link http://tools.ietf.org/html/rfc3986#section-5.2.4

Parameters

string $path:

Return value

string

3 calls to UriResolver::removeDotSegments()
Uri::removeDotSegments in vendor/guzzlehttp/psr7/src/Uri.php
Removes dot segments from a path and returns the new path.
UriNormalizer::normalize in vendor/guzzlehttp/psr7/src/UriNormalizer.php
Returns a normalized URI.
UriResolver::resolve in vendor/guzzlehttp/psr7/src/UriResolver.php
Converts the relative URI into a new URI that is resolved against the base URI.

File

vendor/guzzlehttp/psr7/src/UriResolver.php, line 23

Class

UriResolver
Resolves a URI reference in the context of a base URI and the opposite way.

Namespace

GuzzleHttp\Psr7

Code

public static function removeDotSegments($path) {
  if ($path === '' || $path === '/') {
    return $path;
  }
  $results = [];
  $segments = explode('/', $path);
  foreach ($segments as $segment) {
    if ($segment === '..') {
      array_pop($results);
    }
    elseif ($segment !== '.') {
      $results[] = $segment;
    }
  }
  $newPath = implode('/', $results);
  if ($path[0] === '/' && (!isset($newPath[0]) || $newPath[0] !== '/')) {

    // Re-add the leading slash if necessary for cases like "/.."
    $newPath = '/' . $newPath;
  }
  elseif ($newPath !== '' && ($segment === '.' || $segment === '..')) {

    // Add the trailing slash if necessary
    // If newPath is not empty, then $segment must be set and is the last segment from the foreach
    $newPath .= '/';
  }
  return $newPath;
}