public function combine($url)
    {
        $url = static::fromString($url);

        // Use the more absolute URL as the base URL
        if (!$this->isAbsolute() && $url->isAbsolute()) {
            $url = $url->combine($this);
        }

        $parts = $url->getParts();

        // Passing a URL with a scheme overrides everything
        if ($parts['scheme']) {
            return new static(
                $parts['scheme'],
                $parts['host'],
                $parts['user'],
                $parts['pass'],
                $parts['port'],
                $parts['path'],
                clone $parts['query'],
                $parts['fragment']
            );
        }

        // Setting a host overrides the entire rest of the URL
        if ($parts['host']) {
            return new static(
                $this->scheme,
                $parts['host'],
                $parts['user'],
                $parts['pass'],
                $parts['port'],
                $parts['path'],
                clone $parts['query'],
                $parts['fragment']
            );
        }

        if (!$parts['path'] && $parts['path'] !== '0') {
            // The relative URL has no path, so check if it is just a query
            $path = $this->path ?: '';
            $query = count($parts['query']) ? $parts['query'] : $this->query;
        } else {
            $query = $parts['query'];
            if ($parts['path'][0] == '/' || !$this->path) {
                // Overwrite the existing path if the rel path starts with "/"
                $path = $parts['path'];
            } else {
                // If the relative URL does not have a path or the base URL
                // path does not end in a "/" then overwrite the existing path
                // up to the last "/"
                $path = substr($this->path, 0, strrpos($this->path, '/') + 1) . $parts['path'];
            }
        }

        $result = new self(
            $this->scheme,
            $this->host,
            $this->username,
            $this->password,
            $this->port,
            $path,
            clone $query,
            $parts['fragment']
        );

        if ($path) {
            $result->removeDotSegments();
        }

        return $result;
    }