Build a URI from template and assigned parameters.

@param boolean $trailingSlash
@return Uri

@throws \OutOfBoundsException When at least one path parameter value is missing.
357506
public function build($trailingSlash = false)
	{
		$result = '';
		
		foreach(preg_split("'(\\{(?:(?>[^\\{\\}]+)|(?R))+\\})'S", $this->uri, -1, PREG_SPLIT_DELIM_CAPTURE) as $part)
		{
			if(substr($part, 0, 1) == '{')
			{
				$result .= Uri::encode($this->path[substr($part, 1, -1)]);
			}
			else
			{
				$result .= $part;
			}
		}
		
		$result = $this->replaceParams($this->uri, $this->path);
		
		if($trailingSlash && substr($result, -1) != '/')
		{
			$uri = new Uri($result . '/');
		}
		else
		{
			$uri = new Uri($result);
		}
		
		if(!empty($this->query))
		{
			$uri = $uri->setQuery($this->query);
		}
		
		if($this->fragment !== NULL)
		{
			if(false === strpos($this->fragment, '{'))
			{
				$uri = $uri->setFragment($this->fragment);
			}
			else
			{
				$uri = $uri->setFragment($this->replaceParams($this->fragment, $this->path));
			}
		}
		
		return $uri;
	}
1327003
public static function get_rest_path($uri=false) {
        if ( ! $uri ) $uri = $_SERVER['REQUEST_URI'];     // /tsugi/lti/some/cool/stuff
        $pos = strpos($uri,'?');
        if ( $pos > 0 ) $uri = substr($uri,0,$pos);
        if ( self::endsWith($uri, '/') ) {
            $uri = substr($uri, 0, strlen($uri)-1);
        }
        return $uri;
    }
414094
public static function user_trailingslashit( $link ) {
		$link_parts = parse_url($link);

		if ( !$link_parts ) {
			return $link;
		}

		if ( isset($link_parts['path']) && $link_parts['path'] != '/' ) {
			$new_path = user_trailingslashit($link_parts['path']);

			if ( $new_path != $link_parts['path'] ) {
				$link = str_replace($link_parts['path'], $new_path, $link);
			}
		}
		return $link;
	}
803641
def uri_join(*paths)
      return nil if paths.length == 0
      leadingslash = paths[0][0] == '/' ? '/' : ''
      trailingslash = paths[-1][-1] == '/' ? '/' : ''
      paths.map! { |path| path.sub(/^\/+/, '').sub(/\/+$/, '') }
      leadingslash + paths.join('/') + trailingslash
    end
864498
public function joinUrlParts() {
		if (0 === func_num_args()) {
			throw new \InvalidArgumentException('Need at least one argument');
		}

		$parts = func_get_args();
		$trimSlashes = function(&$part) {
			$part = trim($part, '/');
		};
		array_walk($parts, $trimSlashes);

		return implode('/', $parts);
	}
653352
public static function addTrailingSlash($uri)
    {
        $uri = self::filterUri($uri);
        $path = $uri->getPath();
        if ('/' !== substr($path, -1)) {
            $path .= '/';
        }

        return self::normalizePath($uri, $path);
    }
363580
public function uri(){
        $basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
        $uri = substr($_SERVER['REQUEST_URI'], strlen($basepath));
        if (strstr($uri, '?')) $uri = substr($uri, 0, strpos($uri, '?'));
        $uri = '/' . trim($uri, '/');
        return $uri;
    }
18639
public static function uriToPath(string $uri)
    {
        $fragments = parse_url($uri);
        if ($fragments === false || !isset($fragments['scheme']) || $fragments['scheme'] !== 'file') {
            throw new \InvalidArgumentException("Not a valid file URI: $uri");
        }
        $filepath = urldecode((string) $fragments['path']);
        if (strpos($filepath, ':') !== false) {
            if ($filepath[0] === '/') {
                $filepath = substr($filepath, 1);
            }
            $filepath = str_replace('/', '\\', $filepath);
        }
        return $filepath;
    }
697367
public static function removeTrailingSlash($uri)
    {
        $uri = self::filterUri($uri);
        $path = $uri->getPath();
        if ('' !== $path && '/' === substr($path, -1)) {
            $path = substr($path, 0, -1);
        }

        return self::normalizePath($uri, $path);
    }
1121098
protected function createAbsoluteUri($path, $allowQueryString = false)
    {
        $value = '/'.ltrim(parse_url($path, PHP_URL_PATH), '/');

        if ($allowQueryString && $query = parse_url($path, PHP_URL_QUERY)) {
            $value .= '?'.$query;
        }

        return $value;
    }
1085749
public static function parse(string $uri): Uri
    {
        preg_match(static::URI_PATTERN, $uri, $matches);

        $components = static::componentsFromMatches($matches);
        $scheme = $components['scheme'];
        $authority = $components['authority'];
        $path = $components['path'];
        $query = $components['query'];
        $fragment = $components['fragment'];

        return new static($path, $scheme, $authority, $query, $fragment);
    }
205237
protected function getAdditionalUriData($uri)
    {
        if (strpos($uri, '?') !== false) {
            return substr($uri, strpos($uri, '?'));
        } else if (strpos($uri, '#') !== false) {
            return substr($uri, strpos($uri, '#'));
        }
        
        return '';
    }
962383
private function trailingSlash($routeName)
    {
        $routeName = rtrim($routeName, ']');
        $routeName = rtrim($routeName, '[/');
        $missing   = substr_count($routeName, '[') - substr_count($routeName, ']');
        $routeName .= '[/]' . str_repeat(']', $missing);

        return $routeName;
    }
158367
public function setUri($uri)
    {
        if (null !== $uri && !is_string($uri)) {
            require_once 'Zend/Navigation/Exception.php';
            throw new Zend_Navigation_Exception(
                    'Invalid argument: $uri must be a string or null');
        }

        $this->_uri = $uri;
        return $this;
    }
405080
private static URL stripTrailingSlash(URL url) {
        String externalForm = url.toExternalForm();
        if (externalForm.endsWith("/")) {
            externalForm = externalForm.substring(0, externalForm.length() - 1);
            try {
                url = new URL(externalForm);
            } catch (MalformedURLException e) {
                // will never happen.
            }
        }
        return url;
    }
544140
public function withFragment(string $fragment): URI
    {
        $result = clone $this;
        $result->fragment = $this->filterFragment($fragment);

        return $result;
    }
301641
public function to(
        $path = null,
        ?array $query = [],
        ?string $fragment = null,
        bool $trailingSlash = false
    ): string {
        return $this->create(null, $this->appUrl, $path, $query, $fragment, $trailingSlash);
    }