You are here

public static function Uri::removeDotSegments in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/guzzlehttp/psr7/src/Uri.php \GuzzleHttp\Psr7\Uri::removeDotSegments()

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

1 call to Uri::removeDotSegments()
Uri::resolve in vendor/guzzlehttp/psr7/src/Uri.php
Resolve a base URI with a relative URI and return a new URI.

File

vendor/guzzlehttp/psr7/src/Uri.php, line 77

Class

Uri
Basic PSR-7 URI implementation.

Namespace

GuzzleHttp\Psr7

Code

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

  // Add the leading slash if necessary
  if (substr($path, 0, 1) === '/' && substr($newPath, 0, 1) !== '/') {
    $newPath = '/' . $newPath;
  }

  // Add the trailing slash if necessary
  if ($newPath != '/' && isset($ignoreSegments[end($segments)])) {
    $newPath .= '/';
  }
  return $newPath;
}