You are here

public static function Uri::resolve in Zircon Profile 8

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

Resolve a base URI with a relative URI and return a new URI.

Parameters

UriInterface $base Base URI:

string $rel Relative URI:

Return value

UriInterface

1 call to Uri::resolve()
UriTest::testResolvesUris in vendor/guzzlehttp/psr7/tests/UriTest.php
@dataProvider getResolveTestCases

File

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

Class

Uri
Basic PSR-7 URI implementation.

Namespace

GuzzleHttp\Psr7

Code

public static function resolve(UriInterface $base, $rel) {
  if ($rel === null || $rel === '') {
    return $base;
  }
  if (!$rel instanceof UriInterface) {
    $rel = new self($rel);
  }

  // Return the relative uri as-is if it has a scheme.
  if ($rel
    ->getScheme()) {
    return $rel
      ->withPath(static::removeDotSegments($rel
      ->getPath()));
  }
  $relParts = [
    'scheme' => $rel
      ->getScheme(),
    'authority' => $rel
      ->getAuthority(),
    'path' => $rel
      ->getPath(),
    'query' => $rel
      ->getQuery(),
    'fragment' => $rel
      ->getFragment(),
  ];
  $parts = [
    'scheme' => $base
      ->getScheme(),
    'authority' => $base
      ->getAuthority(),
    'path' => $base
      ->getPath(),
    'query' => $base
      ->getQuery(),
    'fragment' => $base
      ->getFragment(),
  ];
  if (!empty($relParts['authority'])) {
    $parts['authority'] = $relParts['authority'];
    $parts['path'] = self::removeDotSegments($relParts['path']);
    $parts['query'] = $relParts['query'];
    $parts['fragment'] = $relParts['fragment'];
  }
  elseif (!empty($relParts['path'])) {
    if (substr($relParts['path'], 0, 1) == '/') {
      $parts['path'] = self::removeDotSegments($relParts['path']);
      $parts['query'] = $relParts['query'];
      $parts['fragment'] = $relParts['fragment'];
    }
    else {
      if (!empty($parts['authority']) && empty($parts['path'])) {
        $mergedPath = '/';
      }
      else {
        $mergedPath = substr($parts['path'], 0, strrpos($parts['path'], '/') + 1);
      }
      $parts['path'] = self::removeDotSegments($mergedPath . $relParts['path']);
      $parts['query'] = $relParts['query'];
      $parts['fragment'] = $relParts['fragment'];
    }
  }
  elseif (!empty($relParts['query'])) {
    $parts['query'] = $relParts['query'];
  }
  elseif ($relParts['fragment'] != null) {
    $parts['fragment'] = $relParts['fragment'];
  }
  return new self(static::createUriString($parts['scheme'], $parts['authority'], $parts['path'], $parts['query'], $parts['fragment']));
}